yt_xarray.transformations

class yt_xarray.transformations.GeocentricCartesian(radial_type: str = 'radius', radial_axis: str | None = None, r_o: float | unyt_quantity | None = None, coord_aliases: dict[str, str] | None = None, use_neg_lons: bool = False)[source]

Bases: Transformer

A transformer to convert between Geodetic coordinates and cartesian, geocentric coordinates.

Parameters

radial_type: str

one of (“radius”, “depth”, “altitude”) to indicate the type of radial axis.

radial_axis: str

Optional string to use as the name for the radial axis, defaults to whatever you provide for radial_type.

r_o: float like

The reference radius, default is the radius of the Earth.

coord_aliases: dict

Optional dictionary of additional coordinate aliases.

use_neg_lons: bool

If False (the default), will expect longitude in the range 0, 360. If True, will expect longitude in the range -180, 180.

transformed_coords names are (“x”, “y”, “z”) and native_coords names are (radial_axis, “latitude”, “longitude”). Supply latitude and longitude vlaues in degrees.

Examples

>>> from yt_xarray.transformations import GeocentricCartesian
>>> gc = GeocentricCartesian("depth")
>>> x, y, z = gc.to_transformed(depth=100., latitude=42., longitude=220.)
>>> print((x, y, z))
# (-3626843.0297669284, -3043282.6486153184, 4262969.546178633)
>>> print(gc.to_native(x=x,y=y,z=z))
# (100.00000000093132, 42.0, 220.0)
calculate_transformed_bbox(bbox_dict: Mapping[str, ndarray[tuple[int, ...], dtype[_ScalarType_co]]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Calculates a bounding box in transformed coordinates for a bounding box dictionary in native coordinates.

Parameters

bbox_dictdict

dictionary with the ranges for each native dimension.

Returns

np.ndarray

2D bounding box array

class yt_xarray.transformations.LinearScale(native_coords: tuple[str, ...], scale: dict[str, float] | None = None)[source]

Bases: Transformer

A transformer that linearly scales between coordinate systems.

This transformer is mostly useful for demonstration purposes and simply applies a constant scaling factor for each dimension:

(x_sc, y_sc, z_sc) = (x_scale, y_scale, z_scale) * (x, y, z)

Parameters

native_coords: tuple[str, …]

the names of the native coordinates, e.g., (‘x’, ‘y’, ‘z’), on which data is defined.

scale: dict

a dictionary containing the scale factor for each dimension. keys should match the native_coords names and missing keys default to a value of 1.0

The scaled coordinate names are given by appending ‘_sc’ to each native coordinate name. e.g., if native_coords=(‘x’, ‘y’, ‘z’), then the transformed coordinate names are (‘x_sc’, ‘y_sc’, ‘z_sc’).

Examples

>>> from yt_xarray.transformations import LinearScale
>>> native_coords = ('x', 'y', 'z')
>>> scale_factors = {'x': 2., 'y':3., 'z':1.5}
>>> lin_scale = LinearScale(native_coords, scale_factors)
>>> print(lin_scale.to_transformed(x=1, y=1, z=1))
[2., 3., 1.5]
>>> print(lin_scale.to_native(x_sc=2., y_sc=3., z_sc=1.5))
[1., 1., 1.]
calculate_transformed_bbox(bbox_dict: Mapping[str, ndarray[tuple[int, ...], dtype[_ScalarType_co]]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Calculates a bounding box in transformed coordinates for a bounding box dictionary in native coordinates.

Parameters

bbox_dictdict

dictionary with the ranges for each native dimension.

Returns

np.ndarray

2D bounding box array

class yt_xarray.transformations.Transformer(native_coords: tuple[str, ...], transformed_coords: tuple[str, ...], coord_aliases: dict[str, str] | None = None)[source]

Bases: ABC

The transformer base class, meant to be subclassed, do not instantiate directly.

Parameters

native_coords: tuple[str, …]

the names of the native coordinates, e.g., (‘x0’, ‘y0’, ‘z0’), on which data is defined.

transformed_coords: tuple[str, …]

the names of the transformed coordinates, e.g., (‘x1’, ‘y1’, ‘z1’)

coord_aliases: dict

optional dictionary of coordinate aliases to map arbitrary keys to a native or transformed coordinate name.

the names of the coordinates will be expected as keyword arguments in the ‘to_native’ and ‘to_transformed’ methods.

abstract calculate_transformed_bbox(bbox_dict: Mapping[str, ndarray[tuple[int, ...], dtype[_ScalarType_co]]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Calculates a bounding box in transformed coordinates for a bounding box dictionary in native coordinates.

Parameters

bbox_dictdict

dictionary with the ranges for each native dimension.

Returns

np.ndarray

2D bounding box array

to_native(**coords: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) list[ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]

Calculate the native coordinates from transformed coordinates.

Parameters

coords:

coordinate values in transformed coordinate system, provided as individual keyword arguments.

Returns

list

coordinate values in the native coordinate system, in order of the native_coords attribute.

to_transformed(**coords: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) list[ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]

Calculate the transformed coordinates from native coordinates.

Parameters

coords:

coordinate values in native coordinate system, provided as individual keyword arguments.

Returns

list

coordinate values in the transformed coordinate system, in order of the transformed_coords attribute.

yt_xarray.transformations.build_interpolated_cartesian_ds(xr_ds: Dataset, transformer: Transformer, fields: str | tuple[str, ...] | list[str] | None = None, grid_resolution: tuple[int, ...] | list[int] | None = None, fill_value: float | None = None, length_unit: str | float = 'km', refine_grid: bool = False, refine_by: int = 2, refine_max_iters: int = 200, refine_min_grid_size: int = 10, refinement_method: str = 'division', sel_dict: dict[str, Any] | None = None, sel_dict_type: str = 'isel', bbox_dict: Mapping[str, ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | None = None, interp_method: str = 'nearest', interp_func: Callable[[...], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | None = None)[source]

Build a yt cartesian dataset containing fields interpolated on demand from data defined on a 3D Geodetic grid to a uniform, cartesian grid

Parameters

xr_ds: xr.Dataset

the xarray dataset

transformer:

a Transformer instance that will convert between 3D cartesian coordinates and the native coordinates of the dataset

fields: tuple

the fields to include

grid_resolution:

the interpolated grid resolution, defaults to (64, 64, 64)

fill_value: float

Optional value to use for filling grid values that fall outside the original data. Defaults to np.nan, but for volume rendering you may want to adjust this.

length_unit: str

the length unit to use, defaults to ‘km’

refine_grid: bool

if True (default False), will decompose the interpolated grid one level.

refine_max_iters: int

if refine_grid is True, max iterations for grid refinement (default 200)

refine_min_grid_size:

if refine_grid is True, minimum number of elements in refined grid (default 10)

refinement_method:

One of 'division' (the default) or 'signature_filter'. If 'division', refinement will proceed by iterative bisection in each dimension. If 'signature_filter', will use the image mask signature decomposition of Berger and Rigoutsos 1991 (https://doi.org/10.1109/21.120081).

interp_method: str

interpolation method: 'nearest' or 'interpolate'. Defaults to 'nearest'. If 'interpolate', will use linear nd interpolation.

interp_func: Callable

a custom interpolation function. Will over-ride interp_method. The function will be called with interp_func(data=data_array, coords=eval_coords), where data_array is an xarray DataArray and eval_coords is a list of 1d np.ndarray ordered by the transformer native coordinate order and should return an np.ndarray of the same shape as the eval_coords

Returns

yt.Dataset

a yt dataset: cartesian, uniform grid with references to the provided xarray dataset. Interpolation from geodetic to geocentric cartesian happens on demand on data reads.