Debian Codegraph @

geotiepoints/viiinterpolator.py

raw
1 """Interpolation of geographical tiepoints for the VII products.
2
3 It follows the description provided in document "EPS-SG VII Level 1B Product Format Specification".
4 Tiepoints are typically subsampled by a factor 8 with respect to the pixels, along and across the satellite track.
5 Because of the bowtie effect, tiepoints at the scan edges are not continuous between neighbouring scans,
6 therefore each scan has its own edge tiepoints in the along-track direction.
7 However, for computational efficiency, the edge tie points that lie outside the original data point raster
8 are excluded from the interpolation grid which is then carried out per granule, rather than per scan
9 at a (very) small geolocation accuracy cost at the swath edge (investigation to quantify this ongoing).
10 The interpolation functions are implemented for xarray.DataArrays as input.
11 This version works with vii test data V2 to be released Jan 2022 which has the data stored
12 in alt, act (row,col) format instead of act,alt (col,row)
13 """
14
15 import xarray as xr
16 import dask.array as da
17 import numpy as np
18
19 # MEAN EARTH RADIUS AS DEFINED BY IUGG
20 MEAN_EARTH_RADIUS = 6371008.7714 # [m]
21
22
23 def tie_points_interpolation(data_on_tie_points, scan_alt_tie_points, tie_points_factor):
24 """Interpolate the data from the tie points to the pixel points.
25 The data are provided as a list of xarray DataArray objects, allowing to interpolate on several arrays
26 at the same time; however the individual arrays must have exactly the same dimensions.
27 Args:
28 data_on_tie_points: list of xarray DataArray objects containing the values defined on the tie points.
29 scan_alt_tie_points: number of tie points along the satellite track for each scan
30 tie_points_factor: sub-sampling factor of tie points wrt pixel points
31 Returns:
32 list of xarray DataArray objects containing the interpolated values on the pixel points.
33 """
34 # Extract the dimensions of the tie points array across and along track
35 n_tie_alt, n_tie_act = data_on_tie_points[0].shape
36 dim_alt, dim_act = data_on_tie_points[0].dims
37
38 # Check that the number of tie points along track is multiple of the number of tie points per scan
39 if n_tie_alt % scan_alt_tie_points != 0:
40 raise ValueError("The number of tie points in the along-route dimension must be a multiple of %d",
41 scan_alt_tie_points)
42
43 # Compute the number of scans
44 n_scans = n_tie_alt // scan_alt_tie_points
45
46 # Compute the dimensions of the pixel points array across and along track
47 n_pixel_act = (n_tie_act - 1) * tie_points_factor
48 n_pixel_alt = (n_tie_alt - 1) * tie_points_factor
49
50 # Create the grids used for interpolation across the track
51 tie_grid_act = np.arange(0, n_pixel_act + 1, tie_points_factor)
52 pixel_grid_act = np.arange(0, n_pixel_act)
53
54 # Create the grids used for the interpolation along the track (must not include the spurious points between scans)
55 tie_grid_alt = np.arange(0, n_pixel_alt + 1, tie_points_factor)
56 n_pixel_alt_per_scan = (scan_alt_tie_points - 1) * tie_points_factor
57 pixel_grid_alt = []
58
59 for j_scan in range(n_scans):
60 start_index_scan = j_scan * scan_alt_tie_points * tie_points_factor
61 pixel_grid_alt.append(np.arange(start_index_scan, start_index_scan + n_pixel_alt_per_scan))
62 pixel_grid_alt = np.concatenate(pixel_grid_alt)
63
64 # Loop on all arrays
65 data_on_pixel_points = []
66 for data in data_on_tie_points:
67
68 if data.shape != (n_tie_alt, n_tie_act) or data.dims != (dim_alt, dim_act):
69 raise ValueError("The dimensions of the arrays are not consistent")
70
71 # Interpolate using the xarray interp function twice: first across, then along the scan
72 # (much faster than interpolating directly in the two dimensions)
73 data = data.assign_coords({dim_alt: tie_grid_alt, dim_act: tie_grid_act})
74 data_pixel = data.interp({dim_alt: pixel_grid_alt}, assume_sorted=True) \
75 .interp({dim_act: pixel_grid_act}, assume_sorted=True).drop_vars([dim_alt, dim_act])
76
77 data_on_pixel_points.append(data_pixel)
78
79 return data_on_pixel_points
80
81
82
83 def tie_points_geo_interpolation(longitude, latitude,
84 scan_alt_tie_points, tie_points_factor,
85 lat_threshold_use_cartesian=60.,
86 z_threshold_use_xy=0.8):
87 """Interpolate the geographical position from the tie points to the pixel points.
88
89 The longitude and latitude values are provided as xarray DataArray objects.
90
91 Args:
92 data_on_tie_points: list of xarray DataArray objects containing the values defined on the tie points.
93 scan_alt_tie_points: number of tie points along the satellite track for each scan
94 tie_points_factor: sub-sampling factor of tie points wrt pixel points
95
96 Returns:
97 list of xarray DataArray objects containing the interpolated values on the pixel points.
98
99 Args:
100 longitude: xarray DataArray containing the longitude values defined on the tie points (degrees).
101 latitude: xarray DataArray containing the latitude values defined on the tie points (degrees).
102 scan_alt_tie_points: number of tie points along the satellite track for each scan.
103 tie_points_factor: sub-sampling factor of tie points wrt pixel points.
104 lat_threshold_use_cartesian: latitude threshold to use cartesian coordinates.
105 z_threshold_use_xy: z threshold to compute latitude from x and y in cartesian coordinates.
106
107 Returns:
108 two xarray DataArray objects containing the interpolated longitude and latitude values on the pixel points.
109
110 """
111 # Check that the two arrays have the same dimensions
112 if longitude.shape != latitude.shape:
113 raise ValueError("The dimensions of longitude and latitude don't match")
114
115 # Determine if the interpolation should be done in cartesian or geodetic coordinates
116 to_cart = np.max(np.fabs(latitude)) > lat_threshold_use_cartesian or (np.max(longitude) - np.min(longitude)) > 180.
117
118 if to_cart:
119
120 x, y, z = _lonlat2xyz(longitude, latitude)
121
122 interp_x, interp_y, interp_z = tie_points_interpolation([x, y, z],
123 scan_alt_tie_points,
124 tie_points_factor)
125
126 interp_longitude, interp_latitude = _xyz2lonlat(interp_x, interp_y, interp_z, z_threshold_use_xy)
127
128 else:
129
130 interp_longitude, interp_latitude = tie_points_interpolation([longitude, latitude],
131 scan_alt_tie_points,
132 tie_points_factor)
133
134 return interp_longitude, interp_latitude
135
136
137 def _lonlat2xyz(lons, lats):
138 """Convert longitudes and latitudes to cartesian coordinates.
139
140 Args:
141 lons: array containing the longitude values in degrees.
142 lats: array containing the latitude values in degrees.
143
144 Returns:
145 tuple of arrays containing the x, y, and z values in meters.
146
147 """
148 lons_rad = np.deg2rad(lons)
149 lats_rad = np.deg2rad(lats)
150 x_coords = MEAN_EARTH_RADIUS * np.cos(lats_rad) * np.cos(lons_rad)
151 y_coords = MEAN_EARTH_RADIUS * np.cos(lats_rad) * np.sin(lons_rad)
152 z_coords = MEAN_EARTH_RADIUS * np.sin(lats_rad)
153 return x_coords, y_coords, z_coords
154
155
156 def _xyz2lonlat(x_coords, y_coords, z_coords, z_threshold_use_xy=0.8):
157 """Get longitudes and latitudes from cartesian coordinates.
158
159 Args:
160 x_coords: array containing the x values in meters.
161 y_coords: array containing the y values in meters.
162 z_coords: array containing the z values in meters.
163 z_threshold_use_xy: z threshold to compute latitude from x and y in cartesian coordinates.
164
165 Returns:
166 tuple of arrays containing the longitude and latitude values in degrees.
167
168 """
169 r = np.sqrt(x_coords ** 2 + y_coords ** 2)
170 thr_z = z_threshold_use_xy * MEAN_EARTH_RADIUS
171 lons = np.rad2deg(np.arccos(x_coords / r)) * np.sign(y_coords)
172 # Compute latitude from z at low z and from x and y at high z
173 lats = xr.where(
174 np.logical_and(np.less(z_coords, thr_z), np.greater(z_coords, -thr_z)),
175 90. - np.rad2deg(np.arccos(z_coords / MEAN_EARTH_RADIUS)),
176 np.sign(z_coords) * (90. - np.rad2deg(np.arcsin(r / MEAN_EARTH_RADIUS)))
177 )
178 return lons, lats