xarray-contrib / xwrf

A lightweight interface for working with the Weather Research and Forecasting (WRF) model output in Xarray.
https://xwrf.readthedocs.io/
Apache License 2.0
59 stars 16 forks source link

[BUG]: WRF Units are rejected by .metpy.quantify() #162

Closed fipoucat closed 7 months ago

fipoucat commented 8 months ago

What is your issue?

I am testing xwrf for metpy on wrf arw file with below command but getting error and hope a hint to solve this:

destaggered = ds.xwrf.destagger().metpy.quantify() destaggered['wind_speed'] = wind_speed(destaggered.U, destaggered.V) destaggered.wind_speed

Traceback (most recent call last):

Cell In[42], line 1 destaggered = ds.xwrf.destagger().metpy.quantify()

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:1097 in quantify return self._dataset.map(lambda da: da.metpy.quantify()).assign_attrs(

File ~/anaconda3/lib/python3.11/site-packages/xarray/core/dataset.py:6037 in map variables = {

File ~/anaconda3/lib/python3.11/site-packages/xarray/core/dataset.py:6038 in k: maybe_wrap_array(v, func(v, *args, **kwargs))

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:1097 in return self._dataset.map(lambda da: da.metpy.quantify()).assign_attrs(

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:235 in quantify quantified_dataarray = self._data_array.copy(data=self.unit_array)

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:165 in unit_array return units.Quantity(self._data_array.data, self.units)

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:142 in units return units.parse_units(self._data_array.attrs.get('units', default_unit))

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1189 in parse_units self.parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py:70 in parse_units_as_container return super().parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1204 in parse_units_as_container return self._parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1232 in _parse_units_as_container units = ParserHelper.from_string(input_string, self.non_int_type)

File ~/anaconda3/lib/python3.11/site-packages/pint/util.py:767 in from_string ret = build_eval_tree(gen).evaluate(

File ~/anaconda3/lib/python3.11/site-packages/pint/pint_eval.py:385 in evaluate self.right.evaluate(define_op, bin_op, un_op),

File ~/anaconda3/lib/python3.11/site-packages/pint/pint_eval.py:383 in evaluate return bin_op[op_text](

File ~/anaconda3/lib/python3.11/site-packages/pint/pint_eval.py:64 in _power return operator.pow(left, right)

File ~/anaconda3/lib/python3.11/site-packages/pint/util.py:866 in pow return self.class(self.scale**other, d, non_int_type=self._non_int_type)

TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'ParserHelper'

destaggered = ds.xwrf.destagger().metpy.quantify() Traceback (most recent call last):

Cell In[43], line 1 destaggered = ds.xwrf.destagger().metpy.quantify()

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:1097 in quantify return self._dataset.map(lambda da: da.metpy.quantify()).assign_attrs(

File ~/anaconda3/lib/python3.11/site-packages/xarray/core/dataset.py:6037 in map variables = {

File ~/anaconda3/lib/python3.11/site-packages/xarray/core/dataset.py:6038 in k: maybe_wrap_array(v, func(v, *args, **kwargs))

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:1097 in return self._dataset.map(lambda da: da.metpy.quantify()).assign_attrs(

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:235 in quantify quantified_dataarray = self._data_array.copy(data=self.unit_array)

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:165 in unit_array return units.Quantity(self._data_array.data, self.units)

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:142 in units return units.parse_units(self._data_array.attrs.get('units', default_unit))

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1189 in parse_units self.parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py:70 in parse_units_as_container return super().parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1204 in parse_units_as_container return self._parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1232 in _parse_units_as_container units = ParserHelper.from_string(input_string, self.non_int_type)

File ~/anaconda3/lib/python3.11/site-packages/pint/util.py:767 in from_string ret = build_eval_tree(gen).evaluate(

File ~/anaconda3/lib/python3.11/site-packages/pint/pint_eval.py:385 in evaluate self.right.evaluate(define_op, bin_op, un_op),

File ~/anaconda3/lib/python3.11/site-packages/pint/pint_eval.py:383 in evaluate return bin_op[op_text](

File ~/anaconda3/lib/python3.11/site-packages/pint/pint_eval.py:64 in _power return operator.pow(left, right)

File ~/anaconda3/lib/python3.11/site-packages/pint/util.py:866 in pow return self.class(self.scale**other, d, non_int_type=self._non_int_type)

TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'ParserHelper'

lpilz commented 8 months ago

Sounds like there might be some problems with quantities not being able to be defined correctly. Maybe try quantifying this dataset in a for loop and see which variable it breaks at. Like this:

for var in ds.data_vars:
    try:
        ds[var] = ds[var].metpy.quantify()
    except TypeError:
       print(f"Caught TypeError at {var}")
fipoucat commented 8 months ago

Below is the output of your suggested command:

Caught TypeError at DUST_1 Caught TypeError at DUST_2 Caught TypeError at DUST_3 Caught TypeError at DUST_4 Caught TypeError at DUST_5 Traceback (most recent call last):

Cell In[14], line 3 ds[var] = ds[var].metpy.quantify()

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:235 in quantify quantified_dataarray = self._data_array.copy(data=self.unit_array)

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:165 in unit_array return units.Quantity(self._data_array.data, self.units)

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:142 in units return units.parse_units(self._data_array.attrs.get('units', default_unit))

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1189 in parse_units self.parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py:70 in parse_units_as_container return super().parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1204 in parse_units_as_container return self._parse_units_as_container(input_string, as_delta, case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:1239 in _parse_units_as_container cname = self.get_name(name, case_sensitive=case_sensitive)

File ~/anaconda3/lib/python3.11/site-packages/pint/facets/plain/registry.py:647 in get_name raise UndefinedUnitError(name_or_alias)

UndefinedUnitError: 'pvu' is not defined in the unit registry

lpilz commented 7 months ago

Okay, I think we fixed this in #116 already but it's not released yet. I'll try to wrap up the open PR and release the new version of xwrf tomorrow.

In the meantime (and you'll maybe have to do this after the new release, too depending on the unit which is involved) you can try to only quantify the variables which you're actively working with. So change your code to

destaggered = ds.xwrf.destagger()
for var in ["U", "V"]:
    destaggered[var] = destaggered[var].metpy.quantify()
destaggered['wind_speed'] = wind_speed(destaggered.U, destaggered.V)
destaggered.wind_speed
lpilz commented 7 months ago

I just created a new release, it should hopefully be online in conda-forge in the next couple of hours, whenever the bot discovers it.

lpilz commented 7 months ago

Hi @fipoucat, I just wanted to ask whether the problem still exists with xwrf version 0.0.3.

fipoucat commented 7 months ago

I have an issue with my Xarray installation and need to fix it before testing again with your new version. I'll update you when done.

Thank you

On Tue, Mar 19, 2024 at 9:19 AM Lukas Pilz @.***> wrote:

Hi @fipoucat https://github.com/fipoucat, I just wanted to ask whether the problem still exists with xwrf version 0.0.3.

— Reply to this email directly, view it on GitHub https://github.com/xarray-contrib/xwrf/issues/162#issuecomment-2006458323, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAXY6LI5DRA6V2XAPRZSOOLYY77L3AVCNFSM6AAAAABEOHRU36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBWGQ2TQMZSGM . You are receiving this because you were mentioned.Message ID: @.***>

fipoucat commented 7 months ago

Thanks, the problem is fixed.

lpilz commented 7 months ago

Good to hear, thanks for the report :)