pint-xarray icon indicating copy to clipboard operation
pint-xarray copied to clipboard

DataArray aware `pint.context`

Open LecrisUT opened this issue 2 years ago • 5 comments

For example, consider the spectroscopy context:

@context(n=1) spectroscopy = sp

There the conversion depends on the variable n used. Now, consider a DataArray/DataSet where n is a dimension/coordinate. Then when calling DataArray.pint.to("nm") we would want the variable n to be dynamically imported from the current context of the DataArray coordinate.

API-wise I am not sure how that would work, maybe by passing a list/dict of context objects (+ variables)? This might also involve upstream pint support for more complicated context and decorators.

LecrisUT avatar Dec 07 '23 14:12 LecrisUT

Is there any support for pint.context right now? I've been trying to get the chemistry context working with the pint.to method, but have not had success.

jmineau avatar Jul 23 '24 03:07 jmineau

Is there any support for pint.context right now? I've been trying to get the chemistry context working with the pint.to method, but have not had success.

Sorry, https://github.com/hgrecco/pint/issues/1559 was actually my issue - trying to convert a flux from particles per area per time to mass per area per time.

jmineau avatar Jul 23 '24 05:07 jmineau

no progress (but it looks like you figured that out already). Mostly because I personally have no need for contexts, but in general someone just needs to figure out what would need to be done (and we'd need to figure out a clean way to integrate that in the API). The context manager should already work, though?

keewis avatar Jul 23 '24 10:07 keewis

Yeah, the context manager works with simple transformations defined by the context. So for the chemistry context, I can do

da: xr.dataArray
da = da.pint.quantify('particle')
with pint.context('chemistry', mw=16.04 * ureg('g/mol')):
    da_mass = da.pint.to('kg')

but I am working with emissions inventories (ex: EPA Methane Emissions Inventory) which are often given as netcdfs (perfect for xarray) in units of either [substance] / [area] / [time] OR [mass] / [area] / [time]. I want to get all my emission inventories in the same units, but from pint's issue 1559 (linked above), it appears I need to define a custom context to get this to work.

jmineau avatar Jul 23 '24 17:07 jmineau

I remember what exactly I was aiming with this issue. Indeed static context can work, but the difficult part is if we want to use an index name or a DataArray as the context variable to convert. For example, let's say we have a dim that has all simulation runs and we associate different values of a refractive index n. Then we might want to plot against wavelength or frequency, in which case we have to do it one slice at a time. It would be better if the pint conversion can extract the dependent dimension and perform xarray operations to do the necessary conversions.

With #163 finished, this might be possible

LecrisUT avatar Jul 23 '24 20:07 LecrisUT