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

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

Closed
fipoucat opened this issue Mar 9, 2024 · 8 comments
Closed

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

fipoucat opened this issue Mar 9, 2024 · 8 comments
Assignees
Labels
waiting for response Waiting for response of user

Comments

@fipoucat
Copy link

fipoucat commented Mar 9, 2024

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
Copy link
Collaborator

lpilz commented Mar 11, 2024

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
Copy link
Author

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
Copy link
Collaborator

lpilz commented Mar 13, 2024

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 lpilz changed the title [MISC]: destaggered error [BUG]: WRF Units are rejected by .metpy.quantify() Mar 13, 2024
@lpilz
Copy link
Collaborator

lpilz commented Mar 15, 2024

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
Copy link
Collaborator

lpilz commented Mar 19, 2024

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

@lpilz lpilz added the waiting for response Waiting for response of user label Mar 19, 2024
@lpilz lpilz self-assigned this Mar 19, 2024
@fipoucat
Copy link
Author

fipoucat commented Mar 19, 2024 via email

@fipoucat
Copy link
Author

Thanks, the problem is fixed.

@lpilz
Copy link
Collaborator

lpilz commented Mar 19, 2024

Good to hear, thanks for the report :)

@lpilz lpilz closed this as completed Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for response Waiting for response of user
Projects
None yet
Development

No branches or pull requests

2 participants