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

% Parses data from OASIS controller dump .txt file, sames to *.mat file.

% The DATALOG.txt file was corrupt; Jules transfered all files in the
%  controller directory to a controller in the lab (via a CF card) and
%  re-dumped the data from the OAS0000*.DAT files (which are similar
%  but difficult to parse manually). Thus, the output file is formatted
%  like a 'dump' file as opposed to a DATALOG.txt file.

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

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

%initialize variables
 SPECIAL1     = []; % charge controller battery info
 SPECIAL2     = []; % VR2C status msg AND/OR tag hits
 CTD78        = []; % t, T, C, p, fl, pHV1, o2phase, o2temp V
 CTD79        = []; % t, T, C, p, pHV1, o2phase, o2temp V
 CTD80        = []; % t, T, C, p, pHV1, o2phase, o2temp V
 EXT_ANALOG1  = []; % t, Surface pH temp, pH battery V
 EXT_ANALOG2  = []; % t, Surface pH V1
 AIRMAR_PB200 = []; % t, p, T, RH, dewpoint, windDir, windSpd
 PCO2         = []; % t, T, p, xCO2, [extra columns], cycle #

%parse to matrices
 while ~feof(fid)
   fline = fgetl(fid);
   if ~isempty(strfind(fline,'SPECIAL1')) %charge controller
    %
   end

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

   if ~isempty(strfind(fline,'CTD'))
     break;
    %controller time stamp
     ti = datenum(fline(end-19:end-1));
    %CTD string
     fline2 = fgetl(fid);
     st = strsplit(fline2,{': ',',\s*'},...
       'DelimiterType','RegularExpression');
     if length(st)==8 || length(st)==9
       if ~isempty(strfind(st{1},'78'))
         CTD78 = [CTD78; ti, str2double(st(2:8))];
       elseif ~isempty(strfind(st{1},'79'))
         CTD79 = [CTD79; ti, str2double(st(2:7))];
       elseif ~isempty(strfind(st{1},'80'))
         CTD80 = [CTD80; ti, str2double(st(2:7))];
       end
     end
     clear ti fline2 st;
   end

   if ~isempty(strfind(fline,'EXT_ANALOG1'))
     fline2 = fgetl(fid);
     EXT_ANALOG1 = [EXT_ANALOG1; datenum(fline(end-19:end-1)) str2num(fline2)];
     clear fline2;
   end

   if ~isempty(strfind(fline,'EXT_ANALOG2'))
     fline2 = fgetl(fid);
     EXT_ANALOG2 = [EXT_ANALOG2; datenum(fline(end-19:end-1)) str2num(fline2)];
     clear fline2;
   end

   if ~isempty(strfind(fline,'AIRMAR_PB200'))
    %controller time stamp
     ti = datenum(fline(end-19:end-1));
    %NMEA string
     fline2 = fgetl(fid);
     st = strsplit(fline2,',');
     nu = str2double(st([3,5]));
     la = floor(nu(1)/100) + (rem(nu(1),100)/60);
     lo =  - ( floor(nu(2)/100) + (rem(nu(2),100)/60) );
     clear fline2 st nu;
    %skip past any empty lines
     fline3 = '';
     while isempty(fline3)
       fline3 = fgetl(fid);
     end
    %MET string
     if ~isempty(strfind(fline3,'$WIMDA'))
       st = strsplit(fline3,',');
       AIRMAR_PB200 = [AIRMAR_PB200; ...
         ti,la,lo,str2double( st([4,6,8,9,11,17]) )];
     else
       AIRMAR_PB200 = [AIRMAR_PB200; ti,la,lo,NaN(1,6)];
     end
     clear ti fline2 st nu la lo fline3;
   end

  if ~isempty(strfind(fline,'PCO2'))
    fline2 = fgetl(fid);
    s = str2num(fline2);
    if length(s)==7
      PCO2 = [PCO2; datenum(fline(end-19:end-1)) s];
    end
    clear fline2 s;
  end

  clear fline;

end
fclose(fid);
clear fid ans;

%save as .mat file
 save([mydir myfile '.mat'],'AIRMAR_PB200','CTD78','CTD79','CTD80',...
   'EXT_ANALOG1','EXT_ANALOG2','PCO2','SPECIAL1','SPECIAL2');
