Exemplo n.º 1
0
    def generate_from_phil(params, beam=None):
        '''
    Create a new detector model from phil parameters

    '''
        from cctbx.eltbx import attenuation_coefficient
        detector = Detector()

        # Create a list of panels
        panel_list = {}
        for panel_params in params.detector.panel:
            panel = Panel()
            if panel_params.name is not None:
                panel.set_name(panel_params.name)
            if panel_params.type is not None:
                panel.set_type(panel_params.type)
            if panel_params.gain is not None:
                panel.set_gain(panel_params.gain)
            if panel_params.pixel_size is not None:
                panel.set_pixel_size(panel_params.pixel_size)
            else:
                raise RuntimeError('No pixel size set')
            if panel_params.image_size is not None:
                panel.set_image_size(panel_params.image_size)
            else:
                raise RuntimeError('No image size set')
            if panel_params.trusted_range is not None:
                panel.set_trusted_range(panel_params.trusted_range)
            else:
                raise RuntimeError('No trusted range set')
            if panel_params.thickness is not None:
                panel.set_thickness(panel_params.thickness)
            if panel_params.material is not None:
                panel.set_material(panel_params.material)
            if panel_params.parallax_correction is True:
                if panel_params.material is None:
                    raise RuntimeError("No material for parallax correction")
                if panel_params.thickness is None:
                    raise RuntimeError("No thickness for parallax correction")
                if beam is None:
                    raise RuntimeError("No beam for parallax correction")
                table = attenuation_coefficient.get_table(
                    panel_params.material)
                mu = table.mu_at_angstrom(beam.get_wavelength()) / 10.0
                t0 = panel_params.thickness
                panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0))
            if panel_params.fast_axis is None:
                panel_params.fast_axis = (1, 0, 0)
            if panel_params.slow_axis is None:
                panel_params.slow_axis = (0, 1, 0)
            if panel_params.origin is None:
                panel_params.origin = (0, 0, 0)
            panel.set_local_frame(panel_params.fast_axis,
                                  panel_params.slow_axis, panel_params.origin)
            if panel_params.id in panel_list:
                raise RuntimeError('Multiple panels with id=%d' %
                                   panel_params.id)
            panel_list[panel_params.id] = panel

        # Create the hierarchy
        panel_counter = 0
        root = detector.hierarchy()
        if params.detector.hierarchy.name is not None:
            root.set_name(params.detector.hierarchy.name)
        if params.detector.hierarchy.fast_axis is None:
            params.detector.hierarchy.fast_axis = (1, 0, 0)
        if params.detector.hierarchy.slow_axis is None:
            params.detector.hierarchy.slow_axis = (0, 1, 0)
        if params.detector.hierarchy.origin is None:
            params.detector.hierarchy.origin = (0, 0, 0)
        root.set_frame(params.detector.hierarchy.fast_axis,
                       params.detector.hierarchy.slow_axis,
                       params.detector.hierarchy.origin)

        def get_parent(node, index):
            if len(index) == 0:
                return node
            return get_parent(node[index[0]], index[1:])

        for group_params in params.detector.hierarchy.group:
            parent = get_parent(root, group_params.id[:-1])
            assert len(parent) == group_params.id[-1]
            group = parent.add_group()
            if group_params.name is not None:
                group.set_name(group_params.name)
            if group_params.fast_axis is None:
                group_params.fast_axis = (1, 0, 0)
            if group_params.slow_axis is None:
                group_params.slow_axis = (0, 1, 0)
            if group_params.origin is None:
                group_params.origin = (0, 0, 0)
            group.set_local_frame(group_params.fast_axis,
                                  group_params.slow_axis, group_params.origin)
            for panel_id in group_params.panel:
                assert panel_id == panel_counter
                group.add_panel(panel_list[panel_id])
                panel_counter += 1
        if panel_counter == 0:
            for panel_id in range(max(panel_list.keys()) + 1):
                detector.add_panel(panel_list[panel_id])
        elif panel_counter != len(panel_list):
            raise RuntimeError("Inconsistent number of panels in hierarchy")

        # Return detector
        return detector