
% This uses the old, differential CO2 system. The raw data on 
%  oasis_coredata has an annoying time stamp issue that I'm not
%  touching with a 10-foot pole. Get the data off ssds instead.

 dep = '201202';
 dep = '201309';
%  mydir=['/Volumes/ssdsdata/deployments/m1/' dep];
 mydir=['//atlas.shore.mbari.org/ssdsdata/deployments/m1/' dep '/'];
 myfile='m1_pco2_20120222';
 myfile='m1_pco2_20130923';
 
%read co2 data
 setup_nctoolbox;
 ds = ncdataset([mydir myfile '.nc']);

%define data var
 data = [datenum([1970 1 1])+(ds.data('esecs')/86400) ...
   double(ds.data('delta_pCO2_ppm'))];
 clear ds;

%sort by time (may not be nec)
 [~,i]=sort(data(:,1));
 data=data(i,:);
 clear i;

%manually QC data

%axes param's
 ax.NextPlot = 'add';
 ax.Box = 'on';
 ax.XLim = [min(data(:,1)) max(data(:,1))] + [-3 3];

%plot
 figure('Units','Inches','Position',[0 0 11 8.5]);
 ha = tight_subplot(3,1,0.03,0.1,[0.1 0.15]);
 set(ha,ax);

 %pCO2
  axes(ha(3));
  plot(data(:,1),data(:,2),'k.-');
  ylabel({'pCO2'; '[ppm]'});
 %labels, etc.
  linkaxes(ha,'x');
  clear f1 myCol n le ga gl;

% Dates agree with the OASIS log, I am considering these data final,
%  with the exception of ONE outlier.

 %define final data
  co2.time = data(:,1);
  co2.dpCO2 = data(:,2);
  co2.dpCO2(2633) = NaN;
  clear data;

%save final data as .mat file
 mydir = '/Volumes/jsevadjian/M-Buoy/final CO2 data/';
 save([mydir dep '.mat'],'co2');

%write final data to .txt file
 dv = datevec(co2.time);
 fid = fopen([mydir dep '.txt'],'w+');
 fprintf(fid,'%s\n',['# GMT Year, Month, Day, Hr, Min, ' ...
   'Delta pCO2 [ppm]']);
 fmt = [repmat('%02d,',1,5) '%.2f\n'];
 for n=1:length(co2.time)
   fprintf(fid,fmt,...
     dv(n,1), dv(n,2), dv(n,3), dv(n,4), dv(n,5), ...
     co2.dpCO2(n));
 end
 fclose(fid);
 clear dv fid fmt n;

%--%

% JS added 2017.03.20 to check against Gernot's daily-avg dpCO2 xls file

 u = unique(floor(co2.time));
 for n=1:length(u)
  %final pCO2 w,a
   f = find( floor(co2.time) == u(n) );
   dpCO2aAvg(n,1) = nanmean(co2.dpCO2(f));
 end
 clear n f;
