-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add support for netcdf4 enum #8144
Comments
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
Also, it might be nice to have a way to automatically translate variable typed with enums to the CF flag_meanings, flag_values attributes. And the other way around too. |
I'll add ncdump/h5dump of the above file for those who are interested: ncdump -h mre.nc
h5dump mre.nc
|
A core problem is that there isn't an enumerated array type other than the 1D pandas.Categorical so we can't decode to anything very useful. IMO the best solution at the moment is to "decode" by saving the enum dictionary as an attribute at read time and "encode" by specifying enum types at write time.
👍 PR welcome! |
@bzah Thanks for tackling this. We've just discussed this at the dev-meeting. It should be decoded into For the user it might be more discoverable that a certain To roundtrip we could use |
Ah snap I would have tagged along if I new the dev-meeting was yesterday, my bad. The only issue with attrs so far is that when saving the file with netCDF4 we have to remove the enum dict from the attributes because an attribute cannot be a dict for netCDF4. |
@bzah Yeah, sorry, I've put this on the agenda just short before the meeting. You are very welcome to attend any meeting. I've added some ideas on your PR how the suggested attributes/encoding solution could be laid out. |
In case people are interested, there will be a lightning talk and a hackaton session about adding Enums in CF at the CF workshop. |
What do you think of saving an That feels a lot more explicit to me. |
If you mean generating the Enum with something like |
Is your feature request related to a problem?
When a netcdf file contains netcdf4 enums , xarray ignores the underlying enum type.
The association between the values of the variable and their actual meaning is then lost.
MRE:
If you know CF, enums could replace replace
flag_meanings
andflag_values
, see CFEnums are not yet part of CF though.
Describe the solution you'd like
As far as I understand, to describe the enum we only need a dictionary that map numbers (enum key) to string (enum value) and a way to reference this dictionary in variables that are "typed" to this enum.
Bear in mind that the dtype of the variable would still be a number, the enum type would be a secondary metadata.
Describe alternatives you've considered
Most people that produce data could get away with using flag_meanings and flag_values to describe their data in a way which is both CF proof and properly managed by xarray.
For me, the only workaround at the moment is to use the netCDF4 library directly.
Additional context
The text was updated successfully, but these errors were encountered: