% -------------------------------------------------------------------------
% An example of what Make_ACS_Binned_from_WAP actually does:
% -------------------------------------------------------------------------
% inputs
% 1 "extracted" raw ascii data from WAP
% 2 directory for the "binned" output file to go in
% 3 range of time to remove [ ]
% 4 'plot' or 'noplot'
% 5 'save' or 'nosave'
% output is name of file written, or [] indicating nothing was saved

close all
clear all

%%%%%%%%%%%%%%%% this is an example using only 1 extracted file
% Make_ACS_Binned_from_WAP('Transect3\extracted\eqbox_06_248_21_ACS.005', 'Transect3\ACS_Binned\', [], 'plot','save')

% % Look at what we just did to see what is written to the "binned" file

% % now, using the binned file:
% load Transect3\ACS_Binned\eqbox_06_248_ACS_005.mat
% % Two structs:
% %   Name       Size             Bytes  Class     Attributes
% % 
% %   acs_a      1x1             124508  struct              
% %   acs_c      1x1             124508  struct              
% % Each struct is something like:
% % acs_c = 
% % 
% %        time: [61x1 double]
% %       wavel: [1x83 double]
% %         med: [61x83 double]
% %        mean: [61x83 double]
% %         std: [61x83 double]
% %          ns: [61x1 double]
% %     nsclean: [61x1 double]

%%%%%%%%%%%%%%%% end of example

% -------------------------------------------------------------------------
% Run basic processing on extracted WAP data for Transect3 ACS files
% -------------------------------------------------------------------------
files = dir('Transect3\extracted\eqbox_06_???_21_ACS.*') % ??? are wildcards for indiv char
files = strcat('Transect3\extracted\', {files.name}')
for kf = 1:length(files)
    Make_ACS_Binned_from_WAP(files{kf}, ['Transect3\ACS_Binned\'], [], 'noplot','save')
end
copyfile('Transect3\ACS_Binned\', 'Transect3\ACS_Binned.BAK\')

% -------------------------------------------------------------------------
% Run plots and QC
% -------------------------------------------------------------------------

% Plot Transect ACS data to determine ranges where additional QC is needed
close all
files_binned = dir('Transect3\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('Transect3\ACS_Binned\', {files_binned.name}');
% Plot_ACS_Binned(files_binned)

% Basic QC by removing minute bins with stdev of a555,c555 greater than threshold
% QC_ACS_by_std inputs are:
% 1 list of binned files
% thresh_a440
% thresh_c535
files_binned = dir('Transect3\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('Transect3\ACS_Binned\', {files_binned.name}');
QC_ACS_by_std(files_binned, 0.01, 0.01); % Wayne picked the 0.01 thresholds for both absorp & atten

% replot Transect ACS data to determine ranges where additional QC is needed
close all
files_binned = dir('Transect3\ACS_Binned\eqbox_06_???_ACS_*.*'); % see all days
% files_binned = dir('Transect3\ACS_Binned\eqbox_06_250_ACS_*.*'); % days: 247-250
files_binned = strcat('Transect3\ACS_Binned\', {files_binned.name}');
% Plot_ACS_Binned(files_binned)

% -------------------------------------------------------------------------
% QC on specific sections of data
% -------------------------------------------------------------------------

% only run this section to remove data:
Make_ACS_Binned_from_WAP('Transect3\extracted\eqbox_06_249_21_ACS.011', 'Transect3\ACS_Binned\', ...
        [datenum(2006,0,249,11,11,00),datenum(2006,0,249,12,00,00)], 'noplot','save');
Make_ACS_Binned_from_WAP('Transect3\extracted\eqbox_06_249_21_ACS.000', 'Transect3\ACS_Binned\', ...
        [datenum(2006,0,249,00,00,00),datenum(2006,0,249,00,07,00)], 'noplot','save');
Make_ACS_Binned_from_WAP('Transect3\extracted\eqbox_06_250_21_ACS.000', 'Transect3\ACS_Binned\', ...
        [datenum(2006,0,250,00,11,00),datenum(2006,0,250,00,50,00)], 'noplot','save');
Make_ACS_Binned_from_WAP('Transect3\extracted\eqbox_06_250_21_ACS.004', 'Transect3\ACS_Binned\', ...
        [datenum(2006,0,250,04,40,00),datenum(2006,0,250,05,00,00)], 'noplot','save');
Make_ACS_Binned_from_WAP('Transect3\extracted\eqbox_06_250_21_ACS.017', 'Transect3\ACS_Binned\', ...
        [datenum(2006,0,250,17,11,00),datenum(2006,0,250,18,00,00)], 'noplot','save');
    
% 07 Sep (250) Removed acs data starting at 250_ACS_018  Entering port in Nuku Hiva?
delete Transect3\ACS_Binned\eqbox_06_250_ACS_018.mat
delete Transect3\ACS_Binned\eqbox_06_250_ACS_019.mat
delete Transect3\ACS_Binned\eqbox_06_250_ACS_020.mat
delete Transect3\ACS_Binned\eqbox_06_250_ACS_021.mat
delete Transect3\ACS_Binned\eqbox_06_250_ACS_022.mat

% replot Transect ACS data for final review
close all
files_binned = dir('Transect3\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('Transect3\ACS_Binned\', {files_binned.name}');
Plot_ACS_Binned(files_binned)
% -------------------------------------------------------------------------
% Now load all of the binned data and do the processing to make particle
% optical properties
% -------------------------------------------------------------------------

% Load binned data for transect
[acs_a, acs_c] = Load_ACS_Binned(files_binned);

% Processing to subtract filtered measurement and do residual temperature
% and scattering correction
[acs_ap, acs_cp, acs_bp] = Calc_ACS_apcp(acs_a, acs_c, 0,10);

ACS_apcp_Transect3_version = datestr(now,30)
save(['Transect3\ACS_apcp_Transect3_' ACS_apcp_Transect3_version '.mat'], 'ACS_apcp_Transect3_version', 'files_binned', ...
                                    'acs_ap', 'acs_cp', 'acs_bp') 

% these plots can go on the web
figure, hold on
plot(acs_ap.time, acs_ap.mean(:,10), 'b.-')
datetick
xlabel('GMT Date (2006)')
ylabel('Particulate Absorption, ap(440) [1/m]')

figure, hold on
plot(acs_cp.time, acs_cp.mean(:,34), 'r.-')
datetick
xlabel('GMT Date (2006)')
ylabel('Particulate Attenuation, cp(555) [1/m]')

figure, hold on
plot(acs_ap.wavel, acs_ap.mean, 'b-')
xlabel('Wavelength, \lambda [nm]')
ylabel('Particulate Absorption, ap(\lambda) [1/m]')

figure, hold on
plot(acs_cp.wavel, acs_cp.mean, 'r-')    
xlabel('Wavelength, \lambda [nm]')
ylabel('Particulate Absorption, ap(\lambda) [1/m]')
                                
                               
                                