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

(Regridder unification) improve curvilinear regridding, generalise _create_cube #4807

Merged
merged 27 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1d36e4e
make _create_cube more generic
stephenworsley Jun 16, 2022
698d973
reinstate original _create_cube
stephenworsley Jun 16, 2022
c2c148e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 16, 2022
5fb32c2
fix tests
stephenworsley Jun 17, 2022
d2b396f
fix tests
stephenworsley Jun 17, 2022
e0bdf6c
fix curvilinear regridding
stephenworsley Sep 15, 2022
d3d2deb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2022
00e8cef
fix curvilinear regridding
stephenworsley Sep 15, 2022
8714ead
fix curvilinear regridding
stephenworsley Sep 16, 2022
adaa633
fix 1D regridding
stephenworsley Sep 29, 2022
40120d5
improve support for scalar lat-lon
stephenworsley Oct 5, 2022
ddb9d56
fix regridding for circular case
stephenworsley Oct 5, 2022
9b8ede8
fix scalar handling
stephenworsley Oct 5, 2022
5809145
fix mask handling
stephenworsley Oct 5, 2022
d258020
fix handling of higher dimensional cases
stephenworsley Oct 5, 2022
a049cea
include new derived coord/scalar coord support in tests
stephenworsley Oct 5, 2022
77d2fe2
remove old _create_cube method
stephenworsley Oct 5, 2022
b408049
update docstrings
stephenworsley Oct 5, 2022
5785ca5
fix regrid_conservative
stephenworsley Oct 6, 2022
e309788
make _create_cube more robust for extra dims
stephenworsley Oct 6, 2022
00462ff
fix and test for derived coords
stephenworsley Oct 26, 2022
e4916b6
add benchmark
stephenworsley Oct 27, 2022
6831c5b
address review comments
stephenworsley Nov 9, 2022
8386865
address review comments
stephenworsley Nov 10, 2022
f68115d
add whatsnew
stephenworsley Nov 10, 2022
f772787
Merge branch 'main' into regridding_attempt
lbdreyer Nov 11, 2022
a496c35
Merge branch 'main' into regridding_attempt
lbdreyer Nov 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion benchmarks/benchmarks/regridding.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
# importing anything else
from iris import tests # isort:skip

import numpy as np

import iris
from iris.analysis import AreaWeighted
from iris.analysis import AreaWeighted, PointInCell
from iris.coords import AuxCoord


class HorizontalChunkedRegridding:
Expand Down Expand Up @@ -53,3 +56,48 @@ def time_regrid_area_w_new_grid(self) -> None:
out = self.chunked_cube.regrid(self.template_cube, self.scheme_area_w)
# Realise data
out.data


class CurvilinearRegridding:
def setup(self) -> None:
# Prepare a cube and a template

cube_file_path = tests.get_data_path(
["NetCDF", "regrid", "regrid_xyt.nc"]
)
self.cube = iris.load_cube(cube_file_path)

# Make the source cube curvilinear
x_coord = self.cube.coord("longitude")
y_coord = self.cube.coord("latitude")
xx, yy = np.meshgrid(x_coord.points, y_coord.points)
self.cube.remove_coord(x_coord)
self.cube.remove_coord(y_coord)
x_coord_2d = AuxCoord(
xx,
standard_name=x_coord.standard_name,
units=x_coord.units,
coord_system=x_coord.coord_system,
)
y_coord_2d = AuxCoord(
yy,
standard_name=y_coord.standard_name,
units=y_coord.units,
coord_system=y_coord.coord_system,
)
self.cube.add_aux_coord(x_coord_2d, (1, 2))
self.cube.add_aux_coord(y_coord_2d, (1, 2))

template_file_path = tests.get_data_path(
["NetCDF", "regrid", "regrid_template_global_latlon.nc"]
)
self.template_cube = iris.load_cube(template_file_path)

# Prepare a regridding scheme
self.scheme_pic = PointInCell()

def time_regrid_pic(self) -> None:
# Regrid the cube onto the template.
out = self.cube.regrid(self.template_cube, self.scheme_pic)
# Realise the data
out.data
7 changes: 7 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ This document explains the changes made to Iris for this release
both dim and aux coords of the same type e.g. ``longitude`` and ``grid_longitude``.
(:issue:`3916`, :pull:`5029`).

#. `@stephenworsley`_ added the ability to regrid derived coordinates with the
:obj:`~iris.analysis.PointInCell` regridding scheme. (:pull:`4807`)


🐛 Bugs Fixed
=============
Expand Down Expand Up @@ -135,6 +138,10 @@ This document explains the changes made to Iris for this release
:meth:`iris.coords.Coord.intersect`.
(:pull:`4955`)

#. `@stephenworsley`_ improved the speed of the :obj:`~iris.analysis.PointInCell`
regridding scheme. (:pull:`4807`)


🔥 Deprecations
===============

Expand Down
36 changes: 25 additions & 11 deletions lib/iris/analysis/_area_weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from iris._lazy_data import map_complete_blocks
from iris.analysis._interpolation import get_xy_dim_coords, snapshot_grid
from iris.analysis._regrid import RectilinearRegridder
from iris.analysis._regrid import RectilinearRegridder, _create_cube
import iris.analysis.cartography
import iris.coord_systems
from iris.util import _meshgrid
Expand Down Expand Up @@ -1111,18 +1111,32 @@ def _regrid_area_weighted_rectilinear_src_and_grid__perform(
)

# Wrap up the data as a Cube.
regrid_callback = RectilinearRegridder._regrid
new_cube = RectilinearRegridder._create_cube(

_regrid_callback = functools.partial(
RectilinearRegridder._regrid,
src_x_coord=src_x,
src_y_coord=src_y,
sample_grid_x=meshgrid_x,
sample_grid_y=meshgrid_y,
)
# TODO: investigate if an area weighted callback would be more appropriate.
# _regrid_callback = functools.partial(
# _regrid_area_weighted_array,
# weights_info=weights_info,
# index_info=index_info,
# mdtol=mdtol,
# )

def regrid_callback(*args, **kwargs):
_data, dims = args
return _regrid_callback(_data, *dims, **kwargs)

new_cube = _create_cube(
new_data,
src_cube,
src_x_dim,
src_y_dim,
src_x,
src_y,
grid_x,
grid_y,
meshgrid_x,
meshgrid_y,
[src_x_dim, src_y_dim],
[grid_x, grid_y],
2,
regrid_callback,
)

Expand Down
Loading