NumPtr.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """
  2. Copyright 2003,2004 Christian Dieterich
  3. rca@geosci.uchicago.edu
  4. Department of the Geophysical Sciences, University of Chicago
  5. 2003
  6. The Numeric Pointer Module is free software; you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation; either version 2 of the License,
  9. or (at your option) any later version.
  10. The Numeric Pointer Module is distributed in the hope that it will be
  11. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with the Numeric Pointer Module; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. """
  18. import _NumPtr
  19. def _swig_setattr(self,class_type,name,value):
  20. if (name == "this"):
  21. if isinstance(value, class_type):
  22. self.__dict__[name] = value.this
  23. if hasattr(value,"thisown"): self.__dict__["thisown"] = value.thisown
  24. del value.thisown
  25. return
  26. method = class_type.__swig_setmethods__.get(name,None)
  27. if method: return method(self,value)
  28. self.__dict__[name] = value
  29. def _swig_getattr(self,class_type,name):
  30. method = class_type.__swig_getmethods__.get(name,None)
  31. if method: return method(self)
  32. raise AttributeError,name
  33. import types
  34. try:
  35. _object = types.ObjectType
  36. _newclass = 1
  37. except AttributeError:
  38. class _object : pass
  39. _newclass = 0
  40. def getpointer(A):
  41. """
  42. Returns a SWIG pointer to the data in NumPy array object A.
  43. Array can be 1, 2 or 3 dimensional and must be of type Float64,
  44. Float32 or Int32.
  45. """
  46. if (len(A.shape) == 0):
  47. if (A.typecode() == 'd'):
  48. p = _NumPtr.getdpointer1(A)
  49. elif (A.typecode() == 'f'):
  50. p = _NumPtr.getfpointer1(A)
  51. elif (A.typecode() == 'i'):
  52. p = _NumPtr.getipointer1(A)
  53. else:
  54. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  55. return p
  56. elif (len(A.shape) == 1):
  57. if (A.typecode() == 'd'):
  58. p = _NumPtr.getdpointer1(A)
  59. elif (A.typecode() == 'f'):
  60. p = _NumPtr.getfpointer1(A)
  61. elif (A.typecode() == 'i'):
  62. p = _NumPtr.getipointer1(A)
  63. else:
  64. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  65. return p
  66. elif (len(A.shape) == 2):
  67. if (A.typecode() == 'd'):
  68. p = _NumPtr.getdpointer2(A)
  69. elif (A.typecode() == 'f'):
  70. p = _NumPtr.getfpointer2(A)
  71. elif (A.typecode() == 'i'):
  72. p = _NumPtr.getipointer2(A)
  73. else:
  74. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  75. return p
  76. elif (len(A.shape) == 3):
  77. if (A.typecode() == 'd'):
  78. p = _NumPtr.getdpointer3(A)
  79. elif (A.typecode() == 'f'):
  80. p = _NumPtr.getfpointer3(A)
  81. elif (A.typecode() == 'i'):
  82. p = _NumPtr.getipointer3(A)
  83. else:
  84. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  85. return p
  86. else:
  87. raise TypeError, "Your array has rank > 3."
  88. def verifypointer(Aptr, n=0, m=None, l=None):
  89. """
  90. Prints the data in NumPy array object A, where Aptr points to.
  91. Array can be 1, 2 or 3 dimensional and must be of type Float64,
  92. Float32 or Int32.
  93. """
  94. if (Aptr.count('_p') == 0):
  95. if (Aptr.count('_double') == 1):
  96. p = _NumPtr.testd1(Aptr, n)
  97. elif (Aptr.count('_float') == 1):
  98. p = _NumPtr.testf1(Aptr, n)
  99. elif (Aptr.count('_int') == 1):
  100. p = _NumPtr.testi1(Aptr, n)
  101. else:
  102. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  103. return p
  104. elif (Aptr.count('_p') == 1):
  105. if (Aptr.count('_double') == 1):
  106. p = _NumPtr.testd1(Aptr, n)
  107. elif (Aptr.count('_float') == 1):
  108. p = _NumPtr.testf1(Aptr, n)
  109. elif (Aptr.count('_int') == 1):
  110. p = _NumPtr.testi1(Aptr, n)
  111. else:
  112. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  113. return p
  114. elif (Aptr.count('_p') == 2):
  115. if m == None: m = 0
  116. if (Aptr.count('_double') == 1):
  117. p = _NumPtr.testd2(Aptr, n, m)
  118. elif (Aptr.count('_float') == 1):
  119. p = _NumPtr.testf2(Aptr, n, m)
  120. elif (Aptr.count('_int') == 1):
  121. p = _NumPtr.testi2(Aptr, n, m)
  122. else:
  123. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  124. return p
  125. elif (Aptr.count('_p') == 3):
  126. if m == None: m = 0
  127. if l == None: l = 0
  128. if (Aptr.count('_double') == 1):
  129. p = _NumPtr.testd3(Aptr, n, m, l)
  130. elif (Aptr.count('_float') == 1):
  131. p = _NumPtr.testf3(Aptr, n, m, l)
  132. elif (Aptr.count('_int') == 1):
  133. p = _NumPtr.testi3(Aptr, n, m, l)
  134. else:
  135. raise TypeError, "Your array does not have typecode 'd', 'f' or 'i'."
  136. return p
  137. else:
  138. raise TypeError, "Your array has rank greater than 3."
  139. def getpointer1(A):
  140. return getpointer(A)
  141. def getpointer2(A):
  142. return getpointer(A)
  143. def getpointer3(A):
  144. return getpointer(A)