Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

update regrid to use utils #626

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Unreleased
.. Add your changes here, highlighting any user facing changes. E.g:
.. "* `@gh-user`_ did foo to bar in :pr:`9999`. This enables baz."

* `@jwarner8`_ use common operator to identify x/y coord names in regrid operator :pr:`626`
* `@jwarner8`_ added generic cube util for common functions so all operators can
use to reduce repetition in :pr:`620`
* `@jfrost-mo`_ added a code of conduct in :pr:`618`
Expand Down
43 changes: 5 additions & 38 deletions src/CSET/operators/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import iris.cube
import numpy as np

# Usual names for spatial coordinates.
# TODO can we determine grid coord names in a more intelligent way?
X_COORD_NAMES = ["longitude", "grid_longitude", "projection_x_coordinate", "x"]
Y_COORD_NAMES = ["latitude", "grid_latitude", "projection_y_coordinate", "y"]
from CSET.operators._utils import get_cube_xycoordname


def regrid_onto_cube(
Expand Down Expand Up @@ -56,25 +53,10 @@ def regrid_onto_cube(

Notes
-----
The acceptable coordinate names for X and Y coordinates are currently described
in X_COORD_NAMES and Y_COORD_NAMES. These cover commonly used coordinate types,
though a user can append new ones.
Currently rectlinear grids (uniform) are supported.
"""
# Get a list of coordinate names for the cube
coord_names = [coord.name() for coord in incube.coords()]

# Check which x-coordinate we have, if any
x_coords = [coord for coord in coord_names if coord in X_COORD_NAMES]
if len(x_coords) != 1:
raise ValueError("Could not identify a unique x-coordinate in cube")
x_coord = incube.coord(x_coords[0])

# Check which y-coordinate we have, if any
y_coords = [coord for coord in coord_names if coord in Y_COORD_NAMES]
if len(y_coords) != 1:
raise ValueError("Could not identify a unique y-coordinate in cube")
y_coord = incube.coord(y_coords[0])
# Get x,y coord names
x_coord, y_coord = get_cube_xycoordname(incube)

# List of supported grids - check if it is compatible
supported_grids = (iris.coord_systems.GeogCS,)
Expand Down Expand Up @@ -127,26 +109,11 @@ def regrid_onto_xyspacing(

Notes
-----
The acceptable coordinate names for X and Y coordinates are currently described
in X_COORD_NAMES and Y_COORD_NAMES. These cover commonly used coordinate types,
though a user can append new ones.
Currently rectlinear grids (uniform) are supported.

"""
# Get a list of coordinate names for the cube
coord_names = [coord.name() for coord in incube.coords()]

# Check which x-coordinate we have, if any
x_coords = [coord for coord in coord_names if coord in X_COORD_NAMES]
if len(x_coords) != 1:
raise ValueError("Could not identify a unique x-coordinate in cube")
x_coord = incube.coord(x_coords[0])

# Check which y-coordinate we have, if any
y_coords = [coord for coord in coord_names if coord in Y_COORD_NAMES]
if len(y_coords) != 1:
raise ValueError("Could not identify a unique y-coordinate in cube")
y_coord = incube.coord(y_coords[0])
# Get x,y coord names
x_coord, y_coord = get_cube_xycoordname(incube)

# List of supported grids - check if it is compatible
supported_grids = (iris.coord_systems.GeogCS,)
Expand Down
34 changes: 0 additions & 34 deletions tests/operators/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@ def test_regrid_onto_cube(regrid_source_cube, regrid_test_cube):
)


def test_regrid_onto_cube_missing_coord(regrid_source_cube, regrid_test_cube):
"""Missing coordinate raises error."""
# Missing X coordinate.
source = regrid_source_cube.copy()
source.remove_coord("longitude")
with pytest.raises(ValueError):
regrid.regrid_onto_cube(source, regrid_test_cube, method="Linear")

# Missing Y coordinate.
source = regrid_source_cube.copy()
source.remove_coord("latitude")
with pytest.raises(ValueError):
regrid.regrid_onto_cube(source, regrid_test_cube, method="Linear")


def test_regrid_onto_cube_unknown_crs(regrid_source_cube, regrid_test_cube):
"""Coordinate reference system is unrecognised."""
# Exchange X to unsupported coordinate system.
Expand Down Expand Up @@ -108,25 +93,6 @@ def test_regrid_onto_xyspacing(regrid_source_cube, regrid_test_cube):
)


def test_regrid_onto_xyspacing_missing_coord(regrid_source_cube):
"""Missing coordinate raises error."""
# Missing X coordinate.
source = regrid_source_cube.copy()
source.remove_coord("longitude")
with pytest.raises(ValueError):
regrid.regrid_onto_xyspacing(
source, xspacing=0.5, yspacing=0.5, method="Linear"
)

# Missing Y coordinate.
source = regrid_source_cube.copy()
source.remove_coord("latitude")
with pytest.raises(ValueError):
regrid.regrid_onto_xyspacing(
source, xspacing=0.5, yspacing=0.5, method="Linear"
)


def test_regrid_onto_xyspacing_unknown_crs(regrid_source_cube):
"""Coordinate reference system is unrecognised."""
# Exchange X to unsupported coordinate system.
Expand Down