fabio.readbytestream

Package fabio :: Module readbytestream |
[hide private]
[frames] | no frames]

# Source Code for Module fabio.readbytestream

     1  #!/usr/bin/env python
     2  #coding: utf8
     3
     4  """
     5  Reads a bytestream
     6
     7  Authors: Jon Wright    Henning O. Sorensen & Erik Knudsen
     8           ESRF          Risoe National Laboratory
     9  """
    10
    11  import numpy, logging
    12  logger = logging.getLogger("readbytestream")
    13  DATATYPES = {
    14      # type  sign bytes
    15      ("int", 'n', 1) : numpy.uint8,
    16      ("int", 'n', 2) : numpy.uint16,
    17      ("int", 'n', 4) : numpy.uint32,
    18      ("int", 'y', 1) : numpy.int8,
    19      ("int", 'y', 2) : numpy.int16,
    20      ("int", 'y', 4) : numpy.int32,
    21      ('float', 'y', 4) : numpy.float32, # does this occur in bruker?
    22      ('double', 'y', 4): numpy.float64
    23      }
    24
    25
    26 -def readbytestream(fil,
    27                     offset,
    28                     x,
    29                     y,
    30                     nbytespp,
    31                     datatype='int',
    32                     signed='n',
    33                     swap='n',
    34                     typeout=numpy.uint16):
    35      """
    36      Reads in a bytestream from a file (which may be a string indicating
    37      a filename, or an already opened file (should be "rb"))
    38      offset is the position (in bytes) where the pixel data start
    39      nbytespp = number of bytes per pixel
    40      type can be int or float (4 bytes pp) or double (8 bytes pp)
    41      signed: normally signed data 'y', but 'n' to try to get back the
    42      right numbers when unsigned data are converted to signed
    43      (python once had no unsigned numeric types.)
    44      swap, normally do not bother, but 'y' to swap bytes
    45      typeout is the numpy type to output, normally uint16,
    46      but more if overflows occurred
    47      x and y are the pixel dimensions
    48
    49      TODO : Read in regions of interest
    50
    51      PLEASE LEAVE THE STRANGE INTERFACE ALONE -
    52      IT IS USEFUL FOR THE BRUKER FORMAT
    53      """
    54      tin = "dunno"
    55      length = nbytespp * x * y # bytes per pixel times number of pixels
    56      if datatype in ['float', 'double']:
    57          signed = 'y'
    58
    59      key = (datatype, signed, nbytespp)
    60      try:
    61          tin = DATATYPES[key]
    62      except:
    63          logging.warning("datatype,signed,nbytespp " + str(key))
    64          raise Exception("Unknown combination of types to readbytestream")
    65
    66      # Did we get a string (filename) or a readable stream object?
    67      if hasattr(fil, "read") and hasattr(fil, "seek"):
    68          infile = fil
    69          opened = False
    70      else:
    71          infile = open(fil, 'rb')
    72          opened = True
    73
    74      infile.seek(offset)
    75
    76      arr = numpy.array(numpy.reshape(
    77              numpy.fromstring(
    78                  infile.read(length), tin) , (x, y)), typeout)
    79
    80      if swap == 'y':
    81          arr = arr.byteswap()
    82
    83      if opened:
    84          infile.close()
    85
    86      return arr
    87