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

Add suggestion in ValueError: Cannot parse units for lat/lon #107

Closed
raybellwaves opened this issue May 30, 2021 · 2 comments · Fixed by #128
Closed

Add suggestion in ValueError: Cannot parse units for lat/lon #107

raybellwaves opened this issue May 30, 2021 · 2 comments · Fixed by #128

Comments

@raybellwaves
Copy link
Member

raybellwaves commented May 30, 2021

First time dabbling with pint-xarray.

precip in the ERA5 catalog hosted by planet os is in m (total precipitation). Wanted to convert it to mm. I could have simply multiplied it by 1,000 but was a good excuse to try this package.

import pint_xarray
import xarray as xr


ds = xr.open_mfdataset(
    "s3://era5-pds/zarr/2020/01/data/precipitation_amount_1hour_Accumulation.zarr",
    engine="zarr",
)
ds = ds.drop("time1_bounds")

q = ds.pint.quantify(precipitation_amount_1hour_Accumulation="m")

ValueError                                Traceback (most recent call last)
<ipython-input-42-fa56e0f4f598> in <module>
----> 1 q = ds.pint.quantify(precipitation_amount_1hour_Accumulation="m")

~/opt/miniconda3/envs/main/lib/python3.9/site-packages/pint_xarray/accessors.py in quantify(self, units, unit_registry, **unit_kwargs)
    972 
    973         if invalid_units:
--> 974             raise ValueError(format_error_message(invalid_units, "parse"))
    975 
    976         return self.ds.pipe(conversion.strip_unit_attributes).pipe(

ValueError: Cannot parse units:
 -- invalid units for variable 'lat': degrees_north (attribute) (reason: 'degrees_north' is not defined in the unit registry)
 -- invalid units for variable 'lon': degrees_east (attribute) (reason: 'degrees_east' is not defined in the unit registry)

From perusing the issues it's probably related to #26

Tested on main as well

mamba create -n test_env python=3.9 --y
conda activate test_env
mamba install -c conda-forge xarray dask zarr s3fs --y
pip install pip install git+https://github.com/xarray-contrib/pint-xarray.git
@raybellwaves
Copy link
Member Author

raybellwaves commented May 30, 2021

The docs help here: https://pint-xarray.readthedocs.io/en/latest/examples/plotting.html#convert-units-into-a-format-understood-by-pint

works fine when doing

ds.lat.attrs["units"] = "degree"
ds.lon.attrs["units"] = "degree"

You could parse for lat/lon in the ValueError: Cannot parse units and explicitly state you could try setting ds.variable.attrs["units"] = "degree" if it contains lat/lon/latitude/longitude etc. or look for degrees_[north/east/south/west].

@raybellwaves raybellwaves changed the title invalid units for variable 'lat': degrees_north Add suggestion in ValueError: Cannot parse units for lat/lon May 30, 2021
@keewis
Copy link
Collaborator

keewis commented May 30, 2021

the code in the tutorial notebook really is a workaround, and I think we'll change that to depend on cf-xarray>0.5.2 (once that is released) which provides a UnitRegistry that can parse these units (and also other cfunits like m s-2). We'll still need to figure out some issues regarding the registering of UnitRegistry objects, though.

Once again, thanks for opening these issues. pint-xarray is currently in a state where most things should work but there are still some issues that need to be fixed (both bugs and design choices), so I really appreciate the feedback.

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

Successfully merging a pull request may close this issue.

2 participants