Skip to content

Commit

Permalink
(Regridder unification) improve curvilinear regridding, generalise _c…
Browse files Browse the repository at this point in the history
…reate_cube (#4807)

* make _create_cube more generic

* reinstate original _create_cube

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix tests

* fix tests

* fix curvilinear regridding

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix curvilinear regridding

* fix curvilinear regridding

* fix 1D regridding

* improve support for scalar lat-lon

* fix regridding for circular case

* fix scalar handling

* fix mask handling

* fix handling of higher dimensional cases

* include new derived coord/scalar coord support in tests

* remove old _create_cube method

* update docstrings

* fix regrid_conservative

* make _create_cube more robust for extra dims

* fix and test for derived coords

* add benchmark

* address review comments

* address review comments

* add whatsnew

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: lbdreyer <[email protected]>
  • Loading branch information
3 people committed Nov 11, 2022
1 parent 3da5341 commit f815437
Show file tree
Hide file tree
Showing 8 changed files with 589 additions and 271 deletions.
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

0 comments on commit f815437

Please sign in to comment.