%% ncout1.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 : 
%% ncout1;
%%
%% 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=['calCTD1.nc'];
 fprintf('   Exporting data to netcdf file %12s\n', 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(s1)); if ~isempty(idx); s1(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(p1)); if ~isempty(idx); p1(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(t1)); if ~isempty(idx); t1(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(o2)); if ~isempty(idx); o2(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(d1)); if ~isempty(idx); d1(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(obs)); if ~isempty(idx); obs(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(dpth1)); if ~isempty(idx); dpth1(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, temperature, salinity, density, optical backscatter, oxygen';
 
 %%-----------------------------------------------------------------------------
 % 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(c1Aabstime));
  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;
 
 %% Press
 outfile{'Press'}                = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'Press'}.long_name      = 'Pressure';
 outfile{'Press'}.units          = 'dbar';
 outfile{'Press'}.FillValue_     = -999;
 outfile{'Press'}.missing_value  = -999;
 
 %% Depth 
 outfile{'Depth'}                = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'Depth'}.long_name      = 'Depth';
 outfile{'Depth'}.units          = 'm';
 outfile{'Depth'}.FillValue_     = -999;
 outfile{'Depth'}.missing_value  = -999;
 
 %% Salinity
 outfile{'s1'}                = ncfloat('time');
 outfile{'s1'}.long_name      = 'Salinity';
 outfile{'s1'}.units          = 'psu (PSS-78)';
 outfile{'s1'}.FillValue_     = -999;
 outfile{'s1'}.missing_value  = -999;
 
 %% Temperature
 outfile{'t1'}                = ncfloat('time');
 outfile{'t1'}.long_name      = 'Temperature';
 outfile{'t1'}.units          = 'deg. C (IPTS-68)';
 outfile{'t1'}.FillValue_     = -999;
 outfile{'t1'}.missing_value  = -999;
 
 %% Density
 outfile{'dens1'}                = ncfloat('time');
 outfile{'dens1'}.long_name      = 'Density';
 outfile{'dens1'}.units          = 'kg/m**3';
 outfile{'dens1'}.FillValue_     = -999;
 outfile{'dens1'}.missing_value  = -999;
 
 %% Optical Backscatter 
 outfile{'obs'}                = ncfloat('time');
 outfile{'obs'}.long_name      = 'Optical backscatter';
 outfile{'obs'}.units          = 'volts (uncalibrated)';
 outfile{'obs'}.FillValue_     = -999;
 outfile{'obs'}.missing_value  = -999;
 
 %% Oxygen 
 outfile{'o2'}                = ncfloat('time');
 outfile{'o2'}.long_name      = 'Dissolved Oxygen concentration';
 outfile{'o2'}.units          = 'mL/L';
 outfile{'o2'}.FillValue_     = -999;
 outfile{'o2'}.missing_value  = -999;
 
 
 outfile{'time'}(1:length(c1Aabstime)) = c1Aabstime;
 %
 % Multiply p1 by 10 to get decibars (dbar)
 %
 outfile{'Press'}(:) = p1*10;
 outfile{'Depth'}(:) = dpth1;
 outfile{'s1'}(:) = s1;
 outfile{'t1'}(:) = t1;
 outfile{'dens1'}(:) = d1;
 outfile{'obs'}(:) = obs;
 outfile{'o2'}(:) = o2;
 close(outfile)

