function X = readdecode(filename)
% READDECODE - Reads output from bobs decode program for oasis output
%
% Use As: x = readdecode(filename)
%         x = readdecode
%
% Ouputs: x = a nested structure of data. For example to plot the Specprr Ed490 try
%             x = readdecode;
%             plot(x.SpecPRR.time,x.SpecPRR.Ed490)

% Brian Schlining
% 12 Oct 1999

%===========
% Open File
%===========
if ~nargin
   [infile inpath] = uigetfile('*.out','Select Ascii grid file',0,0);
   if infile == 0
      S = [];
      return                            % Return if CANCEL is selected
   end
   filename = [inpath infile];
end

X.filename = filename;

fid = fopen(filename, 'rt');

nCO2Pump = 0;
nMicroCat = 0;
nShutter0 = 0;
nOASIS = 0;
nFluorometer = 0;
nSpecPRR = 0;
npCO2 = 0;

while ~feof(fid)
   S = fgetl(fid);
   ID = stringtokenizer(S,1);
   if isempty(ID)
      ID = 'doNothing';
   end
   
   
   switch ID
      
   case 'CO2Pump'
      nCO2Pump = nCO2Pump + 1;
      X.CO2Pump.time(nCO2Pump,1) = getTime(S);
      if strcmp(stringtokenizer(S,4),'On')
         status = 1;
      else
         status = 0;
      end
      X.CO2Pump.status(nCO2Pump,1) = status;
      
   case 'MicroCat'
      try
         nMicroCat = nMicroCat + 1;
         X.MicroCat.time(nMicroCat,1) = getTime(S);
         S = fgetl(fid);
         X.MicroCat.ctdTime(nMicroCat,1) = datenum([stringtokenizer(S,3,',') ' ' stringtokenizer(S,4,',')]);
         X.MicroCat.temp(nMicroCat,1) = sscanf(stringtokenizer(S,1,','),'%f');
         X.MicroCat.sal(nMicroCat,2) = sscanf(stringtokenizer(S,2,','),'%f');
         
      catch 
         nMicroCat = nMicroCat - 1;
      end
      
   case 'Shutter0'
      nShutter0 = nShutter0  + 1;
      X.Shutter0.time(nShutter0,1) = getTime(S);
      X.Shutter0.pos(nShutter0,1) = sscanf(stringtokenizer(S,5), '%f');
      X.Shutter0.stat(nShutter0,1) = sscanf(stringtokenizer(S,7), '%f');
      X.Shutter0.shutter(nShutter0,1) = sscanf(stringtokenizer(S,9), '%f');
      X.Shutter0.try_(nShutter0,1) = sscanf(stringtokenizer(S,11), '%f');
      X.Shutter0.rtn(nShutter0,1) = sscanf(stringtokenizer(S,13), '%f');
      open = stringtokenizer(S,14);
      if strcmp('open', lower(open))
         o = 1;
      else
         o = 0;
      end
      X.Shutter0.open(nShutter0, 1) = o;
      
   case 'OASIS'
      nOASIS = nOASIS + 1;
      
   case 'Fluorometer'
      nFluorometer = nFluorometer + 1;
      X.Fluorometer.time(nFluorometer,1) = getTime(S);
      X.Fluorometer.volt(nFluorometer,1) = sscanf(stringtokenizer(S,4), '%f');
      
   case 'SpecPRR'
      nSpecPRR = nSpecPRR + 1;
      X.SpecPRR.time(nSpecPRR,1) = getTime(S);
      nn = 1;
      for i = 1:12
         S     = fgetl(fid);
         n     = 1;
         while n < 9
            typeBuf  = stringtokenizer(S,n);
            if ~isempty(typeBuf)
               type{nn} = typeBuf;
               value(nn) = sscanf(stringtokenizer(S,n+1),'%f');
               nn = nn + 1;
            end
            
            n = n + 2;
         end
      end
      
      uType = unique(type);
      for iType = uType
         nt = sum(strcmp(iType,type));
         j = strmatch(iType,type);
         X = setfield(X,['SpecPRR.' char(iType)],{nSpecPRR, 1:nt}, value(j));
      end
      
   case 'pCO2'
      npCO2 = npCO2 + 1;
      
   otherwise
      % Do nothing
   end
   
   
end

function d = getTime(S)
ymd   = stringtokenizer(S,2);
hms   = stringtokenizer(S,3);
Year  = stringtokenizer(ymd,1,'/');
Month = stringtokenizer(ymd,2,'/');
Day   = stringtokenizer(ymd,3,'/');
d     = datenum([Month '/' Day '/' Year ' ' hms]);

