def _save(self, nv):
        p = get_spectrometer_config_path()
        config = ConfigParser()
        config.read(p)

        config.set('CDDParameters', 'OperatingVoltage', nv)
        config.write(open(p, 'w'))
        self.info('saving new operating voltage {:0.1f} to {}'.format(nv, p))
    def _save(self, nv):
        p = get_spectrometer_config_path()
        config = ConfigParser()
        config.read(p)

        config.set('CDDParameters', 'OperatingVoltage', nv)
        config.write(open(p, 'w'))
        self.info('saving new operating voltage {:0.1f} to {}'.format(nv, p))
Beispiel #3
0
    def _get_config(self):
        config = ConfigParser()
        try:
            p = get_spectrometer_config_path()
        except IOError:
            p = os.path.join(paths.spectrometer_dir, 'config.cfg')

        config.read(p)

        return config
Beispiel #4
0
    def load(self):
        """
        load detectors
        load setupfiles/spectrometer/config.cfg file
        load magnet
        load deflections coefficients
        :return:
        """
        self.load_detectors()

        # load local configurations
        self.spectrometer_configurations = list_directory2(paths.spectrometer_config_dir,
                                                           remove_extension=True,
                                                           extension='.cfg')

        name = get_spectrometer_config_name()
        sc, _ = os.path.splitext(name)
        self.spectrometer_configuration = sc

        p = get_spectrometer_config_path(name)
        config = self.get_configuration_writer(p)
        pd = 'Protection'

        if config.has_section(pd):

            self.magnet.use_beam_blank = self.config_get(config, pd, 'use_beam_blank',
                                                         cast='boolean', default=False)
            self.magnet.use_detector_protection = self.config_get(config, pd,
                                                                  'use_detector_protection',
                                                                  cast='boolean', default=False)
            self.magnet.beam_blank_threshold = self.config_get(config, pd,
                                                               'beam_blank_threshold', cast='float', default=0.1)

            # self.magnet.detector_protection_threshold = self.config_get(config, pd,
            # 'detector_protection_threshold',
            # cast='float', default=0.1)
            ds = self.config_get(config, pd, 'detectors')
            if ds:
                ds = ds.split(',')
                self.magnet.protected_detectors = ds
                for di in ds:
                    self.info('Making protection available for detector "{}"'.format(di))

        if config.has_section('Deflections'):
            if config.has_option('Deflections', 'max'):
                v = config.getint('Deflections', 'max')
                if v:
                    self.max_deflection = v

        self.magnet.load()

        self.debug('Detectors {}'.format(self.detectors))
        for d in self.detectors:
            d.load_deflection_coefficients()
Beispiel #5
0
    def update_config(self, **kw):
        # p = os.path.join(paths.spectrometer_dir, 'config.cfg')
        p = get_spectrometer_config_path()
        config = self.get_configuration_writer(p)
        for k, v in kw.items():
            for option, value in v:
                config.set(k, option, value)

        with open(p, 'w') as wfile:
            config.write(wfile)

        self.clear_cached_config()
    def dump(self):
        p = get_spectrometer_config_path()

        cfp = ConfigParser()
        cfp.read(p)
        for gn, pn, v in self.itervalues():
            cfp.set(gn, pn, v)

        with open(p, 'w') as wfile:
            cfp.write(wfile)

        return p
Beispiel #7
0
    def update_config(self, **kw):
        # p = os.path.join(paths.spectrometer_dir, 'config.cfg')
        p = get_spectrometer_config_path()
        config = self.get_configuration_writer(p)
        for k, v in kw.items():
            for option, value in v:
                config.set(k, option, value)

        with open(p, 'w') as wfile:
            config.write(wfile)

        self.clear_cached_config()
Beispiel #8
0
    def load(self):
        self.load_molecular_weights()
        self.load_detectors()
        self.magnet.load()
        # load local configurations
        self.spectrometer_configurations = glob_list_directory(paths.spectrometer_config_dir, remove_extension=True,
                                                               extension='.cfg')

        name = get_spectrometer_config_name()
        sc, _ = os.path.splitext(name)
        self.spectrometer_configuration = sc

        p = get_spectrometer_config_path(name)
        config = self.get_configuration_writer(p)

        return config
Beispiel #9
0
    def _get_cached_config(self):
        if self._config is None:
            p = get_spectrometer_config_path()
            if not os.path.isfile(p):
                self.warning(
                    'Spectrometer configuration file {} not found'.format(p))
                return

            self.debug('caching configuration from {}'.format(p))
            config = self.get_configuration_writer(p)
            d = {}
            defl = {}
            trap = {}
            for section in config.sections():
                if section in [
                        'Default', 'Protection', 'General', 'Trap', 'Magnet'
                ]:
                    continue

                for attr in config.options(section):
                    v = config.getfloat(section, attr)
                    if v is not None:
                        if section == 'Deflections':
                            defl[attr.upper()] = v
                        else:
                            d[attr] = v

            section = 'Trap'
            if config.has_section(section):
                for attr in ('current', 'ramp_step', 'ramp_period',
                             'ramp_tolerance'):
                    if config.has_option(section, attr):
                        trap[attr] = config.getfloat(section, attr)

            section = 'Magnet'
            magnet = {}
            if config.has_section(section):
                for attr in ('mftable', ):
                    if config.has_option(section, attr):
                        magnet[attr] = config.get(section, attr)

            if 'hv' in d:
                self.source.nominal_hv = d['hv']

            self._config = (d, defl, trap, magnet)

        return self._config
Beispiel #10
0
    def _get_cached_config(self):
        if self._config is None:
            p = get_spectrometer_config_path()
            if not os.path.isfile(p):
                self.warning('Spectrometer configuration file {} not found'.format(p))
                return

            self.debug('caching configuration from {}'.format(p))
            config = self.get_configuration_writer(p)
            d = {}
            defl = {}
            trap = {}
            for section in config.sections():
                if section in ['Default', 'Protection', 'General', 'Trap',
                               'Magnet']:
                    continue

                for attr in config.options(section):
                    v = config.getfloat(section, attr)
                    if v is not None:
                        if section == 'Deflections':
                            defl[attr.upper()] = v
                        else:
                            d[attr] = v

            section = 'Trap'
            if config.has_section(section):
                for attr in (
                        'current', 'ramp_step', 'ramp_period',
                        'ramp_tolerance'):
                    if config.has_option(section, attr):
                        trap[attr] = config.getfloat(section, attr)

            section = 'Magnet'
            magnet = {}
            if config.has_section(section):
                for attr in ('mftable',):
                    if config.has_option(section, attr):
                        magnet[attr] = config.get(section, attr)

            if 'hv' in d:
                self.source.nominal_hv = d['hv']

            self._config = (d, defl, trap, magnet)

        return self._config
    def load(self):
        cfp = ConfigParser()
        # p = os.path.join(paths.spectrometer_dir, 'config.cfg')
        p = get_spectrometer_config_path()
        cfp.read(p)
        gs = []
        for section in cfp.sections():
            g = SpectrometerParametersGroup(name=section)
            ps = []
            for pp in cfp.options(section):
                v = cfp.getfloat(section, pp)
                ps.append(Parameter(name=pp, value=v))

            g.parameters = ps
            gs.append(g)

        self.groups = gs
Beispiel #12
0
    def load(self):
        self.load_molecular_weights()
        self.load_detectors()
        self.magnet.load()
        # load local configurations
        self.spectrometer_configurations = glob_list_directory(
            paths.spectrometer_config_dir,
            remove_extension=True,
            extension='.cfg')

        name = get_spectrometer_config_name()
        sc, _ = os.path.splitext(name)
        self.spectrometer_configuration = sc

        p = get_spectrometer_config_path(name)
        config = self.get_configuration_writer(p)

        return config
Beispiel #13
0
    def _post_execute(self):
        """
            calculate all peak centers

            calculate relative shifts to a reference detector. not necessarily the same
            as the reference detector used for setting the magnet
        """
        graph = self.graph
        plot = graph.plots[0]
        time.sleep(0.05)

        # wait for graph to fully update
        time.sleep(0.1)

        # def get_peak_center(i, di):
        def get_peak_center(di):
            try:
                lp = plot.plots[di][0]
            except KeyError:
                lp = plot.plots["*{}".format(di)][0]

            xs = lp.index.get_data()
            ys = lp.value.get_data()

            cx = None
            if len(xs) and len(ys):
                try:
                    result = calculate_peak_center(xs, ys)
                    cx = result[0][1]
                except PeakCenterError:
                    self.warning("no peak center for {}".format(di))

            return cx

        spec = self.spectrometer

        centers = {d: get_peak_center(d) for d in self.active_detectors}
        print centers
        ref = self.reference_detector
        post = centers[ref]
        if post is None:
            return

        results = []
        for di in self.active_detectors:
            di = spec.get_detector(di)
            cen = centers[di.name]
            if cen is None:
                continue

            dac_dev = post - cen
            if self.spectrometer.simulation:
                dac_dev = -random()

            if abs(dac_dev) < 0.001:
                self.info("no offset detected between {} and {}".format(ref, di.name))
                continue

            defl = di.map_dac_to_deflection(dac_dev)
            self.info("{} dac dev. {:0.5f}. converted to deflection voltage {:0.1f}.".format(di.name, dac_dev, defl))

            curdefl = di.deflection
            newdefl = int(curdefl + defl)
            newdefl = max(0, min(newdefl, self.spectrometer.max_deflection))

            if newdefl >= 0:
                results.append(DeflectionResult(di.name, curdefl, newdefl))

        if not results:
            self.information_dialog("no deflection changes needed")
        else:
            rv = ResultsView(results=results)
            info = rv.edit_traits()
            if info.result:
                config = ConfigParser()
                # p = os.path.join(paths.spectrometer_dir, 'config.cfg')
                p = get_spectrometer_config_path()
                config.read(p)
                for v in rv.clean_results:
                    config.set("Deflections", v.name, v.new_deflection)
                    det = next((d for d in self.active_detectors if d.lower() == v.name.lower()))
                    det = spec.get_detector(det)
                    det.deflection = v.new_deflection

                with open(p, "w") as wfile:
                    config.write(wfile)

                self.spectrometer.clear_cached_config()
Beispiel #14
0
    def _post_execute(self):
        """
            calculate all peak centers

            calculate relative shifts to a reference detector. not necessarily the same
            as the reference detector used for setting the magnet
        """
        graph = self.graph
        plot = graph.plots[0]

        # time.sleep(0.05)

        # wait for graph to fully update
        # time.sleep(0.1)

        # def get_peak_center(i, di):
        def get_peak_center(di):
            try:
                lp = plot.plots[di][0]
            except KeyError:
                lp = plot.plots['*{}'.format(di)][0]

            xs = lp.index.get_data()
            ys = lp.value.get_data()

            cx = None
            if len(xs) and len(ys):
                try:
                    result = calculate_peak_center(xs, ys)
                    cx = result[0][1]
                except PeakCenterError:
                    self.warning('no peak center for {}'.format(di))

            return cx

        spec = self.spectrometer

        centers = {d: get_peak_center(d) for d in self.active_detectors}
        print(centers)
        ref = self.reference_detector
        post = centers[ref]
        if post is None:
            return

        results = []
        for di in self.active_detectors:
            di = spec.get_detector(di)
            cen = centers[di.name]
            if cen is None:
                continue

            dac_dev = post - cen
            if self.spectrometer.simulation:
                dac_dev = -random()

            if abs(dac_dev) < 0.001:
                self.info('no offset detected between {} and {}'.format(
                    ref, di.name))
                continue

            defl = di.map_dac_to_deflection(dac_dev)
            self.info(
                '{} dac dev. {:0.5f}. converted to deflection voltage {:0.1f}.'
                .format(di.name, dac_dev, defl))

            curdefl = di.deflection
            newdefl = int(curdefl + defl)
            newdefl = max(0, min(newdefl, self.spectrometer.max_deflection))

            if newdefl >= 0:
                results.append(DeflectionResult(di.name, curdefl, newdefl))

        if not results:
            self.information_dialog('no deflection changes needed')
        else:
            rv = ResultsView(results=results)
            info = rv.edit_traits()
            if info.result:
                config = ConfigParser()
                # p = os.path.join(paths.spectrometer_dir, 'config.cfg')
                p = get_spectrometer_config_path()
                config.read(p)
                for v in rv.clean_results:
                    config.set('Deflections', v.name, v.new_deflection)
                    det = next((d for d in self.active_detectors
                                if d.lower() == v.name.lower()))
                    det = spec.get_detector(det)
                    det.deflection = v.new_deflection

                with open(p, 'w') as wfile:
                    config.write(wfile)

                self.spectrometer.clear_cached_config()
Beispiel #15
0
    def load(self):
        """
        load detectors
        load setupfiles/spectrometer/config.cfg file
        load magnet
        load deflections coefficients

        :return:
        """
        self.load_detectors()

        # load local configurations
        self.spectrometer_configurations = list_directory2(
            paths.spectrometer_config_dir,
            remove_extension=True,
            extension='.cfg')

        name = get_spectrometer_config_name()
        sc, _ = os.path.splitext(name)
        self.spectrometer_configuration = sc

        p = get_spectrometer_config_path(name)
        config = self.get_configuration_writer(p)
        pd = 'Protection'

        if config.has_section(pd):

            self.magnet.use_beam_blank = self.config_get(config, pd,
                                                         'use_beam_blank',
                                                         cast='boolean',
                                                         default=False)
            self.magnet.use_detector_protection = self.config_get(config, pd,
                                                                  'use_detector_protection',
                                                                  cast='boolean',
                                                                  default=False)
            self.magnet.beam_blank_threshold = self.config_get(config, pd,
                                                               'beam_blank_threshold',
                                                               cast='float',
                                                               default=0.1)

            # self.magnet.detector_protection_threshold = self.config_get(config, pd,
            # 'detector_protection_threshold',
            # cast='float', default=0.1)
            ds = self.config_get(config, pd, 'detectors')
            if ds:
                ds = ds.split(',')
                self.magnet.protected_detectors = ds
                for di in ds:
                    self.info(
                        'Making protection available for detector "{}"'.format(
                            di))

        if config.has_section('Deflections'):
            if config.has_option('Deflections', 'max'):
                v = config.getint('Deflections', 'max')
                if v:
                    self.max_deflection = v

        self.magnet.load()

        self.debug('Detectors {}'.format(self.detectors))
        for d in self.detectors:
            d.load_deflection_coefficients()