[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #JFJ-494364]: easiest way to change attribute



Hi Fan,

> The question is, among the available tools, what the easiest way is to
> change (modify or add) the attributes (variable or global) in a netcdf
> file from the command-line without costly rereading/rewriting netcdf data.

If they are netCDF-4 files, you can add all the attributes you want without
rereading/rewriting any of the netCDF data.

If the files are netCDF classic or netCDF 64-bit offset format, then
it depends on how the files were created.  If whoever created the files
anticipated the need to later add more attributes, by calling special
versions of nc_enddef (or nf_enddef or nf90_endef) to reserve extra space
in the header, then if there is enough space no data needs to be rewritten
or moved.  As the User's Guide says:

  ... you can use an alternative version of the enddef function with
  two underbar characters instead of one to explicitly reserve extra
  space in the file header when the file is created: in C nc__enddef
  (see nc__enddef), in Fortran NF__ENDDEF (see NF__ENDDEF), after a
  previous call to the redef function. This avoids the expense of
  moving all the data later by reserving enough extra space in the
  header to accommodate anticipated changes, such as the addition of
  new attributes or the extension of existing string attributes to
  hold longer strings.
  http://www.unidata.ucar.edu/netcdf/docs/netcdf.html#Classic-File-Parts

> There seem to have options of using netcdf-C, netcdf-Java, NCL, NcML,
> etc.  The NcML seems to be specifically for this purpose.  But since
> ncgen doesn't read NcML we still need to use the libraries to write out
> netcdf - is this correct?

The ncgen in netCDF-4.1.1 can write NcML from CDL input.  So you could
generate the CDL with ncdump, edit it to add the needed attributes, use ncgen
on that to create new NcML, and just put the additional attributes from 
that NcML in a file on a TDS server to add new attributes to existing files on
the server, without modifying them.  The files could be read from the server
by C or Fortran programs, that would see the new attributes as if they were
in the original file.

I don't know any other ways to accomplish this without rewriting data.

--Russ

Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: JFJ-494364
Department: Support netCDF
Priority: Normal
Status: Closed