~libecbufr-dev/libecbufr/trunk

« back to all changes in this revision

Viewing changes to Bindings/python/ecbufr/template.pyx

  • Committer: vanh.souvanlasy at canada
  • Date: 2019-11-05 21:50:59 UTC
  • Revision ID: vanh.souvanlasy@canada.ca-20191105215059-0a0yolehj3jcmm67
add python bindings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# file: ecbufr.pyx
 
2
 
 
3
from ecbufr.ctables cimport BUFR_Tables
 
4
from cpython.pycapsule cimport *
 
5
 
 
6
from ecbufr.tables import EntryTableB
 
7
from ecbufr.tables import EntryTableD
 
8
 
 
9
from ecbufr cimport ctemplate
 
10
from libc.stdlib cimport malloc, free
 
11
 
 
12
cdef class BUFR_Template:
 
13
   cdef ctemplate.BUFR_Template* _this_ptr
 
14
   cdef ctemplate.BUFR_Template* _this
 
15
 
 
16
   def __cinit__(self):
 
17
      self._this_ptr=NULL
 
18
      self._this=self._this_ptr
 
19
 
 
20
   def __dealloc__(self):
 
21
      if self._this_ptr is not NULL:
 
22
          ctemplate.bufr_free_template(self._this_ptr)
 
23
 
 
24
   cdef load(self,basestring filename, tbl):
 
25
      cdef BUFR_Tables  *tbl_ptr
 
26
 
 
27
      tobj=tbl.get_obj()
 
28
      tbl_ptr = <BUFR_Tables *>PyCapsule_GetPointer(tobj,"BUFR_Tables")
 
29
      self._this_ptr = ctemplate.bufr_load_template( filename.encode(), tbl_ptr )
 
30
 
 
31
   def save(self,basestring filename):
 
32
      ctemplate.bufr_save_template( filename.encode(), self._this )
 
33
 
 
34
   def allocate(self,BufrDescValue dv, tbl, int edition):
 
35
      cdef BUFR_Tables  *tbl_ptr
 
36
 
 
37
      nb=len(dv)
 
38
      print "nb=", nb
 
39
      print "edition=", edition
 
40
      tobj=tbl.get_obj()
 
41
      tbl_ptr = <BUFR_Tables *>PyCapsule_GetPointer(tobj,"BUFR_Tables")
 
42
      self._this_ptr = ctemplate.bufr_create_template(dv._this,nb,tbl_ptr,edition)
 
43
      if self._this_ptr is NULL:
 
44
         raise MemoryError()
 
45
      self._this=self._this_ptr
 
46
 
 
47
   def compare(self, BUFR_Template other):
 
48
      return ctemplate.bufr_compare_template( self._this, other._this )
 
49
 
 
50
   def copy(self):
 
51
      bt=BUFR_Template()
 
52
      bt._this_ptr = ctemplate.bufr_copy_template( self._this )
 
53
      bt._this = bt._this_ptr
 
54
      return bt
 
55
 
 
56
   def add(self,BufrDescValue dv,tbl):
 
57
      cdef ctemplate.BufrDescValue *descs
 
58
 
 
59
      descs = dv._this
 
60
      nb=len(dv)
 
61
      for i in range(nb):
 
62
         dsc=descs[i].descriptor
 
63
         tbe=tbl.fetch_tableB( dsc )
 
64
         if not isinstance(tbe,EntryTableB):
 
65
            tde=tbl.fetch_tableD( dsc )
 
66
            if not isinstance(tbe,EntryTableD):
 
67
               print "Error Not a Descriptor:",dsc
 
68
               raise ValueError()
 
69
 
 
70
      ctemplate.bufr_template_add_DescValue( self._this, dv._this, len(dv) )
 
71
 
 
72
   def finalize(self):
 
73
      ctemplate.bufr_finalize_template(self._this)
 
74
 
 
75
cdef class BufrDescValue:
 
76
   cdef ctemplate.BufrDescValue*  _this_ptr
 
77
   cdef ctemplate.BufrDescValue*  _this
 
78
   cdef int                       nb
 
79
 
 
80
   def __cinit__(self,int nb):
 
81
      self._this_ptr=NULL
 
82
      self._this=self._this_ptr
 
83
 
 
84
      cdef ctemplate.BufrDescValue *descs
 
85
 
 
86
      descs = <ctemplate.BufrDescValue *>malloc(nb*sizeof(BufrDescValue))
 
87
      if descs is NULL:
 
88
         raise MemoryError()
 
89
      self.nb = nb
 
90
      self._this_ptr=descs
 
91
      self._this=self._this_ptr
 
92
      for i in range(nb):
 
93
         ctemplate.bufr_init_DescValue ( &descs[i] )
 
94
 
 
95
 
 
96
   def __dealloc__(self):
 
97
      if self._this_ptr is not NULL:
 
98
          free(self._this_ptr)
 
99
 
 
100
   def __len__(self):
 
101
      return self.size()
 
102
 
 
103
   def size(self):
 
104
      return self.nb
 
105
 
 
106
   def set_desc(self,pos,desc):
 
107
      cdef ctemplate.BufrDescValue *descs
 
108
      descs = self._this
 
109
      descs[pos].descriptor=desc
 
110
 
 
111
   def get_desc(self,pos):
 
112
      cdef ctemplate.BufrDescValue *descs
 
113
      descs = self._this
 
114
      return descs[pos].descriptor