Skip to content

Utilities API

Physical corrections, masking for plots, and helpers for reading FITS data.

Barycentric corrections

lvm_tools.physical_properties.barycentric_corr.get_v_barycentric(mjd, α, δ, unit='km/s')

Barycentric radial velocity correction at Las Campanas Observatory.

Uses astropy's SkyCoord.radial_velocity_correction with the "barycentric" kind. The inputs broadcast against each other.

Parameters:

Name Type Description Default
mjd ArrayLike

Modified Julian Date(s) of the observation(s).

required
α ArrayLike

Right ascension(s) (ICRS), in degrees.

required
δ ArrayLike

Declination(s) (ICRS), in degrees.

required
unit str

Unit of the result, any velocity unit astropy understands.

'km/s'

Returns:

Type Description
NDArray

The correction(s), in unit: a NumPy array, or a float for scalar inputs.

Example
import numpy as np
from lvm_tools.physical_properties.barycentric_corr import get_v_barycentric

# Single observation
v_bary = get_v_barycentric(mjd=60000.5, α=180.0, δ=-30.0)
print(f"v_bary = {v_bary:.2f} km/s")

# Several observations
v_bary = get_v_barycentric(
    mjd=np.array([60000.5, 60001.5]),
    α=np.array([180.0, 180.1]),
    δ=np.array([-30.0, -30.1]),
)

Spatial masking

mask_near_points is useful for restricting a model evaluated on a grid (e.g. a map made on a grid of sky_x, sky_y) to the region covered by data, so sparse regions aren't extrapolated into.

lvm_tools.utils.mask.mask_near_points(xgrid, ygrid, xpoints, ypoints, threshold=None)

A boolean mask of the points of a 2D grid that are close to at least one data point.

The nearest data point to each grid point is found with a KD-tree. Use it to restrict a model evaluated on a grid to where there are data.

Parameters:

Name Type Description Default
xgrid ndarray

Grid coordinates along x (monotonically increasing).

required
ygrid ndarray

Grid coordinates along y (monotonically increasing).

required
xpoints ndarray

x coordinates of the data points.

required
ypoints ndarray

y coordinates of the data points.

required
threshold float | None

A grid point is kept if its nearest data point is closer than this. If None, 1.5 times the larger of the mean grid spacings in x and y.

None

Returns:

Type Description
ndarray

Boolean mask of shape (len(ygrid), len(xgrid)), True where the grid point is near a data point ("keep").

Example
import numpy as np
from lvm_tools.utils.mask import mask_near_points

xgrid = np.linspace(0, 10, 100)
ygrid = np.linspace(0, 10, 100)
xpoints = np.random.uniform(2, 8, 50)
ypoints = np.random.uniform(2, 8, 50)

mask = mask_near_points(xgrid, ygrid, xpoints, ypoints)
data = np.ones((len(ygrid), len(xgrid)))
masked_data = np.where(mask, data, np.nan)

Helper functions

These are in lvm_tools.data.helper.

lvm_tools.data.helper.daskify_native(array, chunks)

Lazily wrap an array as a Dask array with native byte order.

Nothing is read until a chunk is computed, so selecting part of a memory-mapped (e.g. big-endian FITS) array only reads and byte-swaps the chunks it touches.

Parameters:

Name Type Description Default
array ArrayLike

The array to wrap, typically a FITS memory map.

required
chunks str | int | tuple

Dask chunk specification, as for dask.array.from_array.

required

Returns:

Type Description
Array

The Dask array, with the same shape and a native-byte-order dtype.

lvm_tools.data.helper.numpyfy_native(array)

Convert an array to a NumPy array with native byte order.

FITS data are big-endian; this byte-swaps them if needed (copying the data), and returns arrays already in native order unchanged.

Parameters:

Name Type Description Default
array ArrayLike

The array to convert.

required

Returns:

Type Description
NDArray

The NumPy array, in native byte order.

lvm_tools.data.helper.summarize_with_units(ds)

A text summary of a dataset, used in the repr of LVMTile and LVMTileCollection.

It lists the dataset's size and dimensions, then each coordinate and data variable with its dimensions, dtype, size and units attribute, and for data variables whether they are Dask arrays (with their chunks) or NumPy arrays.

Parameters:

Name Type Description Default
ds Dataset

The dataset to summarise.

required

Returns:

Type Description
str

The summary, one item per line.

lvm_tools.data.helper.convert_sci_to_int(arr)

Convert IFU labels to integers: "Sci1" to 0, "Sci2" to 1 and "Sci3" to 2.

Parameters:

Name Type Description Default
arr ArrayLike

IFU labels.

required

Returns:

Type Description
NDArray

The integer indices.

Raises:

Type Description
KeyError

If a label is not one of the three.