-
Notifications
You must be signed in to change notification settings - Fork 48
Add iris-esmf-regrid as a dependency #1809
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8ea1502
Add iris-esmf-regrid to env file
sloosvel f97985d
Allow to use regrid_rectilinear_to_rectilinear scheme
sloosvel 263f4b4
Add tests
sloosvel b5dda6f
Clean
sloosvel 5d4a348
Fix flake
sloosvel bc214a5
Merge remote-tracking branch 'origin/main' into dev_install_iris_esfm
sloosvel 473af23
Add package to setup
sloosvel 54b3acf
Fix some tests
sloosvel 41ff490
Correct package name
sloosvel 448f1c4
Fix shape in tests
sloosvel fcf77a2
Remove warning from docs
sloosvel 1de2ccd
Reorder code to make it cleaner
sloosvel aaa0cae
Improve documentation
sloosvel c23699d
Merge remote-tracking branch 'origin/main' into dev_install_iris_esfm
sloosvel 176f4ce
Fix flake
sloosvel 56b85aa
Restore condition
sloosvel 47005ea
Fix flake
sloosvel 6305e7d
Apply suggestions from code review
sloosvel d81ca55
Improve documentation
sloosvel aff8b2a
run GA tests
valeriupredoi c7ec2cc
GA tests run fine turning them off
valeriupredoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||
| """Horizontal and vertical regridding module.""" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import importlib | ||||||||||||||||||||||||||||
| import inspect | ||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||
|
|
@@ -536,8 +537,7 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): | |||||||||||||||||||||||||||
| extrapolation_mode: nanmask | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| To use the area weighted regridder available in | ||||||||||||||||||||||||||||
| :class:`esmf_regrid.schemes.ESMFAreaWeighted`, make sure that | ||||||||||||||||||||||||||||
| :doc:`iris-esmf-regrid:index` is installed and use | ||||||||||||||||||||||||||||
| :class:`esmf_regrid.schemes.ESMFAreaWeighted` use | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. code-block:: yaml | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -547,10 +547,8 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): | |||||||||||||||||||||||||||
| scheme: | ||||||||||||||||||||||||||||
| reference: esmf_regrid.schemes:ESMFAreaWeighted | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. note:: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Note that :doc:`iris-esmf-regrid:index` is still experimental. | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| scheme_args = None | ||||||||||||||||||||||||||||
| if isinstance(scheme, dict): | ||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||
| object_ref = scheme.pop("reference") | ||||||||||||||||||||||||||||
|
|
@@ -568,12 +566,12 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): | |||||||||||||||||||||||||||
| if separator: | ||||||||||||||||||||||||||||
| for attr in scheme_name.split('.'): | ||||||||||||||||||||||||||||
| obj = getattr(obj, attr) | ||||||||||||||||||||||||||||
| loaded_scheme = obj(**scheme) | ||||||||||||||||||||||||||||
| scheme_args = inspect.getfullargspec(obj).args | ||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||
| loaded_scheme = HORIZONTAL_SCHEMES.get(scheme.lower()) | ||||||||||||||||||||||||||||
| if loaded_scheme is None: | ||||||||||||||||||||||||||||
| emsg = 'Unknown regridding scheme, got {!r}.' | ||||||||||||||||||||||||||||
| raise ValueError(emsg.format(scheme)) | ||||||||||||||||||||||||||||
| if loaded_scheme is None: | ||||||||||||||||||||||||||||
| emsg = 'Unknown regridding scheme, got {!r}.' | ||||||||||||||||||||||||||||
| raise ValueError(emsg.format(scheme)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if isinstance(target_grid, str): | ||||||||||||||||||||||||||||
| if os.path.isfile(target_grid): | ||||||||||||||||||||||||||||
|
|
@@ -600,6 +598,13 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): | |||||||||||||||||||||||||||
| if not isinstance(target_grid, iris.cube.Cube): | ||||||||||||||||||||||||||||
| raise ValueError('Expecting a cube, got {}.'.format(target_grid)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if isinstance(scheme_args, list): | ||||||||||||||||||||||||||||
| if 'src_cube' in scheme_args: | ||||||||||||||||||||||||||||
| scheme['src_cube'] = cube | ||||||||||||||||||||||||||||
| if 'grid_cube' in scheme_args: | ||||||||||||||||||||||||||||
| scheme['grid_cube'] = target_grid | ||||||||||||||||||||||||||||
| loaded_scheme = obj(**scheme) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Unstructured regridding requires x2 2d spatial coordinates, | ||||||||||||||||||||||||||||
| # so ensure to purge any 1d native spatial dimension coordinates | ||||||||||||||||||||||||||||
| # for the regridder. | ||||||||||||||||||||||||||||
|
|
@@ -625,11 +630,13 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): | |||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||
| fill_value = GLOBAL_FILL_VALUE | ||||||||||||||||||||||||||||
| da.ma.set_fill_value(cube.core_data(), fill_value) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Perform the horizontal regridding | ||||||||||||||||||||||||||||
| if _attempt_irregular_regridding(cube, scheme): | ||||||||||||||||||||||||||||
| cube = esmpy_regrid(cube, target_grid, scheme) | ||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||
| if isinstance(loaded_scheme, iris.cube.Cube): | ||||||||||||||||||||||||||||
| return loaded_scheme | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| cube = cube.regrid(target_grid, loaded_scheme) | ||||||||||||||||||||||||||||
|
Comment on lines
634
to
642
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe clearer? Could you also add a comment here in which cases
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, I have also updated the documentation. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Preserve dtype and use masked arrays for 'unstructured_nearest' | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it would make sense to add this block to l.569. By doing this you will end up with a
loaded_schemefor both theifandelsestatement. You could also restore the lines 574-576 then.Can
scheme_argsbe something else thanNoneor alist? If not, I thinkmight be a little bit clearer. Also, can you add some comments what you do here? That would really help to understand the code better. E.g., why are you checking the args of
obj, why are you extending them with the cube and tgrid, etc.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.
Yes I thought it would fit better in those lines as well. However, there two schemes in iris-esmf.
The current main, supports the call to
esmf_regrid.schemes.ESMFAreaWeighted(mdtol=0), which returns a scheme to be called by cube.regrid.But
esmf_regrid.schemes.regrid_rectilinear_to_rectilineardoes not return a scheme, it's the regridding routine on it's own. It expects cubes as inputs and returns a regridded cube as output. And at that point the target cube has not been generated yet.That is why the arguments are being checked. Because one scheme does not require them (unless we want to allow mdtol to be set as well, which is not possible right now), whereas another one fails if the src_cube and grid_cube are not given as inputs.
Would it work for you if the routine checked first the
if isinstance(target_grid, str)block and then theif isinstance(scheme, dict)one? At first sight it does not look like it would break anything and it would be more organised.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.
Ah yes, you are totally right, I didn't notice that
target_gridis defined only later! I think swapping the blocksif isinstance(target_grid, str)andif isinstance(scheme, dict)is a good idea!About the
mdtol: I think it currently is possible to set this withThere 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.
Ah true, then essentially the only tricky arguments were src_cube and grid_cube, since those cannot be passed in the recipe. I have updated the code with these changes