default to pic and helpful
[sddekit.git] / python / hfill.pyx
blob7f40fdbc4fecad41fe75e70e29a3d7d5b5d92e76
1 # copyright 2016 Apache 2 sddekit authors
3 from sddekit_h cimport *
4 from sddekit cimport *
6 ctypedef struct hfill_py:
7 sd_hfill hf
8 void *fn
10 cdef sd_stat hfill_py_apply(sd_hfill *hfill, uint32_t n, double * t, uint32_t *i, double * buf):
11 cdef hfill_py *hp = <hfill_py*> hfill.ptr
12 cdef np.ndarray[np.float64_t, ndim=1] np_buf
13 t_ = np_of_ptr(n, t, np.NPY_DOUBLE, 0)
14 i_ = np_of_ptr(n, t, np.NPY_UINT64, 0)
15 try:
16 np_buf = (<object> hp.fn)(t_, i_).astype(np.float64)
17 except Exception as exc:
18 print exc # TODO
19 return SD_ERR
20 for j in xrange(n):
21 buf[j] = np_buf[j]
22 return SD_OK
24 cdef void hfill_py_free(sd_hfill *h):
25 free(h)
27 cdef sd_hfill * hfill_py_new(object fn):
28 cdef hfill_py *h = <hfill_py *> malloc(sizeof(hfill_py))
29 if not hasattr(fn, '__call__'):
30 raise TypeError('arg fn must be callable')
31 if h==NULL:
32 raise Exception('alloc hfill_py failed.')
33 h.fn = <void*> fn
34 h.hf.ptr = h
35 h.hf.apply = &hfill_py_apply
36 h.hf.free = &hfill_py_free
37 return &(h.hf)
39 # vim: sw=4 et