Utilities#
Transform#
- pyathena.util.transform.euler_rotation(vec, angles)[source]#
Rotate coordinate axes to transform the components of vector field vec
This assumes “passive” rotation where the vector itself is not rotated geometrically, but the coordinate axes are rotated. The euler angles take the “intrinsic” convention where the subsequent rotations are applied about the rotated axes.
- Parameters
vec (tuple, list, or numpy.ndarray of xarray.DataArray or numpy.ndarray) – (vx, vy, vz) representing Cartesian vector field components.
angles (tuple, list, or numpy.ndarray) – Euler angles [alpha, beta, gamma] in radian.
- Returns
Cartesian components of the rotated vector.
- Return type
tuple of xarray.DataArray or numpy.ndarray matching the input type
- pyathena.util.transform.to_spherical(vec, origin, newz=None)[source]#
Transform vector components from Cartesian to spherical coordinates.
Supports numpy.ndarray and xarray.DataArray (both dask and numpy-backed).
- Parameters
vec (tuple or list of xarray.DataArray) – (vx, vy, vz) representing Cartesian vector components.
origin (tuple or list) – (x0, y0, z0) representing the origin of the spherical coords.
newz (tuple or list, optional) – Cartesian components of the z-axis vector for the spherical coordinates. If not given, it is assumed to be (0, 0, 1).
- Returns
r (xarray.DataArray) – Binned radius
vec_sph (tuple) – (v_r, v_th, v_ph) representing the three components of vector in spherical coords.
- pyathena.util.transform.to_cylindrical(vec, origin)[source]#
Transform vector components from Cartesian to cylindrical coords.
- Parameters
vec (tuple or list of xarray.DataArray) – (vx, vy, vz) representing Cartesian vector components.
origin (tuple or list) – (x0, y0, z0) representing the origin of the spherical coords.
- Returns
R (xarray.DataArray) – Binned radius
vec_cyl (tuple) – (v_R, v_ph, v_z) representing the three components of velocities in cylindrical coords.
- pyathena.util.transform.groupby_bins(dat, coord, bins, range=None, cumulative=False, skipna=False)[source]#
Alternative to xr.groupby_bins, which is very slow
- Parameters
dat (xarray.DataArray) – input dataArray
coord (str) – coordinate name along which data is binned
bins (int or sequence of scalars) – If bins is an int, it defines the number of equal-width bins in the given range. If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.
range ((float, float), optional) – The lower and upper range of the bins.
cumulative (bool) –
- if True, perform cumulative binning, e.g.,
v_r_binned[i] = v_r( edge[0] <= r < edge[i+1] ).mean()
to calculate average velocity dispersion within radius r
- Returns
res – binned array
- Return type
xarray.DataArray
PiecewisePowerlaw#
- class pyathena.util.PiecewisePowerlaw(limits, powers, coefficients=None, externalval=0.0, norm=True)[source]#
A piecewise powerlaw function.
You can specify the intervals and power indices, and this class will figure out the coefficients needed to make the function continuous and normalized to unit integral.
Notes
Intervals are defined by an array l
Powerlaw indicies by and array p
a_n are the coefficients.
\[\begin{split}f(x) = a_n x^{p_n} \\textrm{ for } l_{n-1} <= x < l_n\end{split}\]Recursion relation for continuity:
\[a_n = a_{n-1} l_n^{p_{n-1} - p_n}\]Integral of a piece:
\[I_n = a_n p_n (l_{n+1}^{p_n - 1} - l_n^{p_n - 1})\]Total integral:
\[I_{tot} = \sum_0^N I_n\]Defined a piecewise powerlaw.
If coefficients is None then the coefficients are determined by requiring the function to be continuous and normalized to an integral of one.
The function is composed of N powerlaws, where N = len(powers).
len(limits) must be one greated than len(powers)
- Parameters
limits (array (length n+1)) – boundaries of the specified powerlaws. Must be one greater in length than coefficents and powers. Specify -numpy.infty for the first limit or numpy.infty for the last limit for unbounded powerlaws.
coefficients (optional array (length n)) – values of the coefficient a_i
powers (array (length n)) – values of the powerlaw indices p_i
externalval (scalar) – Value to return outside the defined domain. None correspons to ‘NaN’.
norm (boolean) – Whether to normalize the integral of the function over the defined domain to unity.
single (The resulting function takes a) –
of (one-dimensional array) –
operate. (values on which to) –
Rebin#
- pyathena.rebin_xyz(arr, bin_factor, fill_value=None)[source]#
Rebin a 3D array by averaging over blocks of size
bin_factorin all dimensions.- Parameters
arr (ndarray) – Masked or unmasked 3D numpy array with shape
(nz, ny, nx).bin_factor (int) – Number of cells to bin in each dimension. Must evenly divide each axis.
fill_value (float, optional) – If
arris a masked array, replace masked elements with this value before averaging. IfNone, masked elements are excluded from the average. Default isNone.
- Returns
arr_rebin – Rebinned array with shape
(nz//bin_factor, ny//bin_factor, nx//bin_factor).- Return type
ndarray
- pyathena.rebin_xy(arr, bin_factor, fill_value=None)[source]#
Rebin a 3D array by averaging over blocks of size
bin_factorin the x-y plane.The z-axis is left unchanged.
- Parameters
arr (ndarray) – Masked or unmasked 3D numpy array with shape
(nz, ny, nx).bin_factor (int) – Number of cells to bin along x and y. Must evenly divide both axes.
fill_value (float, optional) – If
arris a masked array, replace masked elements with this value before averaging. IfNone, masked elements are excluded from the average. Default isNone.
- Returns
arr_rebin – Rebinned array with shape
(nz, ny//bin_factor, nx//bin_factor).- Return type
ndarray