%% ncoutips.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 : 
%% ncoutips;
%%
%% 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=['ips.nc'];
 fprintf('   Exporting data to netcdf file %s\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(ipspresraw)); if ~isempty(idx); ipspresraw(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(ipsdepth)); if ~isempty(idx); ipsdepth(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(ipsdraft)); if ~isempty(idx); ipsdraft(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(ipsdraftraw)); if ~isempty(idx); ipsdraftraw(idx)=repmat(missingval,size(idx)); end
 idx=find(isnan(ipsrange)); if ~isempty(idx); ipsrange(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, ice draft';
 
 %%-----------------------------------------------------------------------------
 % 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(ipsabstime));
  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{'ipspresraw'}                = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'ipspresraw'}.long_name      = 'IPS pressure (raw)';
 outfile{'ipspresraw'}.units          = 'dbar';
 outfile{'ipspresraw'}.FillValue_     = -999;
 outfile{'ipspresraw'}.missing_value  = -999;
 
 %% Depth 
 outfile{'ipsdepth'}                = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'ipsdepth'}.long_name      = 'IPS depth';
 outfile{'ipsdepth'}.units          = 'm';
 outfile{'ipsdepth'}.FillValue_     = -999;
 outfile{'ipsdepth'}.missing_value  = -999;
 
 %% Draft 
 outfile{'ipsdraft'}                = ncfloat('time');
 outfile{'ipsdraft'}.long_name      = 'IPS ice draft';
 outfile{'ipsdraft'}.units          = 'm';
 outfile{'ipsdraft'}.FillValue_     = -999;
 outfile{'ipsdraft'}.missing_value  = -999;
 
 %% Draft (calculated raw)
 outfile{'ipsdraftraw'}                = ncfloat('time');
 outfile{'ipsdraftraw'}.long_name      = 'IPS ice draft (raw)';
 outfile{'ipsdraftraw'}.units          = 'm';
 outfile{'ipsdraftraw'}.FillValue_     = -999;
 outfile{'ipsdraftraw'}.missing_value  = -999;
  
 %% Range 
 outfile{'ipsrange'}                = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'ipsrange'}.long_name      = 'IPS range to ice';
 outfile{'ipsrange'}.units          = 'm';
 outfile{'ipsrange'}.FillValue_     = -999;
 outfile{'ipsrange'}.missing_value  = -999;

 
 outfile{'time'}(1:length(ipsabstime)) = ipsabstime;
 outfile{'ipspresraw'}(:) = ipspresraw;
 outfile{'ipsdepth'}(:) = ipsdepth;
 outfile{'ipsdraft'}(:) = ipsdraft;
 outfile{'ipsdraftraw'}(:) = ipsdraft;
 outfile{'ipsrange'}(:) = ipsdraft;
 close(outfile)

