eradiate.radprops.ParticleProperties#

class eradiate.radprops.ParticleProperties(data)#

Bases: object

An interface to scattering particle radiative properties.

This class manages scattering particle radiative properties and implements interpolation routines to produce kernel-level input data.

The wrapped dataset is in the Prt-Core v1 format, optionally extended with any number of state dimensions inserted right after w (see Extensions). Evaluation methods return one value per state-dimension grid point; interpolation along state dimensions is left to the caller (see interp()).

Parameters:

data (xarray.Dataset) – Single-scattering property dataset.

Fields:

data (xarray.Dataset) – Single-scattering property dataset.

classmethod convert(value)#

Conversion logic:

  • If value is a ParticleProperties instance, return it.

  • If value is a string, attempt resolving it as a dataset keyword.

  • If value is a path-like, load the dataset into memory and close the file.

  • If value is a Dataset, initialize a ParticleProperties with it.

Notes

xr.load_dataset (eager) is used instead of xr.open_dataset (lazy) so that the underlying file handle is closed immediately after reading. With lazy loading, the handle stays open until the dataset is garbage-collected. If construction later raises (e.g. a validator rejects the arguments), the partial object is kept alive by pytest’s traceback capture and the handle is never closed. This leads to handle accumulation and eventual C-level crashes in the HDF5/netCDF4 library.

eval_ext(w)#

Evaluate the extinction coefficient at wavelength(s) w by plain linear interpolation.

Parameters:

w (quantity) – Query wavelength(s); scalar or array.

Returns:

quantity – Extinction coefficient, shape (nw, *state_shape).

eval_phase(w, resample_mu=False, n_mu=None)#

Evaluate the phase function at a single wavelength by spectral interpolation between the two bracketing dataset wavelengths.

Parameters:
  • w (quantity) – Query wavelength (scalar). Each call interpolates a single wavelength, since it may have a different bracketing pair and thus a different union mu grid; looping over an array of wavelengths, if ever needed, is left to the caller.

  • resample_mu (bool, optional) – If False (default), return each state-dimension grid point’s own union of its two bracketing wavelengths’ native mu grids, as-is (see _eval_phase_interp_spectral()). If True, additionally resample every grid point onto a common, fixed-size mu grid of n_mu points (see _eval_phase_resample_mu()).

  • n_mu (int, optional) – Target mu point count, used only when resample_mu is True. Defaults to n_iangle if has_fixed_mu_grid, else 2 * n_iangle, where n_iangle = data.sizes["iangle"].

Returns:

  • mu (ndarray) – Scattering angle cosines, shape (*state_shape, n_mu), or (*state_shape, max_len) when resample_mu=False, NaN-padded past each grid point’s own native length.

  • phase (ndarray) – Phase function values in sr⁻¹, phamat leading, shape (n_phamat, *mu.shape).

Notes

The spectral interpolation scheme is similar to libRadtran’s and weighs bracketing wavelengths by their scattering coefficient. When the two bracketing wavelengths have different mu grids, the phase function is resampled onto their union grid before interpolation.

eval_pmom(w, clip=False)#

Evaluate Legendre moments at wavelength w by interpolation.

The two bracketing wavelengths are weighted by their scattering coefficient (extinction × single-scattering albedo). This ensures that a wavelength with no scattering contributes nothing to the interpolated phase function regardless of its spectral proximity. NaN values in the source data are treated as zero.

Parameters:
  • w (quantity) – Query wavelength(s); scalar or array.

  • clip (bool, optional) – If True, trailing all-zero slices are removed along the imom axis, so values.shape[0] == nleg.

Returns:

  • values (ndarray) – Legendre moment array for the (1,1) phase matrix element, shape (nmom, nw, *state_shape), or (nleg, nw, *state_shape) when clip=True.

  • nleg (int) – Index of the last nonzero moment (+ 1) across all queried wavelengths and state-dimension grid points.

eval_ssa(w)#

Evaluate the single-scattering albedo at wavelength w.

Parameters:

w (quantity) – Query wavelength(s); scalar or array.

Returns:

quantity – Dimensionless SSA, shape (nw, *state_shape).

Notes

The spectral interpolation scheme weighs bracketing wavelengths with the extinction coefficient, similar to libRadtran. The interpolated SSA is computed as:

ssa(w) = [(1-t)*ext_l*ssa_l + t*ext_r*ssa_r] / ext(w)

where ext(w) is the linearly interpolated extinction coefficient. When ext(w) is zero the method falls back to plain linear interpolation.

interp(grid, query)#

Interpolate the array grid at the points described by query. grid’s axes correspond to some of this dataset’s dimensions. The coordinate values for those dimensions are read from this dataset. Each axis must have a matching entry in query, keyed by dimension name. The axis order of grid must match the key order of query. Interpolation is linear. Query points are clipped to each dimension’s own coordinate range.

Parameters:
  • grid (quantity) – Array of values to interpolate. Each axis corresponds to one of this dataset’s dimensions.

  • query (dict[str, quantity]) – Query points. One same-shaped array per dimension of grid, keyed by dimension name, in the same order as grid’s axes.

Returns:

quantity – Interpolated value, with the same shape as query’s arrays.

phase_dataset_at(w, state_idx=(), ext=None)#

Build a single-point Prt-Core v1 dataset at the given wavelength, from the exact state-dimension grid point state_idx (no interpolation along state dimensions).

Parameters:
  • w (quantity) – Wavelength at which ext/ssa/the phase function are evaluated (interpolated along the wavelength axis only).

  • state_idx (tuple[int, ], optional) – Index along each of state_dims, in the same order. Empty (default) for a dataset without state dimensions.

  • ext (quantity, optional) – Extinction coefficient stored in the output dataset. Defaults to the value interpolated at w at grid point state_idx.

Returns:

xr.Dataset – A Prt-Core v1 dataset with a single wavelength and no state dimension.

Raises:

ValueError – If state_idx does not have one entry per state dimension.

state_coord(name)#

Get the coordinate values of a state dimension.

Parameters:

name (str) – Name of the requested state dimension.

Returns:

quantity – Coordinate values of state dimension name.

Raises:

ValueError – If name is not one of state_dims.

property abs#
Returns:

pint.Quantity – Absorption coefficient array, cached to minimize overhead.

property ext#
Returns:

pint.Quantity – Extinction coefficient array, cached to minimize overhead.

property has_fixed_mu_grid#
Returns:

bool – True iff all wavelengths and state-dimension grid points share an identical mu grid (same nangles and same mu values). When True, eval_phase uses n_iangle output points instead of 2 * n_iangle, preserving the original grid exactly.

property has_polarization#
Returns:

bool – True iff the phase matrix has more than one coefficient.

property max_union_size#
Returns:

int – Upper bound on _eval_phase_interp_spectral()’s total_pts across all adjacent wavelength pairs in w.

property particle_shape#
Returns:

str – Particle shape, described as a string, inferred from dataset contents and cached to minimize overhead.

property phase#
Returns:

xarray.DataArray – The phase variable of data, transposed to (phamat, iangle, w, *state_dims) to ensure a correct layout during interpolation, cached to minimize overhead.

property pmom#
Returns:

xarray.DataArray or None – The pmom variable of data, transposed to (imom, w, *state_dims), cached to minimize overhead. None if data has no pmom variable.

property scat#
Returns:

pint.Quantity – Scattering coefficient array, cached to minimize overhead.

property ssa#
Returns:

pint.Quantity – Single-scattering albedo array, cached to minimize overhead.

property state_dims#
Returns:

tuple[str, ] – Names of data’s state dimensions: its ext variable’s dimensions, excluding w. Empty for a plain Prt-Core v1 dataset.

property state_shape#
Returns:

tuple[int, ] – Sizes of state_dims, in the same order.

property w#
Returns:

pint.Quantity – Wavelength array, cached to minimize overhead.