xarray-units

:zap: xarray extension for handling units

https://github.com/astropenguin/xarray-units

Science Score: 57.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 3 DOI reference(s) in README
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.6%) to scientific vocabulary

Keywords

units xarray xarray-accessor xarray-extension
Last synced: 6 months ago · JSON representation ·

Repository

:zap: xarray extension for handling units

Basic Info
Statistics
  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • Open Issues: 1
  • Releases: 6
Topics
units xarray xarray-accessor xarray-extension
Created about 2 years ago · Last pushed about 2 years ago
Metadata Files
Readme License Citation

README.md

xarray-units

Release Python Downloads DOI Tests

xarray extension for handling units

Overview

xarray-units is an import-only package that provides an xarray DataArray accessor .units for handling units such as converting units and numeric operations considering units. Astropy is used as a backend. Unlike similar implementations, xarray-units does not use a special data type to handle units, but uses the original data type in a DataArray. This allows to continue to use powerful features such as parallel and lazy processing with Dask and/or user-defined DataArray subclasses.

Installation

shell pip install xarray-units==0.5.0

Basic usages

Suppose the following imports will be commonly used in the examples below:

python import xarray as xr import xarray_units

Setting and unsetting units

xarray-units sets units in DataArray attributes (.attrs) with the name "units":

python da_km = xr.DataArray([1, 2, 3]).units.set("km") print(da_km)

<xarray.DataArray (dim_0: 3)> array([1, 2, 3]) Dimensions without coordinates: dim_0 Attributes: units: km

And the units can also be unset (deleted):

python da = da_km.units.unset() print(da)

<xarray.DataArray (dim_0: 3)> array([1, 2, 3]) Dimensions without coordinates: dim_0

[!NOTE] These are equivalent to manually un/setting the units in the DataArray attributes, but the units accessor also check that the units are valid when setting.

Converting units to others

xarray-units converts a DataArray with units to other units:

python da_km = xr.DataArray([1, 2, 3]).units.set("km") da_m = da_km.units.to("m") print(da_m)

<xarray.DataArray (dim_0: 3)> array([1000., 2000., 3000.]) Dimensions without coordinates: dim_0 Attributes: units: m

Astropy equivalencies can also be used for equivalences between different types of units:

```python from astropy.units import spectral

damm = xr.DataArray([1, 2, 3]).units.set("mm") daGHz = damm.units.to("GHz", spectral()) print(daGHz) ```

<xarray.DataArray (dim_1: 3)> array([299.792458 , 149.896229 , 99.93081933]) Dimensions without coordinates: dim_0 Attributes: units: GHz

[!TIP] There exist other accessor methods (e.g. decompose, like) for converting units. See the package guide for more details.

Numeric operations considering units

xarray-units performs numerical operations considering units when the units accessor is attached to the DataArray on the left side of the operator:

```python dam = xr.DataArray([1000, 2000, 3000]).units.set("m") dakm = xr.DataArray([1, 2, 3]).units.set("km")

dasumm = dam.units + dakm dasumkm = dakm.units + dam

print(dasumm) print(dasumkm) ```

``` array([2000., 4000., 6000.]) Dimensions without coordinates: dim_0 Attributes: units: m

array([2., 4., 6.]) Dimensions without coordinates: dim_0 Attributes: units: km ```

The units of the DataArray after the operation follows those of the DataArray with the units accessor. The resulting data values will be therefore different depending on the order of the operation. They are, of course, equal when considering units:

python da_eq = (da_sum_m.units == da_sum_km) print(da_eq)

<xarray.DataArray (dim_0: 3)> array([ True, True, True]) Dimensions without coordinates: dim_0

[!IMPORTANT] Because this feature is accessor-based, units are only considered for the operation right after the units accessor. See method and operation chains for performing multiple operations at once.

[!TIP] There exist accessor methods corresponding to each operator (e.g. add+, eq==). See the package guide for more details.

Formatting units

xarray-units converts units to various string formats:

```python da = xr.DataArray([1, 2, 3]).units.set("m / s^2")

daconsole = da.units.format("console") dalatex = da.units.format("latex")

print(daconsole) print(dalatex) ```

``` array([1, 2, 3]) Dimensions without coordinates: dim_0 Attributes: units: m s^-2

array([1, 2, 3]) Dimensions without coordinates: dim_0 Attributes: units: $\mathrm{\frac{m}{s^{2}}}$ ```

This is useful, for example, when plotting a DataArray:

python da.units.format("latex").plot()

[!NOTE] By default, the units of the DataArray coordinates will also be formatted.

Advanced usages

Handling units of coordinates

The units accessor has an option for handling units of DataArray coordinates. For example, the following code will create a DataArray with x and y coordinates in units of meters:

python da_m = xr.DataArray([[1, 2], [3, 4]], dims=["x", "y"]).units.set("deg_C") da_m = da_m.assign_coords( x=xr.DataArray([1000, 2000], dims="x").units.set("m"), y=xr.DataArray([3000, 4000], dims="y").units.set("m"), ) print(da_m.x) print(da_m.y)

``` array([1000, 2000]) Coordinates: * x (x) int64 1000 2000 Attributes: units: m

array([3000, 4000]) Coordinates: * y (y) int64 3000 4000 Attributes: units: m ```

To handling the units of the DataArray coordinates, use an option of for specifying them:

python da_km = da_m.units(of=["x", "y"]).to("km") print(da_km.x) print(da_km.y)

``` array([1., 2.]) Coordinates: * x (x) float64 1.0 2.0 Attributes: units: km

array([3., 4.]) Coordinates: * y (y) float64 3.0 4.0 Attributes: units: km ```

where of accepts the name(s) of the coordinate(s) to be accessed.

Method and operation chains

The units accessor has an option chain for chaining methods or operations while considering units:

python da_m = xr.DataArray([1, 2, 3]).units.set("m") da_s = xr.DataArray([1, 2, 3]).units.set("s") da_a = da_m.units(chain=2) / da_s / da_s print(da_a)

<xarray.DataArray (dim_0: 3)> array([1. , 0.5 , 0.33333333]) Dimensions without coordinates: dim_0 Attributes: units: m / s2

where chain is the length of chained methods or operations. This is equivalent to nesting the units accessors:

python (da_m.units / da_s).units / da_s

Use with static type checking

xarray-units provides a special type hint xarray_units.DataArray with which type checkers can statically handle the units accessor and its methods:

```python from xarray_units import DataArray

da: DataArray = xr.DataArray([1, 2, 3]).units.set("km") ```

[!TIP] xarray_units.DataArray will be replaced by xarray.DataArray at runtime, so it can also be used for creating and subclassing DataArray.

Use without the units accessor

xarray-units provides a function xarray_units.units that returns the units accessor of a DataArray. The following two codes are therefore equivalent:

python xr.DataArray([1, 2, 3]).units.set("km")

```python from xarray_units import units

units(xr.DataArray([1, 2, 3])).set("km") ```

Owner

  • Name: Akio Taniguchi
  • Login: astropenguin
  • Kind: user
  • Location: Nagoya, Japan
  • Company: Nagoya University

Project assistant professor (LMT-FINER)

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."

title: "xarray-units"
abstract: "xarray extension for handling units"
version: 0.5.0
date-released: 2024-02-04
license: "MIT"
doi: "10.5281/zenodo.10354517"
url: "https://github.com/astropenguin/xarray-units/"
authors:
  - given-names: "Akio"
    family-names: "Taniguchi"
    affiliation: "Nagoya University"
    orcid: "https://orcid.org/0000-0002-9695-6183"

GitHub Events

Total
  • Watch event: 3
Last Year
  • Watch event: 3

Issues and Pull Requests

Last synced: over 1 year ago

All Time
  • Total issues: 18
  • Total pull requests: 15
  • Average time to close issues: 6 days
  • Average time to close pull requests: 1 day
  • Total issue authors: 1
  • Total pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 15
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 18
  • Pull requests: 15
  • Average time to close issues: 6 days
  • Average time to close pull requests: 1 day
  • Issue authors: 1
  • Pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 15
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • astropenguin (10)
Pull Request Authors
  • astropenguin (15)
Top Labels
Issue Labels
feature (9) release (7) env (1)
Pull Request Labels
feature (12) release (7) docs (2) env (1)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 39 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 6
  • Total maintainers: 1
pypi.org: xarray-units

xarray extension for handling units

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 39 Last month
Rankings
Dependent packages count: 10.1%
Average: 38.6%
Dependent repos count: 67.1%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/gh-pages.yaml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • peaceiris/actions-gh-pages v3 composite
.github/workflows/pypi.yaml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/tests.yaml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
poetry.lock pypi
  • accessible-pygments 0.0.4
  • alabaster 0.7.13
  • astropy 6.0.0
  • astropy-iers-data 0.2023.12.11.0.31.11
  • asttokens 2.4.1
  • babel 2.13.1
  • beautifulsoup4 4.12.2
  • black 23.11.0
  • certifi 2023.11.17
  • charset-normalizer 3.3.2
  • click 8.1.7
  • colorama 0.4.6
  • decorator 5.1.1
  • docutils 0.20.1
  • exceptiongroup 1.2.0
  • executing 2.0.1
  • idna 3.6
  • imagesize 1.4.1
  • importlib-metadata 7.0.0
  • iniconfig 2.0.0
  • ipython 8.18.1
  • jedi 0.19.1
  • jinja2 3.1.2
  • markdown-it-py 3.0.0
  • markupsafe 2.1.3
  • matplotlib-inline 0.1.6
  • mdit-py-plugins 0.4.0
  • mdurl 0.1.2
  • mypy-extensions 1.0.0
  • myst-parser 2.0.0
  • nodeenv 1.8.0
  • numpy 1.26.2
  • packaging 23.2
  • pandas 2.1.4
  • parso 0.8.3
  • pathspec 0.12.1
  • pexpect 4.9.0
  • platformdirs 4.1.0
  • pluggy 1.3.0
  • prompt-toolkit 3.0.41
  • ptyprocess 0.7.0
  • pure-eval 0.2.2
  • pydata-sphinx-theme 0.14.4
  • pyerfa 2.0.1.1
  • pygments 2.17.2
  • pyright 1.1.339
  • pytest 7.4.3
  • python-dateutil 2.8.2
  • pytz 2023.3.post1
  • pyyaml 6.0.1
  • requests 2.31.0
  • setuptools 69.0.2
  • six 1.16.0
  • snowballstemmer 2.2.0
  • soupsieve 2.5
  • sphinx 7.2.6
  • sphinxcontrib-applehelp 1.0.7
  • sphinxcontrib-devhelp 1.0.5
  • sphinxcontrib-htmlhelp 2.0.4
  • sphinxcontrib-jsmath 1.0.1
  • sphinxcontrib-qthelp 1.0.6
  • sphinxcontrib-serializinghtml 1.1.9
  • stack-data 0.6.3
  • tomli 2.0.1
  • traitlets 5.14.0
  • typing-extensions 4.9.0
  • tzdata 2023.3
  • urllib3 2.1.0
  • wcwidth 0.2.12
  • xarray 2023.12.0
  • zipp 3.17.0
pyproject.toml pypi
  • astropy >=5.2, <7.0
  • python >=3.9, <3.13
  • xarray >=2022.3, <2024.0