function matchup2mdb(filename)
% MATCHUP2MDB - Convert match up files of seawif and cruise data
%
% This function reads the reform files of seawif/cruise data matchups and
% outputs in a database friendly text format.

% Brian Schlining
% 01 Oct 1999

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

%===========
% Read File
%===========
fid    = fopen(filename,'rt');
[p f e] = fileparts(filename);
fidOut = fopen([p filesep f '.prc'],'wt');

while ~feof(fid)
   % Find the Header
   % parse the header
   % read data unitl the next header occurs
   s = fgetl(fid);
   
   if length(s) > 10
      if strcmp(s(1:10), '==========')
         n = 1;
      end
   end
   
   switch n
   case 2   % Get Experiment
      a   = findstr(':', s);
      EXP = s(a+2:end);
      n   = n + 1;
      
   case 3	% Get the SeaWiFS file
      a   = findstr(':', s);
      SWF = s(a+2:end); 
      n   = n + 1;
      
   case 4   % Get the SAT Date
      ix 	  = findstr(':', s);
      iy 	  = findstr('-',s);
      a 		  = ix(1);
      b 		  = iy(1);
      c 		  = iy(2);
      d 		  = ix(2);
      e       = ix(3);
      ymd     = sscanf(s(a+2:c+2),'%4i-%i-%i')
      hms     = sscanf(s(c+3:end),'%2i:%2i:%2i')
      SATdate = datestr(datenum(ymd(1),ymd(2),ymd(3),hms(1),hms(2),hms(3)));
      n       = n + 1;
      
   case 5   % Get the ENV date
      ix = findstr(':', s);
      iy = findstr('-',s);
      a = ix(1);
      b = iy(1);
      c = iy(2);
      d = ix(2);
      e = ix(3);
      f = max(findstr('ENV', s));
      g = ix(4);
      ymd = sscanf(s(a+2:c+2),'%4i-%i-%i')
      hms = sscanf(s(c+3:f-1),'%2i:%2i:%2i')
      ENVdate = datestr(datenum(ymd(1),ymd(2),ymd(3),hms(1),hms(2),hms(3)));
      DT = sscanf(s(f+1:end));
      n = n + 1;
      
   case 6 % Get the solar angles
      ix = findstr('Solar', s);
      iy = findstr(':', s);
      a  = ix(1);
      b = iy(2);
      c = ix(3);
      solZ = sscanf(s(a+1:b-1),'%f');
      solA = sscanf(s(c+1:end),'%f');
      
   case 7 % Get the sat angles
      ix = findstr('SAT', s);
      iy = findstr(':', s);
      a  = ix(1);
      b = iy(2);
      c = ix(3);
      satZ = sscanf(s(a+1:b-1),'%f');
      satA = sscanf(s(c+1:end),'%f');
      
   case 8
      ix = findstr('=', s);
      iy = findstr(';', s);
      a = ix(1);
      b = iy(1);
      c = ix(2);
      d = iy(2);
      
      
      
   case 9
      % Read data
      dat = sscanf(s,'%f');
      %fprintf(1,'Pressure %9.4f',dat(1));
      if max(ismember([0:5:200 210:10:500],dat(1)))
         %fprintf(1,'*');
         fprintf(fidOut,'%s %s %s %s %7.3f %8.3f %9.4f %7.4f %7.4f\n',...
            cruiseName, ctdId, dateS, timeS, lat, lon, dat(1), dat(2), dat(3));
      end
      %fprintf(1,'\n');
   end
   
end
fclose(fid);
fclose(fidOut);
