
% -- 
% Jeff Sevadjian
% Research Technician
% Monterey Bay Aquarium Research Institute
% 7700 Sandholdt Rd
% Moss Landing, CA 95039, USA
% ph: +1 831-775-1888

% Loads OA Mooring .csv data from //atlas/OA_Moorings/deployment_data/
%  and makes some very basic plots.

%define buoy, deployment, data directory
 buoy = 'oa1';
 dep = '201607';
 mydir = ['//atlas.shore.mbari.org/OA_Moorings/deployment_data/' ...
    buoy '/' dep '/realTime/'];

%read-in .csv files
 ctd = csvread([mydir 'CTD.csv'],1,0);
 co2 = csvread([mydir 'CO2.csv'],1,0);
 fluor = csvread([mydir 'Fluor.csv'],1,0);
 met = csvread([mydir 'MET.csv'],1,0);
 metw = csvread([mydir 'METwind.csv'],1,0);
 o2 = csvread([mydir 'Oxygen.csv'],1,0);
 pos = csvread([mydir 'Position.csv'],1,0);
 tagID = csvread([mydir 'tagID.csv'],1,0);

%replace zeros w/ NaNs
 ctd(ctd==0) = NaN;
 co2(co2==0) = NaN;
 fluor(fluor==0) = NaN;
 met(met==0) = NaN;
 metw(metw==0) = NaN;
 o2(o2==0) = NaN;
 pos(pos==0) = NaN;

%convert time in Unix to Matlab datenum
 dn = datenum([1970 1 1]);
 ctd(:,1) = dn + ctd(:,1)/86400/1000;
 co2(:,1) = dn + co2(:,1)/86400/1000;
 met(:,1) = dn + met(:,1)/86400/1000;
 metw(:,1) = dn + metw(:,1)/86400/1000;
 pos(:,1) = dn + pos(:,1)/86400/1000;
 clear dn;

%simple time-series plots
 figure; plot(ctd(:,1),ctd(:,3),'b.-');
 figure; plot(co2(:,1),co2(:,2),'b.-');
 figure; plot(co2(:,1),co2(:,4),'b.-');

%pCO2 vs pH
 %calc theoretical relation
  sali = [33 34];
  tempi = [8 15];
  alk = 2150.0 + 44.0.*(sali-31.25); %(GF)
 %CO2SYS
  sil = 0;
  po4 = 1;
  pH_th = [7.5:0.01:8.5];
  for n=1:length(sali)
    A = CO2SYS(alk(n),pH_th,1,3,sali(n),tempi(n),NaN,0,NaN,...
      sil,po4,1,4,1);
    xPred(:,n) = A(:,4); %(uatm)
  end
  clear sali tempi alk sil po4 n A;
 %plot
  x = deNaN(co2(:,1),co2(:,3));
  y = co2(:,2);
  figure;
  plot(x,y,'.');
  hold on;
  plot(xPred(:,1),pH_th,'-','Color',[.4 .4 .4],'LineWidth',2);
  plot(xPred(:,2),pH_th,'k-','LineWidth',2);
  xlabel('pCO2 [ppm]');
  ylabel('pH');
  scatter(x,y,3,co2(:,1),'filled');
  colormap(jet);
