fabio.bruker100image

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

# Source Code for Module fabio.bruker100image

     1  import numpy
     2  import math
     3  import logging
     4  logger = logging.getLogger("bruker100image")
     5  try:
     6      import Image
     7  except ImportError:
     8      logger.warning("PIL is not installed ... trying to do without")
     9      Image = None
    10
    11  from brukerimage import brukerimage
    12  from readbytestream import readbytestream
    13
    14 -class bruker100image(brukerimage):
    15
    16
    17 -    def toPIL16(self, filename=None):
    18          if not Image:
    19              raise RuntimeError("PIL is not installed !!! ")
    20
    21          if filename:
    22              self.read(filename)
    23          PILimage = Image.frombuffer("F",
    24                                          (self.dim1, self.dim2),
    25                                          self.data,
    26                                          "raw",
    27                                          "F;16", 0, -1)
    28          return PILimage
    29
    30 -    def read(self, fname, frame=None):
    31          f = open(fname, "rb")
    32          try:
    33              self._readheader(f)
    34          except:
    35              raise
    36
    37          rows = int(self.header['NROWS'])
    38          cols = int(self.header['NCOLS'])
    39          npixelb = int(self.header['NPIXELB'][0])
    40          # you had to read the Bruker docs to know this!
    41
    42          # We are now at the start of the image - assuming
    43          #   readbrukerheader worked
    44          # size = rows * cols * npixelb
    45          self.data = readbytestream(f, f.tell(), rows, cols, npixelb,
    46                                      datatype="int", signed='n', swap='n')
    47
    48          noverfl = self.header['NOVERFL'].split() # now process the overflows
    49          #read the set of "underflow pixels" - these will be completely
    50          # disregarded for now
    51          data = self.data
    52          k = 0
    53
    54          while k < 2:#for the time being things - are done in 16 bits
    55              datatype = {'1' : numpy.uint8,
    56                          '2' : numpy.uint16,
    57                          '4' : numpy.uint32 }[("%d" % 2 ** k)]
    58              ar = numpy.array(numpy.fromstring(f.read(int(noverfl[k]) * (2 ** k)),
    59                                          datatype), numpy.uint16)
    60              #insert the the overflow pixels in the image array:
    61              #this is probably a memory intensive way of doing this -
    62              # might be done in a more clever way
    63              lim = 2 ** (8 * k) - 1
    64              #generate an array comprising of the indices into data.ravel()
    65              # where its value equals lim.
    66              M = numpy.compress(numpy.equal(data.ravel(), lim), numpy.arange(rows * cols))
    67              #now put values from ar into those indices
    68              numpy.put(data.ravel(), M, ar)
    69              padding = 16 * int(math.ceil(int(noverfl[k]) * (2 ** k) / 16.)) - \
    70                           int(noverfl[k]) * (2 ** k)
    71              f.seek(padding, 1)
    72              print noverfl[k] + " bytes read + %d bytes padding" % padding
    73              k = k + 1
    74
    75          f.close()
    76
    77          (self.dim1, self.dim2) = (rows, cols)
    78          print self.dim1, self.dim2
    79          self.resetvals()
    80          return self
    81
    82  if __name__ == '__main__':
    83      import sys, time
    84      I = bruker100image()
    85      b = time.clock()
    86      while (sys.argv[1:]):
    87          I.read(sys.argv[1])
    88          r = I.toPIL16()
    89          I.rebin(2, 2)
    90          print sys.argv[1] + (": max=%d, min=%d, mean=%.2e, stddev=%.2e") % (
    91              I.getmax(), I.getmin(), I.getmean(), I.getstddev())
    92          print 'integrated intensity (%d %d %d %d) =%.3f' % (
    93              10, 20, 20, 40, I.integrate_area((10, 20, 20, 40)))
    94          sys.argv[1:] = sys.argv[2:]
    95      e = time.clock()
    96      print (e - b)
    97