| 1 |
"""Geographical interpolation (lon/lats).""" |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
from geotiepoints.interpolator import Interpolator, MultipleGridInterpolator, MultipleSplineInterpolator |
| 5 |
|
| 6 |
|
| 7 |
EARTH_RADIUS = 6370997.0 |
| 8 |
|
| 9 |
|
| 10 |
class GeoInterpolator(Interpolator): |
| 11 |
"""Handles interpolation of geolocation from a grid of tie points. |
| 12 |
|
| 13 |
It is |
| 14 |
preferable to have tie-points out till the edges if the tiepoint grid, but |
| 15 |
a method is provided to extrapolate linearly the tiepoints to the borders |
| 16 |
of the grid. The extrapolation is done automatically if it seems necessary. |
| 17 |
|
| 18 |
Uses numpy, scipy, and optionally pyresample. |
| 19 |
|
| 20 |
The constructor takes in the tiepointed data as *data*, the |
| 21 |
*tiepoint_grid* and the desired *final_grid*. As optional arguments, one |
| 22 |
can provide *kx_* and *ky_* as interpolation orders (in x and y directions |
| 23 |
respectively), and the *chunksize* if the data has to be handled by pieces |
| 24 |
along the y axis (this affects how the extrapolator behaves). If |
| 25 |
*chunksize* is set, don't forget to adjust the interpolation orders |
| 26 |
accordingly: the interpolation is indeed done globaly (not chunkwise). |
| 27 |
|
| 28 |
""" |
| 29 |
|
| 30 |
def __init__(self, lon_lat_data, *args, **kwargs): |
| 31 |
try: |
| 32 |
# Maybe it's a pyresample object ? |
| 33 |
self.lon_tiepoint = lon_lat_data.lons |
| 34 |
self.lat_tiepoint = lon_lat_data.lats |
| 35 |
xyz = lon_lat_data.get_cartesian_coords() |
| 36 |
tie_data = [xyz[:, :, 0], xyz[:, :, 1], xyz[:, :, 2]] |
| 37 |
except AttributeError: |
| 38 |
self.lon_tiepoint = lon_lat_data[0] |
| 39 |
self.lat_tiepoint = lon_lat_data[1] |
| 40 |
x__, y__, z__ = lonlat2xyz(self.lon_tiepoint, self.lat_tiepoint) |
| 41 |
tie_data = [x__, y__, z__] |
| 42 |
|
| 43 |
super().__init__(tie_data, *args, **kwargs) |
| 44 |
|
| 45 |
def interpolate(self): |
| 46 |
"""Run the interpolation.""" |
| 47 |
newx, newy, newz = super().interpolate() |
| 48 |
lon, lat = xyz2lonlat(newx, newy, newz) |
| 49 |
return lon, lat |
| 50 |
|
| 51 |
|
| 52 |
def lonlat2xyz(lons, lats, radius=EARTH_RADIUS): |
| 53 |
"""Convert lons and lats to cartesian coordinates.""" |
| 54 |
lons_rad = np.deg2rad(lons) |
| 55 |
lats_rad = np.deg2rad(lats) |
| 56 |
x_coords = radius * np.cos(lats_rad) * np.cos(lons_rad) |
| 57 |
y_coords = radius * np.cos(lats_rad) * np.sin(lons_rad) |
| 58 |
z_coords = radius * np.sin(lats_rad) |
| 59 |
return x_coords, y_coords, z_coords |
| 60 |
|
| 61 |
|
| 62 |
def xyz2lonlat(x__, y__, z__, radius=EARTH_RADIUS, thr=0.8, low_lat_z=True): |
| 63 |
"""Get longitudes from cartesian coordinates.""" |
| 64 |
lons = np.rad2deg(np.arccos(x__ / np.sqrt(x__ ** 2 + y__ ** 2))) * np.sign(y__) |
| 65 |
lats = np.sign(z__) * (90 - np.rad2deg(np.arcsin(np.sqrt(x__ ** 2 + y__ ** 2) / radius))) |
| 66 |
if low_lat_z: |
| 67 |
# if we are at low latitudes - small z, then get the |
| 68 |
# latitudes only from z. If we are at high latitudes (close to the poles) |
| 69 |
# then derive the latitude using x and y: |
| 70 |
normalized_z = z__ / radius |
| 71 |
lat_mask_cond = abs(normalized_z) < thr |
| 72 |
lat_z_only = 90 - np.rad2deg(np.arccos(normalized_z)) |
| 73 |
lats = np.where(lat_mask_cond, lat_z_only, lats) |
| 74 |
|
| 75 |
return lons, lats |
| 76 |
|
| 77 |
|
| 78 |
def _work_with_lonlats(klass): |
| 79 |
"""Adapt MultipleInterpolator classes to work with geographical coordinates.""" |
| 80 |
|
| 81 |
class GeoKlass(klass): |
| 82 |
|
| 83 |
def __init__(self, tie_points, *data, **interpolator_init_kwargs): |
| 84 |
"""Set up the interpolator.""" |
| 85 |
data = to_xyz(data) |
| 86 |
super().__init__(tie_points, *data, **interpolator_init_kwargs) |
| 87 |
|
| 88 |
def interpolate(self, fine_points, **interpolator_call_kwargs): |
| 89 |
"""Interpolate to *fine_points*.""" |
| 90 |
x, y, z = super().interpolate(fine_points, **interpolator_call_kwargs) |
| 91 |
return xyz2lonlat(x, y, z) |
| 92 |
|
| 93 |
return GeoKlass |
| 94 |
|
| 95 |
|
| 96 |
def to_xyz(data): |
| 97 |
"""Convert data to cartesian. |
| 98 |
|
| 99 |
Data can be a class with a `get_cartesian_coords` method, or a tuple of (lon, lat) arrays. |
| 100 |
""" |
| 101 |
if len(data) == 1: |
| 102 |
xyz = data[0].get_cartesian_coords() |
| 103 |
data = [xyz[:, :, 0], xyz[:, :, 1], xyz[:, :, 2]] |
| 104 |
elif len(data) == 2: |
| 105 |
data = lonlat2xyz(*data) |
| 106 |
else: |
| 107 |
raise ValueError("Either pass lon/lats or a pyresample definition.") |
| 108 |
return data |
| 109 |
|
| 110 |
|
| 111 |
GeoGridInterpolator = _work_with_lonlats(MultipleGridInterpolator) |
| 112 |
GeoSplineInterpolator = _work_with_lonlats(MultipleSplineInterpolator) |