Skip to content

Conversation

@sloosvel
Copy link
Contributor

@sloosvel sloosvel commented Nov 18, 2022

This PR adds iris-esmf-regrid as a dependency, adapts the regridding module so that the available schemes in the package (ESMFAreaWeighted and regrid_rectilinear_to_rectilinear) are supported and adds tests.

Description

Closes #1794

Link to documentation:


Before you get started

Checklist

It is the responsibility of the author to make sure the pull request is ready to review. The icons indicate whether the item will be subject to the 🛠 Technical or 🧪 Scientific review.


To help with the number pull requests:

@sloosvel sloosvel added the preprocessor Related to the preprocessor label Nov 18, 2022
@sloosvel sloosvel added this to the v2.8.0 milestone Nov 18, 2022
@codecov
Copy link

codecov bot commented Nov 18, 2022

Codecov Report

Merging #1809 (c7ec2cc) into main (5644203) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1809   +/-   ##
=======================================
  Coverage   91.50%   91.50%           
=======================================
  Files         202      202           
  Lines       10919    10927    +8     
=======================================
+ Hits         9991     9999    +8     
  Misses        928      928           
Impacted Files Coverage Δ
esmvalcore/preprocessor/_regrid.py 97.18% <100.00%> (+0.07%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@sloosvel sloosvel marked this pull request as ready for review November 18, 2022 15:52
@sloosvel sloosvel added the installation Installation problem label Nov 21, 2022
Copy link
Contributor

@schlunma schlunma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sloosvel, looks good!

One comment on documentation: I think it would make sense to add to the doc that we now not only support schemes that can be used within cube.regrid() (like ESMFAreaWeighted), but also schemes that are functions f(src_cube, grid_cube) -> Cube.

Comment on lines 601 to 607
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)

Copy link
Contributor

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_scheme for both the if and else statement. You could also restore the lines 574-576 then.

Can scheme_args be something else than None or a list? If not, I think

if scheme_args is not None:

might 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.

Copy link
Contributor Author

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_rectilinear does 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 the if isinstance(scheme, dict) one? At first sight it does not look like it would break anything and it would be more organised.

Copy link
Contributor

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_grid is defined only later! I think swapping the blocks if isinstance(target_grid, str) and if isinstance(scheme, dict) is a good idea!

About the mdtol: I think it currently is possible to set this with

scheme:
  reference: esmf_regrid.schemes.ESMFAreaWeighted
  mdtol: 0.5

Copy link
Contributor Author

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

Comment on lines 634 to 640
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
if _attempt_irregular_regridding(cube, scheme):
cube = esmpy_regrid(cube, target_grid, scheme)
elif isinstance(loaded_scheme, iris.cube.Cube):
cube = loaded_scheme
else:
cube = cube.regrid(target_grid, loaded_scheme)

Maybe clearer? Could you also add a comment here in which cases loaded_scheme is a Cube?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I have also updated the documentation.

Copy link
Contributor

@schlunma schlunma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sloosvel for implementing the changes! 3 tiny comments, then I will approve 👍

sloosvel and others added 2 commits November 23, 2022 15:13
Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com>
Copy link
Contributor

@schlunma schlunma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!!

@sloosvel
Copy link
Contributor Author

@ESMValGroup/esmvaltool-coreteam anyone willing to merge?

@valeriupredoi
Copy link
Contributor

@sloosvel two seconds, I want to turn on GA tests just for a wee, then will merge

@valeriupredoi valeriupredoi self-requested a review as a code owner November 23, 2022 16:31
Copy link
Contributor

@valeriupredoi valeriupredoi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good stuff @sloosvel @schlunma 🍺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

installation Installation problem preprocessor Related to the preprocessor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Installing iris-esmf-regrid as a dependency

4 participants