Skip to content

Commit

Permalink
Refactor EWA fornav cython with modern memoryview handling
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jun 24, 2022
1 parent 16a0a83 commit 3050a62
Showing 1 changed file with 17 additions and 46 deletions.
63 changes: 17 additions & 46 deletions pyresample/ewa/_fornav.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -275,63 +275,34 @@ def fornav_wrapper(numpy.ndarray[cr_dtype, ndim=2, mode='c'] cols_array,
if not all(output_array.dtype == out_type for output_array in output_arrays):
raise ValueError("Input arrays must all be of the same data type")

cdef void ** input_pointer = <void ** >malloc(num_items * sizeof(void * ))
cdef cr_dtype** input_pointer = <cr_dtype ** >malloc(num_items * sizeof(void * ))
if not input_pointer:
raise MemoryError()
cdef void ** output_pointer = <void ** >malloc(num_items * sizeof(void * ))
cdef cr_dtype ** output_pointer = <cr_dtype ** >malloc(num_items * sizeof(void * ))
if not output_pointer:
raise MemoryError()
cdef unsigned int * valid_arr = <unsigned int * >malloc(num_items * sizeof(unsigned int))
valid_list = []
cdef numpy.ndarray[numpy.float32_t, ndim= 2] tmp_arr_f32
cdef numpy.ndarray[numpy.float64_t, ndim= 2] tmp_arr_f64
cdef numpy.ndarray[numpy.int8_t, ndim= 2] tmp_arr_i8
cdef cr_dtype * cols_pointer = &cols_array[0, 0]
cdef cr_dtype * rows_pointer = &rows_array[0, 0]
cdef bint mwm = maximum_weight_mode
cdef int func_result
cdef cr_dtype cast_input_fill = input_fill
cdef cr_dtype cast_output_fill = output_fill
cdef cr_dtype[:, ::1] tmp_arr

if in_type == numpy.float32:
for i in range(num_items):
tmp_arr_f32 = input_arrays[i]
input_pointer[i] = &tmp_arr_f32[0, 0]
tmp_arr_f32 = output_arrays[i]
output_pointer[i] = &tmp_arr_f32[0, 0]
with nogil:
func_result = fornav(valid_arr, num_items, swath_cols, swath_rows, grid_cols, grid_rows,
cols_pointer, rows_pointer,
< numpy.float32_t ** >input_pointer, < numpy.float32_t ** >output_pointer,
< numpy.float32_t > input_fill, < numpy.float32_t > output_fill, rows_per_scan,
weight_count, weight_min, weight_distance_max, weight_delta_max, weight_sum_min,
mwm)
elif in_type == numpy.float64:
for i in range(num_items):
tmp_arr_f64 = input_arrays[i]
input_pointer[i] = &tmp_arr_f64[0, 0]
tmp_arr_f64 = output_arrays[i]
output_pointer[i] = &tmp_arr_f64[0, 0]
with nogil:
func_result = fornav(valid_arr, num_items, swath_cols, swath_rows, grid_cols, grid_rows,
cols_pointer, rows_pointer,
< numpy.float64_t ** >input_pointer, < numpy.float64_t ** >output_pointer,
< numpy.float64_t > input_fill, < numpy.float64_t > output_fill, rows_per_scan,
weight_count, weight_min, weight_distance_max, weight_delta_max, weight_sum_min,
mwm)
elif in_type == numpy.int8:
for i in range(num_items):
tmp_arr_i8 = input_arrays[i]
input_pointer[i] = &tmp_arr_i8[0, 0]
tmp_arr_i8 = output_arrays[i]
output_pointer[i] = &tmp_arr_i8[0, 0]
with nogil:
func_result = fornav(valid_arr, num_items, swath_cols, swath_rows, grid_cols, grid_rows,
cols_pointer, rows_pointer,
< numpy.int8_t ** >input_pointer, < numpy.int8_t ** >output_pointer,
< numpy.int8_t > input_fill, < numpy.int8_t > output_fill, rows_per_scan,
weight_count, weight_min, weight_distance_max, weight_delta_max, weight_sum_min,
mwm)
else:
raise ValueError("Unknown input and output data type")
for i in range(num_items):
tmp_arr = input_arrays[i]
input_pointer[i] = &tmp_arr[0, 0]
tmp_arr = output_arrays[i]
output_pointer[i] = &tmp_arr[0, 0]
with nogil:
func_result = fornav(valid_arr, num_items, swath_cols, swath_rows, grid_cols, grid_rows,
cols_pointer, rows_pointer,
input_pointer, output_pointer,
cast_input_fill, cast_output_fill, rows_per_scan,
weight_count, weight_min, weight_distance_max, weight_delta_max, weight_sum_min,
mwm)

for i in range(num_items):
valid_list.append(valid_arr[i])
Expand Down

0 comments on commit 3050a62

Please sign in to comment.