Support for single string attributes in read#453
Conversation
…gle string attributes from a netCDF4 CF-1.9 compliant using string type for attributes. It overloads the nf90_get_att_text function returning single string (note nlen == 1) as if they are character attributes. What is not implemented are multidimensional strings, and the capability to write string attributes, because of the lack of string type in Fortran standard. This patch would allow reading netCDF files like the one created from the Copernicus cds-beta site for ECMWF ERA5 model. See reference to this in issue Unidata#181 "string attributes are not supported yet?"
|
Thank you! |
|
Can you add a test or two as well? |
|
@graziano-giuliani Is there any chance you could help us by adding a test for this as well? |
|
I'll see if I can add a test for this. |
|
@WardF May I ask if there is a test for this new implementation? If not, can I contribute one? File list:
The CDL Essentially it creates I can try to build the NetCDF-Fortran lib from this PR, and write a code using this lib to check if updated |
|
Hello @gmao-cda; contributing a test would be greatly appreciated, as it would help us keep our CI up to date. I have had this on the back burner as something to do for a while, clearly, but if you were able to contribute that I should be able to get things up and running and merged ASAP. Thank you! |
|
@WardF I noticed the related issue #181 that is still open. Should it be closed or does this PR not completely fix that issue? Internally we are also running into this same issue with netcdf network files written with xarray (and |
|
(cc @WardF here) @veenstrajelmer For the 1d string, it has been solved using the source codes from the latest branch main Check this test to see how to read string: https://github.com/Unidata/netcdf-fortran/blob/main/nf03_test4/f90tst_att.F90 The corresponding test data is here: https://github.com/Unidata/netcdf-fortran/blob/main/nf03_test4/ref_att.nc Let me know if it works for you. If you cannot change the netcdf-fortran version, I will provide you an alternative solution. |
|
Great, thanks for the reply. And are there any plans to release somewhere soon? I don't think I can push a non-released version into our pipelines, that won't be accepted. |
| if ( xtype == nf90_string .and. nlen == 1 ) then | ||
| c_ncid = ncid | ||
| c_varid = varid - 1 | ||
| c_aname = name//char(0) |
There was a problem hiding this comment.
Thanks a lot guys for the addition! Just one comment: it works better for me if I add a trim function around name:
- c_aname = name//char(0)
+ c_aname = trim(name)//char(0)There was a problem hiding this comment.
trim removes trailing blanks. Trailing blanks are legal in attribute names, according to current netCDF specs, IIRC. Why would you want to do this?
There was a problem hiding this comment.
Hum, I may be wrong, but the fact that c_aname is defined using len_trim(name)+1), means that, if name is padded with blanks, the special character \0 would be out of bound and erasable by the next allocation...
There was a problem hiding this comment.
@dupontf In the netCDF "Classic Format" spec, I find this:
Names that have trailing space characters are also not permitted.
Therefore my earlier comment was mistaken, and I can not disagree with your suggestion to add trim. Internal blanks are allowed, just not leading or trailing ones. That is sensible.
Partial modification to allow netCDF Fortran library able to read single string attributes from a netCDF4 CF-1.9 compliant using string type for attributes.
It overloads the nf90_get_att_text function returning a single string attribute (note the nlen == 1 in the patch) as if it is a character attributes.
What is not implemented are multidimensional strings, and the capability to write string attributes, because of the lack of string type in Fortran standard. This patch would allow reading netCDF files like the one created from the Copernicus cds-beta site for ECMWF ERA5 model. See reference to this in issue #181 "string attributes are not supported yet?"