nc_attput.m 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
function nc_attput (ncfile , varname , attribute_name , attval )
%
% version of nc_attput for mexec to replace snctools version
% the aim is to replace snctools nc_ calls with faster versions that make
% simple native matlab netcdf calls.
%
% rather than changing every mexec program, the snctools library can be
% replaced with this library
%
% Use numeric varname = -1 to get a global attribute
%
% The old snctools description was
%
% % NC_ATTPUT:  writes an attribute into a netCDF file
% %     NC_ATTPUT(NCFILE,VARNAME,ATTNAME,ATTVAL) writes the data in ATTVAL to
% %     the attribute ATTNAME of the variable VARNAME of the netCDF file NCFILE.
% %     VARNAME should be the name of a netCDF VARIABLE, but one can also use the
% %     mnemonic nc_global to specify a global attribute.  Do not use 'global'.
%
%
% bak jc191 6 Feb 2020
%

ncid=netcdf.open(ncfile,'WRITE');

if isnumeric(varname)
    % global attribute, can enter -1 in the argument
    varid = netcdf.getConstant('GLOBAL'); % usually -1
else
    varid = netcdf.inqVarID(ncid, varname);
end

netcdf.reDef(ncid); % need to enter def mode in case attribute is new

netcdf.putAtt(ncid,varid,attribute_name, attval);

netcdf.endDef(ncid);

netcdf.close(ncid);

% %
% % % we could use the ncattwrite utility, but it seems to be slower, and we
% % % are only using netcdf.xxxxx() routines.
% %
% % if isnumeric(varname)
% %     % global attribute, value = -1, signified here by '/'
% %     varname = '/';
% % end
% %
% % ncwriteatt(ncfile , varname, attribute_name , attval);

return