| 1 |
# cython: language_level=3, boundscheck=False, cdivision=True, wraparound=False, initializedcheck=False, nonecheck=False |
| 2 |
cimport cython |
| 3 |
from ._modis_utils cimport lonlat2xyz, xyz2lonlat, floating, deg2rad |
| 4 |
from .simple_modis_interpolator import scanline_mapblocks |
| 5 |
|
| 6 |
from libc.math cimport asin, sin, cos, sqrt |
| 7 |
cimport numpy as np |
| 8 |
import numpy as np |
| 9 |
|
| 10 |
DEF R = 6370.997 |
| 11 |
# Aqua altitude in km |
| 12 |
DEF H = 709.0 |
| 13 |
|
| 14 |
np.import_array() |
| 15 |
|
| 16 |
|
| 17 |
@scanline_mapblocks |
| 18 |
def interpolate( |
| 19 |
np.ndarray[floating, ndim=2] lon1, |
| 20 |
np.ndarray[floating, ndim=2] lat1, |
| 21 |
np.ndarray[floating, ndim=2] satz1, |
| 22 |
unsigned int coarse_resolution=0, |
| 23 |
unsigned int fine_resolution=0, |
| 24 |
unsigned int coarse_scan_width=0, |
| 25 |
): |
| 26 |
"""Helper function to interpolate scan-aligned arrays. |
| 27 |
|
| 28 |
This function's decorator runs this function for each dask block/chunk of |
| 29 |
scans. The arrays are scan-aligned meaning they are an even number of scans |
| 30 |
(N rows per scan) and contain the entire scan width. |
| 31 |
|
| 32 |
""" |
| 33 |
if coarse_resolution == 5000 and coarse_scan_width not in (0, 270, 271): |
| 34 |
raise NotImplementedError( |
| 35 |
"Can't interpolate if 5km tiepoints have less than 270 columns." |
| 36 |
) |
| 37 |
interp = MODISInterpolator(coarse_resolution, fine_resolution, coarse_scan_width=coarse_scan_width or 0) |
| 38 |
return interp.interpolate(lon1, lat1, satz1) |
| 39 |
|
| 40 |
|
| 41 |
cdef void _compute_expansion_alignment(floating[:, :] satz_a, floating [:, :] satz_b, int scan_width, |
| 42 |
floating[:, ::1] c_expansion, floating[:, ::1] c_alignment) noexcept nogil: |
| 43 |
"""Fill in expansion and alignment. |
| 44 |
|
| 45 |
Input angles should be in degrees and will be converted to radians. |
| 46 |
|
| 47 |
""" |
| 48 |
cdef Py_ssize_t i, j |
| 49 |
cdef floating satz_a_rad, satz_b_rad, phi_a, phi_b, theta_a, theta_b, phi, zeta, theta, denominator, sin_beta_2, d, e |
| 50 |
for i in range(satz_a.shape[0]): |
| 51 |
for j in range(satz_a.shape[1]): |
| 52 |
satz_a_rad = deg2rad(satz_a[i, j]) |
| 53 |
satz_b_rad = deg2rad(satz_b[i, j]) |
| 54 |
phi_a = _compute_phi(satz_a_rad) |
| 55 |
phi_b = _compute_phi(satz_b_rad) |
| 56 |
theta_a = _compute_theta(satz_a_rad, phi_a) |
| 57 |
theta_b = _compute_theta(satz_b_rad, phi_b) |
| 58 |
phi = (phi_a + phi_b) / 2 |
| 59 |
zeta = _compute_zeta(phi) |
| 60 |
theta = _compute_theta(zeta, phi) |
| 61 |
# Workaround for tiepoints symmetrical about the subsatellite-track |
| 62 |
denominator = theta_a * 2 if theta_a == theta_b else theta_a - theta_b |
| 63 |
|
| 64 |
c_expansion[i, j] = 4 * (((theta_a + theta_b) / 2 - theta) / denominator) |
| 65 |
|
| 66 |
sin_beta_2 = scan_width / (2 * H) |
| 67 |
d = ((R + H) / R * cos(phi) - cos(zeta)) * sin_beta_2 |
| 68 |
e = cos(zeta) - sqrt(cos(zeta) ** 2 - d ** 2) |
| 69 |
|
| 70 |
c_alignment[i, j] = 4 * e * sin(zeta) / denominator |
| 71 |
|
| 72 |
|
| 73 |
cdef inline floating _compute_phi(floating zeta) noexcept nogil: |
| 74 |
return asin(R * sin(zeta) / (R + H)) |
| 75 |
|
| 76 |
|
| 77 |
cdef inline floating _compute_theta(floating zeta, floating phi) noexcept nogil: |
| 78 |
return zeta - phi |
| 79 |
|
| 80 |
|
| 81 |
cdef inline floating _compute_zeta(floating phi) noexcept nogil: |
| 82 |
return asin((R + H) * sin(phi) / R) |
| 83 |
|
| 84 |
|
| 85 |
cdef inline floating[:, :] _get_upper_left_corner(floating[:, ::1] arr) noexcept nogil: |
| 86 |
return arr[:arr.shape[0] - 1, :arr.shape[1] - 1] |
| 87 |
|
| 88 |
|
| 89 |
cdef inline floating[:, :] _get_upper_right_corner(floating[:, ::1] arr) noexcept nogil: |
| 90 |
return arr[:arr.shape[0] - 1, 1:] |
| 91 |
|
| 92 |
|
| 93 |
cdef class MODISInterpolator: |
| 94 |
"""Helper class for MODIS interpolation. |
| 95 |
|
| 96 |
Not intended for public use. Use ``modis_X_to_Y`` functions instead. |
| 97 |
|
| 98 |
""" |
| 99 |
|
| 100 |
cdef int _coarse_scan_length |
| 101 |
cdef int _coarse_scan_width |
| 102 |
cdef int _coarse_pixels_per_1km |
| 103 |
cdef int _fine_pixels_per_coarse_pixel |
| 104 |
cdef int _fine_scan_width |
| 105 |
cdef int _fine_scan_length |
| 106 |
cdef int _coarse_resolution |
| 107 |
cdef int _fine_resolution |
| 108 |
cdef Py_ssize_t _factor_5km |
| 109 |
|
| 110 |
def __cinit__(self, unsigned int coarse_resolution, unsigned int fine_resolution, unsigned int coarse_scan_width=0): |
| 111 |
if coarse_resolution == 1000: |
| 112 |
self._coarse_scan_length = 10 |
| 113 |
self._coarse_scan_width = 1354 |
| 114 |
elif coarse_resolution == 5000: |
| 115 |
self._coarse_scan_length = 2 |
| 116 |
if coarse_scan_width == 0: |
| 117 |
self._coarse_scan_width = 271 |
| 118 |
else: |
| 119 |
self._coarse_scan_width = coarse_scan_width |
| 120 |
self._coarse_pixels_per_1km = coarse_resolution // 1000 |
| 121 |
|
| 122 |
cdef int fine_pixels_per_1km = 1000 // fine_resolution |
| 123 |
self._fine_pixels_per_coarse_pixel = fine_pixels_per_1km * self._coarse_pixels_per_1km |
| 124 |
self._fine_scan_width = 1354 * fine_pixels_per_1km |
| 125 |
self._coarse_resolution = coarse_resolution |
| 126 |
self._fine_resolution = fine_resolution |
| 127 |
# partial rows/columns to repeat: 5km->1km => 2, 5km->500m => 4, 5km->250m => 8 |
| 128 |
self._factor_5km = self._fine_pixels_per_coarse_pixel // self._coarse_pixels_per_1km * 2 |
| 129 |
|
| 130 |
cdef tuple interpolate( |
| 131 |
self, |
| 132 |
np.ndarray[floating, ndim=2] lon1, |
| 133 |
np.ndarray[floating, ndim=2] lat1, |
| 134 |
np.ndarray[floating, ndim=2] satz1): |
| 135 |
"""Interpolate MODIS geolocation from 'coarse_resolution' to 'fine_resolution'.""" |
| 136 |
lon1 = np.ascontiguousarray(lon1) |
| 137 |
lat1 = np.ascontiguousarray(lat1) |
| 138 |
satz1 = np.ascontiguousarray(satz1) |
| 139 |
|
| 140 |
cdef int num_fine_scan_rows = self._coarse_scan_length * self._fine_pixels_per_coarse_pixel |
| 141 |
cdef int num_fine_scan_cols = self._fine_scan_width |
| 142 |
cdef np.ndarray[floating, ndim=2] a_track = np.empty((num_fine_scan_rows, num_fine_scan_cols), dtype=lon1.dtype) |
| 143 |
cdef np.ndarray[floating, ndim=2] a_scan = np.empty((num_fine_scan_rows, num_fine_scan_cols), dtype=lon1.dtype) |
| 144 |
cdef floating[:, ::1] a_track_view = a_track |
| 145 |
cdef floating[:, ::1] a_scan_view = a_scan |
| 146 |
|
| 147 |
cdef tuple coords_xy = self._get_coords() |
| 148 |
cdef np.ndarray[floating, ndim=1] x = coords_xy[0] |
| 149 |
cdef np.ndarray[floating, ndim=1] y = coords_xy[1] |
| 150 |
cdef floating[::1] x_view = x |
| 151 |
cdef floating[::1] y_view = y |
| 152 |
|
| 153 |
cdef np.ndarray[floating, ndim=2] tmp_tiepoint_a, tmp_tiepoint_b, tmp_tiepoint_c, tmp_tiepoint_d |
| 154 |
cdef floating[:, ::1] tmp_tiepoint_a_view, tmp_tiepoint_b_view, tmp_tiepoint_c_view, tmp_tiepoint_d_view |
| 155 |
tmp_tiepoint_a = np.empty((num_fine_scan_rows, num_fine_scan_cols), dtype=lon1.dtype) |
| 156 |
tmp_tiepoint_b = np.empty((num_fine_scan_rows, num_fine_scan_cols), dtype=lon1.dtype) |
| 157 |
tmp_tiepoint_c = np.empty((num_fine_scan_rows, num_fine_scan_cols), dtype=lon1.dtype) |
| 158 |
tmp_tiepoint_d = np.empty((num_fine_scan_rows, num_fine_scan_cols), dtype=lon1.dtype) |
| 159 |
tmp_tiepoint_a_view = tmp_tiepoint_a |
| 160 |
tmp_tiepoint_b_view = tmp_tiepoint_b |
| 161 |
tmp_tiepoint_c_view = tmp_tiepoint_c |
| 162 |
tmp_tiepoint_d_view = tmp_tiepoint_d |
| 163 |
|
| 164 |
cdef floating[:, :, ::1] xyz_coarse_view = np.empty((self._coarse_scan_length, self._coarse_scan_width, 3), dtype=lon1.dtype) |
| 165 |
cdef floating[:, :, ::1] xyz_fine_view = np.empty((num_fine_scan_rows, num_fine_scan_cols, 3), dtype=lon1.dtype) |
| 166 |
|
| 167 |
cdef unsigned int scans = satz1.shape[0] // self._coarse_scan_length |
| 168 |
cdef np.ndarray[floating, ndim=2] new_lons = np.empty((satz1.shape[0] * self._fine_pixels_per_coarse_pixel, self._fine_scan_width), dtype=lon1.dtype) |
| 169 |
cdef np.ndarray[floating, ndim=2] new_lats = np.empty((satz1.shape[0] * self._fine_pixels_per_coarse_pixel, self._fine_scan_width), dtype=lon1.dtype) |
| 170 |
cdef floating[:, ::1] coarse_lons = lon1 |
| 171 |
cdef floating[:, ::1] coarse_lats = lat1 |
| 172 |
cdef floating[:, ::1] coarse_satz = satz1 |
| 173 |
cdef floating[:, ::1] fine_lons = new_lons |
| 174 |
cdef floating[:, ::1] fine_lats = new_lats |
| 175 |
with nogil: |
| 176 |
self._interpolate_lons_lats( |
| 177 |
scans, |
| 178 |
coarse_lons, |
| 179 |
coarse_lats, |
| 180 |
coarse_satz, |
| 181 |
x_view, |
| 182 |
y_view, |
| 183 |
a_track_view, |
| 184 |
a_scan_view, |
| 185 |
tmp_tiepoint_a_view, |
| 186 |
tmp_tiepoint_b_view, |
| 187 |
tmp_tiepoint_c_view, |
| 188 |
tmp_tiepoint_d_view, |
| 189 |
xyz_coarse_view, |
| 190 |
xyz_fine_view, |
| 191 |
fine_lons, |
| 192 |
fine_lats, |
| 193 |
) |
| 194 |
return new_lons, new_lats |
| 195 |
|
| 196 |
cdef tuple _get_coords(self): |
| 197 |
cdef np.ndarray[np.float32_t, ndim=1] x, y |
| 198 |
cdef np.float32_t[::1] x_view, y_view |
| 199 |
if self._coarse_scan_length == 10: |
| 200 |
x = np.arange(self._fine_scan_width, dtype=np.float32) % self._fine_pixels_per_coarse_pixel |
| 201 |
y = np.empty((self._coarse_scan_length * self._fine_pixels_per_coarse_pixel,), dtype=np.float32) |
| 202 |
x_view = x |
| 203 |
y_view = y |
| 204 |
with nogil: |
| 205 |
self._get_coords_1km(x_view, y_view) |
| 206 |
else: |
| 207 |
x, y = self._get_coords_5km() |
| 208 |
return x, y |
| 209 |
|
| 210 |
cdef void _get_coords_1km( |
| 211 |
self, |
| 212 |
floating[::1] x_view, |
| 213 |
floating[::1] y_view, |
| 214 |
) noexcept nogil: |
| 215 |
cdef unsigned int scan_idx |
| 216 |
cdef int i |
| 217 |
cdef unsigned int fine_idx |
| 218 |
cdef unsigned int half_scan_length = self._fine_pixels_per_coarse_pixel // 2 |
| 219 |
cdef unsigned int fine_pixels_per_scan = self._coarse_scan_length * self._fine_pixels_per_coarse_pixel |
| 220 |
for fine_idx in range(fine_pixels_per_scan): |
| 221 |
if fine_idx < half_scan_length: |
| 222 |
y_view[fine_idx] = 0.5 + fine_idx - half_scan_length |
| 223 |
elif fine_idx >= fine_pixels_per_scan - half_scan_length: |
| 224 |
y_view[fine_idx] = (self._fine_pixels_per_coarse_pixel + 0.5) + (half_scan_length - (fine_pixels_per_scan - fine_idx)) |
| 225 |
else: |
| 226 |
y_view[fine_idx] = ((fine_idx + half_scan_length) % self._fine_pixels_per_coarse_pixel) + 0.5 |
| 227 |
|
| 228 |
for i in range(self._fine_pixels_per_coarse_pixel): |
| 229 |
x_view[(self._fine_scan_width - self._fine_pixels_per_coarse_pixel) + i] = self._fine_pixels_per_coarse_pixel + i |
| 230 |
|
| 231 |
@cython.wraparound(True) |
| 232 |
cdef tuple _get_coords_5km(self): |
| 233 |
cdef np.ndarray[np.float32_t, ndim=1] y = np.arange(self._fine_pixels_per_coarse_pixel * self._coarse_scan_length, dtype=np.float32) - 2 |
| 234 |
cdef np.ndarray[np.float32_t, ndim=1] x = (np.arange(self._fine_scan_width, dtype=np.float32) - 2) % self._fine_pixels_per_coarse_pixel |
| 235 |
x[0] = -2 |
| 236 |
x[1] = -1 |
| 237 |
if self._coarse_scan_width == 271: |
| 238 |
x[-2] = 5 |
| 239 |
x[-1] = 6 |
| 240 |
else: |
| 241 |
# self._coarse_scan_width == 270 |
| 242 |
x[-7] = 5 |
| 243 |
x[-6] = 6 |
| 244 |
x[-5] = 7 |
| 245 |
x[-4] = 8 |
| 246 |
x[-3] = 9 |
| 247 |
x[-2] = 10 |
| 248 |
x[-1] = 11 |
| 249 |
return x, y |
| 250 |
|
| 251 |
cdef void _interpolate_lons_lats(self, |
| 252 |
unsigned int scans, |
| 253 |
floating[:, ::1] coarse_lons, |
| 254 |
floating[:, ::1] coarse_lats, |
| 255 |
floating[:, ::1] coarse_satz, |
| 256 |
floating[::1] x, |
| 257 |
floating[::1] y, |
| 258 |
floating[:, ::1] a_track, |
| 259 |
floating[:, ::1] a_scan, |
| 260 |
floating[:, ::1] tmp_fine_a, |
| 261 |
floating[:, ::1] tmp_fine_b, |
| 262 |
floating[:, ::1] tmp_fine_c, |
| 263 |
floating[:, ::1] tmp_fine_d, |
| 264 |
floating[:, :, ::1] coarse_xyz, |
| 265 |
floating[:, :, ::1] fine_xyz, |
| 266 |
floating[:, ::1] fine_lons, |
| 267 |
floating[:, ::1] fine_lats, |
| 268 |
) noexcept nogil: |
| 269 |
cdef floating[:, ::1] lons_scan, lats_scan, satz_scan |
| 270 |
cdef floating[:, ::1] new_lons_scan, new_lats_scan |
| 271 |
cdef Py_ssize_t scan_idx |
| 272 |
for scan_idx in range(0, scans): |
| 273 |
lons_scan = coarse_lons[scan_idx * self._coarse_scan_length:(scan_idx + 1) * self._coarse_scan_length] |
| 274 |
lats_scan = coarse_lats[scan_idx * self._coarse_scan_length:(scan_idx + 1) * self._coarse_scan_length] |
| 275 |
satz_scan = coarse_satz[scan_idx * self._coarse_scan_length:(scan_idx + 1) * self._coarse_scan_length] |
| 276 |
new_lons_scan = fine_lons[scan_idx * self._coarse_scan_length * self._fine_pixels_per_coarse_pixel:(scan_idx + 1) * self._coarse_scan_length * self._fine_pixels_per_coarse_pixel] |
| 277 |
new_lats_scan = fine_lats[scan_idx * self._coarse_scan_length * self._fine_pixels_per_coarse_pixel:(scan_idx + 1) * self._coarse_scan_length * self._fine_pixels_per_coarse_pixel] |
| 278 |
self._get_atrack_ascan( |
| 279 |
satz_scan, |
| 280 |
x, |
| 281 |
y, |
| 282 |
tmp_fine_a[:self._coarse_scan_length - 1, :self._coarse_scan_width - 1], |
| 283 |
tmp_fine_b[:self._coarse_scan_length - 1, :self._coarse_scan_width - 1], |
| 284 |
tmp_fine_c, |
| 285 |
tmp_fine_d, |
| 286 |
a_track, |
| 287 |
a_scan) |
| 288 |
lonlat2xyz(lons_scan, lats_scan, coarse_xyz) |
| 289 |
self._compute_fine_xyz(a_track, a_scan, coarse_xyz, |
| 290 |
tmp_fine_a, tmp_fine_b, tmp_fine_c, tmp_fine_d, |
| 291 |
fine_xyz) |
| 292 |
|
| 293 |
xyz2lonlat(fine_xyz, new_lons_scan, new_lats_scan) |
| 294 |
|
| 295 |
cdef void _get_atrack_ascan( |
| 296 |
self, |
| 297 |
floating[:, ::1] satz, |
| 298 |
floating[::1] x, |
| 299 |
floating[::1] y, |
| 300 |
floating[:, ::1] c_exp_coarse, |
| 301 |
floating[:, ::1] c_ali_coarse, |
| 302 |
floating[:, ::1] c_exp_fine, |
| 303 |
floating[:, ::1] c_ali_fine, |
| 304 |
floating[:, ::1] a_track, |
| 305 |
floating[:, ::1] a_scan |
| 306 |
) noexcept nogil: |
| 307 |
cdef floating[:, :] satz_a_view = _get_upper_left_corner(satz) |
| 308 |
cdef floating[:, :] satz_b_view = _get_upper_right_corner(satz) |
| 309 |
cdef Py_ssize_t scan_idx |
| 310 |
_compute_expansion_alignment( |
| 311 |
satz_a_view, |
| 312 |
satz_b_view, |
| 313 |
self._coarse_pixels_per_1km, |
| 314 |
c_exp_coarse, |
| 315 |
c_ali_coarse) |
| 316 |
|
| 317 |
cdef floating[:, :] c_exp_view2 = c_exp_coarse |
| 318 |
self._expand_tiepoint_array(c_exp_view2, c_exp_fine) |
| 319 |
|
| 320 |
cdef floating[:, :] c_ali_view2 = c_ali_coarse |
| 321 |
self._expand_tiepoint_array(c_ali_view2, c_ali_fine) |
| 322 |
|
| 323 |
self._calculate_atrack_ascan( |
| 324 |
x, y, |
| 325 |
c_exp_fine, c_ali_fine, |
| 326 |
a_track, a_scan) |
| 327 |
|
| 328 |
cdef void _calculate_atrack_ascan( |
| 329 |
self, |
| 330 |
floating[::1] coords_x, |
| 331 |
floating[::1] coords_y, |
| 332 |
floating[:, ::1] c_exp_full, |
| 333 |
floating[:, ::1] c_ali_full, |
| 334 |
floating[:, ::1] a_track, |
| 335 |
floating[:, ::1] a_scan, |
| 336 |
) noexcept nogil: |
| 337 |
cdef Py_ssize_t i, j |
| 338 |
cdef floating s_s, s_t |
| 339 |
for j in range(coords_y.shape[0]): |
| 340 |
for i in range(coords_x.shape[0]): |
| 341 |
s_s = coords_x[i] / self._fine_pixels_per_coarse_pixel |
| 342 |
s_t = coords_y[j] / self._fine_pixels_per_coarse_pixel |
| 343 |
a_track[j, i] = s_t |
| 344 |
a_scan[j, i] = s_s + s_s * (1 - s_s) * c_exp_full[j, i] + s_t * (1 - s_t) * c_ali_full[j, i] |
| 345 |
|
| 346 |
cdef void _compute_fine_xyz( |
| 347 |
self, |
| 348 |
floating[:, ::1] a_track_view, |
| 349 |
floating[:, ::1] a_scan_view, |
| 350 |
floating[:, :, ::1] xyz_view, |
| 351 |
floating[:, ::1] data_tiepoint_a_view, |
| 352 |
floating[:, ::1] data_tiepoint_b_view, |
| 353 |
floating[:, ::1] data_tiepoint_c_view, |
| 354 |
floating[:, ::1] data_tiepoint_d_view, |
| 355 |
floating[:, :, ::1] xyz_comp_view, |
| 356 |
) noexcept nogil: |
| 357 |
cdef Py_ssize_t k |
| 358 |
cdef floating[:, :] comp_a_view, comp_b_view, comp_c_view, comp_d_view |
| 359 |
for k in range(3): # xyz |
| 360 |
comp_a_view = xyz_view[:xyz_view.shape[0] - 1, :xyz_view.shape[1] - 1, k] # upper left |
| 361 |
comp_b_view = xyz_view[:xyz_view.shape[0] - 1, 1:, k] # upper right |
| 362 |
comp_c_view = xyz_view[1:, 1:, k] # lower right |
| 363 |
comp_d_view = xyz_view[1:, :xyz_view.shape[1] - 1, k] # lower left |
| 364 |
self._expand_tiepoint_array(comp_a_view, data_tiepoint_a_view) |
| 365 |
self._expand_tiepoint_array(comp_b_view, data_tiepoint_b_view) |
| 366 |
self._expand_tiepoint_array(comp_c_view, data_tiepoint_c_view) |
| 367 |
self._expand_tiepoint_array(comp_d_view, data_tiepoint_d_view) |
| 368 |
self._compute_fine_xyz_component( |
| 369 |
a_track_view, |
| 370 |
a_scan_view, |
| 371 |
data_tiepoint_a_view, |
| 372 |
data_tiepoint_b_view, |
| 373 |
data_tiepoint_c_view, |
| 374 |
data_tiepoint_d_view, |
| 375 |
xyz_comp_view, |
| 376 |
k, |
| 377 |
) |
| 378 |
|
| 379 |
cdef void _compute_fine_xyz_component( |
| 380 |
self, |
| 381 |
floating[:, ::1] a_track_view, |
| 382 |
floating[:, ::1] a_scan_view, |
| 383 |
floating[:, ::1] data_a_2d_view, |
| 384 |
floating[:, ::1] data_b_2d_view, |
| 385 |
floating[:, ::1] data_c_2d_view, |
| 386 |
floating[:, ::1] data_d_2d_view, |
| 387 |
floating[:, :, ::1] xyz_comp_view, |
| 388 |
Py_ssize_t k, |
| 389 |
) noexcept nogil: |
| 390 |
cdef Py_ssize_t i, j |
| 391 |
cdef floating scan1_tmp, scan2_tmp, atrack1, ascan1 |
| 392 |
for i in range(a_scan_view.shape[0]): |
| 393 |
for j in range(a_scan_view.shape[1]): |
| 394 |
atrack1 = a_track_view[i, j] |
| 395 |
ascan1 = a_scan_view[i, j] |
| 396 |
scan1_tmp = (1 - ascan1) * data_a_2d_view[i, j] + ascan1 * data_b_2d_view[i, j] |
| 397 |
scan2_tmp = (1 - ascan1) * data_d_2d_view[i, j] + ascan1 * data_c_2d_view[i, j] |
| 398 |
xyz_comp_view[i, j, k] = (1 - atrack1) * scan1_tmp + atrack1 * scan2_tmp |
| 399 |
|
| 400 |
cdef void _expand_tiepoint_array( |
| 401 |
self, |
| 402 |
floating[:, :] input_arr, |
| 403 |
floating[:, ::1] output_arr, |
| 404 |
) noexcept nogil: |
| 405 |
if self._coarse_scan_length == 10: |
| 406 |
self._expand_tiepoint_array_1km(input_arr, output_arr) |
| 407 |
else: |
| 408 |
self._expand_tiepoint_array_5km(input_arr, output_arr) |
| 409 |
|
| 410 |
cdef void _expand_tiepoint_array_1km(self, floating[:, :] input_arr, floating[:, ::1] expanded_arr) noexcept nogil: |
| 411 |
self._expand_tiepoint_array_1km_main(input_arr, expanded_arr) |
| 412 |
self._expand_tiepoint_array_1km_right_column(input_arr, expanded_arr) |
| 413 |
|
| 414 |
cdef void _expand_tiepoint_array_1km_main(self, floating[:, :] input_arr, floating[:, ::1] expanded_arr) noexcept nogil: |
| 415 |
cdef floating tiepoint_value |
| 416 |
cdef Py_ssize_t row_idx, col_idx, length_repeat_cycle, width_repeat_cycle, half_coarse_pixel_fine_offset, row_offset, col_offset |
| 417 |
cdef Py_ssize_t row_repeat_offset, col_repeat_offset |
| 418 |
half_coarse_pixel_fine_offset = self._fine_pixels_per_coarse_pixel // 2 |
| 419 |
for row_idx in range(input_arr.shape[0]): |
| 420 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel + half_coarse_pixel_fine_offset |
| 421 |
for col_idx in range(input_arr.shape[1]): |
| 422 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel |
| 423 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 424 |
self._expand_tiepoint_array_1km_with_repeat(tiepoint_value, expanded_arr, row_offset, col_offset) |
| 425 |
|
| 426 |
row_idx = 0 |
| 427 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel |
| 428 |
for col_idx in range(input_arr.shape[1]): |
| 429 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel |
| 430 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 431 |
self._expand_tiepoint_array_1km_with_repeat(tiepoint_value, expanded_arr, row_offset, col_offset) |
| 432 |
|
| 433 |
row_idx = input_arr.shape[0] - 1 |
| 434 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel + self._fine_pixels_per_coarse_pixel |
| 435 |
for col_idx in range(input_arr.shape[1]): |
| 436 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel |
| 437 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 438 |
self._expand_tiepoint_array_1km_with_repeat(tiepoint_value, expanded_arr, row_offset, col_offset) |
| 439 |
|
| 440 |
cdef void _expand_tiepoint_array_1km_right_column( |
| 441 |
self, |
| 442 |
floating[:, :] input_arr, |
| 443 |
floating[:, ::1] expanded_arr |
| 444 |
) noexcept nogil: |
| 445 |
cdef floating tiepoint_value |
| 446 |
cdef Py_ssize_t row_idx, col_idx, length_repeat_cycle, width_repeat_cycle, half_coarse_pixel_fine_offset, row_offset, col_offset |
| 447 |
cdef Py_ssize_t row_repeat_offset, col_repeat_offset |
| 448 |
half_coarse_pixel_fine_offset = self._fine_pixels_per_coarse_pixel // 2 |
| 449 |
col_idx = input_arr.shape[1] - 1 |
| 450 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel + self._fine_pixels_per_coarse_pixel |
| 451 |
for row_idx in range(input_arr.shape[0]): |
| 452 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel + half_coarse_pixel_fine_offset |
| 453 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 454 |
self._expand_tiepoint_array_1km_with_repeat(tiepoint_value, expanded_arr, row_offset, col_offset) |
| 455 |
|
| 456 |
self._expand_tiepoint_array_1km_right_column_top_row(input_arr, expanded_arr) |
| 457 |
self._expand_tiepoint_array_1km_right_column_bottom_row(input_arr, expanded_arr) |
| 458 |
|
| 459 |
cdef void _expand_tiepoint_array_1km_right_column_top_row( |
| 460 |
self, |
| 461 |
floating[:, :] input_arr, |
| 462 |
floating[:, ::1] expanded_arr |
| 463 |
) noexcept nogil: |
| 464 |
cdef floating tiepoint_value |
| 465 |
cdef Py_ssize_t row_idx, col_idx, row_offset, col_offset |
| 466 |
row_idx = 0 |
| 467 |
col_idx = input_arr.shape[1] - 1 |
| 468 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 469 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel |
| 470 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel + self._fine_pixels_per_coarse_pixel |
| 471 |
self._expand_tiepoint_array_1km_with_repeat(tiepoint_value, expanded_arr, row_offset, col_offset) |
| 472 |
|
| 473 |
cdef void _expand_tiepoint_array_1km_right_column_bottom_row( |
| 474 |
self, |
| 475 |
floating[:, :] input_arr, |
| 476 |
floating[:, ::1] expanded_arr |
| 477 |
) noexcept nogil: |
| 478 |
cdef floating tiepoint_value |
| 479 |
cdef Py_ssize_t row_idx, col_idx, row_offset, col_offset |
| 480 |
row_idx = input_arr.shape[0] - 1 |
| 481 |
col_idx = input_arr.shape[1] - 1 |
| 482 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 483 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel + self._fine_pixels_per_coarse_pixel |
| 484 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel + self._fine_pixels_per_coarse_pixel |
| 485 |
self._expand_tiepoint_array_1km_with_repeat(tiepoint_value, expanded_arr, row_offset, col_offset) |
| 486 |
|
| 487 |
cdef void _expand_tiepoint_array_1km_with_repeat( |
| 488 |
self, |
| 489 |
floating tiepoint_value, |
| 490 |
floating[:, ::1] expanded_arr, |
| 491 |
Py_ssize_t row_offset, |
| 492 |
Py_ssize_t col_offset, |
| 493 |
) noexcept nogil: |
| 494 |
cdef Py_ssize_t length_repeat_cycle, width_repeat_cycle |
| 495 |
cdef Py_ssize_t row_repeat_offset, col_repeat_offset |
| 496 |
for length_repeat_cycle in range(self._fine_pixels_per_coarse_pixel): |
| 497 |
row_repeat_offset = row_offset + length_repeat_cycle |
| 498 |
for width_repeat_cycle in range(self._fine_pixels_per_coarse_pixel): |
| 499 |
col_repeat_offset = col_offset + width_repeat_cycle |
| 500 |
expanded_arr[row_repeat_offset, col_repeat_offset] = tiepoint_value |
| 501 |
|
| 502 |
cdef void _expand_tiepoint_array_5km(self, floating[:, :] input_arr, floating[:, ::1] expanded_arr) noexcept nogil: |
| 503 |
self._expand_tiepoint_array_5km_main(input_arr, expanded_arr) |
| 504 |
self._expand_tiepoint_array_5km_left(input_arr, expanded_arr) |
| 505 |
if self._coarse_scan_width == 270: |
| 506 |
self._expand_tiepoint_array_5km_270_extra_column(input_arr, expanded_arr) |
| 507 |
self._expand_tiepoint_array_5km_right(input_arr, expanded_arr) |
| 508 |
|
| 509 |
cdef void _expand_tiepoint_array_5km_main(self, floating[:, :] input_arr, floating[:, ::1] expanded_arr) noexcept nogil: |
| 510 |
cdef floating tiepoint_value |
| 511 |
cdef Py_ssize_t row_idx, col_idx, row_offset, col_offset |
| 512 |
for row_idx in range(input_arr.shape[0]): |
| 513 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel * 2 |
| 514 |
for col_idx in range(input_arr.shape[1]): |
| 515 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel + self._factor_5km |
| 516 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 517 |
self._expand_tiepoint_array_5km_with_repeat( |
| 518 |
tiepoint_value, |
| 519 |
expanded_arr, |
| 520 |
row_offset, |
| 521 |
col_offset, |
| 522 |
self._fine_pixels_per_coarse_pixel) |
| 523 |
|
| 524 |
cdef void _expand_tiepoint_array_5km_270_extra_column( |
| 525 |
self, |
| 526 |
floating[:, :] input_arr, |
| 527 |
floating[:, ::1] expanded_arr |
| 528 |
) noexcept nogil: |
| 529 |
"""Copy an extra coarse pixel column between the main copied area and the right-most columns.""" |
| 530 |
cdef floating tiepoint_value |
| 531 |
cdef Py_ssize_t row_idx, col_idx, row_offset, col_offset |
| 532 |
col_idx = input_arr.shape[1] - 1 |
| 533 |
col_offset = col_idx * self._fine_pixels_per_coarse_pixel + self._fine_pixels_per_coarse_pixel + self._factor_5km |
| 534 |
for row_idx in range(input_arr.shape[0]): |
| 535 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel * 2 |
| 536 |
tiepoint_value = input_arr[row_idx, col_idx] |
| 537 |
self._expand_tiepoint_array_5km_with_repeat( |
| 538 |
tiepoint_value, |
| 539 |
expanded_arr, |
| 540 |
row_offset, |
| 541 |
col_offset, |
| 542 |
self._fine_pixels_per_coarse_pixel) |
| 543 |
|
| 544 |
cdef void _expand_tiepoint_array_5km_left( |
| 545 |
self, |
| 546 |
floating[:, :] input_arr, |
| 547 |
floating[:, ::1] expanded_arr, |
| 548 |
) noexcept nogil: |
| 549 |
self._expand_tiepoint_array_5km_edges(input_arr, expanded_arr, 0, 0) |
| 550 |
|
| 551 |
cdef void _expand_tiepoint_array_5km_right( |
| 552 |
self, |
| 553 |
floating[:, :] input_arr, |
| 554 |
floating[:, ::1] expanded_arr, |
| 555 |
) noexcept nogil: |
| 556 |
self._expand_tiepoint_array_5km_edges( |
| 557 |
input_arr, |
| 558 |
expanded_arr, |
| 559 |
input_arr.shape[1] - 1, |
| 560 |
expanded_arr.shape[1] - self._factor_5km) |
| 561 |
|
| 562 |
cdef void _expand_tiepoint_array_5km_edges( |
| 563 |
self, |
| 564 |
floating[:, :] input_arr, |
| 565 |
floating[:, ::1] expanded_arr, |
| 566 |
Py_ssize_t course_col_idx, |
| 567 |
Py_ssize_t fine_col_idx, |
| 568 |
) noexcept nogil: |
| 569 |
cdef floating tiepoint_value |
| 570 |
cdef Py_ssize_t row_idx, row_offset |
| 571 |
for row_idx in range(input_arr.shape[0]): |
| 572 |
row_offset = row_idx * self._fine_pixels_per_coarse_pixel * 2 |
| 573 |
tiepoint_value = input_arr[row_idx, course_col_idx] |
| 574 |
self._expand_tiepoint_array_5km_with_repeat( |
| 575 |
tiepoint_value, |
| 576 |
expanded_arr, |
| 577 |
row_offset, |
| 578 |
fine_col_idx, |
| 579 |
self._factor_5km) |
| 580 |
|
| 581 |
cdef void _expand_tiepoint_array_5km_with_repeat( |
| 582 |
self, |
| 583 |
floating tiepoint_value, |
| 584 |
floating[:, ::1] expanded_arr, |
| 585 |
Py_ssize_t row_offset, |
| 586 |
Py_ssize_t col_offset, |
| 587 |
Py_ssize_t col_width, |
| 588 |
) noexcept nogil: |
| 589 |
cdef Py_ssize_t length_repeat_cycle, width_repeat_cycle |
| 590 |
for length_repeat_cycle in range(self._fine_pixels_per_coarse_pixel * 2): |
| 591 |
for width_repeat_cycle in range(col_width): |
| 592 |
expanded_arr[row_offset + length_repeat_cycle, |
| 593 |
col_offset + width_repeat_cycle] = tiepoint_value |