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

Bilinear/nearest-neighbour regridding with land-sea mask #1438

Merged
merged 28 commits into from
May 14, 2021

Conversation

zfan001
Copy link
Contributor

@zfan001 zfan001 commented Mar 29, 2021

Description
New Code for Regridding with Land-Sea Mask [Author: Zhiliang(Julian) Fan & Tom Gale]

(1) Add new finite-element-based bilinear regridding with land-sea mask awareness. This method is more suitable for smooth continuous variables than the nearest neighbour regridding method. The implementation is very fast (only 10-15% increase on computational time as compared with the bilinear regridding without considering land-sea mask).
(2) Add new implementation of nearest neighbour regridding with land-sea mask awareness. This new implementation achieved a significant speed-up as compared with the original implementation.
(3) Add new implementation of nearest-neighbour regridding, which is much faster than the original Iris version.
(4) Add new implementation of bilinear regridding, which achieved same results and similar computational time as the Iris version.
(5) A new directory ./improver/regrid is created for regridding. Old regridding code (in ./improver/standardise.py)and new regridding code are moved into this directory, and re-organized into several files. A unit testing directory ./improver_tests/regrid also created accordingly.
(6) The implementation is extended from the existing RegridLanSea options("bilinear","nearest","nearest-with-mask"). New options are "nearest-2","bilinear-2","nearest-with-mask-2","bilinear-with-mask-2" (see ../regrid/landsea.py)
(7) New unit test is added by a careful-designed regridding example, in which so many different scenarios and combinations turn up (passed).
(8) New acceptance test cases are also added for four new options respectively.
(9) The code assumes that the source grid is in rectilinear latitude/longitude coordinate system, and the target grid could be in rectilinear latitude/longitude coordinate system or Lambert Azimuthal Equal Area coordinate system ( a curvilinear coordinate system).
(10) Five Jupyter notebooks are available for this new feature, which will be added into improver-aux.
Testing:

  • Ran tests and they passed OK
  • Added new tests for the new feature(s)

CLA

  • If a new developer, signed up to CLA

zfan001 and others added 7 commits March 19, 2021 10:27
@zfan001 zfan001 added MO review required PRs opened by non-Met Office developers that require a Met Office review BoM review required PRs opened by non-BoM developers that require a BoM review labels Mar 29, 2021
@codecov
Copy link

codecov bot commented Mar 29, 2021

Codecov Report

Merging #1438 (34849eb) into master (a735c68) will increase coverage by 0.06%.
The diff coverage is 98.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1438      +/-   ##
==========================================
+ Coverage   97.77%   97.84%   +0.06%     
==========================================
  Files         101      107       +6     
  Lines        8916     9362     +446     
==========================================
+ Hits         8718     9160     +442     
- Misses        198      202       +4     
Impacted Files Coverage Δ
improver/regrid/idw.py 96.36% <96.36%> (ø)
improver/regrid/landsea2.py 98.14% <98.14%> (ø)
improver/regrid/bilinear.py 98.88% <98.88%> (ø)
improver/regrid/landsea.py 99.09% <99.09%> (ø)
improver/regrid/grid.py 100.00% <100.00%> (ø)
improver/regrid/nearest.py 100.00% <100.00%> (ø)
improver/standardise.py 96.66% <100.00%> (-1.52%) ⬇️
improver/utilities/spatial.py 98.66% <100.00%> (+0.68%) ⬆️
improver/blending/utilities.py 100.00% <0.00%> (ø)
improver/blending/weighted_blend.py 100.00% <0.00%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a735c68...34849eb. Read the comment docs.

@zfan001 zfan001 changed the title Bilinear/Nearest-neighbour Regridding with Land-Sea Mask [Author: Zhiliang(Julian) Fan & Tom Gale] Bilinear/nearest-neighbour regridding with land-sea mask Mar 29, 2021
@MoseleyS
Copy link
Contributor

Thanks @zfan001 for this PR. (cc @tjtg). This is the largest PR for a while and it will take a bit of reviewing. What is the time scale for getting this reviewed? Is it holding any other work up?

@zfan001
Copy link
Contributor Author

zfan001 commented Mar 29, 2021

Hi Stephen,
Yes, a lot of new code in this PR, and the code is quite complicated as well. The code is not holding up other work. I will fix the codecov patch problem ASAP. BTW, it adds several new options for regridding. It should have no impact if new options are not used. Kind regards, Julian

@zfan001
Copy link
Contributor Author

zfan001 commented Mar 31, 2021

I re-designed the unit test case to ensure code coverage. I also add a test for coordinate system conversion.
codecov test is okay now.

Kind regards, Julian

Copy link
Contributor

@dmentipl dmentipl left a comment

Choose a reason for hiding this comment

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

Thanks @zfan001 for this work. This is a large PR and I don't understand all of the technical details. My review is focussed on software and code style.

A few general comments:

  • There seems to be a missing __init__.py file in regrid.
  • The docstrings could be better formatted. For example, sentences are usually capitalised with periods at the end. There are quite a few double spaces. (These can be found, e.g., in Vim with this regex [^\s] [^\s#]. The hash is there to ignore inline comments formatted by Black.) See these resources too: PEP 257 and Google Python style guide.
  • I may have made some suggestions/comments on existing code that was moved in the PR. Feel free to ignore those if it's not relevant to this PR.
  • The unit tests are mostly done via the cli interface. E.g. there are no unit tests of the functions in bilinear.py, idw.py, landsea2.py, or nearest.py. It might be good to add a few unit tests for these functions directly.

improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/bilinear.py Outdated Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/bilinear.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Outdated Show resolved Hide resolved
improver/regrid/idw.py Show resolved Hide resolved
improver/regrid/landsea.py Show resolved Hide resolved
improver/regrid/landsea.py Outdated Show resolved Hide resolved
improver/regrid/landsea.py Show resolved Hide resolved
improver/regrid/landsea2.py Outdated Show resolved Hide resolved
Copy link

@owena11 owena11 left a comment

Choose a reason for hiding this comment

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

Hey Julian,

Tried to cover off as much detail as possible but sure I've missed something given the size of the PR, but it all looks generally good! I've mainly focused on the Bilinear regridder with only a quick check of the nearest regridder, but the review was growing in size... thought it was worth putting out these comments first before checking Nearest in more detail. I know @fionaRust is also taking a look.

Summary:

  • Bilinear regridder checks out and is as described 👍
  • Understanding all assumptions about the input data is the most important thing missing from my point of view, maybe add in some optional validation of the data? (Think there is a hidden assumption about all coordinates being ascending, a stated assumption regarding the equal spacing of the source and no extrapolation(?)).
  • A bit of refinement of the structure of the code added would help massively making it approachable for some looking at this in the future and maintaining it.
  • A few area's where I wasn't clear about the intended behaviour, particularly in the edge cases of surface miss matches, I've just asked questions around, as I don't know if it's really right or wrong.

improver/regrid/landsea2.py Outdated Show resolved Hide resolved
improver/regrid/landsea2.py Outdated Show resolved Hide resolved
improver/regrid/landsea2.py Outdated Show resolved Hide resolved
improver/regrid/landsea2.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Outdated Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/nearest.py Show resolved Hide resolved
improver_tests/regrid/test_RegridWithLandSeaMask.py Outdated Show resolved Hide resolved
@fionaRust
Copy link
Contributor

Hi @zfan001

I've started review this today and I'll continue tomorrow but I have a couple of questions in the meantime:

  • You mention in your introduction that there are some jupyter notebooks to go with this PR that you want to put into improver_aux. Have you already made this PR?
  • Can you or @tjtg send me an email to let me know how best to access a copy of your new acceptance test data?

Thanks

@zfan001
Copy link
Contributor Author

zfan001 commented Apr 13, 2021

Hi Daniel, Aled & Fiona, @dmentipl @owena11 @fionaRust

Thank you very much for your review. I came back to work this Monday after a long break. I am working on some code changes based on your feedback, and will reply to each item soon.

@Fiona, Jupyter Notebooks and acceptance test data will be ready tomorrow. I will submit them for merging soon. Tom will help to make the files accessible to you.

Kind regards,
Julian

Copy link
Contributor

@fionaRust fionaRust left a comment

Choose a reason for hiding this comment

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

I'm submitting some of my initial comments. I still have a lot of code to look through. Some of my comments are more notes to myself about which code has been moved and which is new, which I'll delete when I've finished looking through the code.

improver/standardise.py Show resolved Hide resolved
improver/regrid/landsea.py Show resolved Hide resolved
improver/regrid/landsea.py Show resolved Hide resolved
improver/standardise.py Outdated Show resolved Hide resolved
improver/regrid/landsea.py Outdated Show resolved Hide resolved
improver/regrid/landsea2.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Show resolved Hide resolved
improver/regrid/landsea2.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Show resolved Hide resolved
@owena11
Copy link

owena11 commented Apr 23, 2021

@zfan001 , No hurry from my side (I'm on leave next week 24th - 4th), but you've answered lots of my comments from the first pass of reviewing it. Any code changes to push up from them? So I can 'resolve conversations' and take another look at this.

@zfan001
Copy link
Contributor Author

zfan001 commented Apr 23, 2021

@owena11 @dmentipl

Code modifications have been made according to your suggestions (commit 3986b5a ).
More changes will be made once Fiona pushes up more review comments.
Some update on acceptance test data will be updated as the master branch changed SHA256UMS.

BTW, I created a pull request for adding four jupyter notebooks in improver_aux.

Kind regards, Julian

Copy link
Contributor

@fionaRust fionaRust left a comment

Choose a reason for hiding this comment

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

Here are a few more comments and questions.

improver/regrid/bilinear.py Outdated Show resolved Hide resolved
improver/regrid/bilinear.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Show resolved Hide resolved
improver/regrid/idw.py Outdated Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/grid.py Show resolved Hide resolved
improver/regrid/idw.py Show resolved Hide resolved
improver/regrid/landsea2.py Show resolved Hide resolved
improver/regrid/landsea2.py Show resolved Hide resolved
@owena11
Copy link

owena11 commented May 10, 2021

Created #1457 to track the follow on changes we're discussing on this PR so they don't get lost in the merged and forgotten about pile! Please edit / reword / add to as useful.

@zfan001
Copy link
Contributor Author

zfan001 commented May 10, 2021

There is a tiny spacing difference due to rounding error in Australia domain grid. Changes have been made for considering it.
(1) improve "improver/utilities/spatial.py/calculate_grid_spacing() ", allowing tiny spacing difference (rtol=1.e-5)
(2) pre-calculate lat_spacing/lon_spacing.
(3) change check_if_input_grid_is_valid to calculate_input_grid_spacing, and the input grid checking kept inside.
(4) Minor change in unit test accordingly. No change for acceptance test

Kind regards, Julian

improver/regrid/grid.py Outdated Show resolved Hide resolved
Copy link
Contributor

@fionaRust fionaRust left a comment

Choose a reason for hiding this comment

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

I have about 4 more functions I want to have a final glace over but this is the bulk of my comments based on my final readthrough

improver_tests/acceptance/test_regrid.py Show resolved Hide resolved
improver_tests/acceptance/test_regrid.py Show resolved Hide resolved
improver/regrid/landsea.py Outdated Show resolved Hide resolved
improver/regrid/landsea2.py Show resolved Hide resolved
improver/regrid/grid.py Outdated Show resolved Hide resolved
improver/regrid/grid.py Show resolved Hide resolved
improver/regrid/grid.py Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/bilinear.py Show resolved Hide resolved
improver/regrid/idw.py Show resolved Hide resolved
@zfan001
Copy link
Contributor Author

zfan001 commented May 13, 2021

@fionaRust Thanks for the new issues you raised. I fixed most of them, and will do more changes soon. WGS84-related change will be considered in the follow-up PR.

it seems that set_up_variable_cube only generates square-grid-box cubes. In the unit test, we designed rectangle-grid-box, and therefore did not use this function. Anyway, it should not affect the unit test.

@zfan001
Copy link
Contributor Author

zfan001 commented May 13, 2021

@aled thank you for raising "Follow on Documentation for Land Sea Aware Regridding #1457" and adding additional notebooks. I will look into them after I finish the work on unit/acceptance tests.

@zfan001
Copy link
Contributor Author

zfan001 commented May 13, 2021

It is nice to see that both codecov/patch and codecov/project are improved. Only 4 lines are not run in the unit test. It is mainly due to the carefully designed unit test cases in which every scenario is turning up and every path is covered.

owena11
owena11 previously approved these changes May 13, 2021
Copy link

@owena11 owena11 left a comment

Choose a reason for hiding this comment

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

Final few comments to address from @fionaRust , but PR is looking in much better shape now, thanks for addressing the review comments Julian! It's dragged on longer than is pleasant for anyone, nice to be close to the end now 👍 No more comments from me, all covered by the follow on tickets etc.

Taken the AT data provided by Tom, unittest and acceptance tests seem to all run fine on my MO machine.

Copy link
Contributor

@fionaRust fionaRust left a comment

Choose a reason for hiding this comment

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

Thanks Julian. This is really close now, thank you very much for all your work on this and your effort to address all your comments. This PR is adding a lot of good functionality to IMPROVER.

Only a few comments left from my latest review. The only one that is essential to address is using the set up cube function in the unit tests.

@zfan001
Copy link
Contributor Author

zfan001 commented May 14, 2021

I fixed three issues today, and did not add the extra acceptance test. I would like to leave it to the follow-up PR to avoid the acceptance data transfer at this stage. All cube creation in the unit test uses "set_up_variable_cube" plus some work-around for rectangle grid. I did many tests, and it works well.

The small PR has been merged. Two changed files in that PR are integrated into this PR as well, and so there should be no problem when merging this PR. Please check what I did today. Hope that everything goes fine, and this PR can be merged soon. Thanks

@fionaRust fionaRust merged commit b16663e into metoppv:master May 14, 2021
@zfan001
Copy link
Contributor Author

zfan001 commented May 15, 2021

@Fiona @owena11 @dmentipl @tjtg Many thanks!

@zfan001 zfan001 deleted the newregrid3 branch May 15, 2021 10:25
MoseleyS added a commit to MoseleyS/improver that referenced this pull request May 18, 2021
* upstream/master:
  Move wiki to developer guide in docs (metoppv#1456)
  IMPRO-2026: Additional site identifier for spot-cubes and tweaks to existing metadata (metoppv#1458)
  Bilinear/nearest-neighbour  regridding with land-sea mask (metoppv#1438)
  Add tolerance in grid even-spacing checking & calculation (metoppv#1459)
  Removes unused import statements (metoppv#1461)
  Fix metadata interpreter acceptance tests skipping (metoppv#1455)

# Conflicts:
#	improver/standardise.py
MoseleyS pushed a commit to MoseleyS/improver that referenced this pull request Aug 22, 2024
* new regridder

* add contributor

* Update standardise.py

fix a indent mistake

* Cleanup, refactor and comment new regridding code

A number of FIXME comments have been left - these are items to address
before submitting an upstream pull request

* fix new bugs

* fix refactor bugs, add unit test

* fix sorting using isort

* fix codevec low-hit

* add acceptance test

* modifications based on reviewers comments

* update SHA256SUMS

* update SHA256SUMS

* consider identical source and target domain

* update SHA256SUMS for acceptance test

* update SHA256SUMS

* add unit test for better codecov

* update to improver_py37_iris24 env

* existing functions replaces new functions

* edit docstring

* minor change of docstring

* improve calculate_grid_spacing function

* minor fixing plus grid spacing test

* add test_grid.py

* add more unit test plus minor fixings

* refactor grid.py and unit test code

* update gap_spacing change

Co-authored-by: Tom Gale <tom.gale@bom.gov.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BoM review required PRs opened by non-BoM developers that require a BoM review MO review required PRs opened by non-Met Office developers that require a Met Office review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants