Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string attributes not supported? #156

Open
Chrismarsh opened this issue Oct 18, 2024 · 1 comment
Open

string attributes not supported? #156

Chrismarsh opened this issue Oct 18, 2024 · 1 comment

Comments

@Chrismarsh
Copy link

I have a C++ code that loads a netcdf file and accesses the units attribute like this:

    netCDF::NcFile _data;
// open file [...]
    auto a = _data.getVar("valid_time").getAtt("units");
    SPDLOG_DEBUG(a.getType().getName());  // prints string
    std::string epoch;
    a.getValues(epoch); // throws exception

which results in an exception with the following error:

NetCDF: Attempt to convert between text & numbers

This netcdf has string attributes and are Copernicus CDS ERA5 netcdf files. ncdump shows:

	double datetime(datetime) ;
		datetime:_FillValue = NaN ;
		string datetime:standard_name = "time" ;
		string datetime:long_name = "time" ;
		string datetime:units = "seconds since 1970-01-01T00:00:00" ;
		string datetime:calendar = "proleptic_gregorian" ;

If I have a non-string attr like:

	double datetime(datetime) ;
		datetime:_FillValue = NaN ;
		datetime:standard_name = "time" ;
		datetime:long_name = "time" ;
		datetime:units = "seconds since 1970-01-01T00:00:00" ;
		datetime:calendar = "proleptic_gregorian" ;

the above C++ code works as expected.

This problem appears to be the same issue noted in this netcdf-fortran issue Unidata/netcdf-fortran#181 (comment)

However, as I understand it, string attributes is supposed to work with netcdf-c. Is this a limitation to the C++ api?

Versions:

@amstokely
Copy link

amstokely commented Nov 27, 2024

@Chrismarsh, I recently encountered a similar issue, but with reading string variables. When working with string variables, you need to allocate a temporary char** buffer for the number of strings you want to read. Here's the process I followed:

Allocate the first dimension of the char** buffer (NetCDF manages the individual string memory).
Pass the buffer to the getVar method.
After reading, copy the buffer data into a std::vectorstd::string or similar container.
Free the buffer using NetCDF’s string memory management functions.
Deallocate the buffer memory you allocated.
For reference, here is an example of reading a string array variable in C++.

https://github.com/NCAR/obs2ioda/blob/101e9c66e5190a51279f6aa04cc1c207c5e7b698/obs2ioda-v2/src/cxx/netcdf_variable.cc#L199

While this example deals with string variables, I assume the process for string attributes might be similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants