@@ -27,22 +27,19 @@ cimport cython
27
27
cimport numpy as np
28
28
from libc .math cimport fabs , isinf
29
29
30
- ctypedef np .double_t DTYPE_t
31
-
32
30
ctypedef fused data_type :
33
31
double
34
32
float
35
33
36
- ctypedef double float_index
37
- f_index_dtype = float
38
- ctypedef int i_index_type
39
-
34
+ ctypedef fused float_index :
35
+ double
36
+ float
40
37
41
38
np .import_array ()
42
39
43
40
@cython .boundscheck (False )
44
41
@cython .wraparound (False )
45
- cdef inline void nn (const data_type [:, :, :] data , i_index_type l0 , int p0 , double dl , double dp , int lmax , int pmax , data_type [:] res ) noexcept nogil :
42
+ cdef inline void nn (const data_type [:, :, :] data , int l0 , int p0 , float_index dl , float_index dp , int lmax , int pmax , data_type [:] res ) noexcept nogil :
46
43
cdef int nnl , nnp
47
44
cdef size_t z_size = res .shape [0 ]
48
45
cdef size_t i
@@ -62,9 +59,9 @@ cdef inline void nn(const data_type[:, :, :] data, i_index_type l0, int p0, doub
62
59
63
60
@cython .boundscheck (False )
64
61
@cython .wraparound (False )
65
- cdef inline void bil (const data_type [:, :, :] data , int l0 , int p0 , double dl , double dp , int lmax , int pmax , data_type [:] res ) noexcept nogil :
62
+ cdef inline void bil (const data_type [:, :, :] data , int l0 , int p0 , float_index dl , float_index dp , int lmax , int pmax , data_type [:] res ) noexcept nogil :
66
63
cdef int l_a , l_b , p_a , p_b
67
- cdef double w_l , w_p
64
+ cdef float_index w_l , w_p
68
65
cdef size_t z_size = res .shape [0 ]
69
66
cdef size_t i
70
67
if dl < 0 :
@@ -92,26 +89,28 @@ cdef inline void bil(const data_type[:, :, :] data, int l0, int p0, double dl, d
92
89
93
90
@cython .boundscheck (False )
94
91
@cython .wraparound (False )
95
- cdef inline void indices_xy (const data_type [:, :, :] data , int l0 , int p0 , double dl , double dp , int lmax , int pmax , data_type [:] res ) noexcept nogil :
92
+ cdef inline void indices_xy (const data_type [:, :, :] data , int l0 , int p0 , float_index dl , float_index dp , int lmax , int pmax , data_type [:] res ) noexcept nogil :
96
93
cdef int nnl , nnp
97
94
cdef size_t z_size = res .shape [0 ]
98
95
cdef size_t i
99
96
res [1 ] = dl + l0
100
97
res [0 ] = dp + p0
101
98
102
- ctypedef void (* FN )(const data_type [:, :, :] data , int l0 , int p0 , double dl , double dp , int lmax , int pmax , data_type [:] res ) noexcept nogil
99
+
100
+ ctypedef void (* FN )(const data_type [:, :, :] data , int l0 , int p0 , float_index dl , float_index dp , int lmax , int pmax , data_type [:] res ) noexcept nogil
101
+
103
102
104
103
@cython .boundscheck (False )
105
104
@cython .wraparound (False )
106
105
cpdef one_step_gradient_search (const data_type [:, :, :] data ,
107
- DTYPE_t [:, :] src_x ,
108
- DTYPE_t [:, :] src_y ,
109
- DTYPE_t [:, :] xl ,
110
- DTYPE_t [:, :] xp ,
111
- DTYPE_t [:, :] yl ,
112
- DTYPE_t [:, :] yp ,
113
- DTYPE_t [:, :] dst_x ,
114
- DTYPE_t [:, :] dst_y ,
106
+ float_index [:, :] src_x ,
107
+ float_index [:, :] src_y ,
108
+ float_index [:, :] xl ,
109
+ float_index [:, :] xp ,
110
+ float_index [:, :] yl ,
111
+ float_index [:, :] yp ,
112
+ float_index [:, :] dst_x ,
113
+ float_index [:, :] dst_y ,
115
114
str method = 'bilinear' ):
116
115
"""Gradient search, simple case variant."""
117
116
cdef FN fun
@@ -144,18 +143,19 @@ cpdef one_step_gradient_search(const data_type[:, :, :] data,
144
143
# return the output image
145
144
return image
146
145
146
+
147
147
@cython .boundscheck (False )
148
148
@cython .wraparound (False )
149
149
@cython .cdivision (True )
150
150
cdef void one_step_gradient_search_no_gil (const data_type [:, :, :] data ,
151
- const DTYPE_t [:, :] src_x ,
152
- const DTYPE_t [:, :] src_y ,
153
- const DTYPE_t [:, :] xl ,
154
- const DTYPE_t [:, :] xp ,
155
- const DTYPE_t [:, :] yl ,
156
- const DTYPE_t [:, :] yp ,
157
- const DTYPE_t [:, :] dst_x ,
158
- const DTYPE_t [:, :] dst_y ,
151
+ const float_index [:, :] src_x ,
152
+ const float_index [:, :] src_y ,
153
+ const float_index [:, :] xl ,
154
+ const float_index [:, :] xp ,
155
+ const float_index [:, :] yl ,
156
+ const float_index [:, :] yp ,
157
+ const float_index [:, :] dst_x ,
158
+ const float_index [:, :] dst_y ,
159
159
const size_t x_size ,
160
160
const size_t y_size ,
161
161
FN fun ,
@@ -173,7 +173,7 @@ cdef void one_step_gradient_search_no_gil(const data_type[:, :, :] data,
173
173
# intermediate variables:
174
174
cdef int l_a , l_b , p_a , p_b
175
175
cdef size_t i , j , elt
176
- cdef double dx , dy , d , dl , dp
176
+ cdef float_index dx , dy , d , dl , dp
177
177
cdef int col_step = - 1
178
178
# number of iterations
179
179
cdef int cnt = 0
@@ -230,16 +230,17 @@ cdef void one_step_gradient_search_no_gil(const data_type[:, :, :] data,
230
230
p0 = int (p0 + dp )
231
231
j += col_step
232
232
233
+
233
234
@cython .boundscheck (False )
234
235
@cython .wraparound (False )
235
- cpdef one_step_gradient_indices (DTYPE_t [:, :] src_x ,
236
- DTYPE_t [:, :] src_y ,
237
- DTYPE_t [:, :] xl ,
238
- DTYPE_t [:, :] xp ,
239
- DTYPE_t [:, :] yl ,
240
- DTYPE_t [:, :] yp ,
241
- DTYPE_t [:, :] dst_x ,
242
- DTYPE_t [:, :] dst_y ):
236
+ cpdef one_step_gradient_indices (float_index [:, :] src_x ,
237
+ float_index [:, :] src_y ,
238
+ float_index [:, :] xl ,
239
+ float_index [:, :] xp ,
240
+ float_index [:, :] yl ,
241
+ float_index [:, :] yp ,
242
+ float_index [:, :] dst_x ,
243
+ float_index [:, :] dst_y ):
243
244
"""Gradient search, simple case variant, returning float indices.
244
245
245
246
This is appropriate for monotonous gradients only, i.e. not modis or viirs in satellite projection.
@@ -250,18 +251,24 @@ cpdef one_step_gradient_indices(DTYPE_t [:, :] src_x,
250
251
cdef size_t y_size = dst_y .shape [0 ]
251
252
cdef size_t x_size = dst_x .shape [1 ]
252
253
254
+
255
+ if float_index is double :
256
+ dtype = np .float64
257
+ else :
258
+ dtype = np .float32
259
+
253
260
# output indices arrays --> needs to be (lines, pixels) --> y,x
254
- indices = np .full ([2 , y_size , x_size ], np .nan , dtype = f_index_dtype )
261
+ indices = np .full ([2 , y_size , x_size ], np .nan , dtype = dtype )
255
262
cdef float_index [:, :, :] indices_view_result = indices
256
263
257
264
# fake_data is not going to be used anyway as we just fill in the indices
258
- cdef float_index [:, :, :] fake_data = np .full ([1 , 1 , 1 ], np .nan , dtype = f_index_dtype )
265
+ cdef float_index [:, :, :] fake_data = np .full ([1 , 1 , 1 ], np .nan , dtype = dtype )
259
266
260
267
with nogil :
261
- one_step_gradient_search_no_gil [float_index ](fake_data ,
262
- src_x , src_y ,
263
- xl , xp , yl , yp ,
264
- dst_x , dst_y ,
265
- x_size , y_size ,
266
- indices_xy , indices_view_result )
268
+ one_step_gradient_search_no_gil [float_index , float_index ](fake_data ,
269
+ src_x , src_y ,
270
+ xl , xp , yl , yp ,
271
+ dst_x , dst_y ,
272
+ x_size , y_size ,
273
+ indices_xy , indices_view_result )
267
274
return indices
0 commit comments