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

% Converts raw controller-logged data to 'engineering units,'
%  saves to *_conv.mat file

mydir = ['//atlas.shore.mbari.org/Diatom/socal moorings/' ...
  'deployment_data/20161025/controller/'];
myfile = 'OA8_Dump';

%load parsed data
 load([mydir myfile '.mat']);

%cal info - manually entered from OCSD.Sensor.Inventory.xlsx
 %pHcal     de-amp slope  de-amp int  E0        Ts
  pHcal = [ 1             0          -0.3962653 294.24; ... % #78
            1             0          -0.3895079 294.44; ... % #79
            1             0          -0.4178267 294.20; ... % #80
            0.0920257    -0.0359259  -0.3982000 294.07 ];   % Sfc 10/21/2015 cal
%             0.0920257    -0.0359259  -0.4027596 294.27 ];   % Sfc 3/3/2016 cal
 %o2cal    #78,         #79,         #80
  o2cal = [ 1.0513e+000  1.0513e+000  1.0513e+000; ... % A0
           -1.5000e-003 -1.5000e-003 -1.5000e-003; ... % A1
            4.3461e-001  3.8993e-001  4.3721e-001; ... % A2
           -2.3625e-001 -2.4253e-001 -2.4216e-001; ... % B0
            1.6552e+000  1.6173e+000  1.6622e+000; ... % B1
            9.9605e-002  1.0515e-001  9.9404e-002; ... % C0
            4.1836e-003  4.4202e-003  4.1966e-003; ... % C1
            5.8279e-005  6.2610e-005  5.8924e-005; ... % C2
            1.1000e-002  1.1000e-002  1.1000e-002 ];    % E
 %WetLabs cal
  WLcal = [25 0.019]; % SF [ug/L/V], Dark Counts [V]
 %CO2 cal
  CO2cal = [-14.1392 0.8817 1.0440 -0.0027]; % A, B, C, D
  CO2stndKn = 463; % [ppm] **APPROXIMATE**
 %MET air temp 
  METcal = [1 0]; % slope, intercept

%deployment beg, end
 tBeg = datenum([2016 10 26 22 0 0]);
 tEnd = datenum([2017 1 25 20 30 0]);

%--%

% Here, we chop the data to the deployment period, sort by time,
%  and take only unique time stamps.

 %CTD78
  f = find(CTD78(:,1)>tEnd | CTD78(:,1)<tBeg);
  CTD78(f,:) = [];
  [~,i1] = unique(CTD78(:,1));
  CTD78 = CTD78(i1,:);
  [~,i2] = sort(CTD78(:,1));
  CTD78 = CTD78(i2,:);
  clear f i1 i2;
 %CTD79
  f = find(CTD79(:,1)>tEnd | CTD79(:,1)<tBeg);
  CTD79(f,:) = [];
  [~,i1] = unique(CTD79(:,1));
  CTD79 = CTD79(i1,:);
  [~,i2] = sort(CTD79(:,1));
  CTD79 = CTD79(i2,:);
  clear f i1 i2;
 %CTD80
  f = find(CTD80(:,1)>tEnd | CTD80(:,1)<tBeg);
  CTD80(f,:) = [];
  [~,i1] = unique(CTD80(:,1));
  CTD80 = CTD80(i1,:);
  [~,i2] = sort(CTD80(:,1));
  CTD80 = CTD80(i2,:);
  clear f i1 i2;
 %EXT_ANALOG1
  f = find(EXT_ANALOG1(:,1)<tBeg | EXT_ANALOG1(:,1)>tEnd);
  EXT_ANALOG1(f,:) = [];
  [~,i1] = unique(EXT_ANALOG1(:,1));
  EXT_ANALOG1 = EXT_ANALOG1(i1,:);
  [~,i2] = sort(EXT_ANALOG1(:,1));
  EXT_ANALOG1 = EXT_ANALOG1(i2,:);
  clear f i1 i2;
 %EXT_ANALOG2
  f = find(EXT_ANALOG2(:,1)<tBeg | EXT_ANALOG2(:,1)>tEnd);
  EXT_ANALOG2(f,:) = [];
  [~,i1] = unique(EXT_ANALOG2(:,1));
  EXT_ANALOG2 = EXT_ANALOG2(i1,:);
  [~,i2] = sort(EXT_ANALOG2(:,1));
  EXT_ANALOG2 = EXT_ANALOG2(i2,:);
  clear f i1 i2;
 %PCO2
  f = find(PCO2(:,1)<tBeg | PCO2(:,1)>tEnd);
  PCO2(f,:) = [];
  [~,i1] = unique(PCO2(:,1));
  PCO2 = PCO2(i1,:);
  [~,i2] = sort(PCO2(:,1));
  PCO2 = PCO2(i2,:);
  clear f i1 i2;
 %AIRMAR_PB200
  f = find(AIRMAR_PB200(:,1)<tBeg | AIRMAR_PB200(:,1)>tEnd);
  AIRMAR_PB200(f,:) = [];
  [~,i1] = unique(AIRMAR_PB200(:,1));
  AIRMAR_PB200 = AIRMAR_PB200(i1,:);
  [~,i2] = sort(AIRMAR_PB200(:,1));
  AIRMAR_PB200 = AIRMAR_PB200(i2,:);
  clear f i1 i2;

%--%

% Now, derive salinity, sigma-t, o2, pH, fluorescence, pCO2

 %CTD78
  CTD78(:,9) = calcSal(CTD78(:,3),CTD78(:,2),CTD78(:,4)); %sal
  [~,CTD78(:,10)] = swstate(CTD78(:,9),CTD78(:,2),CTD78(:,4)); %sigma-t
  o2 = calcO2(o2cal(:,1),CTD78(:,7),CTD78(:,2),CTD78(:,9),CTD78(:,4)); %o2 [ml/l]
%   CTD78(:,11) = (o2 * 1.4276) .* ...
%     (1e6./((CTD78(:,10)+1000)*32)); % ml/l to umol/kg
  CTD78(:,11) = o2*1.42903; % ml/l to mg/l [ref. SBE-63 manual]
  CTD78(:,12) = calc_pH(pHcal(1,:),CTD78(:,2),CTD78(:,6));
  CTD78(:,13) = WLcal(1) * (CTD78(:,5) - WLcal(2));
  clear o2;

 %CTD79
  CTD79(:,8) = calcSal(CTD79(:,3),CTD79(:,2),CTD79(:,4));
  [~,CTD79(:,9)] = swstate(CTD79(:,8),CTD79(:,2),CTD79(:,4));
  CTD79(:,10) = calcO2(o2cal(:,2),CTD79(:,6),CTD79(:,2),CTD79(:,8),CTD79(:,4)) * 1.42903;
  CTD79(:,11) = calc_pH(pHcal(2,:),CTD79(:,2),CTD79(:,6));

 %CTD80
  CTD80(:,8) = calcSal(CTD80(:,3),CTD80(:,2),CTD80(:,4));
  [~,CTD80(:,9)] = swstate(CTD80(:,8),CTD80(:,2),CTD80(:,4));
  CTD80(:,10) = calcO2(o2cal(:,3),CTD80(:,6),CTD80(:,2),CTD80(:,8),CTD80(:,4)) * 1.42903;
  CTD80(:,11) = calc_pH(pHcal(3,:),CTD80(:,2),CTD80(:,6));

 %Surface
 
  %pH
   tempi = interp1(CTD79(:,1),CTD79(:,2),EXT_ANALOG2(:,1));
   EXT_ANALOG2(:,3) = calc_pH(pHcal(4,:),tempi,EXT_ANALOG2(:,2));
   clear tempi;

  %pCO2
   %calc standard scale factor
    f = find(PCO2(:,8)==8);
    CO2stndSF = CO2stndKn ./ interp1(PCO2(f,1),PCO2(f,4),PCO2(:,1));
    %replace NaNs at beg/end of deployment with nearest m'ment
     fn1 = find(PCO2(:,1) < PCO2(f(1),1));
     CO2stndSF(fn1) = CO2stndSF(fn1(end)+1);
     fn2 = find(PCO2(:,1) > PCO2(f(end),1));
     CO2stndSF(fn2) = CO2stndSF(fn2(1)-1);
     clear f fn1 fn2;
   %index cycles
    f = find(PCO2(1:end-5,end)==1 & PCO2(6:end,end)==6);
   %define new variable, with new time stamp
    pCO2(:,1) = PCO2(f+1,1); %time at equil-off stage
   %interp T, S from 5-m CTD
    tempi = interp1(CTD79(:,1),CTD79(:,2),pCO2(:,1));
    sali = interp1(CTD79(:,1),CTD79(:,8),pCO2(:,1));
   %derive pCO2 water, air
    pCO2(:,2) = calcCO2(CO2cal,PCO2(f+1,4),PCO2(f+3,4),CO2stndSF(f+1),...
      PCO2(f+1,2),tempi,sali);
    pCO2(:,3) = calcCO2(CO2cal,PCO2(f+5,4),PCO2(f+3,4),CO2stndSF(f+5),...
      PCO2(f+5,2),tempi,sali);
   clear f tempi sali;

%--%

%save converted data
 save([mydir myfile '_conv.mat'],'AIRMAR_PB200','CTD78','CTD79','CTD80',...
   'EXT_ANALOG1','EXT_ANALOG2','pCO2','PCO2','SPECIAL1','SPECIAL2');
