% -------------------------------------------------------------------------
% 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

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

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

% Plot Transect ACS data to determine ranges where additional QC is needed
close all
files_binned = dir('Transect5\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('Transect5\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('Transect5\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('Transect5\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('Transect5\ACS_Binned\eqbox_06_???_ACS_*.*'); % see all days
files_binned = dir('Transect5\ACS_Binned\eqbox_06_262_ACS_*.*'); % days: 262-264
files_binned = strcat('Transect5\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('Transect5\extracted\eqbox_06_263_21_ACS.008', 'Transect5\ACS_Binned\', ...
        [datenum(2006,0,263,08,10,00),datenum(2006,0,263,08,35,00)], 'noplot','save'); 
Make_ACS_Binned_from_WAP('Transect5\extracted\eqbox_06_262_21_ACS.008', 'Transect5\ACS_Binned\', ...
        [datenum(2006,0,262,12,10,00),datenum(2006,0,262,13,00,00)], 'noplot','save'); 

delete Transect5\ACS_Binned\eqbox_06_263_ACS_009.mat

% replot Transect ACS data for final review
close all
files_binned = dir('Transect5\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('Transect5\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_transect5_version = datestr(now,30)
save(['Transect5\ACS_apcp_transect5_' ACS_apcp_transect5_version '.mat'], 'ACS_apcp_transect5_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]')
                                
