%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This program is designed to merge data collected by multiple instruments with the DH-4
%All data is merged to CTD data, the slowest sampling instrument.
%applies lags to instrument data
clear all
close all

%instrument specific information
Tcal=17.8;

fnam=['037'];
diss='yes';
direct='/Users/emmanuel.boss/Desktop/EXPORTS01/IOPPackage/Epoch1Day6/'


%reading data in and getting their sampling time
[time, P, T, S, C]=rd_SB37([direct,'run_24_CTD-ENGR.',fnam]);
time_ctd=time;
% time_conductivity=time-3/(24*3600); %delay between pumped conductivity and temperature
% CC=interp1(time_conductivity,C,time_ctd,'linear');
% I=find(isfinite(CC));
% SS=sw_salt(CC(I)/4.2914,T(I),P(I));
% time_salinity=(time_ctd(I));
% T=T(I);
% P=P(I);
% time_ctd=time_salinity;
% S=SS;
% [SVAN,sigma]=swstate(S,T,P);

[time Vchl]=rd_analog([direct,'run_29_ANALOG.',fnam]);
time_chl=time-2.75/(24*3600); %assume a delay for fuorometer
%median filter for analogue 5 sample median  filter (1.25 sec):
Vchl_new=medfilt1(Vchl,5);

[time wl BB9]=rd_BB9counts([direct,'run_22_ECO.',fnam]);
time_bb9=time;

[time FDOM]=rd_Eco1ch([direct,'run_23_ECO.',fnam]);
time_FDOM=time;

[t,dat,wla,wlc,itemp,xtemp]=rd_WAP_acs298([direct,'run_21_ACS.',fnam]);
time_acs=t(1:length(dat)); %assume a 1s delay for ac-9

plot(time_acs,(dat(:,160)-dat(:,150))*100,'k-',time_ctd,T,'b-')

pause


%median filter the ac9 data with a 6 sample median  filter (1.5 sec):
for j=1:168
 acs_new(:,j)=medfilt1(dat(:,j),6);
end

%determine begining and end time for merging.
I=find(time_ctd>=time_chl(1) & time_ctd>=time_bb9(1) & time_ctd>=time_acs(1) & time_ctd>=time_FDOM(1));
I1=find(time_ctd<max(time_chl) & time_ctd<=max(time_bb9) & time_ctd<=max(time_acs) & time_ctd<=max(time_FDOM));
I2=find(time_ctd>=time_ctd(min(I)) & time_ctd<=time_ctd(max(I1)));
time_merge=time_ctd(I2); 
clear dat 
%create interpolated data matrix
dat1(:,1)=(time_merge-min(time_merge))*24*3600; %time in secs'
dat1(:,[2:5])=[P(I2),T(I2),S(I2),sigma(I2)];
dat1(:,6)=interp1(time_chl,Vchl,time_merge,'linear');
dat1(:,7)=interp1(time_FDOM,FDOM,time_merge,'linear');
for j=1:168
    dat1(:,7+j)=interp1(time_acs,acs_new(:,j),time_merge,'linear');
end
for j=1:9
    dat1(:,175+j)=interp1(time_bb9,BB9(:,j),time_merge,'linear');
end


%bin data to 1m bins
[datg,prrg]=binpre(dat1',dat1(:,2),ceil(min(dat1(:,2))),floor(max(dat1(:,2))),3,2);
datg=datg';

%apply calibrations
load /Users/emmanuel.boss/Desktop/EXPORTS01/IOPPackage/Calibrations/cal_298_20180820
datg(:,8:175)=datg(:,8:175)-[calc cala];
%T and S correct attenuation and absorption
[datouta, datout2a, dS, dT]=TS_corr_ACS(datg(:,92:175), 'a', wla, datg(:,4), datg(:,3), Tcal);
[datoutc, datout2c, dSc, dTc]=TS_corr_ACS(datg(:,8:91), 'c', wla, datg(:,4), datg(:,3), Tcal);

figure(1)
plot(wla,datouta,'b',wlc,datoutc,'r')
datgTS(:,8:175)=[datoutc datouta];

clear dat1 dat t acs_new

fnam=['031'];
diss='no';
direct='/Users/emmanuel.boss/Desktop/EXPORTS01/IOPPackage/Epoch1Day6/'


%reading data in and getting their sampling time
[time, P, T, S, C]=rd_SB37([direct,'run_24_CTD-ENGR.',fnam]);
time_ctd=time;
time_conductivity=time-3/(24*3600); %delay between pumped conductivity and temperature
CC=interp1(time_conductivity,C,time_ctd,'linear');
I=find(isfinite(CC));
SS=sw_salt(CC(I)/4.2914,T(I),P(I));
time_salinity=(time_ctd(I));
T=T(I);
P=P(I);
time_ctd=time_salinity;
S=SS;
[SVAN,sigma]=swstate(S,T,P);

[time Vchl]=rd_analog([direct,'run_29_ANALOG.',fnam]);
time_chl=time-2.75/(24*3600); %assume a delay for fuorometer
%median filter for analogue 5 sample median  filter (1.25 sec):
Vchl_new=medfilt1(Vchl,5);

[time wl BB9]=rd_BB9counts([direct,'run_22_ECO.',fnam]);
time_bb9=time;

[time FDOM]=rd_Eco1ch([direct,'run_23_ECO.',fnam]);
time_FDOM=time;

[t,dat,wla,wlc,itemp,xtemp]=rd_WAP_acs298([direct,'run_21_ACS.',fnam]);
time_acs=t(1:length(dat))-1/24/3600; %assume a 1s delay for ac-9

%median filter the ac9 data with a 6 sample median  filter (1.5 sec):
for j=1:168
 acs_new(:,j)=medfilt1(dat(:,j),6);
end

%determine begining and end time for merging.
I=find(time_ctd>=time_chl(1) & time_ctd>=time_bb9(1) & time_ctd>=time_acs(1) & time_ctd>=time_FDOM(1));
I1=find(time_ctd<max(time_chl) & time_ctd<=max(time_bb9) & time_ctd<=max(time_acs) & time_ctd<=max(time_FDOM));
I2=find(time_ctd>=time_ctd(min(I)) & time_ctd<=time_ctd(max(I1)));
time_merge=time_ctd(I2); 
clear dat 
%create interpolated data matrix
dat1(:,1)=(time_merge-min(time_merge))*24*3600; %time in secs'
dat1(:,[2:5])=[P(I2),T(I2),S(I2),sigma(I2)];
dat1(:,6)=interp1(time_chl,Vchl,time_merge,'linear');
dat1(:,7)=interp1(time_FDOM,FDOM,time_merge,'linear');
for j=1:168
    dat1(:,7+j)=interp1(time_acs,acs_new(:,j),time_merge,'linear');
end
for j=1:9
    dat1(:,175+j)=interp1(time_bb9,BB9(:,j),time_merge,'linear');
end

%bin data to 1m bins
[datpg,prrpg]=binpre(dat1',dat1(:,2),ceil(min(dat1(:,2))),floor(max(dat1(:,2))),3,2);
datpg=datpg';

%apply calibrations
load /Users/emmanuel.boss/Desktop/EXPORTS01/IOPPackage/Calibrations/cal_298_20180820
datpg(:,8:175)=datpg(:,8:175)-[calc cala];

[C,IA,IB] = intersect(prrg,prrpg)
ap=datpg(IB,92:175)-datg(IA,92:175);
cp=datpg(IB,8:91)-datg(IA,8:91);
figure(2)
plot(wla,ap)
figure(3)
plot(wla,cp)

