Skip to content

Support for reading NC_STRING type attributes from netCDF files

Fix #323 (closed).

String-like attributes in NetCDF files can be of type NC_STRING and NC_CHAR. The current implementation can only read NC_CHAR types. A different call is required to read NC_STRING types. The latter one is quite unusual, but some xarray versions seem to try to write this format.

Note that the c++ netcdf getValues(string) reads NC_CHAR types, and getValues(char**) NC_STRING types. Currently implemented is only the first one. This merge request adresses this by dynamically reading an attribute as string or char** depending on the underlying type.

Can be tested with the file at /mnt/mccvis/Group/ChristophF/data/era5/stringtype.nc which as the attribute for the U wind:

string u:long_name = "U component of wind"

This gets parsed correctly and can be seen at the long names when adding a variable to an actor.

See also: https://www.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2019/msg00014.html

https://github.com/Unidata/netcdf4-python/issues/529

and: https://stackoverflow.com/questions/74150414/xarray-force-the-writing-of-string-attribute-as-nc-char-because-of-netcdf-fort

Edited by Christoph Fischer

Merge request reports