% 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

close all
clear all

[infile inpath] = uigetfile('n98g*.LWN','Select input *.LWN file, cancel to exit this loop',0,0);
while infile ~= 0,
   if infile == 0
      return
   end
   FileName = [inpath infile];
   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 = 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));
   
   outfile = sprintf('gp7-98-ka_%04.0f%02.0f%02.0f%02.0f%02.0f%02.0f.lwn', year+1900, month, day, hour, minute, second);
   fout = fopen(outfile,'w');
   
   fprintf(fout, '/begin_header\n');
   fprintf(fout, '/affiliations=Monterey_Bay_Aquarium_Research_Institute\n');
   fprintf(fout, '/investigators=Francisco_Chavez,Peter_Strutton\n');
   fprintf(fout, '/experiment=TAO_Moorings\n');
   fprintf(fout, '/cruise=gp7-98-ka\n');
   fprintf(fout, '/contact=chfr@mbari.org,stpe@mbari.org\n');
   fprintf(fout, '/parameters=Lu,Ed,Ku,Kd\n');
   fprintf(fout, '/data_file_name=%s\n', outfile);
   fprintf(fout, '/original_file_name=%s\n', infile);
   fprintf(fout, '/data_type=cast\n');
   fprintf(fout, '/documents=gp7-98-ka_spmr.txt\n');
   fprintf(fout, '/west_longitude=%7.3f[DEG]\n', longitude);
   fprintf(fout, '/east_longitude=%7.3f[DEG]\n', longitude);
   fprintf(fout, '/north_latitude=%6.3f[DEG]\n', latitude);
   fprintf(fout, '/south_latitude=%6.3f[DEG]\n', latitude);
   fprintf(fout, '/start_date=%04.0f%02.0f%02.0f\n', year+1900, month, day);
   fprintf(fout, '/end_date=%04.0f%02.0f%02.0f\n', year+1900, 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=Pro023du.cal,Ref023du.cal\n');
   fprintf(fout, '/station=%s\n', infile(1:8));
   fprintf(fout, '/missing=-9999,-99.99\n');
   fprintf(fout, '/fields=lambda,Kd,Es(z0),Es(0-),Es(0+),Ku,Lu(z0),Lu(0-),Lu(0+),Luwn,Ls(z0),Ls(0-),Ls(0+),Lswn,Ls/Lu,Lsap(0-),Lsap(0+),Lswnap\n');
   fprintf(fout, '/units=nm,1/m,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,1/m,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,none,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr\n');
   fprintf(fout, '/delimiter=space\n');
   fprintf(fout, '/end_header@\n');
   
   while ~feof(fin)
      data = sscanf(lineS,'%f');
      fprintf(fout,'%5.1f ', data(1));
      fprintf(fout,'%f ', data(3:19));
      fprintf(fout,'\n', data(3:19));
      lineS = fgetl(fin);
   end
   data = sscanf(lineS,'%f');
   fprintf(fout,'%5.1f ', data(1));
   fprintf(fout,'%f ', data(3:19));
   fprintf(fout,'\n', data(3:19));
   fclose(fin);
   fclose(fout);
   [infile inpath] = uigetfile('n98g*.LWN','Select input *.LWN file, cancel to exit this loop',0,0);
end
