Skip to main content

Xarray_SimpleUnits

builds package wheel pyversions codecov coveralls

Unitful calculations with xarray

Keeps track of units when working with xarray.DataArrays and xarray.Datasets using astropy.units for the conversions.

⚠️ This package is in alpha stage, that is, it works mostly, but the interface might still be subject to change.

Install

Requirements

  • astropy - required
  • xarray - required
  • pytest - optional, for testing

xarray_simpleunits

An installable pip package called xarray-simpleunits will be available soon from the main package repository, it can then be installed with:

$ pip install xarray_simpleunits

The latest development version can be installed with pip directly from github (see https://pip.pypa.io/en/stable/reference/pip_install/#vcs-support and https://pip.pypa.io/en/stable/reference/pip_install/#git):

$ pip install [-e] git+https://github.com/st-bender/xarray-simpleunits.git

The other option is to use a local clone:

$ git clone https://github.com/st-bender/xarray-simpleunits.git
$ cd xarray-simpleunits

and then using pip (optionally using -e, see https://pip.pypa.io/en/stable/reference/pip_install/#install-editable):

$ pip install [-e] .

or using setup.py:

$ python setup.py install

Optionally, test the correct function of the module with

$ py.test [-v]

or even including the doctests in this document:

$ py.test [-v] --doctest-glob='*.md'

Usage

The python module itself is named xarray_simpleunits and is imported as usual.

All functions should be numpy-compatible and work with scalars and appropriately shaped arrays.

>>> import xarray_simpleunits as xru

The module basically works by adapting (“monkey-patching”) the xarray.Variable arithmetic methods to honour and keep track of the "units" attribute. To initialize unit handling with xarray, call init_units() first:

>>> import xarray_simpleunits as xru
>>> xru.init_units()

(A similar method to restore the original behaviour is planned.)

So far, the supported operations are addition, subtraction, multiplication, and division. Unit mismatch when adding or subtracting unitful arrays raises an exception. Currently, unit handling with xarray.DataArray requires that the respective array is on the left side of any calculation:

>>> from astropy import units as au
>>> import numpy as np
>>> import xarray as xr
>>> import xarray_simpleunits as xru
>>> np.set_printoptions(precision=6)
>>> xru.init_units()
>>> ds = xr.Dataset(
...     data_vars={
...         "s": ("x", [1., 2., 3.], {"units": "m"}),
...         "t": ("x", [3., 2., 1.], {"units": "s"}),
...     },
... )
>>> v = ds["s"] / ds["t"]
>>> v  # doctest: +ELLIPSIS
<xarray.DataArray (x: 3)>...
array([0.333333, 1.      , 3.      ])
Dimensions without coordinates: x
Attributes:
    units:    m / s
>>> # using `astropy` units directly:
>>> v = ds["s"] / (2 * au.Unit("s"))
>>> v  # doctest: +ELLIPSIS
<xarray.DataArray 's' (x: 3)>...
array([0.5, 1. , 1.5])
Dimensions without coordinates: x
Attributes:
    units:    m / s

Basic class and method documentation is accessible via pydoc:

$ pydoc xarray_simpleunits

License

This python package is free software: you can redistribute it or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2 (GPLv2), see local copy or online version.

Metadata

Release files for xarray-simpleunits 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xarray-simpleunits 0.0.5
File Size Uploaded Provenance Actions
xarray_simpleunits-0.0.5.tar.gz 15.0 kB With provenance Details for xarray_simpleunits-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xarray-simpleunits 0.0.5
File Interpreter ABI Platform Actions
xarray_simpleunits-0.0.5-py2.py3-none-any.whl Python 3, Python 2 none any Details for xarray_simpleunits-0.0.5-py2.py3-none-any.whl

Total release size: 27.9 kB

Release files / xarray_simpleunits-0.0.5.tar.gz

Download URL xarray_simpleunits-0.0.5.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2fa314528c965193b0b5435b43a329d039450783de10daedd200ae2a95942c33
BLAKE2b-256 checksum
How to use checksums
f5c384a808a9e81669a6f7b78a69e94978648c2633960db27c88cea77d7383eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 27, 2025.

Transparency log

Release files / xarray_simpleunits-0.0.5-py2.py3-none-any.whl

Download URL xarray_simpleunits-0.0.5-py2.py3-none-any.whl
Size 12.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3a67ea052ff3041f00bc9ea35bdc94f9b61e407b5f02329bd8499e721cb79dfa
BLAKE2b-256 checksum
How to use checksums
3e17077ce1e097c03dd14fa2be811ff2ba8dc6a49fc8c5a4503f130a6a3be951
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 27, 2025.

Transparency log

Security for xarray-simpleunits 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for xarray_simpleunits-0.0.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 27, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page