function seawifsMatchupFormat(filepath)
% This script collects Lwn data from a selection of *.LWN files created by Prosoft version 5.2b
% The relevant data are compiled into a format suitable for the SeaBASS database
% Note that this format is for SeaWiFS matchups only, and does not include the SPMR profile data, just the Lwn
% This script just grabs the Lwn data and compiles it - you have to put the correct SeaBASS header on after
% See http://seabass.gsfc.nasa.gov/~seabass/seabass/html/lw_format_ex.txt for example of this format
% A separate script is used for compiling the profile data into SeaBASS format
% This script also uses dy2date, written by Brian Schlining, so make sure that script exists in this directory

% Brian Schlining
% 30 Jan 2001

%[infile inpath] = uigetfile('*.LWN','Select input *.LWN file, cancel to exit this loop',0,0);

filenames = getfname(fullfile(filepath, '*.lwn'));

if isempty(filenames)
   warning(['No Lwn files were found in ' filepath]);
   return
end

n         = size(filenames, 1);

[p1, cruise, e1] = fileparts(filepath);   % Get the cruise name from the directory

fOut = fopen(fullfile(filepath, [cruise '.lwn']), 'w');  % create a file called <cruise>.lwn

for i = 1:n
   filename = filenames(i, :);
   fIn = fopen(filename, 'r');
   for i=1:7
      lineS = fgetl(fin);
   end
   lineS = fgetl(fin);
   jday = sscanf(lineS,'SATHDR DATE %f');
   lineS = fgetl(fin);
   decimal_time = sscanf(lineS,'SATHDR STARTTIME %f');
   lineS = fgetl(fin);
   latitude = sscanf(lineS,'SATHDR LAT %f');
   lineS = fgetl(fin);
   longitude = sscanf(lineS,'SATHDR LON %f');
   for i=1:8
      lineS = fgetl(fin);
   end
   
   while strcmp(lineS(1),'#')
      lineS = fgetl(fin);
   end
   
   hour = floor(decimal_time);
   minute = floor((decimal_time-hour)*60);
   second = 0;
   %year = str2num(input('Enter the 2-digit year, eg 97\n\n     ','s'));
   year = 98;
   
   if rem(year,4) == 0
      decimal_day = year + jday/366;
   else
      decimal_day = year + jday/365;
   end
   month = str2num(datestr(dy2date(1900+decimal_day),5));
   day = str2num(datestr(dy2date(1900+decimal_day),7));
   fprintf(fout,'%04.0f %02d %02d %02d %02d %02d %f %f ', year+1900, month, day, hour, minute, second, latitude, longitude); 
   
   while ~feof(fin)
      data = sscanf(lineS,'%f');
      if (floor(data(1)) == 412) | (floor(data(1)) == 442) | (floor(data(1)) == 490) | (floor(data(1)) == 509) | (floor(data(1)) == 554) | (floor(data(1)) == 670) 
         fprintf(fout,'%f ', data(10));
      end
      lineS = fgetl(fin);
   end
   %data = sscanf(lineS,'%f');
   %fprintf(fout,'%f ', data(10));    
   
   frewind(fin)
   
   lineS = fgetl(fin);
   while strcmp(lineS(1),'S')
      lineS = fgetl(fin);
   end
   
   while strcmp(lineS(1),'#')
      lineS = fgetl(fin);
   end
   
   while ~feof(fin)
      data = sscanf(lineS,'%f');
      if data(1) > 480 & data(1) < 500
         k490=data(3);
      end
      if (floor(data(1)) == 412) | (floor(data(1)) == 442) | (floor(data(1)) == 490) | (floor(data(1)) == 509) | (floor(data(1)) == 554) | (floor(data(1)) == 670) 
         fprintf(fout,'%f ', data(6));
      end
      lineS = fgetl(fin);
   end
   %data = sscanf(lineS,'%f');
   %fprintf(fout,'%f ', data(6));    
   
   fprintf(fout,'%f\n', k490);
   fclose(fin);
   [infile inpath] = uigetfile('*.LWN','Select input *.LWN file, cancel to exit this loop',0,0);
end
fclose(fout);
