Skip to content

Commit 8f9bdaa

Browse files
committed
allow extract point to support other grids #2177
1 parent 4fd8701 commit 8f9bdaa

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

esmvalcore/preprocessor/_regrid.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from geopy.geocoders import Nominatim
1919
from iris.analysis import AreaWeighted, Linear, Nearest, UnstructuredNearest
2020
from iris.util import broadcast_to_shape
21+
import cartopy.crs as ccrs
2122

2223
from ..cmor._fixes.shared import add_altitude_from_plev, add_plev_from_altitude
2324
from ..cmor.table import CMOR_TABLES
@@ -460,9 +461,55 @@ def extract_point(cube, latitude, longitude, scheme):
460461
raise ValueError(msg)
461462

462463
point = [('latitude', latitude), ('longitude', longitude)]
463-
cube = cube.interpolate(point, scheme=scheme)
464+
cube = get_pt(cube, point, scheme)
464465
return cube
465466

467+
def get_pt(cube, point, scheme):
468+
"""
469+
Extract data at a single point from cubes with any coordinate
470+
system or none (if lat-lon only)
471+
472+
Parameters
473+
----------
474+
cube : cube
475+
The source cube to extract a point from.
476+
477+
latitude, longitude : float, or array of float
478+
The latitude and longitude of the point.
479+
480+
scheme : str
481+
The interpolation scheme. 'linear' or 'nearest'. No default.
482+
483+
Returns
484+
-------
485+
:py:class:`~iris.cube.Cube`
486+
Returns a cube with the extracted point(s)
487+
488+
Raises
489+
------
490+
ValueError
491+
if cube doesn't have a coordinate system and isn't on a lat-lon grid
492+
"""
493+
x_coord = cube.coord(axis='X', dim_coords=True)
494+
y_coord = cube.coord(axis='Y', dim_coords=True)
495+
496+
# some lon-lat cubes don't have a coordinate system
497+
# check if it is lon-lat and if so just use interpolate
498+
# as it is
499+
if cube.coord_system() is None:
500+
# if coords are equal to lat and lon then
501+
# assume we have a lat-lon grid
502+
503+
if x_coord.name() == 'longitude' and y_coord.name() == 'latitude':
504+
return cube.interpolate(point, scheme=scheme)
505+
else:
506+
raise ValueError('Can only interpolate lat-lon grids if no coordinate system on cube')
507+
else:
508+
# convert the target point(s) to lat lon and do the interpolation
509+
ll = ccrs.Geodetic()
510+
trpoints = cube.coord_system().as_cartopy_crs().transform_point(point[1][1], point[0][1], ll)
511+
trpoints = [(y_coord.name(), trpoints[1]),(x_coord.name(), trpoints[0])]
512+
return cube.interpolate(trpoints, scheme=scheme)
466513

467514
def is_dataset(dataset):
468515
"""Test if something is an `esmvalcore.dataset.Dataset`."""

0 commit comments

Comments
 (0)