clear all
close all

% ACS_files = dir('*ACS.log');
% ACS_name = strvcat(ACS_files.name);
% ACS_name = sortrows(ACS_name);
% 
% ap=[];
% cp=[];
% time_acS=[];
% for y = 1:length(ACS_files)
%     yr=str2num(ACS_name(y,1:4));mo=str2num(ACS_name(y,5:6));day=str2num(ACS_name(y,7:8));
%     hr=str2num(ACS_name(y,10:11));min=str2num(ACS_name(y,12:13));sec=str2num(ACS_name(y,14:15));
%     time_acS=[time_acS;datenum(yr,mo,day,hr,min,sec)];
%     [t,dat,wla,wlc]=rd_wetview_acs(ACS_name(y,:));
%     cp_spectra=dat(:,1:80);
%     ap_spectra=dat(:,81:160);
%     ap=[ap;median(ap_spectra)];
%     cp=[cp;median(cp_spectra)];
% end
% 
%save ACS_median cp ap wla wlc time_acS
%load ACS_median 
load ACS_median_corr
wl=[402:2:730];
cd CTD
load ctdOOI.mat
time_ctd=time;
load FL2BBOOI.mat
cd ..

T=interp1(time_ctd,temperature,time_acS,'nearest');
S=interp1(time_ctd,salinity,time_acS,'nearest');
CHL=interp1(time,Fl_chl,time_acS,'nearest');
BB=interp1(time,unk,time_acS,'nearest');

% %read tables for temperature and salinity correction
% tmp = xlsread('Sullivan_etal_2006_instrumentspecific.xls');
% 
% phi_Tc=interp1(tmp(:,1),tmp(:,2),wlc,'linear');
% phi_Sc=interp1(tmp(:,1),tmp(:,4),wlc,'linear');
% phi_Sa=interp1(tmp(:,1),tmp(:,6),wla,'linear');
% phi_Ta=interp1(tmp(:,1),tmp(:,2),wla,'linear');
% 
% Tcal=23.3; %in device file
% 
% for i=1:length(time_acS)
%     cp_TS(i,:)=cp(i,:)-S(i)*phi_Sc-(T(i)-Tcal)*phi_Tc;
%     ap_TS(i,:)=ap(i,:)-S(i)*phi_Sa-(T(i)-Tcal)*phi_Ta;
% end
% 
% wl=[402:2:730];
% cp_corr=interp1(wlc,cp_TS',wl,'linear');
% ap_c=interp1(wla,ap_TS',wl,'linear');
% 
% %residual temp/sal correction
% [ap_corr] = ACS_ResidTempScatCorr(ap_c', cp_corr', wl);

%save ACS_median_corr cp_corr ap_corr wla wlc time_acS

plot(wl,cp_corr-ap_corr')

%derive POC and Chl
ap3=interp1(wl,ap_corr(:,:)',[650 676 715],'linear')';
chl = 157*(ap3(:,2)-(39/65*ap3(:,1)+26/65*ap3(:,3))).^1.22;
cp660 = interp1(wl,cp_corr,660,'linear')';
POC = cp660.*380;
figure;
plot(time_acS,chl,'b',time_acS,CHL/2,'r');
ylabel('chlorophyll [mgChl m^{-3}]')
datetick('x','mm/dd')
xlabel('time');
figure
plot(time_acS,POC,'b',time_acS,BB*100*380,'r');
ylabel('POC [mgC m^{-3}]')
datetick('x','mm/dd')

xlabel('time');


