%% ncoutnav.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=['rawNAV.nc'];
 disp(['Exporting data to netcdf file ' xpfile]);

 %% 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(ILAT)); if ~isempty(idx); ILAT(idx)=repmat(missingval,size(idx)); end
  idx=find(isnan(ILON)); if ~isempty(idx); ILON(idx)=repmat(missingval,size(idx)); end
  idx=find(isnan(IDIST)); if ~isempty(idx); IDIST(idx)=repmat(missingval,size(idx)); end
  idx=find(isnan(Depth)); if ~isempty(idx); Depth(idx)=repmat(missingval,size(idx)); end
  idx=find(isnan(XPOS)); if ~isempty(idx); XPOS(idx)=repmat(missingval,size(idx)); end
  idx=find(isnan(YPOS)); if ~isempty(idx); YPOS(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(navyearday));
  disp(['Coordinate variable (time) has missing data; no netcdf file written.']);
  return
 end
 
nroll = [dataNav(:,mPhi)*180/pi];
npitch = [dataNav(:,mTheta)*180/pi];
nyaw = [dataNav(:,mPsi)*180/pi];
posx0 = dataNav(1,mPos_x);
posy0 = dataNav(1,mPos_y);
nposx = dataNav(:,mPos_x)-posx0;
nposy = dataNav(:,mPos_y)-posy0;
 
 %% 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          = 'seconds since 2001-01-01 00:00:00';
 
 %% Latitude
 outfile{'ILAT'}            = ncfloat('time');  % Specify Co-ordinate variable dimensions
 outfile{'ILAT'}.long_name  = 'Interpolated Latitude (from known mission endpoints)';
 outfile{'ILAT'}.units      = 'degrees_north';
 outfile{'ILAT'}.FillValue_     = -999;
 outfile{'ILAT'}.missing_value  = -999;
 
 %% Longitude
 outfile{'ILON'}           = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'ILON'}.long_name = 'Interpolated Longitude (from known mission endpoints)';
 outfile{'ILON'}.units     = 'degrees_east';
 outfile{'ILON'}.FillValue_     = -999;
 outfile{'ILON'}.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;
 
 %% Distance 
 outfile{'IDIST'}           = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'IDIST'}.long_name = 'Interpolated cumulative segment horizontal distance';
 outfile{'IDIST'}.units     = 'km';
 outfile{'IDIST'}.FillValue_     = -999;
 outfile{'IDIST'}.missing_value  = -999;
 
 %% X position (dead reckoning)
 outfile{'XPOS'}                = ncfloat('time');
 outfile{'XPOS'}.long_name      = 'X position (dead reckoning)';
 outfile{'XPOS'}.units          = 'm';
 outfile{'XPOS'}.FillValue_     = -999;
 outfile{'XPOS'}.missing_value  = -999;
 
 %% Y position (dead reckoning)
 outfile{'YPOS'}                = ncfloat('time');
 outfile{'YPOS'}.long_name      = 'Y position (dead reckoning)';
 outfile{'YPOS'}.units          = 'm';
 outfile{'YPOS'}.FillValue_     = -999;
 outfile{'YPOS'}.missing_value  = -999;
 
 end
 
 
 outfile{'time'}(1:length(navyearday)) = navyearday;
 outfile{'ILAT'}(:) = ILAT;
 outfile{'ILON'}(:) = ILON;
 outfile{'Depth'}(:) = ndepth;
 outfile{'IDIST'}(:) = IDIST;
 outfile{'XPOS'}(:) = XPOS;
 outfile{'YPOS'}(:) = YPOS;
 close(outfile)

