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

%inputs
 mydir = [''];
 myfile = 'DATALOG.txt';

fid = fopen([mydir '/' myfile]);

SPECIAL1=[];
SPECIAL2=[];
CTD78=[];
CTD79=[];
CTD80=[];
EXT_ANALOG1=[];
EXT_ANALOG2=[];
AIRMAR_PB200=[];
PCO2=[];

while ~feof(fid)
  fline = fgetl(fid);
  if ~isempty(strfind(fline,'SPECIAL1')) %VR2C battery info
    %
  end

  if ~isempty(strfind(fline,'SPECIAL2')) %VR2C status msg
    %
  end

  if ~isempty(strfind(fline,'CTD'))
    fl=strfind(fline,'CTD, ');
    fm=strfind(fline,',');
    ctdNum=str2num(fline(fl(1)+5:fl(1)+6));
    if ctdNum==78 %&& length(fline)==113 %t, T, C, p, fl, pHV1, o2ph, o2tempV
      try
        CTD78=[CTD78; datenum(fline(fm(end)+1:end)) ...
          str2num(fline(fl(1)+8:fm(end)-1))];
      end
    elseif ctdNum==79 %&& length(fline)==105
      try
        CTD79=[CTD79; datenum(fline(fm(end)+1:end)) ...
          str2num(fline(fl(1)+8:fm(end)-1))];
      end
    elseif ctdNum==80 %&& length(fline)==105
      try
        CTD80=[CTD80; datenum(fline(fm(end)+1:end)) ...
          str2num(fline(fl(1)+8:fm(end)-1))];
      end
    end
    clear fline2 fl fm ctdNum;
  end

  if ~isempty(strfind(fline,'EXT_ANALOG1')) %pH temp, pH battery
    f=strfind(fline,',');
    EXT_ANALOG1=[EXT_ANALOG1; datenum(fline(1:f(1)-1)) ...
      str2num(fline(f(2)+1:end))];
    clear f;
  end

  if ~isempty(strfind(fline,'EXT_ANALOG2')) %pH V1
    f=strfind(fline,',');
    EXT_ANALOG2=[EXT_ANALOG2; datenum(fline(1:f(1)-1)) ...
      str2num(fline(f(2)+1:end))];
    clear f;
  end

  if ~isempty(strfind(fline,'AIRMAR_PB200'))
   %NMEA string first
    f=strfind(fline,',');
    ti=datenum(fline(1:f(1)-1));
    s=str2num(fline(f(4)+1:f(5)-1));
    la=floor(s/100)+(rem(s,100)/60);
    s=str2num(fline(f(6)+1:f(7)-1));
    lo=-floor(s/100)-(rem(s,100)/60);
    clear f s;
   %Now, MET data
    fline2=fgetl(fid);
    if ~isempty(strfind(fline2,'$WIMDA'))
      f=strfind(fline2,',');
      ap = str2num(fline2(f(3)+1:f(4)-1)); %air press (bar)
      at = str2num(fline2(f(5)+1:f(6)-1)); %air temp (C)
      rh = str2num(fline2(f(9)+1:f(10)-1)); %rel hum (%)
      dp = str2num(fline2(f(11)+1:f(12)-1)); %dewpoint (C)
      wd = str2num(fline2(f(13)+1:f(14)-1)); %wind dir (true)
      ws = str2num(fline2(f(19)+1:f(20)-1)); %wind spd (m/s)
      if isempty(rh), rh=NaN; end
      if isempty(dp), dp=NaN; end
      AIRMAR_PB200 = [AIRMAR_PB200; ti la lo ap at rh dp wd ws];
    else
      AIRMAR_PB200=[AIRMAR_PB200; ti la lo NaN(1,6)];
    end
    clear ti la lo fline2 f ap at rh dp wd ws ;
  end

  if ~isempty(strfind(fline,'PCO2'))
    f=strfind(fline,',');
    PCO2=[PCO2; datenum(fline(1:f(1)-1)) str2num(fline(f(2)+1:end))];
    clear f;
  end

  clear fline;

end
fclose(fid);
clear fid ans;
