
%depl info
 veh='SV3'; %SV2 or SV3
 dep='20150501'; %yyyymmdd of deployment date

if ismac
  pre='/Volumes/';
elseif ispc
  pre='//atlas.shore.mbari.org/'; %direction of / doesn't matter
else
  pre='/mbari/';
end

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

%add path to m-files/functions called from here
 addpath(genpath([pre 'WaveGlider/code/']));

%load QC'd WG mat data from QC_SVx.m
 load([mydir myfile '.mat']);

%burst-average CTD data
 gliderCTDavg=struct;
 f=[0 find(diff(gliderCTD.time)>1/1440) length(gliderCTD.time)];
 fn=fieldnames(gliderCTD);
 for n=1:length(fn)
   e=eval(['gliderCTD.' fn{n}]);
   ea=NaN(1,length(f)-1);
   for m=1:length(f)-1
     ind=[f(m)+1:f(m+1)];
     ea(m)=nanmean(e(ind));
   end
   gliderCTDavg=setfield(gliderCTDavg,fn{n},ea);
 end
 clear f fn n e ea m ind;

%burst-average BB2FB data
 gliderBBavg=struct;
 f=[0 find(diff(gliderBB.time)>1/1440) length(gliderBB.time)];
 fn=fieldnames(gliderBB);
 for n=1:length(fn)
   e=eval(['gliderBB.' fn{n}]);
   ea=NaN(1,length(f)-1);
   for m=1:length(f)-1
     ind=[f(m)+1:f(m+1)];
     ea(m)=nanmean(e(ind));
   end
   gliderBBavg=setfield(gliderBBavg,fn{n},ea);
 end
 clear f fn n e ea m ind;

%define common 10-min sample times
 timei=[round2(gliderCTD.time(1),10/1440):10/1440:...
   gliderCTD.time(end)]';

%index to nearest

%glider
 time=glider.time;
 for n=1:length(timei)
   [m(n),i(n)]=min(abs(time-timei(n)));
 end
 gliderind=struct;
 fn=fieldnames(glider);
 for n=1:length(fn)
   e=eval(['glider.' fn{n}]);
   ei=e(i);
   f=find(m>5/1440);
   ei(f)=NaN;
   gliderind=setfield(gliderind,fn{n},ei);
   gliderm(:,n)=ei';
 end
 gliderind=rmfield(gliderind,'time');
 clear time n m i fn n e ei f;

%CTD
 time=gliderCTDavg.time;
 for n=1:length(timei)
   [m(n),i(n)]=min(abs(time-timei(n)));
 end
 gliderCTDind=struct;
 fn=fieldnames(gliderCTDavg);
 for n=1:length(fn)
   e=eval(['gliderCTDavg.' fn{n}]);
   ei=e(i);
   f=find(m>5/1440);
   ei(f)=NaN;
   gliderCTDind=setfield(gliderCTDind,fn{n},ei);
   gliderCTDm(:,n)=ei';
 end
 gliderCTDind=rmfield(gliderCTDind,'time');
 clear time n m i fn n e ei f;

%CO2
 timec=gliderCO2.time;
 timep=reshape(gliderCO2.pHtime,1,numel(gliderCO2.pHtime));
 for n=1:length(timei)
   [mc(n),ic(n)]=min(abs(timec-timei(n)));
   [mp(n),ip(n)]=min(abs(timep-timei(n)));
 end
 gliderCO2ind=struct;
 fn=fieldnames(gliderCO2);
 for n=1:length(fn)
   e=eval(['gliderCO2.' fn{n}]);
   if size(e,1)>1 %pH
     er=reshape(e,1,numel(e));
     ei=er(ip);
     f=find(mp>5/1440);
     ei(f)=NaN;
   else
     ei=e(ic);
     f=find(mc>5/1440);
     ei(f)=NaN;
   end
   gliderCO2ind=setfield(gliderCO2ind,fn{n},ei);
   gliderCO2m(:,n)=ei';
 end
 gliderCO2ind=rmfield(gliderCO2ind,'time');
 clear timec timep n mc ic mp ip fn n e er ei f;

%Wx (tends to be on the 10-min already)
 time=gliderWx.time;
 for n=1:length(timei)
   [m(n),i(n)]=min(abs(time-timei(n)));
 end
 gliderWxind=struct;
 fn=fieldnames(gliderWx);
 for n=1:length(fn)
   e=eval(['gliderWx.' fn{n}]);
   ei=e(i);
   f=find(m>5/1440);
   ei(f)=NaN;
   gliderWxind=setfield(gliderWxind,fn{n},ei);
   gliderWxm(:,n)=ei';
 end
 gliderWxind=rmfield(gliderWxind,'time');
 clear time n m i fn n e ei f;

%BB2FB
 time=gliderBBavg.time;
 for n=1:length(timei)
   [m(n),i(n)]=min(abs(time-timei(n)));
 end
 gliderBBind=struct;
 fn=fieldnames(gliderBBavg);
 for n=1:length(fn)
   e=eval(['gliderBBavg.' fn{n}]);
   ei=e(i);
   f=find(m>5/1440);
   ei(f)=NaN;
   gliderBBind=setfield(gliderBBind,fn{n},ei);
   gliderBBm(:,n)=ei';
 end
 gliderBBind=rmfield(gliderBBind,'time');
 clear time n m i fn n e ei f;

%calc distance from origin
 [x,y]=lonlat2km(gliderind.lon(1),gliderind.lat(1),...
   gliderind.lon,gliderind.lat);
 gliderind.dist=[0 cumsum(sqrt( (diff(x).^2) + (diff(y).^2) ))];
 clear x y;

%calc depth of CTD
 gliderCTDind.depth=press2depth(gliderCTDind.press,...
   gliderCTDind.lat);

% %calc depth of Sub
%  gliderBBind.depth=press2depth(gliderBBind.press,...
%    gliderBBind.lat);

 clear glider gliderBB gliderBBavg gliderCO2 gliderCTD ...
   gliderCTDavg gliderWx;

%write text file
 fid=fopen([mydir myfile '.txt'],'w+');

%header info (comma-delimited)
 header=['Year, Month, Day, Hour (GMT), Minute, Second, ' ...
   'Longitude, Latitude, ' ...
   'Distance from origin (km), ' ...
   'Wind Dir, Avg Wind Speed (m/s), Max Wind Speed (m/s), ' ...
   'Atmospheric pressure (mbar), Air temperature (C), ' ...
   'CTD depth (m),' ...
   'T (C), S, O2 (umol/kg), ' ...
   'Beta-470, bb-470 (1/(m sr)), Beta-650, bb-650 (1/(m sr)), ' ...
   'Chl (ug/l), ' ...
   'pCO2 in water (uatm), pCO2 in air (uatm), ' ...
   'pH'];
%write header
 fprintf(fid,'%s\r\n',header);
 clear header;

%format data rows
 fmt=['%s%s' ... %time stmp
      '%3.5f%s%3.5f%s%.3f%s' ... %lon,lat,dist
      '%d%s%2.1f%s%2.1f%s%4.1f%s%2.1f%s' ... %wx
      '%2.2f%s' ... %depth of ctd
      '%2.3f%s%2.3f%s%2.2f%s' ... %ctd
      '%0.6f%s%0.6f%s%0.6f%s%0.6f%s%2.2f%s' ... %chl/bb
      '%3.1f%s%3.1f%s' ... %pco2
      '%.2f' ... %pH
      '\r\n'];
 %write data rows
  for n=1:length(timei)
    fprintf(fid,fmt,...
      datestr(timei(n),'yyyy,mm,dd,HH,MM,SS'), ',', ...
      gliderind.lon(n), ',', gliderind.lat(n), ',', ...
      gliderind.dist(n), ',', ...
      round(gliderWxind.wdirAvg(n)), ',', ...
      gliderWxind.wspdAvg(n), ',', ...
      gliderWxind.wspdMax(n), ',', ...
      gliderWxind.airPress(n), ',', ...
      gliderWxind.airTemp(n), ',', ...
      gliderCTDind.depth(n), ',', ...
      gliderCTDind.temp(n), ',', ...
      gliderCTDind.sal(n), ',', ...
      gliderCTDind.o2umolkg(n), ',', ...
      gliderBBind.beta470(n), ',', ...
      gliderBBind.bb470(n), ',', ...
      gliderBBind.beta650(n), ',', ...
      gliderBBind.bb650(n), ',', ...
      gliderBBind.chl(n), ',', ...
      gliderCO2ind.pCO2water(n), ',', ...
      gliderCO2ind.pCO2air(n), ',', ...
      gliderCO2ind.pH(n) );
  end
  clear n;
  fclose(fid);
  clear fid ans;
