Last active
July 23, 2018 10:25
-
-
Save dvalters/d34f96cdb24bcfc1a652b44cd6d655e9 to your computer and use it in GitHub Desktop.
Mapping the 2D array of burn days to a True/False voxelspace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plain for loops | |
import numpy as np | |
for j in range(one_month_data.shape[1]): | |
for i in range(one_month_data.shape[0]): | |
if not np.isnan(one_month_data[i,j]): | |
voxelspace[int(one_month_data[i,j]), i, j] = True if one_month_data[i,j] > 0 else False | |
elif np.isnan(one_month_data[i,j]); | |
date_start, date_end = get_dates_for_current_month() # return integers | |
voxelspace[date_start:date_end, i, j] = np.nan | |
elif one_month_data[i,j] == 0; | |
date_start, date_end = get_dates_for_current_month() | |
voxelspace[date_start:date_end, i, j] = False | |
# Cythonised version | |
import cython | |
cimport numpy | |
from libc.math cimport isnan | |
cdef float NAN | |
NAN = float("NaN") | |
# Or use a NoData vlue | |
cdef int NoData = -9999 | |
# set up ctypes for the variables in the function to allow it to cythonise nicely | |
cdef int i = 0 | |
cdef int j = 0 | |
cdef int jmax = one_month_data.shape[1] | |
cdef int imax = one_month_data.shape[0] | |
# Best to turn off these decorators if you are debugging | |
# (Segfaults etc.) | |
@cython.boundscheck(False) | |
@cython.wraparound(False) | |
@cython.nonecheck(False) | |
def voxelspace(np.ndarray[DTYPE_t, ndim=3] voxelspace, np.ndarray[DTYPE_t, ndim=2] one_month_data, MORE_ARGS...): | |
for j in range(jmax): | |
for i in range(imax): | |
if not isnan(one_month_data[i,j]): | |
voxelspace[int(one_month_data[i,j]), i, j] = True if one_month_data[i,j] > 0 else False | |
elif np.isnan(one_month_data[i,j]); | |
date_start, date_end = get_dates_for_current_month() # return C-type integers from this function | |
voxelspace[date_start:date_end, i, j] = NAN # Or NoData value | |
elif one_month_data[i,j] == 0; | |
date_start, date_end = get_dates_for_current_month() | |
voxelspace[date_start:date_end, i, j] = False | |
return voxelspace | |
def get_dates_for_current_month(month): | |
#...code here | |
cdef int start_date | |
cdef int end_date | |
# More code.... | |
return start_date, end_date | |
# Add in later paralelism once working: | |
with nogil, parallel(num_threads=num_threads_use): | |
# OpenMP threads created for the outer loop. | |
for i in prange(jmax): | |
# etc...... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@DTMilodowski points out
np.isfinite
would get rid of theis not
formation 👍