Skip to main content

Physical units interface to xarray using astropy

Project description

CI code coverage docs PyPI version codestyle conda-forge

astropy-xarray

A convenience wrapper for using astropy with xarray.

Simple Usage

import astropy_xarray
import xarray as xr

ds = xr.Dataset({"a": ("x", [0, 1, 2]), "b": ("y", [-3, 5, 1], {"units": "m"})})
ds

output:

<xarray.Dataset> Size: 48B
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) int64 24B 0 1 2
    b        (y) int64 24B -3 5 1

To convert the variables of a Dataset to quantities, use accessor .astropy.quantify():

q = ds.astropy.quantify(a="s")
q

output:

<xarray.Dataset> Size: 48B
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) float64 24B [s] 0.0 1.0 2.0
    b        (y) float64 24B [m] -3.0 5.0 1.0

to convert to different units, use accessor .astropy.to():

c = q.astropy.to({"a": "ms", "b": "km"})
c

output:

<xarray.Dataset> Size: 48B
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) float64 24B [ms] 0.0 1e+03 2e+03
    b        (y) float64 24B [km] -0.003 0.005 0.001

to convert back to non-quantities for portability, use accessor .astropy.dequantify():

d = c.astropy.dequantify()
d

output:

<xarray.Dataset> Size: 48B
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) float64 24B 0.0 1e+03 2e+03
    b        (y) float64 24B -0.003 0.005 0.001

SkyCoord Usage

To convert a astropy.skyCoord to a Dataset, use skycoord_to_dataset().

import astropy.units as u
from astropy.coordinates import ICRS, SkyCoord
from astropy.time import Time

from astropy_xarray.coordinates.sky_coord import (
    skycoord_to_dataset,
)

sc = skycoord_to_dataset(
    SkyCoord(
        ra=[[2, 6, 7, 4]] * u.deg,
        dec=[[4, 7, 4, 3]] * u.deg,
        pm_ra_cosdec=[[1, 1, 1, 1]] * u.mas / u.yr,
        pm_dec=[[1, 1, 1, 1]] * u.mas / u.yr,
        frame="icrs",
    ),
    coords={
        "timestamp": ("time", Time([1.7e9], format="unix")),
        "field_label": ("field", ["a", "b", "c", "d"]),
    },
)
sc

output:

<xarray.Dataset> Size: 152B
Dimensions:       (time: 1, field: 4)
Coordinates:
    timestamp     (time) float64 8B [utc unix] 2023-11-14T22:13:20.000000000
    field_label   (field) <U1 16B 'a' 'b' 'c' 'd'
Dimensions without coordinates: time, field
Data variables:
    ra            (time, field) float64 32B [°] 2.0 6.0 7.0 4.0
    dec           (time, field) float64 32B [°] 4.0 7.0 4.0 3.0
    pm_ra_cosdec  (time, field) float64 32B [mas yr⁻¹] 1.0 1.0 1.0 1.0
    pm_dec        (time, field) float64 32B [mas yr⁻¹] 1.0 1.0 1.0 1.0
Attributes:
    frame:    {'name': 'icrs', 'representation_type': 'spherical', 'different...

To convert SkyCoord-like datasets back to astropy types, use .astropy.to_skycoord():

sc.astropy.to_skycoord()

output:

<SkyCoord (ICRS): (ra, dec) in deg
    [[(2., 4.), (6., 7.), (7., 4.), (4., 3.)]]
 (pm_ra_cosdec, pm_dec) in mas / yr
    [[(1., 1.), (1., 1.), (1., 1.), (1., 1.)]]>

For more, see the documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

astropy_xarray-0.2.0.tar.gz (69.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

astropy_xarray-0.2.0-py3-none-any.whl (57.2 kB view details)

Uploaded Python 3

File details

Details for the file astropy_xarray-0.2.0.tar.gz.

File metadata

  • Download URL: astropy_xarray-0.2.0.tar.gz
  • Upload date:
  • Size: 69.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for astropy_xarray-0.2.0.tar.gz
Algorithm Hash digest
SHA256 876c37e6694cb99dd72a5356ece1b4afe4864d5e5b9ac719a14f6acc52a251a8
MD5 caccd88a5d86e37a459120d864c40d87
BLAKE2b-256 449ab7ab3123bdd851cc91cd8cd5858c783da758161938160e578b4b0ddbd2f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for astropy_xarray-0.2.0.tar.gz:

Publisher: pypi.yaml on calgray/astropy-xarray

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file astropy_xarray-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: astropy_xarray-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 57.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for astropy_xarray-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8823f98ba312062437b98e61654a58926c1c462461c296f53c2fcdb0f4687193
MD5 dd1ebfe4fe02c06965c0aef586a1a87e
BLAKE2b-256 15f6e1beee3d51d35d16be834fed89e030f7a9ba3f93b71ac3c3da8ba306d671

See more details on using hashes here.

Provenance

The following attestation bundles were made for astropy_xarray-0.2.0-py3-none-any.whl:

Publisher: pypi.yaml on calgray/astropy-xarray

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page