Ejemplo n.º 1
0
    def read(self, fname, frame=None):
        """
        Read in and unpack the pixels (including overflow table
        """
        infile = self._open(fname, "rb")
        try:
            self._readheader(infile)
        except:
            raise

        rows = self.dim1
        cols = self.dim2

        try:
            # you had to read the Bruker docs to know this!
            npixelb = int(self.header['NPIXELB'])
        except:
            errmsg = "length " + str(len(self.header['NPIXELB'])) + "\n"
            for byt in self.header['NPIXELB']:
                errmsg += "char: " + str(byt) + " " + str(ord(byt)) + "\n"
            logger.warning(errmsg)
            raise

        self.data = readbytestream(infile,
                                   infile.tell(),
                                   rows,
                                   cols,
                                   npixelb,
                                   datatype="int",
                                   signed='n',
                                   swap='n')

        #handle overflows
        nov = int(self.header['NOVERFL'])
        if nov > 0:  # Read in the overflows
            # need at least int32 sized data I guess - can reach 2^21
            self.data = self.data.astype(numpy.uint32)
            # 16 character overflows:
            #      9 characters of intensity
            #      7 character position
            for i in range(nov):
                ovfl = infile.read(16)
                intensity = int(ovfl[0:9])
                position = int(ovfl[9:16])
                # relies on python style modulo being always +
                row = position % rows
                # relies on truncation down
                col = position / rows
                #print "Overflow ", r, c, intensity, position,\
                #    self.data[r,c],self.data[c,r]
                self.data[col, row] = intensity
        infile.close()

        self.resetvals()
        self.pilimage = None
        return self
Ejemplo n.º 2
0
    def read(self, fname):
        """
        Read in and unpack the pixels (including overflow table
        """
        infile = self._open(fname, "rb")
        try:
            self._readheader(infile)
        except:
            raise

        rows = self.dim1
        cols = self.dim2

        try:
            # you had to read the Bruker docs to know this!
            npixelb = int(self.header['NPIXELB'])
        except:
            errmsg = "length " + str(len(self.header['NPIXELB'])) + "\n"
            for byt in self.header['NPIXELB']:
                errmsg += "char: " + str(byt) + " " + str(ord(byt)) + "\n"
            logging.warning(errmsg)
            raise

        self.data = readbytestream(infile, infile.tell(),
                                   rows, cols, npixelb,
                                   datatype="int",
                                   signed='n',
                                   swap='n')

        #handle overflows
        nov = int(self.header['NOVERFL'])
        if nov > 0:   # Read in the overflows
            # need at least int32 sized data I guess - can reach 2^21
            self.data = self.data.astype(N.uint32)
            # 16 character overflows:
            #      9 characters of intensity
            #      7 character position
            for i in range(nov):
                ovfl = infile.read(16)
                intensity = int(ovfl[0: 9])
                position = int(ovfl[9: 16])
                # relies on python style modulo being always +
                row = position % rows
                # relies on truncation down
                col = position / rows
                #print "Overflow ", r, c, intensity, position,\
                #    self.data[r,c],self.data[c,r]
                self.data[col, row] = intensity
        infile.close()

        self.resetvals()
        self.pilimage = None
        return self
Ejemplo n.º 3
0
    def read(self, fname, frame=None):
        f = open(fname, "rb")
        try:
            self._readheader(f)
        except:
            raise

        rows = int(self.header['NROWS'])
        cols = int(self.header['NCOLS'])
        npixelb = int(self.header['NPIXELB'][0])
        # you had to read the Bruker docs to know this!

        # We are now at the start of the image - assuming 
        #   readbrukerheader worked
        # size = rows * cols * npixelb
        self.data = readbytestream(f, f.tell(), rows, cols, npixelb,
                                    datatype="int", signed='n', swap='n')

        noverfl = self.header['NOVERFL'].split() # now process the overflows
        #read the set of "underflow pixels" - these will be completely 
        # disregarded for now
        data = self.data
        k = 0

        while k < 2:#for the time being things - are done in 16 bits
            datatype = {'1' : numpy.uint8,
                        '2' : numpy.uint16,
                        '4' : numpy.uint32 }[("%d" % 2 ** k)]
            ar = numpy.array(numpy.fromstring(f.read(int(noverfl[k]) * (2 ** k)),
                                        datatype), numpy.uint16)
            #insert the the overflow pixels in the image array:
            #this is probably a memory intensive way of doing this - 
            # might be done in a more clever way
            lim = 2 ** (8 * k) - 1
            #generate an array comprising of the indices into data.ravel() 
            # where its value equals lim.
            M = numpy.compress(numpy.equal(data.ravel(), lim), numpy.arange(rows * cols))
            #now put values from ar into those indices
            numpy.put(data.ravel(), M, ar)
            padding = 16 * int(math.ceil(int(noverfl[k]) * (2 ** k) / 16.)) - \
                         int(noverfl[k]) * (2 ** k)
            f.seek(padding, 1)
            print noverfl[k] + " bytes read + %d bytes padding" % padding
            k = k + 1

        f.close()

        (self.dim1, self.dim2) = (rows, cols)
        print self.dim1, self.dim2
        self.resetvals()
        return self
Ejemplo n.º 4
0
    def read(self, fname, frame=None):
        f = open(fname, "rb")
        try:
            self._readheader(f)
        except:
            raise

        rows = int(self.header['NROWS'])
        cols = int(self.header['NCOLS'])
        npixelb = int(self.header['NPIXELB'][0])
        # you had to read the Bruker docs to know this!

        # We are now at the start of the image - assuming
        #   readbrukerheader worked
        # size = rows * cols * npixelb
        self.data = readbytestream(f,
                                   f.tell(),
                                   rows,
                                   cols,
                                   npixelb,
                                   datatype="int",
                                   signed='n',
                                   swap='n')

        noverfl = self.header['NOVERFL'].split()  # now process the overflows
        #read the set of "underflow pixels" - these will be completely
        # disregarded for now
        data = self.data
        k = 0

        while k < 2:  #for the time being things - are done in 16 bits
            datatype = {
                '1': numpy.uint8,
                '2': numpy.uint16,
                '4': numpy.uint32
            }[("%d" % 2**k)]
            ar = numpy.array(
                numpy.fromstring(f.read(int(noverfl[k]) * (2**k)), datatype),
                numpy.uint16)
            #insert the the overflow pixels in the image array:
            #this is probably a memory intensive way of doing this -
            # might be done in a more clever way
            lim = 2**(8 * k) - 1
            #generate an array comprising of the indices into data.ravel()
            # where its value equals lim.
            M = numpy.compress(numpy.equal(data.ravel(), lim),
                               numpy.arange(rows * cols))
            #now put values from ar into those indices
            numpy.put(data.ravel(), M, ar)
            padding = 16 * int(math.ceil(int(noverfl[k]) * (2 ** k) / 16.)) - \
                         int(noverfl[k]) * (2 ** k)
            f.seek(padding, 1)
            print noverfl[k] + " bytes read + %d bytes padding" % padding
            k = k + 1

        f.close()

        (self.dim1, self.dim2) = (rows, cols)
        print self.dim1, self.dim2
        self.resetvals()
        return self