interp_masked

PlasmaCalcs.mhd.mhd_radiative_loader.interp_masked(array, bins, dim, *, mode='correct', return_weights=False)

Array masked to bins, possibly with interpolation correction and normalization factors,

for use in (Velocity) Differential Emission Measure ((V)DEM) binning.
The result has array dims plus bins dim, with values from 0 to 1.
Conceptually - basically, this just tells whether array overlaps with each bin.
E.g. result.isel(x=i, y=j, bindim=k) tells whether the (i,j)th x,y point overlaps with bin k.
Actually - it may include an interpolation correction factor,
accounting for adjacent cell values, and any jumps larger than bin widths.
E.g. result.isel(x=i, bindim=k) tells how much of bin k is covered from the ith to (i+1)th x.
Additionally - if return_weights, this method also returns xn, which can be applied to a value,
to properly account for interpolation between adjacent cells.
See Rempel et al. (2017) for details.
(Note - Rempel+2017 formulas are slightly different than those here,
because those formulas solve a slightly different problem.
Testing those formulas here led to more numerical artifacts.)
If binning multiple quantities (e.g., temperature and velocity), the canonical pattern to use is:
mask1, xn = self.interp_masked(quantity1, bins1, return_weights=’exp’)
mask2 = self.interp_masked(quantity2, bins2)
mask3 = self.interp_masked(quantity3, bins3)
… # etc. i.e., only the first call computes interpolation weights.
emiss_interped = emiss * (1.0 - xn) + emiss.roll({x:1}) * xn
Note - the result’s first value along dim may be unreliable (if non-periodic array),
as it relies on array.roll({dim:1}).
Parameters
———-
array : xarray.DataArray
variable to be interp-masked. E.g., array of log10(temperature) values.
bins : xarray.DataArray
1D array of (evenly-spaced) bin centers (internally, assumes bin width = (bins[1]-bins[0])/2).
dim : str
apply the correction along this dimension of array.
For VDEMS, this is the dimension along which the LOS integration will be performed.
mode: ‘correct’ or ‘simple’
tells how to compute the result.
‘correct’ –> includes correction factor.
‘simple’ –> exclude correction factor, just tells whether each point falls within each bin.
Equivalent to: result = (bins - delta <= array) & (array < bins + delta),
where delta = (bins[1] - bins[0]) / 2 == bin width.
This can be much faster than mode=’correct’, but produces noisier results.
(Also, only allowed if return_weights=False).
return_weights : False, True, or ‘exp’
whether to also return interpolation weights xn for each bin.
False –> returns masked
True –> returns (masked, xn)
‘exp’ –> returns (masked, xn) but compute xn using (10**value, 10**bins) not (value, bins).
Returns
——-
masked : xarray.DataArray
array of fn * overlaps, where overlaps = whether each cell overlaps with each bin,
and fn = normalization factor for each bin, clipped to [0, 1].
(Or, if mode==’simple’, this is just (bins - delta <= array) & (array < bins + delta).)
xn : xarray.DataArray
Interpolation weight for each bin, with values clipped to [0, 1].
(Only returned if return_weights.)