function pctd2mdb(filename)
% PCTD2MDB - Convert  CTD files from gp cruises to a format used by Access
%
% This function reads in a 1m interval ctd file and decimates the data to the 
% following depths [0:5:200 210:10:500]. It also parses information out of the
% header and writes it to a new column

% 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) > 3
      if strcmp(s(1:3), 'CAS')
         n = 1;
      end
   end
   
   switch n
   case 1
      % Get the Cruise name (i.e GP1-99_KA) and CTD ID (i.e. KA10011)
      a     = findstr('GP', s);
      ix    = findstr('-',s);
      b     = min(ix);
      c     = ix(2);
      d     = ix(3);
      cruiseName = s(a:d-1);
      ctdId = [s(c+1:d-1) s(a+2:b-1) s(d+1:d+4)]; 
      
      % Get the date
      a     = findstr('DATE', s);
      dateS = [s(a+5:a+6) '-' s(a+8:a+10) '-' s(a+12:a+13)];
      
      % Get the time
      a     = findstr('TIME', s);
      timeS = [s(a+5:a+6) ':' s(a+7:a+8)];
      
      n = n + 1;
   case 2
      % Get the latitude
      a = findstr('LAT', s);
      lat1 = sscanf(s(a+4:a+5),'%f');
      lat2 = sscanf(s(a+7:a+10),'%f');
      lat  = lat1 + lat2./60;
      if strcmp(s(a+11),'S')
         lat = -lat;
      end
      
      % Get the Longitude
      a = findstr('LONG', s);
      lon1 = sscanf(s(a+5:a+7),'%f');
      lon2 = sscanf(s(a+9:a+12),'%f');
      lon  = lon1 + lon2./60;
      if strcmp(s(a+13),'W')
         lon = -lon;
      end
      
      n = n + 1;
      
   case 3
      n = n + 1;
   case 4
      n = n + 1;
   case 5
      n = n + 1;
   case 6
      n = n + 1;
   case 7
      n = n + 1;
   case 8
      n = n + 1;
   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);
