%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This program is designed to merge data collected by multiple instruments with the DH-4
%All data is merged to ac9 data which is averaged every 10 secs.

clear all

fnam='030';

%determine starting and end time of digital instruments
[dat]=rd_TimeStp(['Timestp.',fnam]);
start_port1=dat(1);
end_port1=dat(2);
start_port2=dat(3);
end_port2=dat(4);
start_port3=dat(5);
end_port3=dat(6);
clear dat

%reading data in and getting their sampling time
[P, T, S]=rd_SB37(['port1.',fnam]);
time_ctd=start_port1+(end_port1-start_port1)/(length(T))*[1:length(T)];

[analog]=rd_analog(['analog.',fnam]);
time_analog=analog(:,1)';

[t1,ac9]=rd_wetview_TmSrs(['ac9_',fnam,'.dat']); %adjust for two packets lost by wetview
time_ac9=start_port3+3200+t1*1000;

[Eco]=rd_EcoVSF(['port2.',fnam]);
%2 lines are incomplete in Eco-VSF output. Attempts to correct for that:
t_Eco=(end_port2-start_port2)/(length(Eco(:,1)+2));
time_Eco=start_port2+t_Eco+t_Eco*[1:length(Eco(:,1))];

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

%median filter the CTD, analog and Eco data with a 10 sec filter:
P_new=medfilt1(P,7);
T_new=medfilt1(T,7);
S_new=medfilt1(S,7);
for i=1:3
 analog_new(:,i+1)=medfilt1(analog(:,i+1),10);
end
for i=1:4
 Eco_new(:,i+2)=medfilt1(Eco(:,i+2),20);
end

%header for combined file
    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,'LSS(V)');
	hdr_out=str2mat(hdr_out,'Trans(V)');
	hdr_out=str2mat(hdr_out,'Chl(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,'Eco_100');
    hdr_out=str2mat(hdr_out,'Eco_125');
    hdr_out=str2mat(hdr_out,'Eco_150');
    hdr_out=str2mat(hdr_out,'Eco_REF');
 
%create interpolated data matrix
dat(:,1)=(time_merge'-min(time_merge))/1000; %time in secs'
dat(:,2)=interp1(time_ctd,P_new,time_merge','linear');
dat(:,3)=interp1(time_ctd,T_new,time_merge','linear');
dat(:,4)=interp1(time_ctd,S_new,time_merge','linear');
for i=1:3
    dat(:,4+i)=interp1(time_analog,analog_new(:,1+i),time_merge','linear');
end

dat(:,8:25)=ac9(I2,1:18);

for i=1:4
    dat(:,25+i)=interp1(time_Eco,Eco_new(:,i+2)',time_merge','linear');
end

cd merged_files
file_name_current_out=['merfile_',fnam,'.mer'];
fprintf('\nwriting to file: %s\n',file_name_current_out);
wrtmerg(file_name_current_out,dat',hdr_out);
cd ..