dtype.pyx 1.53 KB
Newer Older
1 2 3 4 5 6

cimport numpy as np
import numpy as np

import cython as ct

Mozul Rémy's avatar
Mozul Rémy committed
7
from libc.string cimport strcpy
8
from dtype cimport C_param, C_datatype, addOne, displayOne, changeOne
9 10 11 12 13 14

np.import_array()

cdef class P_datatype(object):

  cdef int id
15
  cdef C_datatype * f_image_ptr
16 17 18 19

  cdef public np.ndarray rtab

  @ct.locals(i=int)
20
  def __init__(self,nn,rv,iv):
21
    self.f_image_ptr = addOne(nn, rv, iv)
22
    self.rtab = np.PyArray_SimpleNewFromData(1, [self.f_image_ptr.itab[0]], np.NPY_DOUBLE, self.f_image_ptr.rtab)
23

Mozul Rémy's avatar
Mozul Rémy committed
24 25 26 27 28
    # beurk
    self.f_image_ptr.nickname[30] = '\0'
    for i in range(self.f_image_ptr.itab[1]):
      self.f_image_ptr.weird[i].field[30] = '\0'

29 30
  def fortranContent(self):
    displayOne(self.f_image_ptr)
31 32

  def fortranChange(self,nn,rv,iv):
33
    changeOne(self.f_image_ptr, nn, rv, iv)
34 35

  def cythonContent(self):
36 37 38 39 40 41 42 43
    print 'f_image_ptr :'
    print self.f_image_ptr.nickname
    print self.f_image_ptr.rsingle
    print self.f_image_ptr.isingle
    for i in range(self.f_image_ptr.itab[0]):
      print self.f_image_ptr.rtab[i]
    for i in range(self.f_image_ptr.itab[1]):
      print self.f_image_ptr.weird[i]
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
  def cythonChange(self,nn,rv,iv):
    #this is not possible because char[30] is a non-lvalue
    #self.f_image_ptr.nickname = nn
    strcpy(self.f_image_ptr.nickname, nn)
    self.f_image_ptr.rsingle  = rv
    self.f_image_ptr.isingle  = iv

  def getWeirdCopy(self,i):
    #as usual careful between Fortran/Python numbering
    if 0 < i <= self.f_image_ptr.itab[1]:
      return self.f_image_ptr.weird[i-1]
    else:
      raise IndexError