% -------------------------------------------------------------------------
% 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 Transect4 ACS files
% -------------------------------------------------------------------------
files = dir('Transect4\extracted\eqbox_06_???_21_ACS.*') % ??? are wildcards for indiv char
files = strcat('Transect4\extracted\', {files.name}')
for kf = 1:length(files)
    Make_ACS_Binned_from_WAP(files{kf}, ['transect4\ACS_Binned\'], [], 'noplot','save')
end
copyfile('transect4\ACS_Binned\', 'transect4\ACS_Binned.BAK\')

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

% Plot Transect ACS data to determine ranges where additional QC is needed
close all
files_binned = dir('transect4\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('transect4\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('transect4\ACS_Binned\eqbox_06_???_ACS_*.*');
files_binned = strcat('transect4\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('transect4\ACS_Binned\eqbox_06_???_ACS_*.*'); % see all days
files_binned = dir('transect4\ACS_Binned\eqbox_06_260_ACS_*.*'); % days: 254-261
files_binned = strcat('transect4\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('transect4\extracted\eqbox_06_254_21_ACS.001', 'transect4\ACS_Binned\', ...
        [datenum(2006,0,254,01,00,00),datenum(2006,0,254,02,00,00)], 'noplot','save'); 
Make_ACS_Binned_from_WAP('transect4\extracted\eqbox_06_257_21_ACS.005', 'transect4\ACS_Binned\', ...
        [datenum(2006,0,257,05,11,00),datenum(2006,0,257,05,20,00)], 'noplot','save'); 
Make_ACS_Binned_from_WAP('transect4\extracted\eqbox_06_260_21_ACS.003', 'transect4\ACS_Binned\', ...
        [datenum(2006,0,260,03,04,30),datenum(2006,0,260,03,45,00)], 'noplot','save'); 

delete transect4\ACS_Binned\eqbox_06_257_ACS_009.mat
delete transect4\ACS_Binned\eqbox_06_260_ACS_005.mat

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