 Timestamp:
 31/05/07 15:04:34 (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

TI02CSML/trunk/csml/csmllibs/csmldataiface.py
r2526 r2544 597 597 # Read the data from the raw file into a multidimensional Numeric array 598 598 def readFile(self, **kwargs): 599 # Valid values for numericType and their associated struct format 600 # character: 601 numericLookup = { 602 'uint8':'B', 'uint16':'H', 'uint32':'I', 'uint64':'Q', 603 'int8':'b', 'int16':'h', 'int32':'i', 'int64':'q', 604 'float':'f', 'double':'d' 605 } 606 607 # Valid values for endianness and their associated struct format 608 # character: 609 endiannessLookup = { 610 'big':'>', 'little':'<', 'host':'=' 611 } 612 613 # Build the desired format string: 614 try: 615 format_str = endiannessLookup[kwargs['endianness']] 616 format_str += str(reduce(lambda a,b:a*b, kwargs['dimensions'])) 617 format_str += numericLookup[kwargs['numericType']] 618 except KeyError, TypeError: 619 raise TypeError("Missing or invalid parameters. Required: endianness=str, dimensions=list and numericType=str") 620 621 # Read and unpack data into a Numeric array. 622 self.data = Numeric.array(struct.unpack(format_str, self.file.read())) 623 self.data.shape = tuple(map(int, kwargs['dimensions'])) 624 625 # If numericTransform or fillValue were provided, store them as 626 # attributes. 627 if 'numericTransform' in kwargs: 628 self.numericTransform = NumericTransform.infixExpression(kwargs['numericTransform']) 629 if 'fillValue' in kwargs: 630 self.fillValue = kwargs['fillValue'] 631 632 599 # Determine the numeric type: 600 if 'numericType' in kwargs: 601 try: 602 numericTypeCode = { 603 'uint8':Numeric.UInt8, 604 'uint16':Numeric.UInt16, 605 'uint32':Numeric.UInt32, 606 'int8':Numeric.Int8, 607 'int16':Numeric.Int16, 608 'int32':Numeric.Int32, 609 'float':Numeric.Float32, 610 'double':Numeric.Float64 611 }[kwargs['numericType']] 612 except KeyError: 613 raise TypeError("Invalid numericType: " + str(kwargs['numericType'])) 614 else: 615 raise KeyError("numericType is mandatory.") 616 617 # Read the file into a numpy array: 618 self.data = Numeric.fromstring(self.file.read(), numericTypeCode) 619 # If necessary, swap the byte order: 620 if 'endianess' in kwargs: 621 if ((kwargs['endianness'] == 'little' and not Numeric.LittleEndian) or (kwargs['endianness'] == 'big' and Numeric.LittleEndian)): 622 self.data = self.data.byteswapped() 623 # Declare the shape of the array: 624 dimensions = map(int,kwargs['dimensions']) 625 dimensions.reverse() 626 self.data.shape = tuple(dimensions) 627 # If numericTransform or fillValue were provided, store them as 628 # attributes. 629 if 'numericTransform' in kwargs: 630 self.numericTransform = NumericTransform.infixExpression(kwargs['numericTransform']) 631 if 'fillValue' in kwargs: 632 self.fillValue = kwargs['fillValue'] 633 633 634 # Return the fill value, if set, and transform if necessary: 634 635 def getFillValue(self): … … 673 674 # Requested upper bound of subset is beyond the size of the the full 674 675 # data array, so raise an exception 675 raise IndexE xception("Subset out of range")676 raise IndexError("Subset out of range") 676 677 elif Numeric.sometrue(Numeric.less( kwargs['upper'], Numeric.zeros(len(self.data.shape)))): 677 678 # Requested lower bound of subset is beyond the size of the the full 678 679 # data array, so raise an exception 679 raise IndexE xception("Subset out of range")680 raise IndexError("Subset out of range") 680 681 elif Numeric.sometrue(Numeric.less_equal(kwargs['upper'], kwargs['lower'])): 681 682 # lower bound <= upper_bound for at least one dimension, so raise an 682 683 # exception 683 raise IndexE xception("Upper bound less than lower bound")684 raise IndexError("Upper bound less than lower bound") 684 685 elif tuple(kwargs['lower']) == (0,)*len(self.data.shape) and tuple(kwargs['upper']) == self.data.shape: 685 686 # Special case of requested subset == entire data file.
Note: See TracChangeset
for help on using the changeset viewer.