
% Have to import/merge BB data here too...

%depl info
 veh = 'wgOA';
 dep = '20130315';

atlas = '//atlas.shore.mbari.org/';

%working directory
 mydir = [atlas 'WaveGlider/deployment_data/' veh '/' dep '/'];
 myfile = ['SV2_' dep 'QC'];

%import BB data
 fid = fopen([mydir '/BB2FB227/BB2FB227_20130315.eng']);
 fgetl(fid); %header
 gliderBB.time = [];
 gliderBB.beta470 = [];
 gliderBB.bb470 = [];
 gliderBB.beta700 = [];
 gliderBB.bb700 = [];
 gliderBB.chl = [];
 gliderBB.xTemp = [];
 gliderBB.press = [];
 while ~feof(fid)
   fline = fgetl(fid);
   f = strfind(fline, sprintf('\t'));
   gliderBB.time = [gliderBB.time, datenum(fline(f(1)+1:f(3)-1),'mmddyyHHMMSS')];
   s = str2num(fline(f(3)+1:end));
   gliderBB.beta470 = [gliderBB.beta470, s(1)];
   gliderBB.bb470 = [gliderBB.bb470, s(4)];
   gliderBB.beta700 = [gliderBB.beta700, s(5)];
   gliderBB.bb700 = [gliderBB.bb700, s(8)];
   gliderBB.chl = [gliderBB.chl, s(9)];
   gliderBB.xTemp = [gliderBB.xTemp, s(10)];
   gliderBB.press = [gliderBB.press, s(11)];
 end
 fclose(fid);
 clear fid fline f s ans;

%quick QC of BB data
 indBad = [1:36, 19597:length(gliderBB.time)];
 gliderBB.beta470(indBad) = NaN;
 gliderBB.bb470(indBad) = NaN;
 gliderBB.beta700(indBad) = NaN;
 gliderBB.bb700(indBad) = NaN;
 gliderBB.chl(indBad) = NaN;
 gliderBB.xTemp(indBad) = NaN;
 gliderBB.press(indBad) = NaN; 
 clear indBad;

%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 BB 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 = [round(gliderCTD.time(1)/(10/1440))*(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=gliderBB.time;
 for n=1:length(timei)
   [m(n),i(n)]=min(abs(time-timei(n)));
 end
 gliderBBind=struct;
 fn=fieldnames(gliderBB);
 for n=1:length(fn)
   e=eval(['gliderBB.' 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);
 dist=[0 sqrt( (diff(x).^2) + (diff(y).^2) )];
 for n=2:length(x) %cumsum doesn't work w/ NaNs
   gliderind.dist(n)=sum(dist(1:n),'omitnan');
 end
 gliderind.dist(isnan(dist))=NaN;
 clear x y dist;

%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 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, ' ...
   'Cumulative distance (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.beta700(n), ',', ...
      gliderBBind.bb700(n), ',', ...
      gliderBBind.chl(n), ',', ...
      gliderCO2ind.pCO2water(n), ',', ...
      gliderCO2ind.pCO2air(n), ',', ...
      gliderCO2ind.pH(n) );
  end
  clear n;
  fclose(fid);
  clear fid ans;
