-
Paolo Veglio authored
first working version of the hires-mvcm. It only works for ocean day. Results shown at AMS 2022 in poster session
Paolo Veglio authoredfirst working version of the hires-mvcm. It only works for ocean day. Results shown at AMS 2022 in poster session
ancillary.pyx 6.20 KiB
# cython: language_level=3
# cython: c_string_Type=unicode, c_string_encoding=utf8
cdef extern void get_Reynolds_SST(float *, float *, int, char *, char *, float *)
cdef extern void get_NDVI_background(float *, float *, int, char *, char *, float *)
cdef extern void get_Olson_eco(float *, float *, int, char *, unsigned char *)
cdef extern void get_GEOS(float *, float *, int, char *, char *, char *, char *, char *, char *, char *,
float *, float *, float *, float *, float *, float *)
cdef extern void snow_mask(char *, unsigned char)
cdef extern float cithr(int, float, float)
cdef extern void check_reg_uniformity(int, int, int, int, int, unsigned char, unsigned char *,
float *, float *, unsigned char *, int *, int *, int *, int *)
import cython
from cython.view cimport array as cvarray
import numpy as np
cimport numpy as np
np.import_array()
ctypedef np.float_t DTYPE_t
DTYPE = np.float
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_get_Reynolds_SST(np.ndarray[float, ndim=1] lat,
np.ndarray[float, ndim=1] lon, res,
char *anc_dir, char *sst_file, sst):
# cdef np.ndarray sst = np.zeros((3232*3200, ), order='C', dtype=np.float32)
if not sst.flags['C_CONTIGUOUS']:
sst = np.ascontiguousarray(sst)
cdef float[::1] sst_mv = sst
get_Reynolds_SST(&lat[0], &lon[0], res, anc_dir, sst_file, &sst_mv[0])
return sst
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_get_NDVI_background(np.ndarray[float, ndim=1] lat,
np.ndarray[float, ndim=1] lon, res,
char *anc_dir, char *ndvi_file, ndvi):
if not ndvi.flags['C_CONTIGUOUS']:
ndvi = np.ascontiguousarray(ndvi)
cdef float[::1] ndvi_mv = ndvi
get_NDVI_background(&lat[0], &lon[0], res, anc_dir, ndvi_file, &ndvi_mv[0])
return ndvi
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_get_Olson_eco(np.ndarray[float, ndim=1] lat,
np.ndarray[float, ndim=1] lon, res,
char *anc_dir, eco):
if not eco.flags['C_CONTIGUOUS']:
eco = np.ascontiguousarray(eco)
cdef unsigned char[::1] eco_mv = eco
get_Olson_eco(&lat[0], &lon[0], res, anc_dir, &eco_mv[0])
return eco
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_get_GEOS(np.ndarray[float, ndim=1] lat, np.ndarray[float, ndim=1] lon, int res, char *startTime,
char *anc_dir, char *geos1, char *geos2, char *geos_lnd, char *geos_ocn, char *geos_cnst,
geos_data):
for v in geos_data:
if not geos_data[v].flags['C_CONTIGUOUS']:
geos_data[v] = np.ascontiguousarray(geos_data[v])
cdef float[::1] tpw_mv = geos_data['tpw']
cdef float[::1] snowfr_mv = geos_data['snowfr']
cdef float[::1] icefr_mv = geos_data['icefr']
cdef float[::1] ocnfr_mv = geos_data['ocnfr']
cdef float[::1] landicefr_mv = geos_data['landicefr']
cdef float[::1] sfct_mv = geos_data['sfct']
get_GEOS(&lat[0], &lon[0], res, startTime, anc_dir, geos1, geos2, geos_lnd, geos_ocn, geos_cnst,
&tpw_mv[0], &snowfr_mv[0], &icefr_mv[0], &ocnfr_mv[0], &landicefr_mv[0], &sfct_mv[0])
geos_dict = {'tpw': geos_data['tpw'],
'snowfr': geos_data['snowfr'],
'icefr': geos_data['icefr'],
'ocnfr': geos_data['ocnfr'],
'landicefr': geos_data['landicefr'],
'sfct': geos_data['sfct']
}
return geos_dict
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_snow_mask(char *satname, unsigned char lsf):
# need to have for loop here to compute all the pixels since the function, as with everything else,
# is run per pixel.
pass
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_cithr(int key, np.ndarray[float, ndim=1] sec_vza, np.ndarray[float, ndim=1] bt11):
cdef np.ndarray tci_thr = np.zeros((bt11.shape[0], ), order='C', dtype=np.float)
for i in range(bt11.shape[0]):
tci_thr[i] = cithr(key, sec_vza[i], bt11[i])
return tci_thr
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def py_check_reg_uniformity(np.ndarray[unsigned char, ndim=2] eco_type, np.ndarray[unsigned char, ndim=2] eco,
np.ndarray[float, ndim=2] snowfr,
np.ndarray[float, ndim=2] icefr, np.ndarray[unsigned char, ndim=2] lsf):
cdef int coast, land, water, loc_uniform
cdef int i, j
cdef np.ndarray coast_r = np.zeros((eco_type.shape[0], eco_type.shape[1]), order='C', dtype=np.int32)
cdef np.ndarray land_r = np.zeros((eco_type.shape[0], eco_type.shape[1]), order='C', dtype=np.int32)
cdef np.ndarray water_r = np.zeros((eco_type.shape[0], eco_type.shape[1]), order='C', dtype=np.int32)
cdef np.ndarray loc_uniform_r = np.zeros((eco_type.shape[0], eco_type.shape[1]), order='C', dtype=np.int32)
cdef int[:, ::1] coast_mv = coast_r
cdef int[:, ::1] land_mv = land_r
cdef int[:, ::1] water_mv = water_r
cdef int[:, ::1] loc_uniform_mv = loc_uniform_r
lines = eco_type.shape[0]
eles = eco_type.shape[1]
for i in range(lines):
for j in range(eles):
if (i == 0 or i == lines):
line_edge = 1
else:
line_edge = 0
if (j == 0 or j == eles):
elem_edge = 1
else:
elem_edge = 0
check_reg_uniformity(eles, line_edge, elem_edge, i, j, eco_type[i][j],
&eco[0, 0], &snowfr[0, 0], &icefr[0, 0], &lsf[0, 0],
&coast, &land, &water, &loc_uniform)
coast_mv[i][j] = coast
land_mv[i][j] = land
water_mv[i][j] = water
loc_uniform_mv[i][j] = loc_uniform
scene_uniformity = {'coast': coast_r,
'land': land_r,
'water': water_r,
'loc_uniform': loc_uniform_r}
return scene_uniformity