
% Parse OASIS dump from manual connection

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

mydir = [atlas 'OA_Moorings/test_data/201609 OA2 Ano tank/'];
% myfile = 'OA2016_ANO_TESTTANK';
myfile = 'oa2_ano_test_tank_logfile2_12sep2016';
clear atlas;

fid = fopen([mydir myfile '.log']);

AANDERAA_O2 = [];
% AIRMAR_PB200=[]; %not connected for this test
EXT_ANALOG1 = [];
EXT_ANALOG2 = [];
EXT_ANALOG3 = [];
MICROCAT = [];
SPECIAL1 = [];
SPECIAL2 = [];
PCO2 = [];

while ~feof(fid)
  fline = fgetl(fid);
 %O2
  if ~isempty(strfind(fline,'AANDERAA_O2'))
    fgetl(fid); %blank
    fline2 = fgetl(fid);
    AANDERAA_O2 = [AANDERAA_O2; datenum(fline(end-19:end-1)) str2num(fline2)];
    clear fline2;
  end
 %can humidity V1, V2
  if ~isempty(strfind(fline,'EXT_ANALOG1'))
    fgetl(fid);
    fline2 = fgetl(fid);
    EXT_ANALOG1 = [EXT_ANALOG1; datenum(fline(end-19:end-1)) str2num(fline2)];
    clear fline2;
  end
 %pH V1
  if ~isempty(strfind(fline,'EXT_ANALOG2'))
    fgetl(fid);
    fline2 = fgetl(fid);
    EXT_ANALOG2 = [EXT_ANALOG2; datenum(fline(end-19:end-1)) str2num(fline2)];
    clear fline2;
  end
 %fluor
  if ~isempty(strfind(fline,'EXT_ANALOG3'))
    fgetl(fid);
    fline2 = fgetl(fid);
    EXT_ANALOG3 = [EXT_ANALOG3; datenum(fline(end-19:end-1)) str2num(fline2)];
    clear fline2;
  end
 %CTD
  if ~isempty(strfind(fline,'MICROCAT'))
    fgetl(fid);
    fline2 = fgetl(fid);
    MICROCAT = [MICROCAT; datenum(fline(end-19:end-1)) str2num(fline2(1:end-23))];
  end
 %charging system
  if ~isempty(strfind(fline,'SPECIAL1'))
    fline2 = '';
    while isempty(strfind(fline2,'Avg Charge:'))
      fline2 = fgetl(fid);
    end
    SPECIAL1 = [SPECIAL1; datenum(fline(end-19:end-1)) str2num(fline2(14:15))];
  end
 %VR2C tag ID OR status msg
  if ~isempty(strfind(fline,'SPECIAL2'))
    fgetl(fid);
    fline2 = fgetl(fid);
    if ~strcmp(fline2(5), '*')
      f = strfind(fline2,',');
      SPECIAL2 = [SPECIAL2; datenum(fline(end-19:end-1)) ...
        str2num(fline2(f(4)+1:f(6)-1))];
    end
  end
 %MET
  if ~isempty(strfind(fline,'AIRMAR_PB200'))
    %
  end
 %CO2
  if ~isempty(strfind(fline,'PCO2'))
    fgetl(fid);
    fline2 = fgetl(fid);
    PCO2 = [PCO2; datenum(fline(end-19:end-1)) str2num(fline2)];
    clear fline2;
  end

  clear fline;

end
fclose(fid);
clear fid ans;

%--%

%CO2
 f1 = find(PCO2(:,9) == 1);
 f2 = find(PCO2(:,9) == 2);
 f3 = find(PCO2(:,9) == 3);
 f4 = find(PCO2(:,9) == 4);
 f5 = find(PCO2(:,9) == 5);
 f6 = find(PCO2(:,9) == 6);
 %pressure
  figure; hold on;
  %equil
   plot(PCO2(f1,1), PCO2(f1,3), '.-', 'Color', [1 .6 .6]);
   plot(PCO2(f2,1), PCO2(f2,3), '.-', 'Color', [1 .1 .1]);
  %zero
   plot(PCO2(f3,1), PCO2(f3,3), '.-', 'Color', [.6 1 .6]);
   plot(PCO2(f4,1), PCO2(f4,3), '.-', 'Color', [.1 1 .1]);
  %air
   plot(PCO2(f5,1), PCO2(f5,3), '.-', 'Color', [.6 .6 1]);
   plot(PCO2(f6,1), PCO2(f6,3), '.-', 'Color', [.1 .1 1]);
 %xCO2
  figure; hold on;
  plot(PCO2(f1,1), PCO2(f1,4), '.-', 'Color', [1 .6 .6]);
  plot(PCO2(f2,1), PCO2(f2,4), '.-', 'Color', [1 .1 .1]);
  plot(PCO2(f3,1), PCO2(f3,4), '.-', 'Color', [.6 1 .6]);
  plot(PCO2(f4,1), PCO2(f4,4), '.-', 'Color', [.1 1 .1]);
  plot(PCO2(f5,1), PCO2(f5,4), '.-', 'Color', [.6 .6 1]);
  plot(PCO2(f6,1), PCO2(f6,4), '.-', 'Color', [.1 .1 1]);

%--%

 R=8.31451; %gas constant (J mol-1 K-1)
 F=96487; %converts between Coulombs and Faradays
 pHcal=[-0.355412 294.33];
 T = interp1(MICROCAT(:,1),MICROCAT(:,2),EXT_ANALOG2(:,1));
 ST = ( R * (T+273.15) * log(10)) ./ F;
 E0T = pHcal(1)-0.001 * ( (T+273.15) - pHcal(2));
 pH = ( (EXT_ANALOG2(:,2)/1000) - E0T)./ST;

%--%

 B0 = -6.24097e-3;
 B1 = -6.93498e-3;
 B2 = -6.90358e-3;
 B3 = -4.29155e-3;
 C0 = -3.11680e-7;
 T = interp1(MICROCAT(:,1),MICROCAT(:,2),AANDERAA_O2(:,1));
 Ts = log((298.15-T) ./ (273.15+T));
 S = interp1(MICROCAT(:,1),MICROCAT(:,5),AANDERAA_O2(:,1));
 o2uM = AANDERAA_O2(:,4) .* exp( S.* (B0 + B1*Ts + B2*(Ts.^2) + B3*(Ts.^3)) + C0*(S.^2) );
 [~,sigt] = swstate(S, T, 0.1);
 o2umolkg = o2uM*1000 ./ (sigt+1000);
