
% This is for post-processing, not real-time processing.

% veh is SV2 or SV3
% dep is yyyymmdd of deployment date
% pCO2cal is 5-element vector [A B C D E] of pCO2 cal coeff's
% pHcal is 2-element vector [E0 Ts]
% BBcal is 3x2 matrix from WetLabs *.dev file:
%  rows are per wavelength
%  columns are [scale-factor dark-value]

veh='SV2';
dep='20120829';
pCO2cal=[-16.59560 0.30550 1.09010 -0.00284 1]; %WG_cal_config.xlsx
pHcal=[-0.41639003 293.59]; %[-0.418781 293.34]; %WG_cal_config.xlsx
BBcal=[];

if ismac
  pre='/Volumes/';
elseif ispc
  pre='//atlas.shore.mbari.org/';
else
  pre='/mbari/';
end

%working directory
 mydir=[pre 'WaveGlider/deployment_data/' veh '/' dep '/'];
 myfile=[veh '_' dep];

%add path to m-files/functions called from here
% i.e., codes which derive bb, pH, pCO2 (conv_*.m)
 addpath([pre 'WaveGlider/code/']);

%import csv data into Matlab
 fid=fopen([mydir myfile '.csv']);

%skip past header
 hd=1;
 while hd
   ft=ftell(fid);
   fline=fgetl(fid);
   s=strfind(fline,',');
   hd=strcmp(fline(s(1)-1),'*');
 end
 clear fline s hd;

%data
 dn=datenum([1970 1 1]);
 ct=0; ctWx=0; ctCTD=0; ctCO2=0; ctBB=0;
 fseek(fid,ft,'bof'); %go back one line
 while ~feof(fid)
   fline=fgetl(fid);
   f=strfind(fline,',');
   s=str2num(fline(f(1)+1:end));
   if ~isempty(strfind(fline,'Waveglider')) && length(s)==11
     ct=ct+1;
     glider.lat(ct)=s(1);
     glider.lon(ct)=s(2);
     glider.time(ct)=dn+s(3)/86400/1000; %ms since epoch
     glider.tempSub(ct)=s(9);
   elseif ~isempty(strfind(fline,'CTD')) && length(s)==11
     ctCTD=ctCTD+1;
     gliderCTD.lat(ctCTD)=s(1);
     gliderCTD.lon(ctCTD)=s(2);
     gliderCTD.time(ctCTD)=dn+s(3)/86400/1000;
     gliderCTD.cond(ctCTD)=s(4);
     gliderCTD.o2(ctCTD)=s(5);
     gliderCTD.o2Hz(ctCTD)=s(6);
     gliderCTD.o2sol(ctCTD)=s(7);
     gliderCTD.press(ctCTD)=s(8);
     gliderCTD.sal(ctCTD)=s(9);
     gliderCTD.temp(ctCTD)=s(10);
     gliderCTD.sid(ctCTD)=s(11); %serial num?
   elseif ~isempty(strfind(fline,'pCO2')) && length(s)==38
     ctCO2=ctCO2+1;
     gliderCO2.lat(ctCO2)=s(1);
     gliderCO2.lon(ctCO2)=s(2);
     gliderCO2.time(ctCO2)=dn+s(3)/86400/1000; %'sample time'
%        gliderCO2.time(ctCO2)=dn+s(4)/86400/1000; %'time'
     gliderCO2.pHtime(:,ctCO2)=gliderCO2.time(ctCO2)+...
       [0:10:50]/1440;
     gliderCO2.pHV1(:,ctCO2)=s(30:35)/1000;
    %Equil
     gliderCO2.EquilPumpOnCO2(ctCO2)=s(5);
     gliderCO2.EquilPumpOnTemp(ctCO2)=s(6);
     gliderCO2.EquilPumpOnPress(ctCO2)=s(7);
     gliderCO2.EquilPumpOffCO2(ctCO2)=s(8);
     gliderCO2.EquilPumpOffTemp(ctCO2)=s(9);
     gliderCO2.EquilPumpOffPress(ctCO2)=s(10);
     gliderCO2.EquilPumpOffHum(ctCO2)=s(11);
    %Zero
     gliderCO2.ZeroPumpOnCO2(ctCO2)=s(12);
     gliderCO2.ZeroPumpOnTemp(ctCO2)=s(13);
     gliderCO2.ZeroPumpOnPress(ctCO2)=s(14);
     gliderCO2.ZeroPumpOffCO2(ctCO2)=s(15);
     gliderCO2.ZeroPumpOffTemp(ctCO2)=s(16);
     gliderCO2.ZeroPumpOffPress(ctCO2)=s(17);
    %Air
     gliderCO2.AirPumpOnCO2(ctCO2)=s(18);
     gliderCO2.AirPumpOnTemp(ctCO2)=s(19);
     gliderCO2.AirPumpOnPress(ctCO2)=s(20);
     gliderCO2.AirPumpOffCO2(ctCO2)=s(21);
     gliderCO2.AirPumpOffTemp(ctCO2)=s(22);
     gliderCO2.AirPumpOffPress(ctCO2)=s(23);
     gliderCO2.AirPumpOffHum(ctCO2)=s(24);
    %Standard
     gliderCO2.StandardFlowOnPress(ctCO2)=s(25);
     gliderCO2.StandardFlowOffCO2(ctCO2)=s(26);
     gliderCO2.StandardFlowOffTemp(ctCO2)=s(27);
     gliderCO2.StandardFlowOffPress(ctCO2)=s(28);
     gliderCO2.StandardFlowOffHum(ctCO2)=s(29);
    %Can Humidity
     gliderCO2.CanHumidity(ctCO2)=s(36);
    %Two extra columns of only zeros (?)
   elseif ~isempty(strfind(fline,'Weather')) && length(s)==10
     ctWx=ctWx+1;
     gliderWx.lat(ctWx)=s(1);
     gliderWx.lon(ctWx)=s(2);
     gliderWx.time(ctWx)=dn+s(3)/86400/1000;
     gliderWx.wdirAvg(ctWx)=s(4);
     gliderWx.wspdAvg(ctWx)=s(5)*0.514444; %knots to m/s
     gliderWx.wspdMax(ctWx)=s(6)*0.514444;
     gliderWx.airPress(ctWx)=s(7);
     gliderWx.airTemp(ctWx)=s(10);
   elseif ~isempty(strfind(fline,'ECO-Puck'))
     s=str2num(fline(f(1)+1:f(end-1))); %last col is garb txt
     ctBB=ctBB+1;
     gliderBB.lat(ctBB)=s(1);
     gliderBB.lon(ctBB)=s(2);
     gliderBB.time(ctBB)=dn+s(3)/86400/1000;
     gliderBB.beta1(ctBB)=BBcal(1,1) * (s(5) - BBcal(1,2));
     gliderBB.beta2(ctBB)=BBcal(2,1) * (s(7) - BBcal(2,2));
     gliderBB.chl(ctBB)=BBcal(3,1) * (s(9) - BBcal(3,2));
     gliderBB.wavel(:,ctBB)=s([4 6 8]); %is constant
     gliderBB.therm(ctBB)=s(10); %thermistor (not used)
   end
 end
 fclose(fid);
 clear dn fid ft n ct* fline f s ans;

%no internally-recorded BB2FB data this depl
 clear pre;

%get unique time stamps only
 w=whos('glider*');
 for n=1:length(w)
   e=eval(w(n).name);
   fn=fieldnames(e);
   [~,i]=unique(e.time);
   for m=1:length(fn)
     ee=eval(['e.' fn{m}]);
     e=setfield(e,fn{m},ee(:,i));
   end
   assignin('base',w(n).name,e); %overwrite
 end
 clear w n e fn i m ee;

%NaN placeholder CO2 standard values = 0
 if exist('gliderCO2')
   f=find(gliderCO2.StandardFlowOffCO2==0);
   gliderCO2.StandardFlowOnPress(f)=NaN;
   gliderCO2.StandardFlowOffCO2(f)=NaN;
   gliderCO2.StandardFlowOffTemp(f)=NaN;
   gliderCO2.StandardFlowOffPress(f)=NaN;
   gliderCO2.StandardFlowOffHum(f)=NaN;
   clear f;
 end

%derive CTD variables
 if exist('gliderCTD')
  %calc sigma-t
   [~,gliderCTD.sigt]=swstate(gliderCTD.sal,gliderCTD.temp,...
     gliderCTD.press);
  %convert oxygen from ml/liter to micromoles/kg (ryjo)
   gliderCTD.o2umolkg = (gliderCTD.o2 * 1.4276) .* ...
     (1e6./((gliderCTD.sigt+1000)*32));
  %calc O2 perc saturation?
 end

%derive bb from beta
 if exist('gliderBB')
   if exist('gliderCTD')
     gliderBB=conv_BB(gliderBB,gliderCTD);
   else
     gliderBB=conv_BB(gliderBB,[]);
   end
 end

%derive pCO2 and pH from raw m'ments
 if exist('gliderCO2')
   if exist('gliderCTD')
     gliderCO2=conv_pCO2(gliderCO2,gliderCTD,pCO2cal);
     gliderCO2=conv_pH(gliderCO2,gliderCTD,pHcal);
   else
     gliderCO2=conv_pCO2(gliderCO2,[],pCO2cal);
     gliderCO2=conv_pH(gliderCO2,[],pHcal);
   end
 end

%save mat file to working directory
 w=whos('glider*');
 save([mydir myfile '.mat'],w.name);
