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
ENH: Update convert_units_to function #1206
Conversation
Added auto conversion from amount to rate when the input variable is known to be a convertible amount.
[skip ci] No need to run CI for that.
A more generalist approach could be to do something like (pseudo-code):
That would work for "thickness_of_rainfall_amount" case, but also for any attempted conversion between an amount and a rate. WDYT ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice thanks!
Apart from my comment on amount2rate
, I think this automatic behaviour is right in xclim's scope.
I was going to suggest to open this up to all other thickness/amount<->rate/flux conversions, but I realized that would need extra care because of the non-liquid precipitation where the amount<->rate and thickness<->flux conversions are non-trivial.
If we were to add them, I think the machinery would be more complex and differentiate between all 4 categories and their possible conversions.
(Recall : amount= [kg m-2], thickness=m, rate=[m s-1] and flux=[kg m=2 s=1])
This is meant to fix the issue we have with e-obs dataset where the precipitation is not a flux but rather a "thickness_of_rainfall_amount". Now, it will be automatically converted to a rate using xclim amount2rate function. Note that if the dataset is discontinuous, amount2rate might convert, amounts to a wrong rate[0]. [0] Ouranosinc/xclim#1206 (comment)
@aulemahal Ready for final review. Should we add a test for irregular time series to confirm it fails ? |
…lim into enh/auto_convert_thickness
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Woupsi sorry. |
@huard and the others, I believe this is ready! I edited the top post to explain what was done here. Grosso modo : Instead of abel's units-based hook into |
Co-authored-by: David Huard <huard.david@ouranos.ca>
Nice to see this being merge 🎉 |
Pull Request Checklist:
number
) and pull request (:pull:number
) has been addedAUTHORS.rst
and.zenodo.json
What kind of change does this PR introduce?
This PR adds standard-name-aware unit conversions. Those are listed in
data/variables.yml
in a new "conversions" section. For now, theamount2rate
and the newamount2lwethickness
are available (and reverse operations).convert_units_to
will also perform those conversion automatically if the change in dimensionality requires it and the input has a valid standard name.The new functions are often the equivalent of the "hydro" context. Both are complementary: the hydro context is still to be used in indicators for which the transformation is implicitly valid, no matter the CF attributes.
This is necessary to be able to compute precipitation indices on datasets that express RR as "thickness_of_rainfall_amount" instead of the usual "precipitation_flux" such as the e-obs datasets.
I can't make these transformations available automatically in the indices with the current structure state of
declare_units
, I'll look into this in a future PR. For now, as shown in the editedunits.ipynb
these "CF conversions" must be down explicitly beforehand.Does this PR introduce a breaking change?
Yes, the error
pint.errors.DimensionalityError
is no longer raised when attempting to convert this kind of datasets described above into a[length] / [time]
compatible unit.