function simbiosLwnFormat(filename)
% This script collects Lwn data from an individual *.LWN files created by Prosoft version 5.2b
% The relevant data are transformed into a format suitable for the SeaBASS database
% Note that this script is different to seawifs_matchup_format.m which compiles the Lw and Es data for an entire cruise
% See http://seabass.gsfc.nasa.gov/~seabass/seabass/html/simb_mer.txt for hints regarding 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


%===========
% Open File
%===========
if ~nargin | isempty(filename)
   [infile inpath] = uigetfile('*.lwn','Select SPMR BIN data file',0,0);
   if infile == 0
      y = [];
      return                            % Return if CANCEL is selected
   end
   filename = [inpath infile];
end

warning off
x = readSpmr(filename);
warning on

% Parse the year out of the staion ID
for i = 1:length(x.STATIONID)
   if abs(x.STATIONID(i) >= 48 & x.STATIONID(i) <= 57)
      start = i;
      break
   end
end

year = sscanf(x.STATIONID(start:start+1), '%f');
if year < 95
   year = year + 2000;
else
   year = year + 1900;
end

dayOfYear = str2num(x.DATE) + str2num(x.STARTTIME)/24;  % Day of Year
castDate = jul2date(dayOfYear, year);                       % Convert date to matlab format
[year month day hour minute second] = datevec(castDate);    

jFile            = java.io.File(filename);
fullFilename     = char(jFile.getCanonicalPath);
[filepath n1 e1] = fileparts(fullFilename);
[p2 n2 e2]       = fileparts(filepath);
[p3 cruise e3]   = fileparts(p2);
filename         = [n1 e1];    

longitude = sscanf(x.LONGITUDE, '%f');
latitude  = sscanf(x.LATITUDE, '%f');

outFile   = sprintf('%s_%04.0f%02.0f%02.0f%02.0f%02.0f%02.0f.spmr', cruise, year, month, day, hour, minute, second);
fout      = fopen(fullfile(filepath, outFile),'w');

fprintf(fout, '/begin_header\n');
fprintf(fout, '/affiliations=Monterey_Bay_Aquarium_Research_Institute\n');
fprintf(fout, '/investigators=Francisco_Chavez,Peter_Strutton,Brian_Schlining\n');
fprintf(fout, '/experiment=TAO_Moorings\n');
fprintf(fout, '/cruise=%s\n', cruise);
fprintf(fout, '/contact=chfr@mbari.org,stpe@mbari.org,brian@mbari.org\n');
fprintf(fout, '/original_file_name=%s\n', filename);
fprintf(fout, '/data_file_name=%s\n', outFile);
fprintf(fout, '/data_type=cast\n');
fprintf(fout, '/documents=%s_spmr_processing.txt\n', cruise);
fprintf(fout, '/west_longitude=%0.3f[DEG]\n', longitude);
fprintf(fout, '/east_longitude=%0.3f[DEG]\n', longitude);
fprintf(fout, '/north_latitude=%0.3f[DEG]\n', latitude);
fprintf(fout, '/south_latitude=%0.3f[DEG]\n', latitude);
fprintf(fout, '/water_depth=2000m\n');
fprintf(fout, '/start_date=%04.0f%02.0f%02.0f\n', year, month, day);
fprintf(fout, '/end_date=%04.0f%02.0f%02.0f\n', year, month, day);
fprintf(fout, '/start_time=%02.0f:%02.0f:%02.0f[GMT]\n', hour, minute, second);
fprintf(fout, '/end_time=%02.0f:%02.0f:%02.0f[GMT]\n', hour, minute, second);
fprintf(fout, '/calibration_files=%s,%s\n', x.REFCAL, x.PROCAL);
fprintf(fout, '/station=%s\n', x.STATIONID);
fprintf(fout, '/missing=-9999,-99.99\n');

% Locate the pressure, Lu and Ed columns
c1 = find(strcmp(lower(x.fields), 'pres'));
c1 = c1(1);    % There's duplicate pressure columns in some files, use the first one only
c2 = strmatch('lu', lower(x.fields));
c3 = strmatch('ed', lower(x.fields));

nnn = 0;
for i = 1:length(c2)                            % ignore lu fields that end with dark
   if isempty(findstr(lower(x.fields{c2(i)}), 'dark'));
      nnn = nnn + 1;
      buf(nnn) = c2(i);
   end
end
c2 = buf';

buf = [];
nnn = 0;
for i = 1:length(c3)                            % ignore ed fields that end with dark
   if isempty(findstr(lower(x.fields{c3(i)}), 'dark'));
      nnn = nnn + 1;
      buf(nnn) = c3(i);
   end
end
c3 = buf';
      
% Write the column names
fprintf(fout, '/fields=pressure');
for i = 1:length(c2)
   fprintf(fout, ',%s', x.fields{c2(i)});
end
for i = 1:length(c3)
   fprintf(fout, ',%s', x.fields{c3(i)});
end
fprintf(fout, '\n');

% Add the correct units
fprintf(fout, '/units=dbar');
for i = 1:length(c2)
   fprintf(fout, ',uW/cm^2/nm/sr', x.fields{c2(i)});
end
for i = 1:length(c3)
   fprintf(fout, ',uW/cm^2/nm', x.fields{c3(i)});
end
fprintf(fout, '\n');

fprintf(fout, '/delimiter=space\n');
if ~isempty(x.comments_)
   for i = 1:length(x.comments_)
      if strcmp(x.comments_(i), '#')
         x.comments_(i) = '!';
      end
   end
end
fprintf(fout, '! %s\n!\n', x.COMMENTS);
fprintf(fout, '%s', x.comments_);
fprintf(fout, '/end_header@\n');

% Write the data
x.data(isnan(x.data)) = -999;
for i = 1:size(x.data, 1)
   fprintf(fout, '%f ', x.data(i, [c1 c2' c3']));
   fprintf(fout, '\n');
end

fclose(fout);

