function xport_auv_survey(fmt);
%% xport_auv_survey: to write finalized AUV survey science data to ascii or netcdf.  
%% This must be run from the top directory of a survey (series of missions).
%% Note that if you are writing to netcdf from matlab, you must
%% have installed the netcdf toolbox and have it in your path.
%% Usage : 
%% xport_auv_survey('ascii') or xport_auv_survey('nc');
%%
%% 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.
%% ----------------------------
%%
%% Perhaps the next revision should generalize this tool to include a vector
%% of on-off toggles at the top level (placed in the survey_assembly_setup.m
%% file), for a standard variable suite, e.g. [1 1 1 0 0 1] to indicate the 
%% presence or absence of variables [T, S, ...].  Writing could then be adapted
%% to the specific instrument configuration by simply adjusting the vector values.
%%
%% Current standard variable output list:
%% TEMPORAL/SPATIAL: time lat lon depth distance xpos ypos 
%% WATER : salinity temperature density obs fluor oxygen pH
%%
%% Other file types might be made optional.
%%
%% It would also be good to include the instrument identification in the metadata,
%% at least the type of instrument used, if not also serial number.


%%----------------------------------
%% Survey set up; load processed data 
%%----------------------------------
survey_assembly_setup; cd products; eval(['load ' surveyfilename]);

%%----------------------------
%%   ascii
%%----------------------------
if strcmp(fmt,'ascii')==1;
 xpfile=[surveyfilename '.asc'];
 disp(['Exporting data to ascii file ' xpfile]);
 odat = [time ILAT ILON Depth IDIST XPOS YPOS s t dens obs f o2 ph];
 eval(['fid=fopen(','''',xpfile,'''',',','''w''',');']);
 fprintf(fid,'%10.0f,%8.3f,%9.3f,%7.3f,%8.3f,%8.3f,%8.3f,%7.3f,%7.3f,%9.3f,%7.3f,%7.3f,%7.3f,%7.3f\n',odat');
 fclose(fid);

%%----------------------------
%%   netcdf 
%%----------------------------
elseif strcmp(fmt,'nc')==1;
 xpfile=[surveyfilename '.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

 %%---------------------------
 %% Add global Attributes
 %%---------------------------
 [Year Month Day]     = datevec(now);
 dateS                = sprintf('%02i/%02i/%04i', Month, Day, Year);
 
 outfile.conventions  = 'MBARI/auv/odyssey';
 outfile.creationDate = dateS;
 outfile.lastModified = dateS;
 outfile.platform     = 'odyssey2B';
 outfile.keywords     = 'AUV, temperature, salinity, fluorescence, optical backscatter, pH, 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(time));
  disp(['Coordinate variable (time) has missing data; not 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          = 'seconds since 1970-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_     = NaN;
 outfile{'ILAT'}.missing_value  = NaN;
 
 %% 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_     = NaN;
 outfile{'ILON'}.missing_value  = NaN;
 
 %% Depth 
 outfile{'Depth'}           = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'Depth'}.long_name = 'Depth';
 outfile{'Depth'}.units     = 'm';
 outfile{'Depth'}.FillValue_     = NaN;
 outfile{'Depth'}.missing_value  = NaN;
 
 %% 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_     = NaN;
 outfile{'IDIST'}.missing_value  = NaN;
 
 %% X position (dead reckoning)
 outfile{'XPOS'}                = ncfloat('time');
 outfile{'XPOS'}.long_name      = 'X position (dead reckoning)';
 outfile{'XPOS'}.units          = 'm';
 outfile{'XPOS'}.FillValue_     = NaN;
 outfile{'XPOS'}.missing_value  = NaN;
 
 %% Y position (dead reckoning)
 outfile{'YPOS'}                = ncfloat('time');
 outfile{'YPOS'}.long_name      = 'Y position (dead reckoning)';
 outfile{'YPOS'}.units          = 'm';
 outfile{'YPOS'}.FillValue_     = NaN;
 outfile{'YPOS'}.missing_value  = NaN;
 
 %% Salinity
 outfile{'s'}                = ncfloat('time');
 outfile{'s'}.long_name      = 'Salinity';
 outfile{'s'}.units          = 'psu';
 outfile{'s'}.FillValue_     = NaN;
 outfile{'s'}.missing_value  = NaN;
 
 %% Temperature
 outfile{'t'}                = ncfloat('time');
 outfile{'t'}.long_name      = 'Temperature';
 outfile{'t'}.units          = 'deg C';
 outfile{'t'}.FillValue_     = NaN;
 outfile{'t'}.missing_value  = NaN;
 
 %% Density
 outfile{'dens'}                = ncfloat('time');
 outfile{'dens'}.long_name      = 'Density';
 outfile{'dens'}.units          = 'kg m^{-3}';
 outfile{'dens'}.FillValue_     = NaN;
 outfile{'dens'}.missing_value  = NaN;
 
 %% Optical Backscatter 
 outfile{'obs'}                = ncfloat('time');
 outfile{'obs'}.long_name      = 'Optical Backscatter';
 outfile{'obs'}.units          = 'volts (uncalibrated)';
 outfile{'obs'}.FillValue_     = NaN;
 outfile{'obs'}.missing_value  = NaN;
 
 %% Fluorometer 
 outfile{'f'}                = ncfloat('time');
 outfile{'f'}.long_name      = 'Fluorescence';
 outfile{'f'}.units          = 'volts (uncalibrated)';
 outfile{'f'}.FillValue_     = NaN;
 outfile{'f'}.missing_value  = NaN;
 
 %% Oxygen 
 outfile{'o2'}                = ncfloat('time');
 outfile{'o2'}.long_name      = 'Oxygen concentration';
 outfile{'o2'}.units          = 'ml l^{-1}';
 outfile{'o2'}.FillValue_     = NaN;
 outfile{'o2'}.missing_value  = NaN;
 
 %% pH 
 outfile{'ph'}                = ncfloat('time');
 outfile{'ph'}.long_name      = 'pH';
 outfile{'ph'}.units          = ' ';
 outfile{'ph'}.FillValue_     = NaN;
 outfile{'ph'}.missing_value  = NaN;
 
 
 outfile{'time'}(1:length(time)) = time;
 outfile{'ILAT'}(:) = ILAT;
 outfile{'ILON'}(:) = ILON;
 outfile{'Depth'}(:) = Depth;
 outfile{'IDIST'}(:) = IDIST;
 outfile{'XPOS'}(:) = XPOS;
 outfile{'YPOS'}(:) = YPOS;
 outfile{'s'}(:) = s;
 outfile{'t'}(:) = t;
 outfile{'dens'}(:) = dens;
 outfile{'obs'}(:) = obs;
 outfile{'f'}(:) = f;
 outfile{'o2'}(:) = o2;
 outfile{'ph'}(:) = ph;
 %ncquit(outfile)	% Brian's routine to automatically update file dates
 close(outfile)
 
%%----------------------------
%%   Incorrect specification 
%%----------------------------
else
 disp(['No file written; please check file format specification (ascii or nc)']);
end
