Coverage for src/CSET/operators/_utils.py: 100%
13 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-01 10:31 +0000
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-01 10:31 +0000
1# Copyright 2022-2024 Met Office and contributors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""
16Common operator functionality used across CSET.
18Functions below should only be added if it is not suitable as a standalone
19operator, and will be used across multiple operators.
20"""
22import iris
23import iris.cube
26def get_cube_yxcoordname(cube: iris.cube.Cube) -> tuple[str, str]:
27 """
28 Return horizontal coordinate name(s) from a given cube.
30 Arguments
31 ---------
33 cube: iris.cube.Cube
34 An iris cube which will be checked to see if it contains coordinate
35 names that match a pre-defined list of acceptable horizontal
36 coordinate names.
38 Returns
39 -------
40 (y_coord, x_coord)
41 A tuple containing the horizontal coordinate name for latitude and longitude respectively
42 found within the cube.
44 Raises
45 ------
46 ValueError
47 If a unique y/x horizontal coordinate cannot be found.
48 """
49 # Acceptable horizontal coordinate names.
50 X_COORD_NAMES = ["longitude", "grid_longitude", "projection_x_coordinate", "x"]
51 Y_COORD_NAMES = ["latitude", "grid_latitude", "projection_y_coordinate", "y"]
53 # Get a list of coordinate names for the cube
54 coord_names = [coord.name() for coord in cube.coords()]
56 # Check which x-coordinate we have, if any
57 x_coords = [coord for coord in coord_names if coord in X_COORD_NAMES]
58 if len(x_coords) != 1:
59 raise ValueError("Could not identify a unique x-coordinate in cube")
61 # Check which y-coordinate we have, if any
62 y_coords = [coord for coord in coord_names if coord in Y_COORD_NAMES]
63 if len(y_coords) != 1:
64 raise ValueError("Could not identify a unique y-coordinate in cube")
66 return (y_coords[0], x_coords[0])