Skip to main content

Physical units interface to xarray using astropy

Project description

astropy-xarray

CI code coverage docs PyPI version codestyle conda-forge

Seamless interoperability between astropy and 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.1.tar.gz (302.3 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.1-py3-none-any.whl (57.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: astropy_xarray-0.2.1.tar.gz
  • Upload date:
  • Size: 302.3 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.1.tar.gz
Algorithm Hash digest
SHA256 0ac8f63d9b25d0c8798d91068c045bfb31201b7cacb4e3357248139a86c6aab5
MD5 4511f304ff7892d7a1ceeaf63d23e27f
BLAKE2b-256 8f4efdd98e2c5fdd798980caad8376a8fa3f8f72d0d53c5c73ba65972a01f82c

See more details on using hashes here.

Provenance

The following attestation bundles were made for astropy_xarray-0.2.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: astropy_xarray-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 57.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c80db050e86d59d0b6a3247f24955ea57e4f91e723c319f56a8e6bc0b9267d83
MD5 8012dd8b99edb3cca41f0a140c1c0cfc
BLAKE2b-256 def1f1668daf62696cfec095db59cc018daaffd8c2dab16ef379872e6401b23a

See more details on using hashes here.

Provenance

The following attestation bundles were made for astropy_xarray-0.2.1-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