%% Remus Log File Creation
%vprlog format
%01 - Time (REMUS Time is seconds from mission start)
%02 - Latitude
%03 - Longitude
%04 - Depth
%05 - Temperature 
%06 - Salinity
%07 - Density
%08 - Fluorescence
%09 - Turbidity (Optical Backscatter)
%10 - PAR
%11 - Bottom Depth
%12 - Oxygen
%13 - Roll
%14 - Pitch
%15 - Altitude

load('C:/data/TN368_REMUS/MSN001/EXPORTED/V0CTD.mat')

%% Merging
%Since time is measured in seconds, interpolate to 1 second

%Load CTD
disp('CTD Loading...')
load('C:/data/TN368_REMUS/MSN001/EXPORTED/V0CTD.mat');
filelength(1,:)=[length(mission_time),mission_time(2)-mission_time(1)];
vprlog(:,1)=min(mission_time):max(mission_time);
vprlog(:,2)=interp1(mission_time,latitude,vprlog(:,1));
vprlog(:,3)=interp1(mission_time,longitude,vprlog(:,1));
vprlog(:,4)=interp1(mission_time,depth,vprlog(:,1));
vprlog(:,5)=interp1(mission_time,temperature,vprlog(:,1));
vprlog(:,6)=interp1(mission_time,salinity,vprlog(:,1));
ctdsalinity=salinity;ctdtemperature=temperature;refdepth=depth;
pdensity=gsw_rho(gsw_SA_from_SP(ctdsalinity,refdepth,-70.8,40),gsw_CT_from_t(gsw_SA_from_SP(ctdsalinity,refdepth,-70.8,40),ctdtemperature,refdepth),refdepth)-1000;
vprlog(:,7)=interp1(mission_time,pdensity,vprlog(:,1));
clearvars -except vprlog filelength
%Load ECO
disp('ECO Loading...')
load('C:/data/TN368_REMUS/MSN001/EXPORTED/V0ECO.mat');
filelength(2,:)=[length(mission_time),mission_time(2)-mission_time(1)];
chlorophyll=(chl_sig-33)*0.0111;
vprlog(:,8)=interp1(mission_time,chlorophyll,vprlog(:,1));
turbidity=(turbidity_raw-50)*0.0051;
vprlog(:,9)=interp1(mission_time,turbidity,vprlog(:,1));
clearvars -except vprlog filelength
%Load DO
% load('C:/data/TN368_REMUS/MSN001/EXPORTED/V0DOSBE43.mat');
% t=vprlog(:,5);
% stamp=find(diff(time)~=0);
% oxygen=interp1(time(stamp),raw(stamp),vprlog(:,1));
% temp_K=t+273.15;
% Ts=log((298.15-t)./(273.15+t));
% Ts2=Ts.^2;
% Ts3=Ts.^3;
% Ts4=Ts.^4;
% Ts5=Ts.^5;
% co_2=co.^2;
% test1=(2.00907+(3.22014*Ts)+(4.0501*(Ts2))+(4.94457*(Ts3))+(-0.25687*(Ts4))+(3.88767*(Ts5)));
% test2=co.*(-.00624523+(-0.00737614*Ts)+(-0.010341*(Ts2))+(-0.00817083*(Ts3)));
% test3=(-0.000000488682*(co_2));
% oxsol=exp(2.00907+(3.22014*Ts)+(4.0501*(Ts2))+(4.94457*(Ts3))+(-0.25687*(Ts4))+(3.88767*(Ts5))+co.*(-.00624523+(-0.00737614*Ts)+(-0.010341*(Ts2))+(-0.00817083*(Ts3)))+(-0.000000488682*(co_2)));
% oxygen=0.4268*((oxygen/1000)-0.5189).*(1.0-(5.5721*10^(-3).*t)+(2.8735*10^(-4))*(t.^2)+((-3.9442*10^(-6))*(t.^3))).*oxsol.*exp(0.036.*p./temp_K);
% vprlog(:,12)=oxygen;
varnames=['Time               ';...
           'Latitude           ';...
           'Longitude          ';...
           'Depth              ';...
           'Temperature        ';...
           'Salinity           ';...
           'Density            ';...
           'Fluorescence       ';...
           'Optical Backscatter'];
save 'C:/data/TN368_REMUS/MSN001/EXPORTED/vprlog' vprlog filelength varnames
disp('Complete.')