Пример #1
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())
Пример #2
0
class FormatNexus(FormatHDF5):
    @staticmethod
    def understand(image_file):
        try:
            return is_nexus_file(image_file)
        except IOError:
            return False

    def _start(self):

        # Read the file structure
        self._reader = reader = NXmxReader(self._image_file)

        # 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].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]
        data = entry.data[0]

        # Construct the models
        self._beam_factory = BeamFactory(beam)
        self._beam_factory.load_model(0)

        self._setup_gonio_and_scan(sample, detector)

        if self._scan_model:
            array_range = self._scan_model.get_array_range()
            num_images = array_range[1] - array_range[0]
        else:
            num_images = 0

        if len(instrument.detector_groups) == 0:
            assert (
                len(reader.entries[0].instruments[0].detectors) == 1
            ), "Currently only supports 1 NXdetector unless in a detector group"
            assert (
                len(reader.entries[0].instruments[0].detectors[0].modules) == 1
            ), "Currently only supports 1 NXdetector_module unless in a detector group"

            self._raw_data = DataFactory(data, max_size=num_images)
            self._detector_model = DetectorFactory(
                detector,
                self._beam_factory.model,
                shape=self._raw_data.shape()).model
        else:
            self._raw_data = detectorgroupdatafactory(data, instrument)
            self._detector_model = DetectorFactoryFromGroup(
                instrument, self._beam_factory.model).model

    def _setup_gonio_and_scan(self, sample, detector):
        """Set up rotation-specific models"""
        self._goniometer_model = GoniometerFactory(sample).model
        self._scan_model = generate_scan_model(sample, detector)

    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_spectrum(self, index=None):
        self._beam_model, _ = self._beam_factory.read_models(index)
        return self._beam_factory.spectrum

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

    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):
        if self._scan() is not None:
            return self._scan().get_num_images()
        return len(self._raw_data)

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

    def get_detectorbase(self, index=None):
        raise NotImplementedError

    @staticmethod
    def get_instrument_name(handle):
        if "short_name" in handle["/entry/instrument"].attrs:
            name = handle["/entry/instrument"].attrs["short_name"]
        elif "/entry/instrument/name" in handle:
            if "short_name" in handle["/entry/instrument/name"].attrs:
                name = handle["/entry/instrument/name"].attrs["short_name"]
            else:
                name = handle["/entry/instrument/name"][()]
        else:
            name = None
        return name