Esempio n. 1
0
    def _read_raw_data(self, f):
        """Read raw data from a positioned file"""

        # is this image a type we can read?
        assert self._data_type in ["h", "f", "B", "l", "b", "H", "I", "d"]

        if self._data_type == "f":
            raw_data = read_float32(streambuf(f), self._image_num_elements)
        elif self._data_type == "B":
            raw_data = read_uint8(streambuf(f), self._image_num_elements)
        elif self._data_type == "h":
            raw_data = read_int16(streambuf(f), self._image_num_elements)
        elif self._data_type == "H":
            raw_data = read_uint16(streambuf(f), self._image_num_elements)
        elif self._data_type == "l":
            raw_data = read_int32(streambuf(f), self._image_num_elements)
        elif self._data_type == "I":
            raw_data = read_uint32(streambuf(f), self._image_num_elements)

        # no C++ reader for remaining types (should be unusual anyway)
        else:
            vals = struct.unpack(
                self._byteord + self._data_type * self._image_num_elements,
                f.read(self._data_size * self._image_num_elements),
            )
            if self._data_type == "d":
                raw_data = flex.double(vals)
            if self._data_type in ["b", "?"]:
                raw_data = flex.int(vals)

        raw_data.reshape(flex.grid(self._image_size[1], self._image_size[0]))
        return raw_data
Esempio n. 2
0
    def _read_cbf_image(self):
        start_tag = binascii.unhexlify("0c1a04d5")

        with self.open_file(self._image_file, "rb") as fh:
            data = fh.read()
        data_offset = data.find(start_tag) + 4
        cbf_header = self._parse_cbf_header(data[:data_offset - 4].decode(
            "ascii", "ignore"))

        if cbf_header["byte_offset"]:
            pixel_values = uncompress(
                packed=data[data_offset:data_offset + cbf_header["size"]],
                fast=cbf_header["fast"],
                slow=cbf_header["slow"],
            )
        elif cbf_header["no_compression"]:
            assert len(self.get_detector()) == 1
            with self.open_file(self._image_file) as f:
                f.read(data_offset)
                pixel_values = read_int32(streambuf(f), cbf_header["length"])
            pixel_values.reshape(
                flex.grid(cbf_header["slow"], cbf_header["fast"]))

        else:
            raise ValueError(
                "Compression of type other than byte_offset or none is not supported (contact authors)"
            )

        return pixel_values
Esempio n. 3
0
    def read_cbf_image(self, cbf_image):
        from cbflib_adaptbx import uncompress
        import binascii

        start_tag = binascii.unhexlify("0c1a04d5")

        data = self.open_file(cbf_image, "rb").read()
        data_offset = data.find(start_tag) + 4
        cbf_header = data[:data_offset - 4]

        fast = 0
        slow = 0
        length = 0
        byte_offset = False
        no_compression = False

        for record in cbf_header.split("\n"):
            if "X-Binary-Size-Fastest-Dimension" in record:
                fast = int(record.split()[-1])
            elif "X-Binary-Size-Second-Dimension" in record:
                slow = int(record.split()[-1])
            elif "X-Binary-Number-of-Elements" in record:
                length = int(record.split()[-1])
            elif "X-Binary-Size:" in record:
                size = int(record.split()[-1])
            elif "conversions" in record:
                if "x-CBF_BYTE_OFFSET" in record:
                    byte_offset = True
                elif "x-CBF_NONE" in record:
                    no_compression = True

        assert length == fast * slow

        if byte_offset:
            pixel_values = uncompress(packed=data[data_offset:data_offset +
                                                  size],
                                      fast=fast,
                                      slow=slow)
        elif no_compression:
            from boost.python import streambuf
            try:
                from dxtbx.ext import read_int32
            except ImportError:
                from dxtbx import read_int32
            from scitbx.array_family import flex

            assert len(self.get_detector()) == 1
            f = self.open_file(self._image_file)
            f.read(data_offset)
            pixel_values = read_int32(streambuf(f), int(slow * fast))
            pixel_values.reshape(flex.grid(slow, fast))

        else:
            raise ValueError(
                "Uncompression of type other than byte_offset or none "
                " is not supported (contact authors)")

        return pixel_values
Esempio n. 4
0
    def get_raw_data(self):
        """Read the data - assuming it is streaming 4-byte unsigned ints following the
        header..."""

        assert len(self.get_detector()) == 1
        size = self.get_detector()[0].get_image_size()
        f = self.open_file(self._image_file)
        f.read(int(self._header_dictionary["HEADER_BYTES"]))
        raw_data = read_int32(streambuf(f), int(size[0] * size[1]))
        raw_data.reshape(flex.grid(size[1], size[0]))

        return raw_data
Esempio n. 5
0
    def get_raw_data(self, index):

        # is this image a type we can read?
        assert self._data_type in ["h", "f", "B", "l", "b", "H", "I", "d"]

        with FormatGatanDM4.open_file(self._image_file, "rb") as f:
            f.seek(self._data_offset)

            skip_bytes = index * self._image_num_elements * self._data_size
            f.seek(skip_bytes, whence=1)

            if self._data_type == "f":
                raw_data = read_float32(streambuf(f), self._image_num_elements)
            elif self._data_type == "B":
                raw_data = read_uint8(streambuf(f), self._image_num_elements)
            elif self._data_type == "h":
                raw_data = read_int16(streambuf(f), self._image_num_elements)
            elif self._data_type == "H":
                raw_data = read_uint16(streambuf(f), self._image_num_elements)
            elif self._data_type == "l":
                raw_data = read_int32(streambuf(f), self._image_num_elements)
            elif self._data_type == "I":
                raw_data = read_uint32(streambuf(f), self._image_num_elements)

            # no C++ reader for remaining types (should be unusual anyway)
            else:
                vals = struct.unpack(
                    self._byteord + self._data_type * self._image_num_elements,
                    f.read(self._data_size * self._image_num_elements),
                )
                if self._data_type == "d":
                    raw_data = flex.double(vals)
                if self._data_type in ["b", "?"]:
                    raw_data = flex.int(vals)

        raw_data.reshape(flex.grid(self._image_size[1], self._image_size[0]))
        return raw_data