% This script formats the Satlantic OCP profile data (not Lwn data) ready for SeaBASS.
% See http://seabass.gsfc.nasa.gov/~seabass/seabass/html/simb_mer.txt for example of this 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('*.BIN','Select input *.BIN 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);
   year = sscanf(lineS,'SATHDR YEAR %f');
   for i=1:2
      lineS = fgetl(fin);
   end
   lineS = fgetl(fin);
   latitude = sscanf(lineS,'SATHDR LATITUDE %f');
   lineS = fgetl(fin);
   longitude = sscanf(lineS,'SATHDR LONGITUDE %f');
   lineS = fgetl(fin);
   decimal_time = sscanf(lineS,'SATHDR STARTTIME %f');
   for i=1:25
      lineS = fgetl(fin);
   end
   hour = floor(decimal_time);
   minute = floor((decimal_time-hour)*60);
   second = 0;
   if rem(year,4) == 0
      decimal_day = year + jday/366;
   else
      decimal_day = year + jday/365;
   end
   month = str2num(datestr(dy2date(decimal_day),5));
   day = str2num(datestr(dy2date(decimal_day),7));
   outfile = sprintf('gp7-98-ka_%04.0f%02.0f%02.0f%02.0f%02.0f%02.0f.spmr', year, 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\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, 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=Pro023cu.cal,Ref023cu.cal\n');
   fprintf(fout, '/station=%s\n', infile(1:8));
   fprintf(fout, '/missing=-9999,-99.99\n');
   fprintf(fout, '/fields=PRES,Lu412.3,Lu442.5,Lu490.2,Lu510.4,Lu519.6,Lu554.7,Lu564.4,Lu619.0,Lu665.5,Lu669.9,Lu682.9,Lu700.6,Lu780.5,Ed412.1,Ed442.8,Ed490.1,Ed509.3,Ed519.7,Ed554.8,Ed564.8,Ed619.2,Ed665.7,Ed670.1,Ed683.4,Ed700.3,Ed780.3\n');
   fprintf(fout, '/units=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,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm/sr,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm,uW/cm2/nm\n');
   fprintf(fout, '/delimiter=space\n');
   fprintf(fout, '/end_header@\n');
   
   while ~feof(fin)
      data = str2num(lineS);
      data(find(isnan(data))) = -99.99;
      fprintf(fout,'%f ', data(2:28));
      fprintf(fout,'\n');
      lineS = fgetl(fin);
   end
   fclose(fin);
   fclose(fout);
   [infile inpath] = uigetfile('*.BIN','Select input *.BIN file, cancel to exit this loop',0,0);
end
