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

Too many attributes converted to string #135

Open
aulemahal opened this issue Dec 16, 2022 · 4 comments
Open

Too many attributes converted to string #135

aulemahal opened this issue Dec 16, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@aulemahal
Copy link
Collaborator

aulemahal commented Dec 16, 2022

In save_to_zarr and save_to_netcdf, we coerce attributes of "funky" types into strings before writing to file. However, our "funky" check is not exhaustive enough.

See:

xscen/xscen/io.py

Lines 333 to 338 in 134c31b

if not (
isinstance(attrs[k], (str, float, int, np.ndarray))
or isinstance(attrs[k], (tuple, list))
and isinstance(attrs[k][0], (str, float, int))
):
attrs[k] = str(attrs[k])

An example is found here:

import xarray as xr

ds = xr.open_dataset('/you/know/where/caq/invariants/areacella_caq_fx.nc')
type(ds.rotated_pole.attrs['grid_north_pole_longitude'])

Which give : numpy.float32 , something that the line above does not catch. And thus, floating point coordinates of the rotated pole are converted to string when written to file, which breaks it.

I suspect something changed in the recent xarray, because I otherwise don't understand how we wouldn't have caught that earlier.

@aulemahal aulemahal added the bug Something isn't working label Dec 16, 2022
@RondeauG
Copy link
Collaborator

RondeauG commented Dec 16, 2022

That doesn't quite seem to be the problem. I just tested it and isinstance(attrs[k], (str, float, int, np.ndarray)) does catch it correctly and keeps it as a numpy.float32. ncview crashes nonetheless.

I also tried to force either str or float (non-Numpy), but without success in ncview.

@aulemahal
Copy link
Collaborator Author

Darn.
On my end isinstance(np.float32(0), (str, float, int, np.ndarray) is False and the ncview error explicitly said: NetCDF: Attempt to convert between text & numbers. I am guessing there are 2 issues at play here, depending on the package versions maybe.

I'm on:
numpy 1.23.5
xarray 2022.12.0
xscen 0.4.10-beta

@RondeauG
Copy link
Collaborator

I'm on the latest xscen (0.4.12), but numpy 1.22.4 and xarray 2022.6.0

@aulemahal
Copy link
Collaborator Author

aulemahal commented Dec 16, 2022

Woups, I'm sorry, I had not noticed that with my code, both methods seem to work.

With to_netcdf, the attributes are indeed in the file as numbers.
With xs.save_to_netcdf, attributes are saved as strings.

However, in both cases, ncview opens and I have not seen different behaviours.

EDIT:
ncview understands the save_to_netcdf version as rlat is latitude and rlon is longitude. If I plot coastlines over the data (see Opts), I actually see Africa instead of North America.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants