Example #1
0
 def detectorbase_start(self, index=0):
   from iotbx.detectors.eiger import EIGERImage
   self.detectorbase = EIGERImage(self._image_file,index=index)
   self.detectorbase.readHeader(dxtbx_instance=self)
   def model_get_raw_data(ptr,index):
     return self.get_raw_data(index)
   self.detectorbase.get_raw_data_callback = model_get_raw_data
class FormatEigerNearlyNexus(FormatHDF5):
    def __init__(self, image_file, **kwargs):
        from dxtbx import IncorrectFormatError
        if not self.understand(image_file):
            raise IncorrectFormatError(self, image_file)
        FormatHDF5.__init__(self, image_file, **kwargs)

    @staticmethod
    def understand(image_file):
        try:
            is_nexus = is_eiger_nearly_nexus_file(image_file)
        except IOError:
            return False
        return is_nexus

    def _start(self):

        # Read the file structure
        import uuid
        temp_file = "tmp_master_%s.nxs" % uuid.uuid1().hex
        fixer = EigerNXmxFixer(self._image_file, temp_file)
        reader = NXmxReader(handle=fixer.handle)

        # Only support 1 set of models at the moment
        assert len(reader.entries) == 1, \
          "Currently only supports 1 NXmx entry"
        assert len(reader.entries[0].data) == 1, \
          "Currently only supports 1 NXdata"
        assert len(reader.entries[0].instruments) == 1, \
          "Currently only supports 1 NXinstrument"
        assert len(reader.entries[0].samples) == 1, \
          "Currently only supports 1 NXsample"
        assert len(reader.entries[0].instruments[0].detectors) == 1, \
          "Currently only supports 1 NXdetector"
        assert len(reader.entries[0].instruments[0].detectors[0].modules) == 1, \
          "Currently only supports 1 NXdetector_module"
        assert len(reader.entries[0].samples[0].beams) == 1, \
          "Currently only supports 1 NXbeam"

        # Get the NXmx model objects
        entry = reader.entries[0]
        instrument = entry.instruments[0]
        detector = instrument.detectors[0]
        sample = entry.samples[0]
        beam = sample.beams[0]

        # Use data from original master file
        data = NXdata(fixer.handle_orig[entry.data[0].handle.name])

        # Construct the models
        self._beam_model = BeamFactory(beam).model
        self._detector_model = DetectorFactory(detector,
                                               self._beam_model).model
        self._goniometer_model = GoniometerFactory(sample).model
        self._scan_model = ScanFactory(sample, detector).model
        self._raw_data = DataFactory(data).model
        self._mask = MaskFactory([detector]).mask

        # update model for masking Eiger detectors
        from dxtbx.format.FormatPilatusHelpers import determine_eiger_mask
        for f0, f1, s0, s1 in determine_eiger_mask(self._detector_model):
            self._detector_model[0].add_mask(f0 - 1, s0 - 1, f1, s1)

    def _end(self):
        return

    def _goniometer(self):
        return self._goniometer_model

    def _detector(self):
        return self._detector_model

    def _beam(self):
        return self._beam_model

    def _scan(self):
        return self._scan_model

    def get_goniometer(self, index=None):
        return self._goniometer()

    def get_detector(self, index=None):
        return self._detector()

    def get_beam(self, index=None):
        return self._beam()

    def get_scan(self, index=None):
        if index is None:
            return self._scan()
        return self._scan()[index]

    def get_raw_data(self, index):
        return self._raw_data[index]

    def get_mask(self, index=None, goniometer=None):
        return self._mask

    def get_num_images(self):
        scan = self._scan()
        if isinstance(scan, list):
            return sum(s.get_num_images() for s in scan)
        return scan.get_num_images()

    def get_image_file(self, index=None):
        return self._image_file

    def detectorbase_start(self, index=0):
        from iotbx.detectors.eiger import EIGERImage
        self.detectorbase = EIGERImage(self._image_file, index=index)
        self.detectorbase.readHeader(dxtbx_instance=self)

        def model_get_raw_data(ptr, index):
            return self.get_raw_data(index)

        self.detectorbase.get_raw_data_callback = model_get_raw_data

    def get_detectorbase(self, index=0):
        self.detectorbase_start(index)
        return self.detectorbase

    def get_vendortype(self):
        from dxtbx.format.FormatPilatusHelpers import get_vendortype_eiger as gv
        return gv(self.get_detector())
class FormatHDF5Dectris(FormatHDF5):
    """ This is a placeholder implementation only.  Open example dataset
      provided by Dectris Ltd, Jan 2013.  Read the first image only.
      Later replace this with a sweep-oriented implementation that
      reads the whole dataset."""
    @staticmethod
    def understand(image_file):
        return False
        try:
            tag = FormatHDF5Dectris.open_file(image_file, 'rb').read(8)
        except IOError:
            return False

        return tag == "\211HDF\r\n\032\n"

    def __init__(self, image_file, **kwargs):

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

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

    def _start(self):
        from iotbx.detectors.eiger import EIGERImage
        self.detectorbase = EIGERImage(self._image_file)
        self.detectorbase.readHeader()

    def _goniometer(self):

        return self._goniometer_factory.single_axis()

    def _detector(self):
        '''Return a model for a simple detector'''

        return self._detector_factory.simple(
            sensor='PAD',
            distance=self.detectorbase.distance,
            beam_centre=(self.detectorbase.beamx, self.detectorbase.beamy),
            fast_direction='+x',
            slow_direction='-y',
            pixel_size=(self.detectorbase.pixel_size,
                        self.detectorbase.pixel_size),
            image_size=(self.detectorbase.size1, self.detectorbase.size2),
            trusted_range=(-1, self.detectorbase.saturation),
            mask=[])  # a list of dead rectangles

    def _beam(self):
        '''Return a simple model for the beam.'''

        return self._beam_factory.simple(self.detectorbase.wavelength)

    def _scan(self):
        '''Replace this with true values later
       when HDF5 sweep support is implemented.'''

        return self._scan_factory.make_scan(
            image_range=(1, 1),
            #dummy value--return to this later please
            exposure_time=1,
            oscillation=(self.detectorbase.osc_start,
                         self.detectorbase.osc_start +
                         self.detectorbase.deltaphi),
            epochs={1: 0.}  # Later get the true time values from HDF5 file
        )

    def get_num_images(self):
        return self.detectorbase.image_count()

    def get_goniometer(self, index=None):
        return Format.get_goniometer(self)

    def get_detector(self, index=None):
        return Format.get_detector(self)

    def get_beam(self, index=None):
        return Format.get_beam(self)

    def get_scan(self, index=None):
        return Format.get_scan(self)

    def get_raw_data(self, index=None):
        return Format.get_raw_data(self)

    def get_detectorbase(self, index=None):
        self.detectorbase.img_number = index
        return self.detectorbase

    def get_image_file(self, index=None):
        return self.detectorbase.get_data_link(index)
 def _start(self):
     from iotbx.detectors.eiger import EIGERImage
     self.detectorbase = EIGERImage(self._image_file)
     self.detectorbase.readHeader()
Example #5
0
class FormatHDF5EigerNearlyNexus(FormatHDF5):
    @staticmethod
    def understand(image_file):
        try:
            return is_eiger_nearly_nexus_file(image_file)
        except IOError:
            return False

    def _start(self):
        # Read the file structure
        temp_file = "tmp_master_%s.nxs" % uuid.uuid1().hex
        fixer = EigerNXmxFixer(self._image_file, temp_file)
        reader = NXmxReader(handle=fixer.handle)

        # Only support 1 set of models at the moment
        assert len(reader.entries) == 1, "Currently only supports 1 NXmx entry"
        assert len(reader.entries[0].data) == 1, "Currently only supports 1 NXdata"
        assert (
            len(reader.entries[0].instruments) == 1
        ), "Currently only supports 1 NXinstrument"
        assert len(reader.entries[0].samples) == 1, "Currently only supports 1 NXsample"
        assert (
            len(reader.entries[0].instruments[0].detectors) == 1
        ), "Currently only supports 1 NXdetector"
        assert (
            len(reader.entries[0].instruments[0].detectors[0].modules) == 1
        ), "Currently only supports 1 NXdetector_module"
        assert (
            len(reader.entries[0].samples[0].beams) == 1
            or len(reader.entries[0].instruments[0].beams) == 1
        ), "Currently only supports 1 NXbeam"

        # Get the NXmx model objects
        entry = reader.entries[0]
        self.instrument = instrument = entry.instruments[0]
        detector = instrument.detectors[0]
        sample = entry.samples[0]
        beam = sample.beams[0] if sample.beams else instrument.beams[0]

        # Use data from original master file
        data = NXdata(fixer.handle_orig[entry.data[0].handle.name])
        self._raw_data = DataFactory(data, cached_information=fixer.data_factory_cache)

        # Construct the models
        self._beam_factory = BeamFactory(beam)
        self._beam_factory.load_model(0)
        self._detector_model = DetectorFactory(
            detector, self._beam_factory.model, shape=self._raw_data.shape()
        ).model

        # Override the minimum trusted value - for Eiger should be -1
        for panel in self._detector_model:
            trusted = panel.get_trusted_range()
            panel.set_trusted_range((-1, trusted[1]))

        self._goniometer_model = GoniometerFactory(sample).model
        self._scan_model = generate_scan_model(sample, detector)

        # update model for masking Eiger detectors
        for f0, f1, s0, s1 in determine_eiger_mask(self._detector_model):
            self._detector_model[0].add_mask(f0 - 1, s0 - 1, f1, s1)

    def _end(self):
        return

    def _goniometer(self):
        return self._goniometer_model

    def _detector(self):
        return self._detector_model

    def _beam(self, index=None):
        self._beam_model, _ = self._beam_factory.read_models(index)
        return self._beam_model

    def _scan(self):
        return self._scan_model

    def get_goniometer(self, index=None):
        return self._goniometer()

    def get_detector(self, index=None):
        return self._detector()

    def get_beam(self, index=None):
        return self._beam(index)

    def get_scan(self, index=None):
        if index is None:
            return self._scan()
        return self._scan()[index]

    def get_raw_data(self, index):
        return self._raw_data[index]

    def get_static_mask(self, index=None, goniometer=None):
        return MaskFactory(self.instrument.detectors, index).mask

    def get_num_images(self):
        scan = self._scan()
        if isinstance(scan, list):
            return sum(s.get_num_images() for s in scan)
        return scan.get_num_images()

    def get_image_file(self, index=None):
        return self._image_file

    def detectorbase_start(self, index=0):
        self.detectorbase = EIGERImage(self._image_file, index=index)
        self.detectorbase.readHeader(dxtbx_instance=self)

        def model_get_raw_data(ptr, index):
            return self.get_raw_data(index)

        self.detectorbase.get_raw_data_callback = model_get_raw_data

    def get_detectorbase(self, index=0):
        self.detectorbase_start(index)
        return self.detectorbase

    def get_vendortype(self):
        return gv(self.get_detector())