I am instantiating a class in cython. I want to declare one of the instance which is an array of float
values once by computing it with a given function and save it in a binary file using c functions. For the further call of my class if the input file does already exist, the instance would be declared by reading the array from the file otherwise it would compute it again.
import numpy as np
cimport numpy as np
cimport cython
from libc.stdio cimport FILE, fopen, fwrite, fscanf, fclose, fseek, SEEK_END, ftell, stdout, stderr
cdef extern from "math.h":
double exp(double) nogil
double log(double) nogil
cdef class Hit(object):
cdef public double[::1] zs, Da
cdef char* path
def __cinit__(self, zs=None, path=None):
if path is None:
raise ValueError("Could not find a path to the file which contains the table of distances")
else:
self.path=path
if zs is None:
raise ValueError("You must give an array which contains the steps!")
self.zs=zs
cdef Py_ssize_t i, N
N=len(self.zs)
cdef FILE *ptr_fr
cdef FILE *ptr_fw
cdef double[::1] ptr_d = np.empty((N,))
ptr_fr = fopen(self.path, "rb")
ptr_fw = fopen(self.path, "wb")
if (ptr_fr==NULL):
print "I/O Error: cannot open file {}".format( self.path)
for i from N > i >= 0:
ptr_d[i]=log(self.zs[i]+1.) /(1- self.zs[i])**0.5
if (ptr_fw == NULL):
print "Unable to open file!\n"
else:
print "Opened file successfully for writing.\n"
fwrite(<void*>&ptr_d[0], sizeof(double), N, ptr_fw)
fclose(ptr_fw)
self.Da = ptr_d
else:
for i from N > i >= 0:
fscanf(ptr_fr,"%f", &ptr_d[i])
fclose(ptr_fr)
self.Da = ptr_d
When I run my code for the second time the values that returns from reading a file to a pointer is correct, however I think the way I allocated the pointer to a memoryview has a problem since all the values in self.Da
instance are zeros. Any suggestion ?!!