class FormatCBFFull(FormatCBF):
  '''An image reading class for full CBF format images i.e. those from
  a variety of cameras which support this format.'''

  @staticmethod
  def understand(image_file):
    '''Check to see if this looks like an CBF format image, i.e. we can
    make sense of it.'''

    header = FormatCBF.get_cbf_header(image_file)

    if not '_diffrn.id' in header and not '_diffrn_source' in header:
      return False

    return True

  def __init__(self, image_file, **kwargs):
    '''Initialise the image structure from the given file.'''

    assert(self.understand(image_file))

    FormatCBF.__init__(self, image_file, **kwargs)

    return

  def __del__(self):
    self._cbf_handle.__swig_destroy__(self._cbf_handle)

  def _start(self):
    '''Open the image file as a cbf file handle, and keep this somewhere
    safe.'''

    FormatCBF._start(self)

  def _get_cbf_handle(self):
    try:
      return self._cbf_handle
    except AttributeError:
      self._cbf_handle = pycbf.cbf_handle_struct()
      self._cbf_handle.read_widefile(self._image_file, pycbf.MSG_DIGEST)
      return self._cbf_handle

  def _goniometer(self):
    '''Return a working goniometer instance.'''

    return self._goniometer_factory.imgCIF_H(self._get_cbf_handle())

  def _detector(self):
    '''Return a working detector instance.'''

    return self._detector_factory.imgCIF_H(self._get_cbf_handle(), 'unknown')

  def _beam(self):
    '''Return a working beam instance.'''

    return self._beam_factory.imgCIF_H(self._get_cbf_handle())

  def _scan(self):
    '''Return a working scan instance.'''

    return self._scan_factory.imgCIF_H(self._image_file, self._get_cbf_handle())

  def detectorbase_start(self):
    from iotbx.detectors.cbf import CBFImage
    self.detectorbase = CBFImage(self._image_file)
    self.detectorbase.readHeader()

  def get_raw_data(self):
    '''Get the pixel intensities (i.e. read the image and return as a
    flex array.'''
    self.detectorbase_start()
    try:
      image = self.detectorbase
      image.read()
      raw_data = image.get_raw_data()

      return raw_data
    except Exception:
      return None
Beispiel #2
0
class FormatCBFFull(FormatCBF):
    """An image reading class for full CBF format images i.e. those from
    a variety of cameras which support this format."""
    @staticmethod
    def understand(image_file):
        """Check to see if this looks like an CBF format image, i.e. we can
        make sense of it."""

        header = FormatCBF.get_cbf_header(image_file)

        if "_diffrn.id" not in header and "_diffrn_source" not in header:
            return False

        return True

    def __init__(self, image_file, **kwargs):
        """Initialise the image structure from the given file."""

        # It appears Pycbf can not handle unicode filenames (see dials/dials#256)
        self._raw_data = None
        super(FormatCBFFull, self).__init__(str(image_file), **kwargs)

    def __del__(self):
        self._cbf_handle.__swig_destroy__(self._cbf_handle)

    def _get_cbf_handle(self):
        try:
            return self._cbf_handle
        except AttributeError:
            self._cbf_handle = pycbf.cbf_handle_struct()

            buffer = None
            # Reopen to tell if it's a gzip - this should be cached, so fast
            with FormatCBF.open_file(self._image_file, "rb") as fin:
                # If this was a gzip or bzip file, read as a buffer
                if (gzip and isinstance(fin._cache_object._file, gzip.GzipFile)
                    ) or (bz2 and isinstance(fin._cache_object._file,
                                             bz2.BZ2File)):
                    buffer = fin.read()

            if buffer:
                cbf_read_buffer(self._cbf_handle, buffer, pycbf.MSG_DIGEST)
            else:
                self._cbf_handle.read_widefile(self._image_file.encode(),
                                               pycbf.MSG_DIGEST)

            return self._cbf_handle

    def _goniometer(self):
        """Return a working goniometer instance."""

        return self._goniometer_factory.imgCIF_H(self._get_cbf_handle())

    def _detector(self):
        """Return a working detector instance."""

        return self._detector_factory.imgCIF_H(self._get_cbf_handle(),
                                               "unknown")

    def _beam(self):
        """Return a working beam instance."""

        return self._beam_factory.imgCIF_H(self._get_cbf_handle())

    def _scan(self):
        """Return a working scan instance."""

        return self._scan_factory.imgCIF_H(self._image_file,
                                           self._get_cbf_handle())

    def detectorbase_start(self):
        self.detectorbase = CBFImage(self._image_file)
        self.detectorbase.readHeader()

    def get_raw_data(self):
        """Get the pixel intensities (i.e. read the image and return as a
        flex array."""
        if self._raw_data is not None:
            return self._raw_data

        self.detectorbase_start()
        try:
            image = self.detectorbase
            image.read()
            self._raw_data = image.get_raw_data()

            return self._raw_data
        except Exception:
            return None
class FormatCBFFull(FormatCBF):
    """An image reading class for full CBF format images i.e. those from
    a variety of cameras which support this format."""

    @staticmethod
    def understand(image_file):
        """Check to see if this looks like an CBF format image, i.e. we can
        make sense of it."""

        header = FormatCBF.get_cbf_header(image_file)

        if "_diffrn.id" not in header and "_diffrn_source" not in header:
            return False

        return True

    def __init__(self, image_file, **kwargs):
        """Initialise the image structure from the given file."""

        # It appears Pycbf can not handle unicode filenames (see dials/dials#256)
        image_file = str(image_file)
        from dxtbx import IncorrectFormatError

        if not self.understand(image_file):
            raise IncorrectFormatError(self, image_file)

        FormatCBF.__init__(self, image_file, **kwargs)
        self._raw_data = None

    def __del__(self):
        self._cbf_handle.__swig_destroy__(self._cbf_handle)

    def _start(self):
        """Open the image file as a cbf file handle, and keep this somewhere
        safe."""

        FormatCBF._start(self)

    def _get_cbf_handle(self):
        try:
            return self._cbf_handle
        except AttributeError:
            self._cbf_handle = pycbf.cbf_handle_struct()
            self._cbf_handle.read_widefile(self._image_file.encode(), pycbf.MSG_DIGEST)
            return self._cbf_handle

    def _goniometer(self):
        """Return a working goniometer instance."""

        return self._goniometer_factory.imgCIF_H(self._get_cbf_handle())

    def _detector(self):
        """Return a working detector instance."""

        return self._detector_factory.imgCIF_H(self._get_cbf_handle(), "unknown")

    def _beam(self):
        """Return a working beam instance."""

        return self._beam_factory.imgCIF_H(self._get_cbf_handle())

    def _scan(self):
        """Return a working scan instance."""

        return self._scan_factory.imgCIF_H(self._image_file, self._get_cbf_handle())

    def detectorbase_start(self):
        from iotbx.detectors.cbf import CBFImage

        self.detectorbase = CBFImage(self._image_file)
        self.detectorbase.readHeader()

    def get_raw_data(self):
        """Get the pixel intensities (i.e. read the image and return as a
        flex array."""
        if self._raw_data is not None:
            return self._raw_data

        self.detectorbase_start()
        try:
            image = self.detectorbase
            image.read()
            self._raw_data = image.get_raw_data()

            return self._raw_data
        except Exception:
            return None