|
18 | 18 | from geopy.geocoders import Nominatim
|
19 | 19 | from iris.analysis import AreaWeighted, Linear, Nearest, UnstructuredNearest
|
20 | 20 | from iris.util import broadcast_to_shape
|
| 21 | +import cartopy.crs as ccrs |
21 | 22 |
|
22 | 23 | from ..cmor._fixes.shared import add_altitude_from_plev, add_plev_from_altitude
|
23 | 24 | from ..cmor.table import CMOR_TABLES
|
@@ -460,9 +461,55 @@ def extract_point(cube, latitude, longitude, scheme):
|
460 | 461 | raise ValueError(msg)
|
461 | 462 |
|
462 | 463 | point = [('latitude', latitude), ('longitude', longitude)]
|
463 |
| - cube = cube.interpolate(point, scheme=scheme) |
| 464 | + cube = get_pt(cube, point, scheme) |
464 | 465 | return cube
|
465 | 466 |
|
| 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) |
466 | 513 |
|
467 | 514 | def is_dataset(dataset):
|
468 | 515 | """Test if something is an `esmvalcore.dataset.Dataset`."""
|
|
0 commit comments