Conflict in unit alias

Open
#543 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
numpy, python
Domain
data

Research direction

Reproduce the example using Pint's application_registry, NumPy, and the cf_xarray.units import shown in the report, then inspect how the CF units are registered and how the electron-volt alias changes. Done means the documented conversion from electron temperature to thermal velocity still converts to km/s after importing cf_xarray.units, with a regression check for the reported conflict.

Written by the indexing model from the issue text.

Description

Take a conversion from electron temperature to thermal velocity as an example:

from pint import application_registry as u
import numpy as np
np.sqrt(100 * u.eV / u.m_e).to("km/s")
# <Quantity(4193.82881, 'kilometer / second')>

If we then import cf_xarray.units to enable CF units, per this page, we get the following error:

import cf_xarray.units
np.sqrt(100 * u.eV / u.m_e).to("km/s")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tvo/.local/share/mamba/envs/test_xarray_units/lib/python3.12/site-packages/pint/facets/plain/quantity.py", line 536, in to
    magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tvo/.local/share/mamba/envs/test_xarray_units/lib/python3.12/site-packages/pint/facets/plain/quantity.py", line 480, in _convert_magnitude_not_inplace
    return self._REGISTRY.convert(self._magnitude, self._units, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tvo/.local/share/mamba/envs/test_xarray_units/lib/python3.12/site-packages/pint/facets/plain/registry.py", line 1041, in convert
    return self._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tvo/.local/share/mamba/envs/test_xarray_units/lib/python3.12/site-packages/pint/facets/context/registry.py", line 405, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tvo/.local/share/mamba/envs/test_xarray_units/lib/python3.12/site-packages/pint/facets/nonmultiplicative/registry.py", line 259, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tvo/.local/share/mamba/envs/test_xarray_units/lib/python3.12/site-packages/pint/facets/plain/registry.py", line 1076, in _convert
    raise factor
pint.errors.DimensionalityError: Cannot convert from 'electron_volt ** 0.5 / electron_mass ** 0.5' ([temperature] ** 0.5 * [length] / [time] ** 1.5 / [current] ** 0.5) to 'kilometer / second' ([length] / [time])
Dominant language
Python
Stars
181
Forks
49
Avg merge
43m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xarray-contrib/cf-xarray

All issues in xarray-contrib/cf-xarray

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.