Re: character dimensions, attribute lengths


On the Unidata website, did you see the section on best practices for "Strings and Variables of type char"? This section is more relevant for dealing with arrays of strings, rather than single strings.

However, your message implies that you are concerned mostly with single strings, as attributes. In my opinion there is a simple and unique best practice for handling single attribute strings.

When writing, simply write the string as a character array of the desired string length, with no added padding or terminator byte. This is effectively a variable length string in Netcdf. Most Netcdf software and files that I have used recently seem to be consistent with this convention.

When reading, follow the reading guidelines under Unidata's best practices reference above. When reading in a language that requires preallocated fixed length strings, simply over-allocate the length of the receiving string, and add padding on the right, as needed.

You're right, you should not be using formal Netcdf dimensions to store attribute lengths. Netcdf stores the length of each attribute separately; this is not to be confused with formal dimensions for data arrays. When reading a Netcdf character attribute, you should usually read the length first, then the string itself. If you are doing dynamic allocation, then allocate the memory for the string between the two calls.

For example, in Fortran 90, use these Netcdf library routines:

 nf90_inquire_attribute        ! get length of string
 nf90_get_att                  ! read string

In C:

 nc_inq_att or nc_inq_attlen   /* get length of string */
 nc_get_att_text               /* read string */

For more information and complete examples, see the Netcdf documentation for these routines. HTH.


Nan Galbraith wrote:
Sorry if this has been covered repeatedly, I have searched the unidata site and the netcdf users guide and just don't see the issue addressed directly.
It's a "best practices" question, but is not actually answered in that
document either.

I'm working on an implementation of NetCDF with a very varied group,
and the issue of string lengths is a real problem for some of them. All our
data is numeric;  attributes are often char though.

Some participants use software that requires them to specify string
lengths for char variables, I assume it's fortran but am not sure. Their
work-around is to declare these as dimensions in the netcdf file: STRING256,
STRING64, STRING32,  etc.

This seems like a misuse of the concept of dimensions in netcdf, but I'd
like to know if it's a widely accepted way to deal with the fact that strings don't exist in netcdf. I'd prefer to deal with the issue in software, and leave this out of our specification. It seems ... messy to me, but if it's commonly
done then I guess I'll stop arguing.

Thanks very much -
Nan Galbraith

To unsubscribe netcdfgroup, visit:

To unsubscribe netcdfgroup, visit: