%% ncoutn2.m : writes AUV data to netcdf format;
%% Note that if you are writing to netcdf from matlab, you must
%% have installed the netcdf toolbox and have it in your path.
%% Usage : 
%% ncoutn2;
%%
%% Revision history
%% ----------------------------
%% August 2000
%% ascii write standards established during August 2000 MUSE to match 
%% ROV ascii data files:
%% John Ryan and Mike McCann
%%
%% June 2001
%% Brian Schlining coded a template for netcdf writing from MATLAB, ensuring
%% that the file would meet standards of self description and portability.
%% John Ryan adapted template to the suite of science data collected by the
%% MBARI Odyssey IIb AUV operated during MUSE day operations.
%%
%% September 2001
%% Nicole Tervalon simplified and modified to use as generic netcdf output
%% script for use during ALTEX October 2001 test cruise
%% ----------------------------


%%----------------------------
%%   netcdf 
%%----------------------------
 xpfile=['calN2.nc'];
 disp([' Exporting data to netcdf file ' xpfile]);

 timeunits = ['Days since ',num2str(startyear),'-',num2str(startmonth),'-',num2str(startday),' 00:00:00'];

 %% Define path for netcdf toolbox
 % myncpath
 
 outfile = netcdf(xpfile, 'clobber'); % outfile is a netcdf object
 if isempty(outfile)
    error(['Unable to create ' xpfile]);
 end

 missingval = -999;
 idx=find(isnan(n2depth)); if ~isempty(idx); n2depth(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(n2)); if ~isempty(idx); n2(idx)=repmat(missingval,size(idx)); end

 %%---------------------------
 %% Add global Attributes
 %%---------------------------
 [Year Month Day]     = datevec(now);
 dateS                = sprintf('%02i/%02i/%04i', Month, Day, Year);
 
 outfile.conventions  = 'MBARI/auv/ALTEX';
 outfile.creationDate = dateS;
 outfile.lastModified = dateS;
 outfile.platform     = 'ALTEX';
 outfile.keywords     = 'AUV, nitrogen';
 
 %%-----------------------------------------------------------------------------
 % Coordinate variable (using time as key) cannot have any missing values; check
 %%-----------------------------------------------------------------------------
 % if any(isnan(ILAT) | isnan(ILAT) | isnan(ILAT) | isnan(ILAT));
 if any(isnan(n2abstime));
  disp(['Coordinate variable (time) has missing data; no netcdf file written.']);
  return
 end
 
 %% Time, unlimited dimension, double precision needed
 outfile('time')                = 0;                     % Define Unlimited dimension
 outfile{'time'}                = ncdouble('time');
 outfile{'time'}.long_name      = 'Time GMT';
 outfile{'time'}.units          = timeunits;
 
 %% Depth 
 outfile{'n2depth'}                = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'n2depth'}.long_name      = 'Depth';
 outfile{'n2depth'}.units          = 'm';
 outfile{'n2depth'}.FillValue_     = -999;
 outfile{'n2depth'}.missing_value  = -999;
 
 %% Nitrogen 
 outfile{'n2'}                = ncfloat('time');
 outfile{'n2'}.long_name      = 'Nitrogen concentration';
 outfile{'n2'}.units          = 'ml/l';
 outfile{'n2'}.FillValue_     = -999;
 outfile{'n2'}.missing_value  = -999;
 
 
 outfile{'time'}(1:length(n2abstime)) = n2abstime;
 outfile{'n2depth'}(:) = n2depth;
 outfile{'n2'}(:) = n2;
 close(outfile)

