Debian Codegraph @

geotiepoints/_simple_modis_interpolator.pyx

raw
1 # cython: language_level=3, boundscheck=False, cdivision=True, wraparound=False, initializedcheck=False, nonecheck=False
2 cimport cython
3
4 from ._modis_utils cimport floating
5 from ._modis_utils cimport lonlat2xyz, xyz2lonlat
6 from ._modis_utils import rows_per_scan_for_resolution
7 cimport numpy as np
8 import numpy as np
9 from scipy.ndimage import map_coordinates
10
11 np.import_array()
12
13 def interpolate_geolocation_cartesian(
14 np.ndarray[floating, ndim=2] lon_array,
15 np.ndarray[floating, ndim=2] lat_array,
16 unsigned int coarse_resolution,
17 unsigned int fine_resolution):
18 lon_array = np.ascontiguousarray(lon_array)
19 lat_array = np.ascontiguousarray(lat_array)
20 cdef unsigned int rows_per_scan = rows_per_scan_for_resolution(coarse_resolution)
21 cdef unsigned int res_factor = coarse_resolution // fine_resolution
22 cdef Py_ssize_t num_rows = lon_array.shape[0]
23 cdef Py_ssize_t num_cols = lon_array.shape[1]
24 cdef unsigned int num_scans = num_rows // rows_per_scan
25
26 # SciPy's map_coordinates requires the x/y dimension to be first
27 cdef np.ndarray[floating, ndim=3] coordinates = np.empty(
28 (2, res_factor * rows_per_scan, res_factor * num_cols), dtype=lon_array.dtype)
29 cdef floating[:, :, ::1] coordinates_view = coordinates
30 _compute_yx_coordinate_arrays(res_factor, coordinates_view)
31
32 cdef np.ndarray[floating, ndim=3] xyz_result = np.empty(
33 (res_factor * rows_per_scan, num_cols * res_factor, 3), dtype=lon_array.dtype)
34 cdef floating[:, :, ::1] xyz_result_view = xyz_result
35 cdef np.ndarray[floating, ndim=3] xyz_in = np.empty(
36 (rows_per_scan, num_cols, 3), dtype=lon_array.dtype)
37 cdef floating[:, :, ::1] xyz_in_view = xyz_in
38 cdef floating[:, ::1] lon_in_view = lon_array
39 cdef floating[:, ::1] lat_in_view = lat_array
40
41 cdef np.ndarray[floating, ndim=2] new_lons = np.empty((res_factor * num_rows, res_factor * num_cols),
42 dtype=lon_array.dtype)
43 cdef np.ndarray[floating, ndim=2] new_lats = np.empty((res_factor * num_rows, res_factor * num_cols),
44 dtype=lon_array.dtype)
45 cdef floating[:, ::1] new_lons_view = new_lons
46 cdef floating[:, ::1] new_lats_view = new_lats
47
48 # Interpolate each scan, one at a time, otherwise the math doesn't work well
49 cdef Py_ssize_t scan_idx, j0, j1, k0, k1, comp_index
50 with nogil:
51 for scan_idx in range(num_scans):
52 # Calculate indexes
53 j0 = rows_per_scan * scan_idx
54 j1 = j0 + rows_per_scan
55 k0 = rows_per_scan * res_factor * scan_idx
56 k1 = k0 + rows_per_scan * res_factor
57 lonlat2xyz(lon_in_view[j0:j1, :], lat_in_view[j0:j1, :], xyz_in_view)
58
59 _compute_interpolated_xyz_scan(
60 res_factor, coordinates_view, xyz_in_view,
61 xyz_result_view)
62
63 xyz2lonlat(xyz_result_view, new_lons_view[k0:k1], new_lats_view[k0:k1], low_lat_z=True)
64 return new_lons, new_lats
65
66
67 @cython.boundscheck(False)
68 @cython.cdivision(True)
69 @cython.wraparound(False)
70 @cython.initializedcheck(False)
71 cdef void _compute_yx_coordinate_arrays(
72 unsigned int res_factor,
73 floating[:, :, ::1] coordinates,
74 ) noexcept nogil:
75 cdef Py_ssize_t i, j
76 for j in range(coordinates.shape[1]):
77 for i in range(coordinates.shape[2]):
78 # y coordinate - 0.375 for 250m, 0.25 for 500m
79 coordinates[0, j, i] = j * (1.0 / res_factor) - (res_factor * (1.0 / 16) + (1.0 / 8))
80 # x coordinate
81 coordinates[1, j, i] = i * (1.0 / res_factor)
82
83
84 @cython.boundscheck(False)
85 cdef void _compute_interpolated_xyz_scan(
86 unsigned int res_factor,
87 floating[:, :, ::1] coordinates_view,
88 floating[:, :, ::1] xyz_input_view,
89 floating[:, :, ::1] xyz_result_view,
90 ) noexcept nogil:
91 cdef Py_ssize_t comp_index
92 cdef floating[:, :] input_view, result_view
93 with gil:
94 for comp_index in range(3):
95 input_view = xyz_input_view[:, :, comp_index]
96 result_view = xyz_result_view[:, :, comp_index]
97 _call_map_coordinates(
98 input_view,
99 coordinates_view,
100 result_view,
101 )
102
103 if res_factor == 4:
104 for comp_index in range(3):
105 result_view = xyz_result_view[:, :, comp_index]
106 _extrapolate_xyz_rightmost_columns(result_view, 3)
107 _interpolate_xyz_250(
108 result_view,
109 coordinates_view,
110 )
111 else:
112 for comp_index in range(3):
113 result_view = xyz_result_view[:, :, comp_index]
114 _extrapolate_xyz_rightmost_columns(result_view, 1)
115 _interpolate_xyz_500(
116 result_view,
117 coordinates_view,
118 )
119
120
121 cdef void _call_map_coordinates(
122 floating[:, :] nav_array_view,
123 floating[:, :, ::1] coordinates_view,
124 floating[:, :] result_view,
125 ):
126 cdef np.ndarray[floating, ndim=2] nav_array = np.asarray(nav_array_view)
127 cdef np.ndarray[floating, ndim=3] coordinates_array = np.asarray(coordinates_view)
128 cdef np.ndarray[floating, ndim=2] result_array = np.asarray(result_view)
129 # Use bilinear interpolation for all 250 meter pixels
130 map_coordinates(nav_array, coordinates_array,
131 output=result_array,
132 order=1, mode='nearest')
133
134
135 @cython.boundscheck(False)
136 @cython.cdivision(True)
137 @cython.wraparound(False)
138 @cython.initializedcheck(False)
139 cdef void _extrapolate_xyz_rightmost_columns(
140 floating[:, :] result_view,
141 int num_columns,
142 ) noexcept nogil:
143 cdef Py_ssize_t row_idx, col_offset
144 cdef floating last_interp_col_diff
145 for row_idx in range(result_view.shape[0]):
146 last_interp_col_diff = result_view[row_idx, result_view.shape[1] - num_columns - 1] - \
147 result_view[row_idx, result_view.shape[1] - num_columns - 2]
148 for col_offset in range(num_columns):
149 # map_coordinates repeated the last columns value, we now add more to it as an "extrapolation"
150 result_view[row_idx, result_view.shape[1] - num_columns + col_offset] += last_interp_col_diff * (col_offset + 1)
151
152
153 @cython.boundscheck(False)
154 @cython.cdivision(True)
155 @cython.wraparound(False)
156 @cython.initializedcheck(False)
157 cdef void _interpolate_xyz_250(
158 floating[:, :] result_view,
159 floating[:, :, ::1] coordinates_view,
160 ) noexcept nogil:
161 cdef Py_ssize_t col_idx
162 cdef floating m, b
163 cdef floating[:] result_col_view
164 cdef floating[:, ::1] y_coordinates = coordinates_view[0]
165 for col_idx in range(result_view.shape[1]):
166 result_col_view = result_view[:, col_idx]
167 # Use linear extrapolation for the first two 250 meter pixels along track
168 m = _calc_slope_250(result_col_view,
169 y_coordinates,
170 2)
171 b = _calc_offset_250(result_col_view,
172 y_coordinates,
173 m,
174 2)
175 result_view[0, col_idx] = m * y_coordinates[0, 0] + b
176 result_view[1, col_idx] = m * y_coordinates[1, 0] + b
177
178 # Use linear extrapolation for the last two 250 meter pixels along track
179 # m = (result_array[k0 + 37, :] - result_array[k0 + 34, :]) / (y[37, 0] - y[34, 0])
180 # b = result_array[k0 + 37, :] - m * y[37, 0]
181 m = _calc_slope_250(result_col_view,
182 y_coordinates,
183 34)
184 b = _calc_offset_250(result_col_view,
185 y_coordinates,
186 m,
187 34)
188 result_view[38, col_idx] = m * y_coordinates[38, 0] + b
189 result_view[39, col_idx] = m * y_coordinates[39, 0] + b
190
191
192 @cython.boundscheck(False)
193 @cython.cdivision(True)
194 @cython.wraparound(False)
195 @cython.initializedcheck(False)
196 cdef void _interpolate_xyz_500(
197 floating[:, :] result_view,
198 floating[:, :, ::1] coordinates_view,
199 ) noexcept nogil:
200 cdef Py_ssize_t col_idx
201 cdef floating m, b
202 for col_idx in range(result_view.shape[1]):
203 # Use linear extrapolation for the first two 250 meter pixels along track
204 m = _calc_slope_500(
205 result_view[:, col_idx],
206 coordinates_view[0],
207 1)
208 b = _calc_offset_500(
209 result_view[:, col_idx],
210 coordinates_view[0],
211 m,
212 1)
213 result_view[0, col_idx] = m * coordinates_view[0, 0, 0] + b
214
215 # Use linear extrapolation for the last two 250 meter pixels along track
216 m = _calc_slope_500(
217 result_view[:, col_idx],
218 coordinates_view[0],
219 17)
220 b = _calc_offset_500(
221 result_view[:, col_idx],
222 coordinates_view[0],
223 m,
224 17)
225 result_view[19, col_idx] = m * coordinates_view[0, 19, 0] + b
226
227
228 @cython.boundscheck(False)
229 @cython.cdivision(True)
230 @cython.wraparound(False)
231 @cython.initializedcheck(False)
232 cdef inline floating _calc_slope_250(
233 floating[:] result_view,
234 floating[:, ::1] y,
235 Py_ssize_t offset,
236 ) noexcept nogil:
237 return (result_view[offset + 3] - result_view[offset]) / \
238 (y[offset + 3, 0] - y[offset, 0])
239
240
241 @cython.boundscheck(False)
242 @cython.cdivision(True)
243 @cython.wraparound(False)
244 @cython.initializedcheck(False)
245 cdef inline floating _calc_offset_250(
246 floating[:] result_view,
247 floating[:, ::1] y,
248 floating m,
249 Py_ssize_t offset,
250 ) noexcept nogil:
251 return result_view[offset + 3] - m * y[offset + 3, 0]
252
253
254 @cython.boundscheck(False)
255 @cython.cdivision(True)
256 @cython.wraparound(False)
257 @cython.initializedcheck(False)
258 cdef inline floating _calc_slope_500(
259 floating[:] result_view,
260 floating[:, ::1] y,
261 Py_ssize_t offset,
262 ) noexcept nogil:
263 return (result_view[offset + 1] - result_view[offset]) / \
264 (y[offset + 1, 0] - y[offset, 0])
265
266
267 @cython.boundscheck(False)
268 @cython.cdivision(True)
269 @cython.wraparound(False)
270 @cython.initializedcheck(False)
271 cdef inline floating _calc_offset_500(
272 floating[:] result_view,
273 floating[:, ::1] y,
274 floating m,
275 Py_ssize_t offset,
276 ) noexcept nogil:
277 return result_view[offset + 1] - m * y[offset + 1, 0]