%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%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
%applied 1500 delay to ac9 and 4000 to Fcdom based on looking at
%files with particulate data while delays are expected to be longer for
%filtered data, those data are significantly less startified.

clear all
close all
%fnam=['000';'001';'002';'003';'004';'005';'006';'007';'008';'009';'010';'011';'012';'013';'014';'015';'016';'017';'018';'019';'020';'021';'022';'023';'024';'025';'026';'027';'028';'029';'030';'031';'032';'033';'034';'035';'036';'037';'038';'039';];
%fnam=['041';'042';'043';'044';'045';'046';'047';'048';'049';'050';'051';'052';'054';'055';'056';'057';'058';'059';'060';'061';'062';'063';'064';'065';'066';'067';'068';'069';'070';];
fnam=['071';'072';'073';'074';'075';'076';'077';'078';'079';'080';'081';];
direct='/Users/emmanuelboss/Desktop/Rivet/IOP_pack/day3/'
for i=1:length(fnam);
    fnam(i,:)
%reading data in and getting their sampling time
[time, T, C, P, S, sigma]=rd_SBE37([direct,'archive_21_T_ASCII.',fnam(i,:)]);
time_ctd=time;
time_conductivity=time-3000; %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, ALT]=rd_ALT([direct,'archive_22_T_ASCII.',fnam(i,:)]);
[B,I1,J]=unique(time);
time=time(I1);ALT=ALT(I1,:);
I=strmatch('R',ALT);  %find lines to keep
time_altimeter=time(I);
ALT=str2num(ALT(I,2:5)); %convert to a number

[time, turb, Vcdom]=rd_analog([direct,'archive_29_T_ASCII.',fnam(i,:)]);
time_analog=time-2750; %assume a delay for fuorometer
time_lss=time;

[time,ac9,Tac9]=rd_wetview([direct,'archive_23_AC9.',fnam(i,:)]); 
time_ac9=time-1000; %assume a 1.5s delay for ac-9
%median filter the ac9 data with a 5 sample median  filter (~.55 sec):
for j=1:18
 ac9_new(:,j)=medfilt1(ac9(:,j),5);
end

[time,wl,BB9]=rd_BB9([direct,'archive_24_T_ASCII.',fnam(i,:)]);
time_Eco=time;

%determine begining and end time for merging.
I=find(time_ctd>=time_analog(1) & time_ctd>=time_Eco(1) & time_ctd>=time_ac9(1) & time_ctd>=time_lss(1));
I1=find(time_ctd<max(time_analog) & time_ctd<=max(time_Eco) & time_ctd<=max(time_ac9) & time_ctd<=max(time_lss));
I2=find(time_ctd>=time_ctd(min(I)) & time_ctd<=time_ctd(max(I1)));
time_merge=time_ctd(I2); 

%header for combined file (can't have empty spaces)
    hdr_out='Time(s)';
	hdr_out=str2mat(hdr_out,'Pressure(db)');
	hdr_out=str2mat(hdr_out,'Temperature(C)');
	hdr_out=str2mat(hdr_out,'Salinity(PSU)');
	hdr_out=str2mat(hdr_out,'Sigma(kgm^-3)');
	hdr_out=str2mat(hdr_out,'ALT');
	hdr_out=str2mat(hdr_out,'Vcdom');
    hdr_out=str2mat(hdr_out,'LSS(V)');
	hdr_out=str2mat(hdr_out,'a412');
	hdr_out=str2mat(hdr_out,'a440');
	hdr_out=str2mat(hdr_out,'a488');
	hdr_out=str2mat(hdr_out,'a510');
	hdr_out=str2mat(hdr_out,'a532');
	hdr_out=str2mat(hdr_out,'a555');
	hdr_out=str2mat(hdr_out,'a650');
    hdr_out=str2mat(hdr_out,'a676');
    hdr_out=str2mat(hdr_out,'a715');
    hdr_out=str2mat(hdr_out,'c412');
	hdr_out=str2mat(hdr_out,'c440');
	hdr_out=str2mat(hdr_out,'c488');
	hdr_out=str2mat(hdr_out,'c510');
	hdr_out=str2mat(hdr_out,'c532');
	hdr_out=str2mat(hdr_out,'c555');
	hdr_out=str2mat(hdr_out,'c650');
    hdr_out=str2mat(hdr_out,'c676');
    hdr_out=str2mat(hdr_out,'c715');
    hdr_out=str2mat(hdr_out,'T_ac9');
    hdr_out=str2mat(hdr_out,'beta412');
    hdr_out=str2mat(hdr_out,'beta440');
    hdr_out=str2mat(hdr_out,'beta488');
    hdr_out=str2mat(hdr_out,'beta510');
    hdr_out=str2mat(hdr_out,'beta532');
    hdr_out=str2mat(hdr_out,'beta595');
    hdr_out=str2mat(hdr_out,'beta650');
    hdr_out=str2mat(hdr_out,'beta715');
    hdr_out=str2mat(hdr_out,'beta880');
 
%create interpolated data matrix
dat(:,1)=(time_merge'-min(time_merge))/1000; %time in secs'
dat(:,[2:5])=[P(I2),T(I2),S(I2),sigma(I2)];
dat(:,6)=interp1(time_altimeter,ALT',time_merge','linear');
dat(:,7)=interp1(time_analog,Vcdom',time_merge','linear');
dat(:,8)=interp1(time_lss,turb',time_merge','linear');

for j=1:18
    dat(:,8+j)=interp1(time_ac9,ac9_new(:,j)',time_merge','linear');
end
dat(:,27)=interp1(time_ac9,Tac9',time_merge','linear');
for j=1:9
    dat(:,27+j)=interp1(time_Eco,BB9(:,j)',time_merge','linear');
end

cd '/Users/emmanuelboss/Desktop/Rivet/IOP_pack/day1/merged'
file_name_current_out=['merfile_',fnam(i,:),'.mer'];
fprintf('\nwriting to file: %s\n',file_name_current_out);
wrtmerg(file_name_current_out,dat',hdr_out);
fclose all
cd '/Users/emmanuelboss/Desktop/Rivet/IOP_pack/software'
clear dat ac9_new BB9 ALT Vcdom turb sigma P T S C CC time_merge time_lss time_Eco time_analog time_altimeter time_ctd time_Eco
end
