function oms_makeSeabassProducts_(infile)
% oms_makeSeabassProducts_ - Creates seabass formatted data form a products file
%
% Use as: oms_makeSeabassProducts_(infile)
% inputs: infile = an OMS netcdf object
%
% The text files will be written to the same directory as the input file

% Brian Schlining
% 22 Aug 2000

inFilename = char(ncnames(infile));					% Get the filename
[filepath filename ext] = fileparts(inFilename);	% Parse it

%===============
% Missing value
%===============
mv = -999;

%=============
% Lat and Lon
%=============
lat = infile{'latitude'}(:);										% Add the lat and lon
lon = infile{'longitude'}(:);

%==========
% Get Time
%==========
t    = nctime(infile);
rows = length(t);
tFields = {'year','month','day','hour','minute','second'};
tUnits  = {'yyyy','mo','dd','hh','mn','ss'};

%=======================
% Get the unique depths
%=======================
S = oms_getinfo(infile);
depths = unique(S.instrumentDepth);
depths = depths(find(~isnan(depths)));

%============
% Other Info
%============
mooring  = lower(infile.mooring(:));
if strcmp(mooring, 'ep1') | strcmp(mooring, 'ep2')
   f = ['\\tsunami.shore.mbari.org\oasis\eqpac\cfg\' mooring '.cfg'];
elseif strcmp(mooring, 'm1') | strcmp(mooring, 'm2')
   f = ['\\tsunami.shore.mbari.org\oasis\cfg\' mooring '.cfg'];
end
calFiles = oms_getcfgfiles(f);

%=======================================
% Write to the output file in this loop
%=======================================
for i = 1:length(depths);
   
   depth = depths(i);
   
   outFile = [filepath filesep filename  num2str(i) '.ftp'];  % Create an output filename
   fid     = fopen(outFile, 'wt');				                    % Open the output file for writing
   
   OK      = find(S.instrumentDepth == depth);               % Get instuments at a single depth
   good    = find(~strcmp(S.symbol(OK), 'null') & ~strcmp(S.symbol(OK), 'v') ...
      & ~strcmp(S.symbol(OK), 'f') & ~strcmp(S.symbol(OK), 'z') & (S.wavelength(OK) < 600));
   OK      = OK(good);                                           % Ignore voltage and nulls
   
   %==========================
   % Begin writing the header
   %==========================
   fprintf(fid, '/begin_header\n');
   fprintf(fid, '/affiliations=Monterey_Bay_Aquarium_Research_Institute\n');
   fprintf(fid, '/experiment=TAO_Moorings\n');
   fprintf(fid, '/investigators=Francisco_Chavez,Pete_Strutton,Brian_Schlining\n');
   fprintf(fid, '/contact=chfr@mbari.org,stpe@mbari.org,brian@mbari.org\n');
   
   parameters = unique(oms_getSeabassFieldname(chopstruct(S,OK), 0));	% Get the seabass names of each variable
   fprintf(fid, '/parameters=');
   for j = 1:length(parameters)
      fprintf(fid, '%s', parameters{j});
      if j < length(parameters)
         fprintf(fid, ',');
      else
         fprintf(fid, '\n');
      end
   end
   
   fprintf(fid, '/data_type=mooring\n');
   fprintf(fid, '/measurement_depth=%4.1f\n', depth);
   fprintf(fid, '/original_file_name=%s%s\n', filename, ext);
   fprintf(fid, '/delimiter=space\n');
   fprintf(fid, '/missing=%f\n', mv);
   
   %==========================
   % Get variable information
   %==========================
   V    = var(infile);										% Variables
   V    = V(OK);                                   % Only get the variables at the current depth
   [fields, units] = oms_getSeabassFieldname(chopstruct(S,OK));	% Get the seabass names of each variable

   
   %==============
   % Get the data
   %==============
   DATA = ones(rows, length(OK))*NaN;
   for i = 1:length(V)						            % Loop through the pertinent variables 
      DATA(:,i) = V{i}(:);  				         	% Extract the data from each variable     
   end
   DATA(find(DATA == mv)) = NaN;
   
   %==========================
   % Ignore rows of all NaN's
   %==========================
   good = find(~all(isnan(DATA'))'); % Ignore rows of all NaN's
   DATA = DATA(good, :);
   DATA(find(isnan(DATA))) = mv;
   
   %======
   % Time
   %======
   t = t(good);                   % Make the time match the data
   [Year Month Day Hour Minute Second] = datevec(t);
   Second  = round(Second);
   tMin      = min(t);											% Get the starting time
   [yearMin monthMin dayMin hourMin minuteMin secondMin] = datevec(tMin);
   secondMin = round(secondMin);								% Round this for clean looking output
   tMax      = max(t);											% Get the ending time
   [yearMax monthMax dayMax hourMax minuteMax secondMax] = datevec(tMax);
   secondMax = round(secondMax);								% Round this for clean looking output
   
   %=============
   % Lat and Lon
   %=============
   fprintf(fid, '/north_latitude=%0.3f[DEG]\n', lat);
   fprintf(fid, '/south_latitude=%0.3f[DEG]\n', lat);
   fprintf(fid, '/east_longitude=%0.3f[DEG]\n', lon);
   fprintf(fid, '/west_longitude=%0.3f[DEG]\n', lon);
   
   %======
   % Time
   %======
   fprintf(fid, '/start_date=%04i%02i%02i\n', yearMin, monthMin, dayMin);
   fprintf(fid, '/end_date=%04i%02i%02i\n', yearMax, monthMax, dayMax);
   %fprintf(fid, '/start_time=%02i:%02i:%02i[GMT]\n', hourMin, minuteMin, secondMin);
   %fprintf(fid, '/end_time=%02i:%02i:%02i[GMT]\n', hourMax, minuteMax, secondMax);
   fprintf(fid, '/start_time=00:00:01[GMT]\n');
   fprintf(fid, '/end_time=23:59:59[GMT]\n');
   
   %============
   % Other info
   %============
   fprintf(fid, '/documents=MbariSeabassMooring.readme\n');
   fprintf(fid, '/calibration_files=%s.cfg,', mooring);
   for i = 1:length(calFiles)-1
      
      fprintf(fid, '%s,', calFiles{i});
   end
   fprintf(fid, '%s\n', calFiles{end});
   
   %========================
   % Add comments into here
   %========================
   %fprintf(fid, '! Comments: Last column is quality flag; -1=bad, 0=unchecked, 1=good\n');
   %fprintf(fid, '! Comments: MorelChl[0m]490 is mean chl over the upper 20m calculated according to Morel (1988), OC2V2Chl is calculated from the NLw. See documentation.\n');
   %fprintf(fid, '! Comments: Lw is Lw calculated using the attenuation coefficient of downwelling irradiance at the relevant wavelength to back-propagate the 20m Lu data.\n');
   %fprintf(fid, '! Comments: Lw is Lwn normalized using surface Ed.\n');
   
   %==================
   % Write the fields
   %==================
   FIELDS = [tFields fields];
   fprintf(fid,'/fields=');
   for k = 1:length(FIELDS)
      fprintf(fid,'%s',FIELDS{k});
      if k < length(FIELDS)
         fprintf(fid, ',');
      else
         fprintf(fid, '\n');
      end
      
   end
   
   %=================
   % Write the units
   %=================
   UNITS = [tUnits units];
   fprintf(fid,'/units=');
   for k = 1:length(UNITS)
      fprintf(fid,'%s',UNITS{k});
      if k < length(UNITS)
         fprintf(fid, ',');
      else
         fprintf(fid, '\n');
      end
   end
   
   fprintf(fid, '/end_header\n');
   
   %================
   % Write the data
   %================
   [r c]  = size(DATA);
   
   
   for n = 1:r
      fprintf(fid,'%4i %02i %02i ',Year(n), Month(n), Day(n));
      fprintf(fid,'%02i %02i %02i ',Hour(n), Minute(n), Second(n));         % Time in GMT
      for m = 1:c
         fprintf(fid,'%12.6f ', DATA(n,m));               % The data
      end
      fprintf(fid,'\n');
   end
   
   fclose(fid);
   
end
