function argnCurr2cdf(metaFile,cdfFileRoot);
% argnCurr2cdf.m  A function to write Argonaut velocity data, downloaded 
%                 from the instrument, to a netCDF (.cdf) file.  
%
%    usage:  argnCurr2cdf(metaFile,cdfFileRoot);
%
%        where:  metaFile    - an ascii file in which metadata is defined,
%                               in single quotes including the file extension
%                cdfFileRoot - the name given to the output .cdf file, no file
%                               extension necessary
%
% Copyright 2004 
% USGS Woods Hole Field Center
% Written by Charlene Sullivan
% csullivan@usgs.gov
%
% Dependencies:
%   get_meta_sontek.m
%   READ_viewarg_beam.m
%   gregorian.m
%   gmin.m
%   gmax.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use of this program is described in:
%
% Sullivan, C.M., Warner, J.C., Martini, M.A., Voulgaris, G., 
% Work, P.A., Haas, K.A., and Hanes, D.H. (2006) 
% South Carolina Coastal Erosion Study Data Report for Observations
% October 2003 - April 2004., USGS Open-File Report 2005-1429.
%
% Program written in Matlab v7.1.0 SP3
% Program ran on PC with Windows XP Professional OS.
%
% "Although this program has been used by the USGS, no warranty, 
% expressed or implied, is made by the USGS or the United States 
% Government as to the accuracy and functioning of the program 
% and related program material nor shall the fact of distribution 
% constitute any such warranty, and no responsibility is assumed 
% by the USGS in connection therewith."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

% C. Sullivan   06/22/05,   version 1.0
% Start using version #'s to keep track of changes to mfiles. Run
% READ_viewarg_beam.m (which is now a function) directly from this mfile.
% Write all ViewArgonaut velocity output (files .vel, .std, .snr, and .dat)
% to a .cdf file. The data in this file is the data as downloaded
% from the instrument, ergo the units of the data will be the units the
% data was collected in. Instrument setup information is ncluded as
% metadata. (Note that all QA/QC will take place AFTER this file is created.
% The QA'd/QC'd data will be converted into EPIC-compliant variables and
% written to a BBV .nc file.)
%
% TODO:  What to do w/ single cell velocitites?


more off

version = '1.0';

% Check inputs
if ~ischar(metaFile) || ~ischar(cdfFileRoot)
    error('File names should be surrounded in single quotes');
end

% Check existence of metadata file and ViewArgonaut output
if isunix
    l = ls(metaFile);
    if isempty(l)
        error(['The metafile ',metaFile,' does not exist in this directory']);
    end

    velFile = ls('*.vel');
    stdFile = ls('*.std');
    snrFile = ls('*.snr');
    ctlFile = ls('*.ctl');
    datFile = ls('*.dat');
    if isempty(velFile) || isempty(stdFile) || isempty(snrFile) || ...
       isempty(ctlFile) || isempty(datFile)
        error(['ViewArgonaut output files do not exist in this directory'])
    end
else
    l = dir(metaFile);
    if isempty(l)
        error(['The metafile ',metaFile,' does not exist in this directory']);
    end

    vel = dir('*.vel');
    std = dir('*.std');
    snr = dir('*.snr');
    ctl = dir('*.ctl');
    dat = dir('*.dat');
    if isempty(vel) || isempty(std) || isempty(snr) || ...
       isempty(ctl) || isempty(dat)
        error(['ViewArgonaut output files do not exist in this directory'])
    elseif length(vel) > 1 || length(std) > 1 || length(snr) > 1 || length(ctl) > 1
        error(['Too many .vel, .std, .snr, and .dat files exist in this directory'])
    else
        velFile = vel.name;
        stdFile = std.name; clear std %b/c std is a function!
        snrFile = snr.name;
        ctlFile = ctl.name;
        datFile = dat.name;
    end
end

% Gather user-defined and instrument metadata. Add ViweArgonaut output file
% names to metadata.
[userMeta, argnMeta] = get_meta_sontek(metaFile, ctlFile);
argnMeta.metaFile = metaFile;
argnMeta.velFile = velFile;
argnMeta.stdFile = stdFile;
argnMeta.snrFile = snrFile;
argnMeta.ctlFile = ctlFile;
argnMeta.datFile = datFile; 

% Load the velocities with READ_viewarg_beam.m.  This mfile is a function
% that loads the data from the ViewArgonaut files *.dat, *.vel, *.snr, 
% and *.std and converts them from BEAM to ENU coordinates (if necessary),
% and corrects for magnetic variation.
[argnData] = READ_viewarg_beam(userMeta, argnMeta);

% Create and define the .cdf file
nc=netcdf([cdfFileRoot,'.cdf'],'clobber');                        

%write user metadata
theAtts = fieldnames(userMeta);
for a=1:length(theAtts)
    eval(['theDef = userMeta.',theAtts{a},';'])
    if ischar(theDef)
        eval(['nc.',theAtts{a},' = ncchar(''',theDef,''');']);
    else
        eval(['nc.',theAtts{a},' = ncfloat([theDef]);']);
    end
end

%write instrument setup information
theAtts = fieldnames(argnMeta);
for a=1:length(theAtts)
    eval(['theDef = argnMeta.',theAtts{a},';'])
    if ischar(theDef)
        eval(['nc.ARGN_',theAtts{a},' = ncchar(''',theDef,''');']);
    else
        eval(['nc.ARGN_',theAtts{a},' = ncfloat([theDef]);']);
    end
end


%define dimensions
nc('burst') = 0;                               
nc('cell')= argnData.nCells; 
nc('lat') = 1;
nc('lon') = 1;

%define variables
nc{'time'} = nclong('burst');
nc{'time'}.FORTRAN_format = ncchar('F10.2');
nc{'time'}.units = ncchar('True Julian Day');
nc{'time'}.type = ncchar('UNEVEN');
nc{'time'}.epic_code = nclong(624);
nc{'time'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'time2'} = nclong('burst') ;
nc{'time2'}.FORTRAN_format = ncchar('F10.2');
nc{'time2'}.units = ncchar('msec since 0:00 GMT');
nc{'time2'}.type = ncchar('UNEVEN');
nc{'time2'}.epic_code = nclong(624);
nc{'time2'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'burst'} = nclong('burst');
nc{'burst'}.FORTRAN_format = ncchar('F10.2');
nc{'burst'}.units = ncchar('counts');
nc{'burst'}.type = ncchar('EVEN');
nc{'burst'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'height'} = ncfloat('cell'); 
nc{'height'}.FORTRAN_format = ncchar('F10.2');
nc{'height'}.units = ncchar('m');
nc{'height'}.type = ncchar('EVEN');
nc{'height'}.epic_code = nclong(3);
nc{'height'}.long_name = ncchar('DEPTH (m)');
nc{'height'}.blanking_distance = ncfloat(argnMeta.BlankDistance);
nc{'height'}.cell_size = ncfloat(argnMeta.CellSize);
nc{'height'}.xducer_offset_from_bottom = ncfloat(userMeta.sensor_height);
nc{'height'}.FillValue_ = ncfloat(1.00000004091848e+035);
nc{'height'}.NOTE = ncchar('Centered cell heights above the bed');

nc{'lon'} = ncfloat('lon'); %% 1 element.
nc{'lon'}.FORTRAN_format = ncchar('f10.4');
nc{'lon'}.units = ncchar('dd');
nc{'lon'}.type = ncchar('EVEN');
nc{'lon'}.epic_code = nclong(502);
nc{'lon'}.name = ncchar('LON');
nc{'lon'}.long_name = ncchar('LONGITUDE');
nc{'lon'}.generic_name = ncchar('lon');
nc{'lon'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'lat'} = ncfloat('lat'); %% 1 element.
nc{'lat'}.FORTRAN_format = ncchar('F10.2');
nc{'lat'}.units = ncchar('dd');
nc{'lat'}.type = ncchar('EVEN');
nc{'lat'}.epic_code = nclong(500);
nc{'lat'}.name = ncchar('LAT');
nc{'lat'}.long_name = ncchar('LATITUDE');
nc{'lat'}.generic_name = ncchar('lat');
nc{'lat'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'VelEast'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'VelEast'}.name = ncchar('u');
nc{'VelEast'}.long_name = ncchar('Eastward Velocity (cm/s)');
nc{'VelEast'}.generic_name = ncchar('u');
nc{'VelEast'}.FORTRAN_format = ncchar(' ');
nc{'VelEast'}.units = ncchar('cm s-1');
nc{'VelEast'}.sensor_type = nc.INST_TYPE(:);
nc{'VelEast'}.height = ncfloat(userMeta.sensor_height);
nc{'VelEast'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'VelEast'}.valid_range = ncfloat([1000 1000]);
nc{'VelEast'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'VelNorth'} = ncfloat('burst', 'cell', 'lat', 'lon'); 
nc{'VelNorth'}.name = ncchar('v');
nc{'VelNorth'}.long_name = ncchar('Northward Velocity (cm/s)');
nc{'VelNorth'}.generic_name = ncchar('v');
nc{'VelNorth'}.FORTRAN_format = ncchar(' ');
nc{'VelNorth'}.units = ncchar('cm s-1');
nc{'VelNorth'}.sensor_type = nc.INST_TYPE(:);
nc{'VelNorth'}.height = ncfloat(userMeta.sensor_height);
nc{'VelNorth'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'VelNorth'}.valid_range = ncfloat([1000 1000]);
nc{'VelNorth'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'VelUp'} = ncfloat('burst', 'cell', 'lat', 'lon'); 
nc{'VelUp'}.name = ncchar('w');
nc{'VelUp'}.long_name = ncchar('Vertical Velocity (cm/s)');
nc{'VelUp'}.generic_name = ncchar('w');
nc{'VelUp'}.FORTRAN_format = ncchar(' ');
nc{'VelUp'}.units = ncchar('cm s-1');
nc{'VelUp'}.sensor_type = nc.INST_TYPE(:);
nc{'VelUp'}.height = ncfloat(userMeta.sensor_height);
nc{'VelUp'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'VelUp'}.valid_range = ncfloat([1000 1000]);
nc{'VelUp'}.FillValue_ = ncfloat(1.00000004091848e+035);

if strcmp(argnMeta.CoordSystem,' BEAM')
    nc{'VelBeam1'} = ncfloat('burst', 'cell', 'lat', 'lon'); 
    nc{'VelBeam1'}.long_name = ncchar('Velocity Along Beam 1 (cm/s)');
    nc{'VelBeam1'}.units = ncchar('cm s-1');
    nc{'VelBeam1'}.valid_range = ncfloat([1000 1000]);
    nc{'VelBeam1'}.sensor_type = nc.INST_TYPE(:);
    nc{'VelBeam1'}.height = ncfloat(userMeta.sensor_height);
    nc{'VelBeam1'}.serial = ncfloat(argnMeta.SerialNumber);
    nc{'VelBeam1'}.FillValue_ = ncfloat(1.00000004091848e+035);

    nc{'VelBeam2'} = ncfloat('burst', 'cell', 'lat', 'lon');
    nc{'VelBeam2'}.long_name = ncchar('Velocity Along Beam 2 (cm/s)');
    nc{'VelBeam2'}.units = ncchar('cm s-1');
    nc{'VelBeam2'}.valid_range = ncfloat([1000 1000]);
    nc{'VelBeam2'}.sensor_type = nc.INST_TYPE(:);
    nc{'VelBeam2'}.height = ncfloat(userMeta.sensor_height);
    nc{'VelBeam2'}.serial = ncfloat(argnMeta.SerialNumber);
    nc{'VelBeam2'}.FillValue_ = ncfloat(1.00000004091848e+035);

    nc{'VelBeam3'} = ncfloat('burst', 'cell', 'lat', 'lon');
    nc{'VelBeam3'}.long_name = ncchar('Velocity Along Beam 3 (cm/s)');
    nc{'VelBeam3'}.units = ncchar('cm s-1');
    nc{'VelBeam3'}.valid_range = ncfloat([1000 1000]);
    nc{'VelBeam3'}.sensor_type = nc.INST_TYPE(:);
    nc{'VelBeam3'}.height = ncfloat(userMeta.sensor_height);
    nc{'VelBeam3'}.serial = ncfloat(argnMeta.SerialNumber);
    nc{'VelBeam3'}.FillValue_ = ncfloat(1.00000004091848e+035);
end

nc{'stderrBeam1'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'stderrBeam1'}.units = ncchar('cm s-1');
nc{'stderrBeam1'}.long_name = ncchar('Beam 1 Standard Error (cm/s)');
nc{'stderrBeam1'}.valid_range = ncfloat([1000 1000]);
nc{'stderrBeam1'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'stderrBeam2'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'stderrBeam2'}.units = ncchar('cm s-1');
nc{'stderrBeam2'}.long_name = ncchar('Beam 2 Standard Error (cm/s)');
nc{'stderrBeam2'}.valid_range = ncfloat([1000 1000]);
nc{'stderrBeam2'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'stderrBeam3'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'stderrBeam3'}.units = ncchar('cm s-1');
nc{'stderrBeam3'}.long_name = ncchar('Beam 3 Standard Error (cm/s)');
nc{'stderrBeam3'}.valid_range = ncfloat([1000 1000]);
nc{'stderrBeam3'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'snrBeam1'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'snrBeam1'}.units = ncchar('dB');
nc{'snrBeam1'}.long_name = ncchar('Beam 1 Signal to Noise Ratio (dB)');
nc{'snrBeam1'}.valid_range = ncfloat([-32768 32767]);
nc{'snrBeam1'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'snrBeam2'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'snrBeam2'}.units = ncchar('dB');
nc{'snrBeam2'}.long_name = ncchar('Beam 2 Signal to Noise Ratio (dB)');
nc{'snrBeam2'}.valid_range = ncfloat([-32768 32767]);
nc{'snrBeam2'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'snrBeam3'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'snrBeam3'}.units = ncchar('dB');
nc{'snrBeam3'}.long_name = ncchar('Beam 3 Signal to Noise Ratio (dB)');
nc{'snrBeam3'}.valid_range = ncfloat([-32768 32767]);
nc{'snrBeam3'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'ampBeam1'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'ampBeam1'}.units = ncchar('counts');
nc{'ampBeam1'}.long_name = ncchar('Beam 1 Amplitude (counts)');
nc{'ampBeam1'}.valid_range = ncfloat([-32768 32767]);
nc{'ampBeam1'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'ampBeam2'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'ampBeam2'}.units = ncchar('counts');
nc{'ampBeam2'}.long_name = ncchar('Beam 2 Amplitude (counts)');
nc{'ampBeam2'}.valid_range = ncfloat([-32768 32767]);
nc{'ampBeam2'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'ampBeam3'} = ncfloat('burst', 'cell', 'lat', 'lon');
nc{'ampBeam3'}.units = ncchar('counts');
nc{'ampBeam3'}.long_name = ncchar('Beam 3 Amplitude (counts)');
nc{'ampBeam3'}.valid_range = ncfloat([-32768 32767]);
nc{'ampBeam3'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'batt'} = ncfloat('burst', 'lat', 'lon');
nc{'batt'}.units = ncchar('volts');
nc{'batt'}.long_name = ncchar('Battery Voltage (volts)');
nc{'batt'}.valid_range = ncfloat([-32768 32767]);
nc{'batt'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'pct_good'} = ncfloat('burst', 'lat', 'lon');
nc{'pct_good'}.units = ncchar('');
nc{'pct_good'}.long_name = ncchar('Percent Good Pings');
nc{'pct_good'}.valid_range = ncfloat([0 100]);
nc{'pct_good'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'heading'} = ncfloat('burst', 'lat', 'lon');
nc{'heading'}.units = ncchar('degrees, magnetic');
nc{'heading'}.long_name = ncchar('INST Heading (degrees, magnetic)');
nc{'heading'}.epic_code = nclong(1215);
nc{'heading'}.valid_range = ncfloat([0 359.999908447266]);
nc{'heading'}.height = ncfloat(userMeta.sensor_height);
nc{'heading'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'heading'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'stdheading'} = ncfloat('burst', 'lat', 'lon');
nc{'stdheading'}.units = ncchar('degrees, magnetic');
nc{'stdheading'}.long_name = ncchar('INST Heading Standard Deviation (degrees, magnetic)');
nc{'stdheading'}.epic_code = nclong(1215);
nc{'stdheading'}.valid_range = ncfloat([0 359.999908447266]);
nc{'stdheading'}.height = ncfloat(userMeta.sensor_height);
nc{'stdheading'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'stdheading'}.FillValue_ = ncfloat(1.00000004091848e+035);
 
nc{'pitch'} = ncfloat('burst', 'lat', 'lon');
nc{'pitch'}.units = ncchar('degrees');
nc{'pitch'}.long_name = ncchar('INST Pitch (degrees)');
nc{'pitch'}.epic_code = nclong(1216);
nc{'pitch'}.valid_range = ncfloat([0 359.999908447266]);
nc{'pitch'}.height = ncfloat(userMeta.sensor_height);
nc{'pitch'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'pitch'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'stdpitch'} = ncfloat('burst', 'lat', 'lon');
nc{'stdpitch'}.units = ncchar('degrees');
nc{'stdpitch'}.long_name = ncchar('INST Pitch Standard Deviation (degrees)');
nc{'stdpitch'}.epic_code = nclong(1216);
nc{'stdpitch'}.valid_range = ncfloat([0 359.999908447266]);
nc{'stdpitch'}.height = ncfloat(userMeta.sensor_height);
nc{'stdpitch'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'stdpitch'}.FillValue_ = ncfloat(1.00000004091848e+035);
 
nc{'roll'} = ncfloat('burst', 'lat', 'lon');
nc{'roll'}.units = ncchar('degrees');
nc{'roll'}.long_name = ncchar('INST roll (degrees)');
nc{'roll'}.epic_code = nclong(1217);
nc{'roll'}.valid_range = ncfloat([0 359.999908447266]);
nc{'roll'}.height = ncfloat(userMeta.sensor_height);
nc{'roll'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'roll'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'stdroll'} = ncfloat('burst', 'lat', 'lon');
nc{'stdroll'}.units = ncchar('degrees');
nc{'stdroll'}.long_name = ncchar('INST Roll Standard Deviation (degrees)');
nc{'stdroll'}.epic_code = nclong(1217);
nc{'stdroll'}.valid_range = ncfloat([0 359.999908447266]);
nc{'stdroll'}.height = ncfloat(userMeta.sensor_height);
nc{'stdroll'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'stdroll'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'temperature'} = ncfloat('burst', 'lat', 'lon');
nc{'temperature'}.units = ncchar('degrees C');
nc{'temperature'}.long_name = ncchar(['Argonaut Mean Transducer Temperature (degrees C)']);
nc{'temperature'}.valid_range = ncfloat([-5 60]);
nc{'temperature'}.height = ncfloat(userMeta.sensor_height);
nc{'temperature'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'temperature'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'pressure'} = ncfloat('burst', 'lat', 'lon');
nc{'pressure'}.units = ncchar('dBar');
nc{'pressure'}.long_name = ncchar('Argonaut Mean Pressure (dBar)');
nc{'pressure'}.valid_range = ncfloat([0 20]);
nc{'pressure'}.height = ncfloat(userMeta.sensor_height);
nc{'pressure'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'pressure'}.FillValue_ = ncfloat(1.00000004091848e+035);

nc{'stdpressure'} = ncfloat('burst', 'lat', 'lon');
nc{'stdpressure'}.units = ncchar('dBar');
nc{'stdpressure'}.long_name = ncchar('Argonaut Mean Pressure Standard Deviation (dBar)');
nc{'stdpressure'}.valid_range = ncfloat([0 20]);
nc{'stdpressure'}.height = ncfloat(userMeta.sensor_height);
nc{'stdpressure'}.serial = ncfloat(argnMeta.SerialNumber);
nc{'stdpressure'}.FillValue_ = ncfloat(1.00000004091848e+035);

endef(nc);

% Get dimensions
nRec = argnData.nBursts;
nCells = argnData.nCells;

% Write the data to the .cdf file
nc{'time'}(1:nRec) = floor(argnData.time);
nc{'time2'}(1:nRec) = (argnData.time-floor(argnData.time)).*(24*3600*1000); 
nc{'height'}(1:nCells) = argnData.multcell.cell_height;
nc{'burst'}(1:nRec) = argnData.burst;
nc{'lat'}(1)=nc.latitude(:);
nc{'lon'}(1)=nc.longitude(:);
nc{'VelEast'}(1:nRec, 1:nCells) = argnData.multcell.u;
nc{'VelNorth'}(1:nRec, 1:nCells) = argnData.multcell.v;
nc{'VelUp'}(1:nRec, 1:nCells) = argnData.multcell.w;
nc{'VelBeam1'}(1:nRec, 1:nCells) = argnData.multcell.velBeam1;
nc{'VelBeam2'}(1:nRec, 1:nCells) = argnData.multcell.velBeam2;
nc{'VelBeam3'}(1:nRec, 1:nCells) = argnData.multcell.velBeam3;
nc{'stderrBeam1'}(1:nRec, 1:nCells) = argnData.multcell.stderrBeam1;
nc{'stderrBeam2'}(1:nRec, 1:nCells) = argnData.multcell.stderrBeam2;
nc{'stderrBeam3'}(1:nRec, 1:nCells) = argnData.multcell.stderrBeam3;
nc{'snrBeam1'}(1:nRec, 1:nCells) = argnData.multcell.snrBeam1;
nc{'snrBeam2'}(1:nRec, 1:nCells) = argnData.multcell.snrBeam1;
nc{'snrBeam3'}(1:nRec, 1:nCells) = argnData.multcell.snrBeam1;
nc{'ampBeam1'}(1:nRec, 1:nCells) = argnData.multcell.ampBeam1;
nc{'ampBeam2'}(1:nRec, 1:nCells) = argnData.multcell.ampBeam2;
nc{'ampBeam3'}(1:nRec, 1:nCells) = argnData.multcell.ampBeam3;
nc{'pct_good'}(1:nRec) = argnData.snglcell.pctGd;
nc{'batt'}(1:nRec) = argnData.snglcell.batt;
nc{'heading'}(1:nRec) = argnData.snglcell.hdg;
nc{'stdheading'}(1:nRec) = argnData.snglcell.stdhdg;
nc{'pitch'}(1:nRec) = argnData.snglcell.ptch;
nc{'stdpitch'}(1:nRec) = argnData.snglcell.stdptch;
nc{'roll'}(1:nRec) = argnData.snglcell.roll;
nc{'stdroll'}(1:nRec) = argnData.snglcell.stdroll;
nc{'temperature'}(1:nRec) = argnData.snglcell.temperature;
nc{'pressure'}(1:nRec) = argnData.snglcell.pressure;
nc{'stdpressure'}(1:nRec) = argnData.snglcell.stdpressure;

% Calculate min/max values and replace NaNs
% with the fill value
theVars = var(nc);
for i = 1:length(theVars),
    data = theVars{i}(:);
    if ~isempty(data) & ...
       ~strcmp(ncnames(theVars{i}),'time') & ...
       ~strcmp(ncnames(theVars{i}),'time2') & ...
       ~strcmp(ncnames(theVars{i}),'depth') & ...
       ~strcmp(ncnames(theVars{i}),'lat') & ...
       ~strcmp(ncnames(theVars{i}),'lon')
            theVars{i}.minimum = gmin(data(:));
            theVars{i}.maximum = gmax(data(:));
            theFillVal = theVars{i}.FillValue_(:);
            bads = find(isnan(data));
            data(bads) = theFillVal;
            theVars{i}(:) = data;
    end
end

% Update/add some attributes
nc.DATA_TYPE = ncchar('Argonaut velocity data output from ViewArgonaut');
history = nc.history(:);
if strcmp(argnMeta.CoordSystem,' BEAM')
    history_new = ['Data converted to netCDF via MATLAB by argnCurr2cdf.m V ',...
                   version,'; Velocities rotated and corrected for ',...
                   'magnetic variation by READ_viewarg_beam.m; ',history];
elseif strcmp(argnMeta.CoordSystem,' ENU')
    history_new = ['Data converted to netCDF via MATLAB by argnCurr2cdf.m V ',...
                version,'; Velocities corrected for magnetic variation ',...
                'by READ_viewarg_beam.m; ',history];
end
nc.history = ncchar(history_new);
nc.DELTA_T = ncfloat(argnData.delta_t * 24 * 3600); %seconds
nc.CREATION_DATE = datestr(now,0);
nc.start_time = datestr(gregorian(argnData.time(1)),0);
nc.stop_time = datestr(gregorian(argnData.time(end)),0);

nc=close(nc);