Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataArray aware pint.context #231

Open
LecrisUT opened this issue Dec 7, 2023 · 5 comments
Open

DataArray aware pint.context #231

LecrisUT opened this issue Dec 7, 2023 · 5 comments

Comments

@LecrisUT
Copy link

LecrisUT commented Dec 7, 2023

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.

@jmineau
Copy link

jmineau commented Jul 23, 2024

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
Copy link

jmineau commented Jul 23, 2024

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, hgrecco/pint#1559 was actually my issue - trying to convert a flux from particles per area per time to mass per area per time.

@keewis
Copy link
Collaborator

keewis commented Jul 23, 2024

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?

@jmineau
Copy link

jmineau commented Jul 23, 2024

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.

@LecrisUT
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants