%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%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.

clear all

fnam='008';

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

%reading data in and getting their sampling time
[P, T, S, C, V1]=rd_SBE19(['port4.',fnam]);
time_ctd=start_port4+(end_port4-start_port4)/(length(T))*[1:length(T)];

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

[t1,ac9]=rd_wetview_new(['ac9',fnam,'.dat']); %adjust for two packets lost by wetview
time_ac9=start_port1+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_ctd>time_analog(1) & time_ctd>time_Eco(1) & time_ctd>time_ac9(1));
I1=find(time_ctd<max(time_analog) & time_ctd<max(time_Eco) & time_ctd<max(time_ac9));
I2=find(time_ctd>=time_ctd(min(I)) & time_ctd<=time_ctd(max(I1)));
time_merge=time_ctd(I2); 

%median filter the ac9 data with a 3 sample median  filter (~.5 sec):
for i=1:18
 ac9_new(:,i)=medfilt1(ac9(:,i),3);
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,'Conductivity(ms)');
	hdr_out=str2mat(hdr_out,'V1');
    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,'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:6])=[P(I2),T(I2),S(I2),C(I2),V1(I2)];
for i=1:1
    dat(:,6+i)=interp1(time_analog,analog(:,1+i)',time_merge','linear');
end
for i=1:18
    dat(:,7+i)=interp1(time_ac9,ac9_new(:,i)',time_merge','linear');
end
for i=1:4
    dat(:,25+i)=interp1(time_Eco,Eco(:,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 ..