Skip to content

Commit

Permalink
regridding regular grids with similar coordinates (#1567)
Browse files Browse the repository at this point in the history
* replace regular lat/lon coords of source cube with target ones when points are close enough

* add test for regular grid comparison (#1551)

* test regular grid coordinates are the same using iris Resolve

* fix bug in previous commit for regrid test
  • Loading branch information
tomaslovato authored Jun 3, 2022
1 parent fd5488e commit ead491a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions esmvalcore/preprocessor/_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,11 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True):
"'%s': %s", original_dtype, cube.core_data().dtype,
str(exc))
cube.data = da.ma.masked_equal(cube.core_data(), fill_value)
else:
# force target coordinates
for coord in ['latitude', 'longitude']:
cube.coord(coord).points = target_grid.coord(coord).points
cube.coord(coord).bounds = target_grid.coord(coord).bounds

return cube

Expand Down
17 changes: 17 additions & 0 deletions tests/integration/preprocessor/_regrid/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ def test_regrid__linear(self):
expected = np.array([[[1.5]], [[5.5]], [[9.5]]])
self.assert_array_equal(result.data, expected)

def test_regrid__regular_coordinates(self):
data = np.ones((1, 1))
lons = iris.coords.DimCoord([1.50000000000001],
standard_name='longitude',
bounds=[[1, 2]],
units='degrees_east',
coord_system=self.cs)
lats = iris.coords.DimCoord([1.50000000000001],
standard_name='latitude',
bounds=[[1, 2]],
units='degrees_north',
coord_system=self.cs)
coords_spec = [(lats, 0), (lons, 1)]
regular_grid = iris.cube.Cube(data, dim_coords_and_dims=coords_spec)
result = regrid(regular_grid, self.grid_for_linear, 'linear')
iris.common.resolve.Resolve(result, self.grid_for_linear)

def test_regrid__linear_do_not_preserve_dtype(self):
self.cube.data = self.cube.data.astype(int)
result = regrid(self.cube, self.grid_for_linear, 'linear')
Expand Down

0 comments on commit ead491a

Please sign in to comment.