fabio.fit2dmaskimage

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

# Source Code for Module fabio.fit2dmaskimage

     1  ## Automatically adapted for numpy.oldnumeric Oct 05, 2007 by alter_code1.py
     2
     3  #!/usr/bin/env python
     4  """
     5
     6  Author: Andy Hammersley, ESRF
     7  Translation into python/fabio: Jon Wright, ESRF
     8  """
     9
    10  import numpy
    11
    12  from fabioimage import fabioimage
    13
    14
    15 -class fit2dmaskimage(fabioimage):
    16      """ Read and try to write Andy Hammersley's mask format """
    17
    18
    19 -    def _readheader(self, infile):
    20          """
    21          Read in a header from an already open file
    22          """
    23          # 1024 bytes gives 256x32 bit integers
    24          header = infile.read(1024)
    25          for i, j in [ ("M", 0),
    26                        ("A", 4),
    27                        ("S", 8),
    28                        ("K", 12)  ]:
    29              if header[j] != i:
    30                  raise Exception("Not a fit2d mask file")
    31          fit2dhdr = numpy.fromstring(header, numpy.int32)
    32          self.dim1 = fit2dhdr[4] # 1 less than Andy's fortran
    33          self.dim2 = fit2dhdr[5]
    34
    35
    36 -    def read(self, fname, frame=None):
    37          """
    38          Read in header into self.header and
    39              the data   into self.data
    40          """
    41          fin = self._open(fname)
    42          self._readheader(fin)
    43          # Compute image size
    44          self.bytecode = numpy.uint8
    45          self.bpp = len(numpy.array(0, self.bytecode).tostring())
    46
    47          # integer division
    48          num_ints = (self.dim1 + 31) // 32
    49          total = self.dim2 * num_ints * 4
    50          data = fin.read(total)
    51          assert len(data) == total
    52          fin.close()
    53
    54          # Now to unpack it
    55          data = numpy.fromstring(data, numpy.uint8)
    56          data = numpy.reshape(data, (self.dim2, num_ints * 4))
    57
    58          result = numpy.zeros((self.dim2, num_ints * 4 * 8), numpy.uint8)
    59
    60          # Unpack using bitwise comparisons to 2**n
    61          bits = numpy.ones((1), numpy.uint8)
    62          for i in range(8):
    63              temp = numpy.bitwise_and(bits, data)
    64              result[:, i::8] = temp.astype(numpy.uint8)
    65              bits = bits * 2
    66          # Extra rows needed for packing odd dimensions
    67          spares = num_ints * 4 * 8 - self.dim1
    68          if spares == 0:
    69              self.data = numpy.where(result == 0, 0, 1)
    70          else:
    71              self.data = numpy.where(result[:, :-spares] == 0, 0, 1)
    72          # Transpose appears to be needed to match edf reader (scary??)
    73  #        self.data = numpy.transpose(self.data)
    74          self.data = numpy.reshape(self.data.astype(numpy.uint16),
    75                                      (self.dim2, self.dim1))
    76          self.pilimage = None
    77          return self
    78
    79
    80
    81 -    def write(self, fname):
    82          """
    83          Try to write a file
    84          check we can write zipped also
    85          mimics that fabian was writing uint16 (we sometimes want floats)
    86          """
    87          raise Exception("Not implemented yet")
    88
    89      @staticmethod
    90 -    def checkData(data=None):
    91          if data is None:
    92              return None
    93          else:
    94              return data.astype(int)
    95