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
Repository
:zap: xarray extension for handling units
Basic Info
- Host: GitHub
- Owner: astropenguin
- License: mit
- Language: Python
- Default Branch: main
- Homepage: https://astropenguin.github.io/xarray-units/
- Size: 1.81 MB
Statistics
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
- Releases: 6
Topics
Metadata Files
README.md
xarray-units
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
unitsaccessor 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) ```
```
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
unitsaccessor. 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) ```
```
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)
```
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)
```
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.DataArraywill be replaced byxarray.DataArrayat runtime, so it can also be used for creating and subclassingDataArray.
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
- Website: https://astropengu.in
- Twitter: astropengu_in
- Repositories: 76
- Profile: https://github.com/astropenguin
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
Pull Request Labels
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
- Homepage: https://github.com/astropenguin/xarray-units/
- Documentation: https://astropenguin.github.io/xarray-units/
- License: MIT
-
Latest release: 0.5.0
published about 2 years ago
Rankings
Maintainers (1)
Dependencies
- actions/checkout v3 composite
- actions/setup-python v4 composite
- peaceiris/actions-gh-pages v3 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- 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
- astropy >=5.2, <7.0
- python >=3.9, <3.13
- xarray >=2022.3, <2024.0