function theResult = burst2nc(theBurst, theNetCDFFile, thePermission) % burst2nc -- Convert burst-struct to NetCDF file. % burst(theBurst, 'theNetCDFFile', 'thePermission') converts % the given burst-structure to a new NetCDF file of the given % name. The NetCDF file permission {'clobber', 'noclobber'} % defaults to 'noclobber'. If the filename is absent or % contains '*', the UIPUTFILE dialog is invoked. % % Also see: UIPUTFILE, NetCDF Toolbox. % Andree Ramsey and Charles R. Denham % United States Geological Survey % Woods Hole Field Center % 384 Woods Hole Road % Woods Hole, MA 02543-1598 % aramsey@usgs.gov, cdenham@usgs.gov % Version of 01-Jul-2003 11:05:44. % Updated 02-Jul-2003 08:17:44. if nargin < 1, help(mfilename), return, end if nargin < 2, theNetCDFFile = '*'; end if nargin < 3, thePermission = 'noclobber'; end % UIPUTFILE Dialog. if any(theNetCDFFile == '*') [theFile, thePath] = uiputfile(theNetCDFFile, 'Save Burst As:'); if ~any(theFile), return, end if thePath(end) ~= filesep, thePath(end+1) = filesep; end theNetCDFFile = [thePath theFile]; end % Number of bursts and records/burst. nBursts = size(theBurst.Vx, 2); nRecords = theBurst.sampleperburst; % Open the NetCDF file. nc = netcdf(theNetCDFFile, thePermission); if isempty(nc), return, end % Create the dimensions: index, one, two, three. nc('index') = 0; theDimnames = {'one', 'two', 'three'}; for i = 1:3 nc(theDimnames{i}) = i*nBursts; end % Define the global attributes. theAttributes = { 'time' 'samprate' 'sampleperburst' 'soundspd' 'brange' 'vbrange' 'status' 'mean' 'std' }; for i = 1:length(theAttributes) att = ncatt(theAttributes{i}, theBurst.(theAttributes{i}), nc); end % Define the metadata as attributes of a "metadata" % scalar-variable. nc{'metadata'} = ncdouble; var = nc{'metadata'}; theMetaData = fieldnames(theBurst.metadata); for i = 1:length(theMetaData) att = ncatt(theMetaData{i}, ... theBurst.metadata.(theMetaData{i}), .... var); end % Define the record-variables. % Names and multiplicity are given here. theRecVariables = { 'Vx', 1, 'Vy', 1, 'Vz', 1, 'amp', 3, 'corr', 3, 'heading', 1, 'pitch', 1, 'roll', 1' 'temperature', 1, 'pressure', 1, 'extsensor', 2, 'extpress', 1 }; theRecVariables = theRecVariables.'; theRecVariables = theRecVariables(:); for i = 2:2:prod(size(theRecVariables)) switch theRecVariables{i} case 1 theDimname = 'one'; case 2 theDimname = 'two'; case 3 theDimname = 'three'; otherwise end theDimname = theDimnames{theRecVariables{i}}; nc{theRecVariables{i-1}} = ... ncdouble('index', theDimname); var = autonan(nc{theRecVariables{i-1}}, 1); end % Expand the record-variables by writing % to the last record. nc{theRecVariables{1}}(nRecords) = 0; % Populate the record-variables. If anything % goes wrong, just go to the next variable. % For example, some of our structures do not % contain temperature or pressure. for i = 2:2:prod(size(theRecVariables)) try b = theBurst.(theRecVariables{i-1}); b = [b{:}]; % Concatenate the columns. nc{theRecVariables{i-1}}(:) = b; catch disp([' ## Unable to write: ' theRecVariables{i-1}]) end end close(nc);