|
| 1 | +from constellation.core.configuration import Configuration |
| 2 | +from constellation.core.satellite import Satellite |
| 3 | +import time |
| 4 | +from constellation.core.cmdp import MetricsType |
| 5 | +from constellation.core.fsm import SatelliteState |
| 6 | +from constellation.core.monitoring import schedule_metric |
| 7 | +import os |
| 8 | +import yaml |
| 9 | +import pymosa |
| 10 | +from pymosa.m26 import m26 |
| 11 | +import logging |
| 12 | +from pymosa.m26_raw_data import save_configuration_dict |
| 13 | +from time import sleep, strftime, time |
| 14 | +from tqdm import tqdm |
| 15 | +from typing import Any |
| 16 | + |
| 17 | + |
| 18 | +class Pymosa(Satellite): |
| 19 | + def __init__(self, *args, **kwargs): |
| 20 | + super().__init__(*args, **kwargs) |
| 21 | + |
| 22 | + def do_initializing(self, config: Configuration) -> str: |
| 23 | + # Open Mimosa26 std. configuration |
| 24 | + pymosa_path = os.path.dirname(pymosa.__file__) |
| 25 | + with open(os.path.join(pymosa_path, 'm26_configuration.yaml'), 'r') as f: |
| 26 | + self.yaml_config = yaml.safe_load(f) |
| 27 | + self._load_config(config) |
| 28 | + # Create telescope object and load hardware configuration |
| 29 | + self.telescope = m26(conf=None) # None: use default hardware configuration |
| 30 | + return "init done" |
| 31 | + |
| 32 | + def do_launching(self) -> str: |
| 33 | + # Initialize telescope hardware and set up parameters |
| 34 | + self.telescope.init(init_conf=self.yaml_config) |
| 35 | + return "launching done" |
| 36 | + |
| 37 | + def do_run(self, payload=None) -> str: |
| 38 | + self._pre_run() |
| 39 | + with self.telescope.access_file(): |
| 40 | + save_configuration_dict(self.telescope.raw_data_file.h5_file, 'configuration', self.telescope.telescope_conf) |
| 41 | + with self.telescope.readout(enabled_m26_channels=self.telescope.enabled_m26_channels): |
| 42 | + got_data = False |
| 43 | + start = time() |
| 44 | + while not self.stop_scan and not self._state_thread_evt.is_set(): |
| 45 | + sleep(1.0) |
| 46 | + if not got_data: |
| 47 | + if self.telescope.m26_readout.data_words_per_second()[0] > 0: |
| 48 | + got_data = True |
| 49 | + logging.info('Taking data...') |
| 50 | + if self.telescope.max_triggers: |
| 51 | + self.pbar = tqdm(total=self.telescope.max_triggers, ncols=80) |
| 52 | + else: |
| 53 | + self.pbar = tqdm(total=self.telescope.scan_timeout, ncols=80) |
| 54 | + else: |
| 55 | + triggers = self.telescope.dut['TLU']['TRIGGER_COUNTER'] |
| 56 | + try: |
| 57 | + if self.telescope.max_triggers: |
| 58 | + self.pbar.update(triggers - self.pbar.n) |
| 59 | + else: |
| 60 | + self.pbar.update(time() - start - self.pbar.n) |
| 61 | + except ValueError: |
| 62 | + pass |
| 63 | + if self.telescope.max_triggers and triggers >= self.telescope.max_triggers: |
| 64 | + self.stop_scan = True |
| 65 | + self.pbar.close() |
| 66 | + logging.info('Trigger limit was reached: %i' % self.telescope.max_triggers) |
| 67 | + logging.info('Total amount of triggers collected: %d', self.telescope.dut['TLU']['TRIGGER_COUNTER']) |
| 68 | + self.logger.removeHandler(self.fh) |
| 69 | + logging.info('Data Output Filename: %s', self.telescope.run_filename + '.h5') |
| 70 | + self.telescope.analyze() |
| 71 | + return "running done" |
| 72 | + |
| 73 | + def do_landing(self) -> str: |
| 74 | + # Close the resources |
| 75 | + self.telescope.close() |
| 76 | + return "landing done" |
| 77 | + |
| 78 | + def _pre_run(self) -> None: |
| 79 | + self.stop_scan = False |
| 80 | + # signal.signal(signal.SIGINT, self.telescope._signal_handler) |
| 81 | + logging.info('Press Ctrl-C to stop run') |
| 82 | + |
| 83 | + # check for filename that is not in use |
| 84 | + while True: |
| 85 | + if not self.telescope.output_filename and self.telescope.run_number: |
| 86 | + filename = 'run_' + str(self.telescope.run_number) + '_' + self.telescope.run_id |
| 87 | + |
| 88 | + else: |
| 89 | + if self.telescope.output_filename: |
| 90 | + filename = self.telescope.output_filename |
| 91 | + else: |
| 92 | + filename = strftime("%Y%m%d-%H%M%S") + '_' + self.telescope.run_id |
| 93 | + if filename in [os.path.splitext(f)[0] for f in os.listdir(self.telescope.working_dir) if os.path.isfile(os.path.join(self.telescope.working_dir, f))]: |
| 94 | + if not self.telescope.output_filename and self.telescope.run_number: |
| 95 | + self.telescope.run_number += 1 # increase run number and try again |
| 96 | + continue |
| 97 | + else: |
| 98 | + raise IOError("Filename %s already exists." % filename) |
| 99 | + else: |
| 100 | + self.telescope.run_filename = os.path.join(self.telescope.working_dir, filename) |
| 101 | + break |
| 102 | + |
| 103 | + # set up logger |
| 104 | + self.fh = logging.FileHandler(self.telescope.run_filename + '.log') |
| 105 | + self.fh.setLevel(logging.DEBUG) |
| 106 | + FORMAT = '%(asctime)s [%(name)-17s] - %(levelname)-7s %(message)s' |
| 107 | + self.fh.setFormatter(logging.Formatter(FORMAT)) |
| 108 | + self.logger = logging.getLogger() |
| 109 | + self.logger.addHandler(self.fh) |
| 110 | + self.telescope.dut['TLU']['TRIGGER_COUNTER'] = 0 |
| 111 | + |
| 112 | + def _load_config(self, config: Configuration) -> None: |
| 113 | + config.set_default(key='scan_timeout', value=None) |
| 114 | + config.set_default(key='run_number', value=None) |
| 115 | + config.set_default(key='output_folder', value=None) |
| 116 | + config.set_default(key='m26_configuration_file', value=None) |
| 117 | + config.set_default(key='m26_jtag_configuration', value=True) |
| 118 | + config.set_default(key='enabled_m26_channels', value=None) |
| 119 | + |
| 120 | + self.yaml_config['no_data_timeout'] = config.get(key='no_data_timeout') |
| 121 | + self.yaml_config['send_data'] = config.get(key='send_data') |
| 122 | + self.yaml_config['max_triggers'] = config.get(key='max_triggers') |
| 123 | + self.yaml_config['scan_timeout'] = config.get(key='scan_timeout') |
| 124 | + self.yaml_config['run_number'] = config.get(key='run_number') |
| 125 | + self.yaml_config['output_folder'] = config.get(key='output_folder') |
| 126 | + self.yaml_config['m26_configuration_file'] = config.get(key='m26_configuration_file') |
| 127 | + self.yaml_config['m26_jtag_configuration'] = config.get(key='m26_jtag_configuration') |
| 128 | + self.yaml_config['enabled_m26_channels'] = config.get(key='enabled_m26_channels') |
| 129 | + |
| 130 | + @schedule_metric("", MetricsType.LAST_VALUE, 1) |
| 131 | + def trigger_number(self) -> int | None: |
| 132 | + if self.fsm.current_state_value == SatelliteState.RUN: |
| 133 | + return self.telescope.dut['TLU']['TRIGGER_COUNTER'] |
| 134 | + else: |
| 135 | + return None |
0 commit comments