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

should pint-xarray return the units attr? #108

Closed
raybellwaves opened this issue May 30, 2021 · 2 comments
Closed

should pint-xarray return the units attr? #108

raybellwaves opened this issue May 30, 2021 · 2 comments

Comments

@raybellwaves
Copy link
Member

raybellwaves commented May 30, 2021

I noticed pint-xarray converts to the desired units but along the way it loses the units attribute. I can see in the html repr it keeps track of the unit within the Quantity. Is it worth keeping a copy in the attrs as well as these are in used in plotting?

import pint_xarray
import xarray as xr

var = "precipitation_amount_1hour_Accumulation"
ds = xr.open_mfdataset(
    "s3://era5-pds/zarr/2020/01/data/{}.zarr".format(var),
    engine="zarr",
)
ds = ds.drop("time1_bounds")
ds.lat.attrs["units"] = "degree"
ds.lon.attrs["units"] = "degree"
da = ds.sel(lat=slice(2, 0), lon=slice(0, 2), time1="2020-01-01T00")[var]
da
<xarray.DataArray 'precipitation_amount_1hour_Accumulation' (lat: 9, lon: 9)>
dask.array<getitem, shape=(9, 9), dtype=float32, chunksize=(9, 9), chunktype=numpy.ndarray>
Coordinates:
  * lat      (lat) float32 2.0 1.75 1.5 1.25 1.0 0.75 0.5 0.25 0.0
  * lon      (lon) float32 0.0 0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0
    time1    datetime64[ns] 2020-01-01
Attributes:
    long_name:       Total precipitation
    nameCDM:         Total_precipitation_1hour_Accumulation
    nameECMWF:       Total precipitation
    product_type:    forecast
    shortNameECMWF:  tp
    standard_name:   precipitation_amount
    units:           m

Screen Shot 2021-05-29 at 11 53 19 PM

q = da.pint.quantify(da.attrs["units"])
q
<xarray.DataArray 'precipitation_amount_1hour_Accumulation' (lat: 9, lon: 9)>
<Quantity(dask.array<getitem, shape=(9, 9), dtype=float32, chunksize=(9, 9), chunktype=numpy.ndarray>, 'meter')>
Coordinates:
  * lat      (lat) float32 2.0 1.75 1.5 1.25 1.0 0.75 0.5 0.25 0.0
  * lon      (lon) float32 0.0 0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0
    time1    datetime64[ns] 2020-01-01
Attributes:
    long_name:       Total precipitation
    nameCDM:         Total_precipitation_1hour_Accumulation
    nameECMWF:       Total precipitation
    product_type:    forecast
    shortNameECMWF:  tp
    standard_name:   precipitation_amount

c = q.pint.to("mm")
c
<xarray.DataArray 'precipitation_amount_1hour_Accumulation' (lat: 9, lon: 9)>
<Quantity(dask.array<mul, shape=(9, 9), dtype=float32, chunksize=(9, 9), chunktype=numpy.ndarray>, 'millimeter')>
Coordinates:
  * lat      (lat) float32 2.0 1.75 1.5 1.25 1.0 0.75 0.5 0.25 0.0
  * lon      (lon) float32 0.0 0.25 0.5 0.75 1.0 1.25 1.5 1.75 2.0
    time1    datetime64[ns] 2020-01-01
Attributes:
    long_name:       Total precipitation
    nameCDM:         Total_precipitation_1hour_Accumulation
    nameECMWF:       Total precipitation
    product_type:    forecast
    shortNameECMWF:  tp
    standard_name:   precipitation_amount

c.plot()

Screen Shot 2021-05-30 at 12 06 00 AM

Desired:

c.attrs["units"] = "mm" # User specified in `pint.to` or the term returned by pint ("millimeter")
c.plot()

Screen Shot 2021-05-30 at 12 08 10 AM

@keewis
Copy link
Collaborator

keewis commented May 30, 2021

thanks for the issue, @raybellwaves. Keeping the units in the attributes is difficult because it would have to be synchronized with the units on the data, and most operations in xarray don't have hooks for that.

For now, the best workaround is to dequantify immediately before calling the plotting functions. In the future, I hope that we can just have this work automatically (you set the default format: ureg.default_format = "~P" (or pint-xarray does set a good default) and the matplotlib.units functionality does the rest). However, that currently doesn't work because xarray's plotting functions convert to ndarray (see #91 for the issue).

@raybellwaves
Copy link
Member Author

Thanks. That works. And thanks a lot for you all your work here!

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