Package MDSplus :: Module mdsdata
[frames] | no frames]

Source Code for Module MDSplus.mdsdata

  1  import numpy 
  2  import copy 
  3  from _tdishr import TdiEvaluate,TdiCompile,TdiDecompile 
  4  from _mdsdtypes import DTYPE_LIST,DTYPE_TUPLE,DTYPE_DICTIONARY 
  5   
6 -def getUnits(item):
7 """Return units of item. Evaluate the units expression if necessary. 8 @rtype: string""" 9 try: 10 return item.units 11 except: 12 return ""
13
14 -def getError(item):
15 """Return the data of the error of an object 16 @rtype: Data""" 17 try: 18 return item.error 19 except: 20 return None
21
22 -def getValuePart(item):
23 """Return the value portion of an object 24 @rtype: Data""" 25 try: 26 return Data.execute('value_of($)',item) 27 except: 28 return None
29
30 -def getDimension(item,idx=0):
31 """Return dimension of an object 32 @rtype: Data""" 33 try: 34 return Data.execute('dim_of($,$)',item,idx) 35 except: 36 return None
37
38 -def data(item):
39 """Return the data for an object converted into a primitive data type 40 @rtype: Data""" 41 return TdiCompile('data($)',(item,)).evaluate().value
42
43 -def decompile(item):
44 """Returns the item converted to a string 45 @rtype: string""" 46 return str(item)
47
48 -def evaluate(item,):
49 """Return evaluation of mdsplus object""" 50 try: 51 return item.evaluate() 52 except: 53 return item
54
55 -def rawPart(item):
56 """Return raw portion of data item""" 57 try: 58 return item.raw 59 except: 60 return None
61
62 -def makeData(value):
63 """Convert a python object to a MDSobject Data object""" 64 if value is None: 65 return EmptyData() 66 if isinstance(value,Data): 67 return value 68 if isinstance(value,numpy.generic) or isinstance(value,int) or isinstance(value,long) or isinstance(value,float) or isinstance(value,str): 69 from mdsscalar import makeScalar 70 return makeScalar(value) 71 if isinstance(value,tuple) or isinstance(value,list): 72 from apd import Apd,List 73 apd = Apd(tuple(value),DTYPE_LIST) 74 return List(apd) 75 if isinstance(value,numpy.ndarray): 76 from mdsarray import makeArray 77 return makeArray(value) 78 if isinstance(value,dict): 79 from apd import Dictionary 80 return Dictionary(value) 81 else: 82 raise TypeError,'Cannot make MDSplus data type from type: %s' % (str(type(value)),)
83
84 -class Data(object):
85 """Superclass used by most MDSplus objects. This provides default methods if not provided by the subclasses. 86 """ 87
88 - def __init__(self,*value):
89 """Cannot create instances of class Data objects. Use Data.makeData(initial-value) instead 90 @raise TypeError: Raised if attempting to create an instance of Data 91 @rtype: Data 92 """ 93 raise TypeError,'Cannot create \'Data\' instances'
94
95 - def __function(self,name,default):
96 found = False 97 ans=self.evaluate() 98 while(self is not ans and hasattr(ans,name) and callable(ans.__getattribute__(name))): 99 found = True 100 ans=ans.__getattribute__(name)() 101 if not found: 102 return default 103 return ans
104
105 - def value_of(self):
106 """Return value part of object 107 @rtype: Data""" 108 return Data.execute('value_of($)',self)
109
110 - def raw_of(self):
111 """Return raw part of object 112 @rtype: Data""" 113 return Data.execute('raw_of($)',self)
114
115 - def getDimensionAt(self,idx=0):
116 """Return dimension of object 117 @param idx: Index of dimension 118 @type idx: int 119 @rtype: Data""" 120 return Data.execute('dim_of($,$)',(self,idx))
121 122 dim_of=getDimensionAt 123
124 - def _getUnits(self):
125 return Data.execute('units($)',self)
126
127 - def _setUnits(self,units):
128 if units is None: 129 if hasattr(self,'_units'): 130 delattr(self,'_units') 131 else: 132 self._units=units 133 return
134 135 units=property(_getUnits,_setUnits) 136 """ 137 The units of the Data instance. 138 @type: String 139 """ 140
141 - def _getError(self):
142 return Data.execute('error_of($)',self)
143
144 - def _setError(self,error):
145 if error is None: 146 if hasattr(self,'_error'): 147 delattr(self,'_error') 148 else: 149 self._error=error 150 return
151 152 error=property(_getError,_setError) 153 """ 154 The error vector to associate with the data. 155 @type: Data 156 """ 157
158 - def _getHelp(self):
159 return Data.execute('help_of($)',self)
160
161 - def _setHelp(self,help):
162 if help is None: 163 if hasattr(self,'_help'): 164 delattr(self,'_help') 165 else: 166 self._help=help 167 return
168 169 help=property(_getHelp,_setHelp) 170 """ 171 The help string associated with the data. 172 @type: String 173 """ 174
175 - def _getValidation(self):
176 return Data.execute('validation_of($)',self)
177
178 - def _setValidation(self,validation):
179 if validation is None: 180 if hasattr(self,'_validation'): 181 delattr(self,'_validation') 182 else: 183 self._validation=validation 184 return
185 186 validation=property(_getValidation,_setValidation) 187 """ 188 A validation procedure for the data. 189 Currently no built-in utilities make use of this validation property. 190 One could envision storing an expression which tests the data and returns 191 a result. 192 @type: Data 193 """ 194
195 - def units_of(self):
196 """Return units part of the object 197 @rtype: Data""" 198 return Data.execute('units_of($)',self)
199
200 - def push_dollar_value(self):
201 """Set $value for expression evaluation 202 @rtype: None""" 203 pass
204
205 - def pop_dollar_value(self):
206 """Pop $value for expression evaluation 207 @rtype: Data""" 208 pass
209
210 - def __abs__(self):
211 """ 212 Absolute value: x.__abs__() <==> abs(x) 213 @rtype: Data 214 """ 215 return Data.execute('abs($)',self)
216
217 - def bool(self):
218 """ 219 Return boolean 220 @rtype: Bool 221 """ 222 from mdsarray import Array 223 from compound import Compound 224 if isinstance(self,Array): 225 return self._value!=0 226 elif isinstance(self,Compound) and hasattr(self,'value'): 227 return self.value.bool() 228 else: 229 ans=int(self) 230 return (ans & 1) == 1
231
232 - def __add__(self,y):
233 """ 234 Add: x.__add__(y) <==> x+y 235 @rtype: Data""" 236 if isinstance(y,Data): 237 return Data.execute('$+$',self,y) 238 else: 239 return self+makeData(y)
240
241 - def __and__(self,y):
242 """And: x.__and__(y) <==> x&y 243 @rtype: Data""" 244 return Data.execute('$ & $',self,y)
245
246 - def __div__(self,y):
247 """Divide: x.__div__(y) <==> x/y 248 @rtype: Data""" 249 return Data.execute('$/$',self,y)
250
251 - def __eq__(self,y):
252 """Equals: x.__eq__(y) <==> x==y 253 @rtype: Bool""" 254 return Data.execute('$ == $',self,y).bool()
255
256 - def __float__(self):
257 """Float: x.__float__() <==> float(x) 258 @rtype: Data""" 259 return float(Data.execute('float($)[0]',self).value)
260
261 - def __floordiv__(self,y):
262 """Floordiv: x.__floordiv__(y) <==> x//y 263 @rtype: Data""" 264 return Data.execute('floor($/$)',self,y)
265
266 - def __ge__(self,y):
267 """Greater or equal: x.__ge__(y) <==> x>=y 268 @rtype: Bool""" 269 return Data.execute('$ >= $',self,y).bool()
270
271 - def __getitem__(self,y):
272 """Subscript: x.__getitem__(y) <==> x[y] 273 @rtype: Data""" 274 from mdsarray import Array 275 ans = Data.execute('$[$]',self,y) 276 if isinstance(ans,Array): 277 if ans.shape[0]==0: 278 raise IndexError 279 return ans
280
281 - def __gt__(self,y):
282 """Greater than: x.__gt__(y) <==> x>y 283 @rtype: Bool""" 284 return Data.execute('$ > $',self,y).bool()
285
286 - def __int__(self):
287 """Integer: x.__int__() <==> int(x) 288 @rtype: int""" 289 return int(self.getInt().value)
290
291 - def __invert__(self):
292 """Binary not: x.__invert__() <==> ~x 293 @rtype: Data""" 294 return Data.execute('~$',self)
295
296 - def __le__(self,y):
297 """Less than or equal: x.__le__(y) <==> x<=y 298 @rtype: Bool""" 299 return Data.execute('$<=$',self,y).bool()
300
301 - def __len__(self):
302 """Length: x.__len__() <==> len(x) 303 @rtype: Data 304 """ 305 return int(TdiCompile('size($)',(self,)).data())
306
307 - def __long__(self):
308 """Convert this object to python long 309 @rtype: long""" 310 return long(self.getLong()._value)
311
312 - def __lshift__(self,y):
313 """Lrft binary shift: x.__lshift__(y) <==> x<<y 314 @rtype: Data""" 315 return Data.execute('$<<$',self,y)
316
317 - def __lt__(self,y):
318 """Less than: x.__lt__(y) <==> x<y 319 @rtype: Bool""" 320 return Data.execute('$<$',self,y).bool()
321
322 - def __mod__(self,y):
323 """Modulus: x.__mod__(y) <==> x%y 324 @rtype: Data""" 325 return Data.execute('$ mod $',self,y)
326
327 - def __mul__(self,y):
328 """Multiply: x.__mul__(y) <==> x*y 329 @rtype: Data""" 330 return Data.execute('$ * $',self,y)
331
332 - def __ne__(self,y):
333 """Not equal: x.__ne__(y) <==> x!=y 334 @rtype: Data""" 335 return Data.execute('$ != $',self,y).bool()
336
337 - def __neg__(self):
338 """Negation: x.__neg__() <==> -x 339 @rtype: Data""" 340 return Data.execute('-$',self)
341
342 - def __nonzero__(self):
343 """Not equal 0: x.__nonzero__() <==> x != 0 344 @rtype: Bool""" 345 return Data.execute('$ != 0',self).bool()
346
347 - def __or__(self,y):
348 """Or: x.__or__(y) <==> x|y 349 @rtype: Data""" 350 return Data.execute('$ | $',self,y)
351
352 - def __pos__(self):
353 """Unary plus: x.__pos__() <==> +x 354 @rtype: Data""" 355 return self
356
357 - def __radd__(self,y):
358 """Reverse add: x.__radd__(y) <==> y+x 359 @rtype: Data""" 360 if isinstance(y,Data): 361 return Data.execute('$+$',y,self) 362 else: 363 return makeData(y)+self
364
365 - def __rdiv__(self,y):
366 """Reverse divide: x.__rdiv__(y) <==> y/x 367 @rtype: Data""" 368 return Data.execute('$/$',y,self)
369
370 - def __rfloordiv__(self,y):
371 """x.__rfloordiv__(y) <==> y//x 372 @rtype: Data""" 373 return Data.execute('floor($/$)',y,self)
374
375 - def __rlshift__(self,y):
376 """Reverse left binary shift: x.__rlshift__(y) <==> y<<x 377 @rtype: Data""" 378 return Data.execute('$ << $',self,y)
379
380 - def __rmod__(self,y):
381 """Reverse modulus: x.__rmod__(y) <==> y%x 382 @rtype: Data""" 383 return Data.execute('$%x',y,self)
384 385 __rmul__=__mul__ 386 """Reverse multiply: x.__rmul__(y) <==> y*x 387 @type: Data""" 388 389 __ror__=__or__ 390 """Reverse or: x.__ror__(y) <==> y|x 391 @type: Data""" 392
393 - def __rrshift__(self,y):
394 """Reverse right binary shift: x.__rrshift__(y) <==> y>>x 395 @rtype: Data""" 396 return Data.execute('$ >> $',y,self)
397
398 - def __rshift__(self,y):
399 """Right binary shift: x.__rshift__(y) <==> x>>y 400 @rtype: Data 401 """ 402 return Data.execute('$ >> $',self,y)
403
404 - def __rsub__(self,y):
405 """Reverse subtract: x.__rsub__(y) <==> y-x 406 @rtype: Data""" 407 return Data.execute('$ - $',y,self)
408
409 - def __rxor__(self,y):
410 """Reverse xor: x.__rxor__(y) <==> y^x 411 @rtype: Data""" 412 return Data.execute('$^$',y,self)
413
414 - def __sub__(self,y):
415 """Subtract: x.__sub__(y) <==> x-y 416 @rtype: Data""" 417 return Data.execute('$ - $',self,y)
418
419 - def __xor__(self,y):
420 """Xor: x.__xor__(y) <==> x^y 421 @rtype: Data""" 422 return Data.execute('$^$',self,y)
423
424 - def _getDescriptor(self):
425 """Return descriptor for passing data to MDSplus library routines. 426 @rtype: descriptor 427 """ 428 from _descriptor import descriptor 429 return descriptor(self)
430 431 descriptor=property(_getDescriptor) 432 """Descriptor of data. 433 @type: descriptor 434 """ 435
436 - def compile(expr, *args):
437 """Static method (routine in C++) which compiles the expression (via TdiCompile()) 438 and returns the object instance correspondind to the compiled expression. 439 @rtype: Data 440 """ 441 return TdiCompile(expr,args)
442 compile=staticmethod(compile) 443
444 - def execute(expr,*args):
445 """Execute and expression inserting optional arguments into the expression before evaluating 446 @rtype: Data""" 447 return TdiCompile(expr,args).evaluate()
448 execute=staticmethod(execute) 449
450 - def setTdiVar(self,tdivarname):
451 """Set tdi public variable with this data 452 @param tdivarname: The name of the public tdi variable to create 453 @type tdivarname: string 454 @rtype: Data 455 @return: Returns new value of the tdi variable 456 """ 457 #from compound import Function 458 #return Function(opcode='equals',args=(Function(opcode='public',args=(str(tdivarname),)),self)).evaluate() 459 return self.execute("`public "+str(tdivarname)+"=$",self)
460
461 - def getTdiVar(tdivarname):
462 """Get value of tdi public variable 463 @param tdivarname: The name of the publi tdi variable 464 @type tdivarname: string 465 @rtype: Data""" 466 from compound import Function 467 try: 468 return Function(opcode='public',args=(str(tdivarname),)).evaluate() 469 except: 470 return None
471 getTdiVar=staticmethod(getTdiVar) 472
473 - def decompile(self):
474 """Return string representation 475 @rtype: string 476 """ 477 return TdiDecompile(self)
478 479 __str__=decompile 480 """String: x.__str__() <==> str(x) 481 @type: String""" 482 483 __repr__=decompile 484 """Representation""" 485 486
487 - def data(self):
488 """Return primitimive value of the data. 489 @rtype: Scalar,Array 490 """ 491 return self.execute("data($)",(self,)).value
492
493 - def evaluate(self):
494 """Return the result of TDI evaluate(this). 495 @rtype: Data 496 """ 497 return TdiEvaluate(self)
498
499 - def _isScalar(x):
500 """Is item a Scalar 501 @rtype: Bool""" 502 from mdsscalar import Scalar 503 return isinstance(x,Scalar)
504 _isScalar=staticmethod(_isScalar) 505
506 - def getByte(self):
507 """Convert this data into a byte. Implemented at this class level by returning TDI 508 data(BYTE(this)). If data() fails or the returned class is not scalar, 509 generate an exception. 510 @rtype: Int8 511 @raise TypeError: Raised if data is not a scalar value 512 """ 513 ans=Data.execute('byte($)',self) 514 if not Data._isScalar(ans): 515 raise TypeError,'Value not a scalar, %s' % str(type(self)) 516 return ans
517
518 - def getShort(self):
519 """Convert this data into a short. Implemented at this class level by returning TDI 520 data(WORD(this)).If data() fails or the returned class is not scalar, generate 521 an exception. 522 @rtype: Int16 523 @raise TypeError: Raised if data is not a scalar value 524 """ 525 ans=Data.execute('word($)',self) 526 if not Data._isScalar(ans): 527 raise TypeError,'Value not a scalar, %s' % str(type(self)) 528 return ans
529
530 - def getInt(self):
531 """Convert this data into a int. Implemented at this class level by returning TDI 532 data(LONG(this)).If data() fails or the returned class is not scalar, generate 533 an exception. 534 @rtype: Int32 535 @raise TypeError: Raised if data is not a scalar value 536 """ 537 ans=Data.execute('long($)',self) 538 if not Data._isScalar(ans): 539 raise TypeError,'Value not a scalar, %s' % str(type(self)) 540 return ans
541
542 - def getLong(self):
543 """Convert this data into a long. Implemented at this class level by returning TDI 544 data(QUADWORD(this)).If data() fails or the returned class is not scalar, 545 generate an exception. 546 @rtype: Int64 547 @raise TypeError: if data is not a scalar value 548 """ 549 ans=Data.execute('quadword($)',self) 550 if not Data._isScalar(ans): 551 raise TypeError,'Value not a scalar, %s' % str(type(self)) 552 return ans
553
554 - def getFloat(self):
555 """Convert this data into a float32. Implemented at this class level by returning TDI 556 data(F_FLOAT(this)).If data() fails or the returned class is not scalar, 557 generate an exception. 558 @rtype: Float32 559 @raise TypeError: Raised if data is not a scalar value 560 """ 561 ans=Data.execute('float($)',self) 562 if not Data._isScalar(ans): 563 raise TypeError,'Value not a scalar, %s' % str(type(self)) 564 return ans
565
566 - def getDouble(self):
567 """Convert this data into a float64. Implemented at this class level by returning TDI 568 data(FT_FLOAT(this)). If data() fails or the returned class is not scalar, 569 generate an exception. 570 @rtype: Float64 571 @raise TypeError: Raised if data is not a scalar value 572 """ 573 ans=Data.execute('ft_float($)',self) 574 if not Data._isScalar(ans): 575 raise TypeError,'Value not a scalar, %s' % str(type(self)) 576 return ans
577
578 - def getShape(self):
579 """Get the array dimensions as an integer array. It is implemented at this class 580 level by computing TDI expression SHAPE(this). If shape fails an exception is 581 generated. 582 @rtype: Int32Array 583 """ 584 return Data.execute('shape($)',self)
585
586 - def getByteArray(self):
587 """Convert this data into a byte array. Implemented at this class level by 588 returning TDI data(BYTE(this)). If data() fails or the returned class is not 589 array, generates an exception. In Java and C++ will return a 1 dimensional 590 array using row-first ordering if a multidimensional array. 591 @rtype: Int8Array 592 """ 593 return Data.execute('byte($)',self)
594
595 - def getShortArray(self):
596 """Convert this data into a short array. Implemented at this class level by 597 returning TDI data(WORD(this)). If data() fails or the returned class is not 598 array, generates an exception. In Java and C++ will return a 1 dimensional 599 array using row-first ordering if a multidimensional array. 600 @rtype: Int16Array 601 """ 602 return Data.execute('word($)',self)
603
604 - def getIntArray(self):
605 """Convert this data into a int array. Implemented at this class level by 606 returning TDI data (LONG(this)). If data() fails or the returned class is not 607 array, generates an exception. In Java and C++ will return a 1 dimensional 608 array using row-first ordering if a multidimensional array. 609 @rtype: Int32Array 610 """ 611 return Data.execute('long($)',self)
612
613 - def getLongArray(self):
614 """Convert this data into a long array. Implemented at this class level by 615 returning TDI data(QUADWORD(this)). If data() fails or the returned class is 616 not array, generates an exception. In Java and C++ will return a 1 dimensional 617 array using row-first ordering if a multidimensional array. 618 @rtype: Int64Array 619 """ 620 return Data.execute('quadword($)',self)
621
622 - def getString(self):
623 """Convert this data into a STRING. Implemented at this class level by returning 624 TDI data((this)). If data() fails or the returned class is not string, 625 generates an exception. 626 @rtype: String 627 """ 628 return str(Data.execute('text($)',self))
629
630 - def getUnits(self):
631 """Return the TDI evaluation of UNITS_OF(this). EmptyData is returned if no units 632 defined. 633 @rtype: Data 634 """ 635 return self.units
636
637 - def getHelp(self):
638 """Returns the result of TDI GET_HELP(this). Returns EmptyData if no help field 639 defined. 640 @rtype: Data 641 """ 642 return self.help
643
644 - def getError(self):
645 """Get the error field. Returns EmptyData if no error defined. 646 @rtype: Data 647 """ 648 return self.error
649
650 - def setUnits(self,units):
651 """Set units 652 @rtype: None 653 """ 654 self.units=units
655
656 - def setHelp(self,help):
657 """Set the Help field for this Data instance. 658 @rtype: None 659 """ 660 self.help=help
661
662 - def setError(self,error):
663 """Set the Error field for this Data instance. 664 @rtype: None 665 """ 666 self.error=error
667
668 - def mayHaveChanged(self):
669 """return true if the represented data could have been changed since the last time 670 this method has been called. 671 @rtype: Bool 672 """ 673 return True
674
675 - def sind(self):
676 """Return sin() of data assuming data is in degrees 677 @rtype: Float32Array 678 """ 679 return Data.execute('sind($)',self)
680
681 - def serialize(self):
682 """Return Uint8Array binary representation. 683 @rtype: Uint8Array 684 """ 685 return Data.execute('SerializeOut($)',self)
686
687 - def deserialize(data):
688 """Return Data from serialized buffer. 689 @param data: Buffer returned from serialize. 690 @type data: Uint8Array 691 @rtype: Data 692 """ 693 return Data.execute('SerializeIn($)',data)
694 deserialize=staticmethod(deserialize)
695
696 -class EmptyData(Data):
697 """No Value"""
698 - def __init__(self):
699 pass
700
701 - def __str__(self):
702 return "<no-data>"
703 pass
704