1  import types 
  2  import logging 
  3   
  4  try: 
  5      import numpy 
  6  except: 
  7      numpy = None 
  8   
  9  try: 
 10      import Numeric 
 11  except: 
 12      Numeric = None 
 13       
 14       
 15   
 16  (ARRAY_DOUBLE, ARRAY_FLOAT, ARRAY_LONG, ARRAY_ULONG, ARRAY_SHORT, \ 
 17   ARRAY_USHORT, ARRAY_CHAR, ARRAY_UCHAR, \ 
 18   ARRAY_STRING, ARRAY_NUMERIC) = (5,6,7,8,9,10,11,12,13,14) 
 19   
 20  (ARRAY_MIN, ARRAY_MAX) = (ARRAY_DOUBLE, ARRAY_STRING) 
 21   
 22  if numpy is not None: 
 23      SPEC_TO_NUM = { 
 24          ARRAY_CHAR   :  numpy.byte, 
 25          ARRAY_UCHAR  :  numpy.ubyte, 
 26          ARRAY_SHORT  :  numpy.short, 
 27          ARRAY_USHORT :  numpy.ushort, 
 28          ARRAY_LONG   :  numpy.int32, 
 29          ARRAY_ULONG  :  numpy.uint32, 
 30          ARRAY_FLOAT  :  numpy.float32, 
 31          ARRAY_DOUBLE :  numpy.float64 
 32          } 
 33   
 34      NUM_TO_SPEC = { 
 35          numpy.ubyte : ARRAY_CHAR, 
 36          numpy.uint : ARRAY_ULONG, 
 37          numpy.uint16 : ARRAY_USHORT, 
 38          numpy.uint32 : ARRAY_ULONG, 
 39          numpy.uint8 : ARRAY_CHAR, 
 40          numpy.ushort : ARRAY_USHORT, 
 41          numpy.short : ARRAY_SHORT, 
 42          numpy.int32 : ARRAY_LONG, 
 43          numpy.int8 : ARRAY_CHAR, 
 44          numpy.float : ARRAY_FLOAT, 
 45          numpy.float32 : ARRAY_FLOAT, 
 46          numpy.float64 : ARRAY_DOUBLE 
 47          } 
 49          return isinstance(data,numpy.ndarray) 
  50  else: 
 51      NUM_TO_SPEC = { 
 52          '1' :  ARRAY_CHAR, 
 53          'b' :  ARRAY_UCHAR, 
 54          's' :  ARRAY_SHORT, 
 55          'w' :  ARRAY_USHORT,  
 56          'l' :  ARRAY_LONG, 
 57          'u' :  ARRAY_ULONG,  
 58          'f' :  ARRAY_FLOAT, 
 59          'd' :  ARRAY_DOUBLE 
 60          } 
 61   
 62      SPEC_TO_NUM = { 
 63          ARRAY_CHAR   :  '1', 
 64          ARRAY_UCHAR  :  'b', 
 65          ARRAY_SHORT  :  's', 
 66          ARRAY_USHORT :  'w',   
 67          ARRAY_LONG   :  'l', 
 68          ARRAY_ULONG  :  'u',   
 69          ARRAY_FLOAT  :  'f', 
 70          ARRAY_DOUBLE :  'd' 
 71          } 
 72   
 74          return isinstance(data,Numeric.ArrayType) 
  75   
 78   
 79   
 81      return type(datatype) == types.IntType and datatype >= ARRAY_MIN and datatype <= ARRAY_MAX 
  82   
 83   
 85      if isinstance(data, SpecArrayData): 
 86           
 87          return SpecArrayData(data.data, data.type, data.shape) 
 88   
 89      if datatype == ARRAY_STRING: 
 90           
 91          newArray = filter(None, [x != chr(0) and x or None for x in data.split(chr(0))]) 
 92          return newArray 
 93      else: 
 94          newArray = None 
 95   
 96      if numpy is not None or Numeric is not None: 
 97          if IS_ARRAY(data) : 
 98               
 99               
100              if len(data.shape) > 2: 
101                  raise SpecArrayError, "Spec arrays cannot have more than 2 dimensions" 
102   
103              try: 
104                  if type(data) == numpy.ndarray: 
105                      numtype = data.dtype.type 
106                      datatype = NUM_TO_SPEC[numtype] 
107                  else: 
108                      numtype = data.typecode() 
109                      datatype = NUM_TO_SPEC[numtype] 
110              except KeyError: 
111                  data = '' 
112                  datatype = ARRAY_CHAR 
113                  rows = 0 
114                  cols = 0 
115                  logging.getLogger('SpecClient').error("Numerical type '%s' not supported" , numtype) 
116              else: 
117                  if len(data.shape) == 2: 
118                      rows, cols = data.shape 
119                  else: 
120                      rows, cols = 1, data.shape[0] 
121                  data = data.tostring() 
122   
123              newArray = SpecArrayData(data, datatype, (rows, cols)) 
124          else: 
125               
126               
127              try: 
128                  numtype = SPEC_TO_NUM[datatype] 
129              except: 
130                  raise SpecArrayError, 'Invalid Spec array type' 
131              else: 
132                  if numpy: 
133                      newArray = numpy.fromstring(data, dtype=numtype) 
134                  else: 
135                      newArray = Numeric.fromstring(data, numtype) 
136   
137                  if rows==1: 
138                    newArray.shape = (cols, ) 
139                  else: 
140                    newArray.shape = (rows, cols) 
141      else: 
142          if isArrayType(datatype): 
143              newArray = SpecArrayData(data, datatype) 
144          else: 
145              raise SpecArrayError, 'Invalid Spec array type' 
146   
147      return newArray 
 148   
149   
151 -    def __init__(self, data, datatype, shape): 
 152          self.data = data 
153          self.type = datatype 
154          self.shape = shape 
 155   
156   
158          return str(self.data) 
  159