thzdaq-core 0.0.6b4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- thzdaq_core/__init__.py +1 -0
- thzdaq_core/adapters/__init__.py +3 -0
- thzdaq_core/adapters/instrument_states.py +51 -0
- thzdaq_core/adapters/json_store.py +177 -0
- thzdaq_core/adapters/legacy_profiles.py +247 -0
- thzdaq_core/adapters/library.py +124 -0
- thzdaq_core/adapters/processes.py +50 -0
- thzdaq_core/adapters/yaml_profiles.py +117 -0
- thzdaq_core/application/__init__.py +1 -0
- thzdaq_core/application/approvals.py +150 -0
- thzdaq_core/application/assembly.py +187 -0
- thzdaq_core/application/bench.py +91 -0
- thzdaq_core/application/check.py +339 -0
- thzdaq_core/application/clock.py +8 -0
- thzdaq_core/application/connections.py +31 -0
- thzdaq_core/application/custom.py +370 -0
- thzdaq_core/application/daemon.py +150 -0
- thzdaq_core/application/describe.py +296 -0
- thzdaq_core/application/devices.py +438 -0
- thzdaq_core/application/dispatch.py +93 -0
- thzdaq_core/application/events.py +97 -0
- thzdaq_core/application/handlers/__init__.py +48 -0
- thzdaq_core/application/handlers/analysis.py +90 -0
- thzdaq_core/application/handlers/approvals.py +51 -0
- thzdaq_core/application/handlers/custom.py +94 -0
- thzdaq_core/application/handlers/daemon.py +90 -0
- thzdaq_core/application/handlers/devices.py +182 -0
- thzdaq_core/application/handlers/measurements.py +206 -0
- thzdaq_core/application/handlers/scripts.py +67 -0
- thzdaq_core/application/handlers/sessions.py +144 -0
- thzdaq_core/application/handlers/state.py +54 -0
- thzdaq_core/application/handlers/station.py +145 -0
- thzdaq_core/application/handlers/ui.py +85 -0
- thzdaq_core/application/kept.py +61 -0
- thzdaq_core/application/local.py +101 -0
- thzdaq_core/application/policy.py +88 -0
- thzdaq_core/application/polling.py +133 -0
- thzdaq_core/application/ports.py +163 -0
- thzdaq_core/application/procedures.py +42 -0
- thzdaq_core/application/processes.py +364 -0
- thzdaq_core/application/runner.py +413 -0
- thzdaq_core/application/runs.py +522 -0
- thzdaq_core/application/scripts.py +612 -0
- thzdaq_core/application/station.py +294 -0
- thzdaq_core/application/values.py +29 -0
- thzdaq_core/application/windows.py +114 -0
- thzdaq_core/application/workspaces.py +123 -0
- thzdaq_core/cli/__init__.py +3 -0
- thzdaq_core/cli/station.py +108 -0
- thzdaq_core/domain/__init__.py +5 -0
- thzdaq_core/domain/errors.py +9 -0
- thzdaq_core/domain/leases.py +110 -0
- thzdaq_core/domain/measurements/__init__.py +1 -0
- thzdaq_core/domain/measurements/catalog.py +37 -0
- thzdaq_core/domain/measurements/custom.py +189 -0
- thzdaq_core/domain/measurements/procedure.py +218 -0
- thzdaq_core/domain/measurements/procedures/__init__.py +37 -0
- thzdaq_core/domain/measurements/procedures/common.py +203 -0
- thzdaq_core/domain/measurements/procedures/grid.py +55 -0
- thzdaq_core/domain/measurements/procedures/if_noise.py +167 -0
- thzdaq_core/domain/measurements/procedures/iv_curve.py +70 -0
- thzdaq_core/domain/measurements/procedures/monitor.py +183 -0
- thzdaq_core/domain/measurements/procedures/pv_curve.py +208 -0
- thzdaq_core/domain/measurements/procedures/reflection.py +120 -0
- thzdaq_core/domain/measurements/procedures/shot_noise.py +166 -0
- thzdaq_core/domain/measurements/procedures/sis_tools.py +141 -0
- thzdaq_core/domain/measurements/procedures/two_sb/__init__.py +21 -0
- thzdaq_core/domain/measurements/procedures/two_sb/balance.py +239 -0
- thzdaq_core/domain/measurements/procedures/two_sb/common.py +327 -0
- thzdaq_core/domain/measurements/procedures/two_sb/optimization.py +159 -0
- thzdaq_core/domain/measurements/procedures/two_sb/srr_tn.py +238 -0
- thzdaq_core/domain/parameters.py +388 -0
- thzdaq_core/domain/sis/__init__.py +1 -0
- thzdaq_core/domain/sis/calibration.py +224 -0
- thzdaq_core/domain/sis/demagnetization.py +38 -0
- thzdaq_core/domain/station/__init__.py +1 -0
- thzdaq_core/domain/station/config.py +114 -0
- thzdaq_core/domain/station/kinds.py +133 -0
- thzdaq_core/domain/station/ports.py +189 -0
- thzdaq_core/domain/station/profile.py +320 -0
- thzdaq_core/domain/station/safety.py +149 -0
- thzdaq_core/domain/station/station.py +733 -0
- thzdaq_core-0.0.6b4.dist-info/METADATA +96 -0
- thzdaq_core-0.0.6b4.dist-info/RECORD +87 -0
- thzdaq_core-0.0.6b4.dist-info/WHEEL +4 -0
- thzdaq_core-0.0.6b4.dist-info/entry_points.txt +2 -0
- thzdaq_core-0.0.6b4.dist-info/licenses/LICENSE +21 -0
thzdaq_core/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""ThzDAQ core: domain, application and adapters of the bench, no GUI and no network."""
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""The InstrumentStates port in a JSON file of the daemon (state.json next to
|
|
2
|
+
daemon.json; the simulator's state-sim.json): every record by instrument id,
|
|
3
|
+
the whole file written again at each change, atomically (.partial, then
|
|
4
|
+
os.replace), under a lock: moves come from several threads. Only its own
|
|
5
|
+
daemon writes it (one per folder, FileLock).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
import os
|
|
11
|
+
import threading
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any, Dict, Mapping, Optional
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class JsonInstrumentStates:
|
|
19
|
+
def __init__(self, path: Path):
|
|
20
|
+
self.path = path
|
|
21
|
+
self._lock = threading.Lock()
|
|
22
|
+
self._records: Dict[str, Dict[str, Any]] = self._load()
|
|
23
|
+
|
|
24
|
+
def _load(self) -> Dict[str, Dict[str, Any]]:
|
|
25
|
+
try:
|
|
26
|
+
found = json.loads(self.path.read_text(encoding="utf-8"))
|
|
27
|
+
except FileNotFoundError:
|
|
28
|
+
return {}
|
|
29
|
+
except (OSError, ValueError) as e:
|
|
30
|
+
logger.error(f"{self.path} is not read, nothing kept is applied: {e}")
|
|
31
|
+
return {}
|
|
32
|
+
if not isinstance(found, dict):
|
|
33
|
+
logger.error(f"{self.path} holds no records, nothing kept is applied")
|
|
34
|
+
return {}
|
|
35
|
+
return {key: value for key, value in found.items() if isinstance(value, dict)}
|
|
36
|
+
|
|
37
|
+
def get(self, instrument_id: str) -> Optional[Mapping[str, Any]]:
|
|
38
|
+
with self._lock:
|
|
39
|
+
record = self._records.get(instrument_id)
|
|
40
|
+
return None if record is None else dict(record)
|
|
41
|
+
|
|
42
|
+
def put(self, instrument_id: str, record: Mapping[str, Any]) -> None:
|
|
43
|
+
with self._lock:
|
|
44
|
+
self._records[instrument_id] = dict(record)
|
|
45
|
+
text = json.dumps(self._records, indent=2, sort_keys=True)
|
|
46
|
+
partial = self.path.with_suffix(".partial")
|
|
47
|
+
try:
|
|
48
|
+
partial.write_text(text, encoding="utf-8")
|
|
49
|
+
os.replace(partial, self.path)
|
|
50
|
+
except OSError as e:
|
|
51
|
+
logger.error(f"{self.path}: {instrument_id} is not kept: {e}")
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Measurements on disk, as the daemon writes them: one JSON file each,
|
|
2
|
+
<root>/<YYYY-MM-DD>/<id>_<procedure>.json (thzdaq_api.measurement_file), the
|
|
3
|
+
procedure's name with _ for what a file name may not hold.
|
|
4
|
+
|
|
5
|
+
Ids are numbers counted per data folder: the next one is one more than the
|
|
6
|
+
largest in the file names, so they keep growing across runs of the program.
|
|
7
|
+
Start and end times (to the millisecond) are in every file as well.
|
|
8
|
+
|
|
9
|
+
While a measurement runs (RunningFile), its file holds its description
|
|
10
|
+
without records, written again only when it changes, and its records are
|
|
11
|
+
appended to the records file next to it, a line each; at its end the whole
|
|
12
|
+
file is written and the records file removed. A long monitor appends a line
|
|
13
|
+
per record instead of writing megabytes every few seconds. Writes of the file
|
|
14
|
+
go to a temporary file first, so a crash leaves the previous complete
|
|
15
|
+
version; a file still marked running reads back as interrupted, with the
|
|
16
|
+
records of its records file.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import logging
|
|
20
|
+
import os
|
|
21
|
+
import re
|
|
22
|
+
import threading
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from typing import IO, Any, Dict, Mapping, Optional, Tuple
|
|
25
|
+
|
|
26
|
+
from thzdaq_api.measurement_file import (
|
|
27
|
+
RUNNING,
|
|
28
|
+
Column,
|
|
29
|
+
Measurement,
|
|
30
|
+
MeasurementFolder,
|
|
31
|
+
PathLike,
|
|
32
|
+
dumps,
|
|
33
|
+
file_id,
|
|
34
|
+
record_line,
|
|
35
|
+
records_path,
|
|
36
|
+
write_file,
|
|
37
|
+
write_measurement,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
logger = logging.getLogger(__name__)
|
|
41
|
+
|
|
42
|
+
# What a file name of every system allows of a procedure's name: letters,
|
|
43
|
+
# digits, _ and -; a script's "script:<title>" has a colon (a stream of a
|
|
44
|
+
# file on NTFS) and may have a slash (a folder)
|
|
45
|
+
UNSAFE = re.compile(r"[^\w-]+")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class MeasurementStore(MeasurementFolder):
|
|
49
|
+
"""A folder of measurement files the daemon numbers and writes."""
|
|
50
|
+
|
|
51
|
+
def __init__(self, root: PathLike):
|
|
52
|
+
super().__init__(root)
|
|
53
|
+
self._last = 0
|
|
54
|
+
self._lock = threading.Lock()
|
|
55
|
+
|
|
56
|
+
def next_id(self) -> int:
|
|
57
|
+
"""A number larger than every measurement in the folder and given so far."""
|
|
58
|
+
with self._lock:
|
|
59
|
+
found = [file_id(path) for path in self.paths()]
|
|
60
|
+
self._last = max([self._last, *(i for i in found if i is not None)]) + 1
|
|
61
|
+
return self._last
|
|
62
|
+
|
|
63
|
+
def path_for(self, measurement: Measurement) -> Path:
|
|
64
|
+
"""data/<day it started>/<number>_<procedure>.json"""
|
|
65
|
+
if not measurement.started:
|
|
66
|
+
raise ValueError(
|
|
67
|
+
"a measurement has no file before it starts (no start time)"
|
|
68
|
+
)
|
|
69
|
+
day = measurement.started[:10]
|
|
70
|
+
procedure = UNSAFE.sub("_", measurement.procedure)
|
|
71
|
+
number = measurement.id
|
|
72
|
+
name = f"{number:06d}" if isinstance(number, int) else str(number)
|
|
73
|
+
return self.root / day / f"{name}_{procedure}.json"
|
|
74
|
+
|
|
75
|
+
def open(self, measurement: Measurement) -> "RunningFile":
|
|
76
|
+
return RunningFile(self, measurement, self.path_for(measurement))
|
|
77
|
+
|
|
78
|
+
def save(self, measurement: Measurement, path: Optional[PathLike] = None) -> Path:
|
|
79
|
+
"""The whole measurement; one that ended needs its records file no
|
|
80
|
+
more (a file left by a crash is gathered so)."""
|
|
81
|
+
path = write_measurement(measurement, path or self.path_for(measurement))
|
|
82
|
+
measurement.metadata["path"] = str(path)
|
|
83
|
+
if measurement.status != RUNNING:
|
|
84
|
+
records_path(path).unlink(missing_ok=True)
|
|
85
|
+
return path
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
Shape = Tuple[str, Tuple[Column, ...]] # a table as its description has it
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class RunningFile:
|
|
92
|
+
"""The file of a running measurement (MeasurementWriter): its description
|
|
93
|
+
when it starts and whenever it changes, a line in the records file per
|
|
94
|
+
record, the whole file at its end.
|
|
95
|
+
|
|
96
|
+
A line is flushed as it is written, so it is the operating system's once
|
|
97
|
+
append returns; update puts the lines on the drive itself. A table or a
|
|
98
|
+
column the description lacks is described before its record is appended,
|
|
99
|
+
so every line has its column titles and units. When a line cannot be
|
|
100
|
+
written, the records file stops and every update writes the whole file,
|
|
101
|
+
as before records files."""
|
|
102
|
+
|
|
103
|
+
def __init__(self, store: MeasurementStore, measurement: Measurement, path: Path):
|
|
104
|
+
self.store = store
|
|
105
|
+
self.measurement = measurement
|
|
106
|
+
self.path = path
|
|
107
|
+
self._description = "" # as the file holds it
|
|
108
|
+
self._shapes: Dict[str, Shape] = {}
|
|
109
|
+
self._records: Optional[IO[str]] = None
|
|
110
|
+
measurement.metadata["path"] = str(path)
|
|
111
|
+
self._describe()
|
|
112
|
+
try:
|
|
113
|
+
self._records = open(
|
|
114
|
+
records_path(path), "w", encoding="utf-8", newline="\n"
|
|
115
|
+
)
|
|
116
|
+
except OSError:
|
|
117
|
+
logger.exception(f"{path}: no records file, the whole file is saved")
|
|
118
|
+
|
|
119
|
+
def append(self, table: str, record: Mapping[str, Any]) -> None:
|
|
120
|
+
if self._records is None:
|
|
121
|
+
return # update saves the whole file
|
|
122
|
+
if self._shape(table) != self._shapes.get(table):
|
|
123
|
+
self._describe()
|
|
124
|
+
try:
|
|
125
|
+
self._records.write(record_line(table, record))
|
|
126
|
+
self._records.flush()
|
|
127
|
+
except OSError:
|
|
128
|
+
logger.exception(f"{self.path}: records stop, the whole file is saved")
|
|
129
|
+
self._stop_records()
|
|
130
|
+
self._save()
|
|
131
|
+
|
|
132
|
+
def update(self) -> None:
|
|
133
|
+
if self._records is None:
|
|
134
|
+
self._save()
|
|
135
|
+
return
|
|
136
|
+
self._describe()
|
|
137
|
+
try:
|
|
138
|
+
os.fsync(self._records.fileno())
|
|
139
|
+
except OSError:
|
|
140
|
+
logger.exception(f"{self.path}: records not synced")
|
|
141
|
+
|
|
142
|
+
def close(self) -> None:
|
|
143
|
+
self._stop_records()
|
|
144
|
+
self._save()
|
|
145
|
+
|
|
146
|
+
def _shape(self, table: str) -> Shape:
|
|
147
|
+
found = self.measurement.tables[table]
|
|
148
|
+
return found.title, tuple(found.columns)
|
|
149
|
+
|
|
150
|
+
def _describe(self) -> None:
|
|
151
|
+
"""Its description, when it is not the file's already (one that
|
|
152
|
+
failed is tried again by the next update, not by every record)."""
|
|
153
|
+
text = dumps(self.measurement.to_dict(records=False), indent=1)
|
|
154
|
+
if text == self._description:
|
|
155
|
+
return
|
|
156
|
+
self._shapes = {name: self._shape(name) for name in self.measurement.tables}
|
|
157
|
+
try:
|
|
158
|
+
write_file(self.path, text)
|
|
159
|
+
except OSError:
|
|
160
|
+
logger.exception(f"{self.path}: description not saved")
|
|
161
|
+
return
|
|
162
|
+
self._description = text
|
|
163
|
+
|
|
164
|
+
def _save(self) -> None:
|
|
165
|
+
try:
|
|
166
|
+
self.store.save(self.measurement, self.path)
|
|
167
|
+
except OSError:
|
|
168
|
+
logger.exception(f"{self.path}: not saved")
|
|
169
|
+
|
|
170
|
+
def _stop_records(self) -> None:
|
|
171
|
+
if self._records is None:
|
|
172
|
+
return
|
|
173
|
+
records, self._records = self._records, None
|
|
174
|
+
try:
|
|
175
|
+
records.close()
|
|
176
|
+
except OSError:
|
|
177
|
+
logger.exception(f"{self.path}: records file not closed")
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""Import device configs of the legacy app (settings.ini, [Configs]) into a station profile.
|
|
2
|
+
|
|
3
|
+
Legacy entries are per "device widget": every SIS config (bias DEV, coil DEV,
|
|
4
|
+
offsets, calibration) on one Scontel block was its own entry. Here a block is
|
|
5
|
+
one instrument and each config becomes a bias and a coil device on it.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from collections import OrderedDict
|
|
10
|
+
from typing import Any, Dict, List, Mapping, Optional, Tuple
|
|
11
|
+
|
|
12
|
+
from thzdaq_instruments.drivers import (
|
|
13
|
+
AgilentE8247C,
|
|
14
|
+
ArduinoGrid,
|
|
15
|
+
Keithley2200,
|
|
16
|
+
LakeShore336,
|
|
17
|
+
ModbusChopper,
|
|
18
|
+
NiYigFilter,
|
|
19
|
+
RigolDP832A,
|
|
20
|
+
RohdeSchwarzFSEK30,
|
|
21
|
+
RohdeSchwarzHMP2030,
|
|
22
|
+
RohdeSchwarzNRX,
|
|
23
|
+
RohdeSchwarzZVA67,
|
|
24
|
+
ScontelSisBlock,
|
|
25
|
+
SumitomoF70Rest,
|
|
26
|
+
)
|
|
27
|
+
from thzdaq_instruments.transport.base import (
|
|
28
|
+
HTTP,
|
|
29
|
+
PROLOGIX,
|
|
30
|
+
PROLOGIX_USB,
|
|
31
|
+
SERIAL,
|
|
32
|
+
SOCKET,
|
|
33
|
+
ConnectionSpec,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
from thzdaq_core.domain.errors import StationError
|
|
37
|
+
from thzdaq_core.domain.sis.calibration import CALIBRATION_KEY
|
|
38
|
+
from thzdaq_core.domain.station.kinds import instrument_type
|
|
39
|
+
from thzdaq_core.domain.station.config import (
|
|
40
|
+
DeviceConfig,
|
|
41
|
+
InstrumentConfig,
|
|
42
|
+
StationConfig,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
LEGACY_DRIVERS = {
|
|
46
|
+
"Keithley Power Supply": Keithley2200.driver_key,
|
|
47
|
+
"Agilent Signal Generator": AgilentE8247C.driver_key,
|
|
48
|
+
"Rigol Power Supply": RigolDP832A.driver_key,
|
|
49
|
+
"Sumitomo F70 Compressor": SumitomoF70Rest.driver_key,
|
|
50
|
+
"LakeShore Temperature Controller": LakeShore336.driver_key,
|
|
51
|
+
"Rohde Schwarz VNA ZVA 67": RohdeSchwarzZVA67.driver_key,
|
|
52
|
+
"Rohde Schwarz Spectrum FSEK 30": RohdeSchwarzFSEK30.driver_key,
|
|
53
|
+
"Rohde Schwarz Power Supply": RohdeSchwarzHMP2030.driver_key,
|
|
54
|
+
"Scontel SIS Block": ScontelSisBlock.driver_key,
|
|
55
|
+
"GRID": ArduinoGrid.driver_key,
|
|
56
|
+
}
|
|
57
|
+
# Connection adapters, not instruments: their address is already in each device config
|
|
58
|
+
LEGACY_ADAPTER_SECTIONS = {"Prologix ethernet"}
|
|
59
|
+
LEGACY_KINDS = {
|
|
60
|
+
"SOCKET": SOCKET,
|
|
61
|
+
"SOCKET SINGLE": SOCKET,
|
|
62
|
+
"PROLOGIX ETHERNET": PROLOGIX,
|
|
63
|
+
"PROLOGIX USB": PROLOGIX_USB,
|
|
64
|
+
"HTTP": HTTP,
|
|
65
|
+
"SERIAL": SERIAL,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def slug(text: str) -> str:
|
|
70
|
+
value = re.sub(r"[^0-9a-zA-Z]+", "_", text).strip("_").lower()
|
|
71
|
+
return value if value and not value[0].isdigit() else f"i_{value}"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def next_id(device_type: str, taken: Mapping[str, Any]) -> str:
|
|
75
|
+
number = 1
|
|
76
|
+
while f"{device_type}_{number}" in taken:
|
|
77
|
+
number += 1
|
|
78
|
+
return f"{device_type}_{number}"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _offset(value: Any) -> float:
|
|
82
|
+
# The legacy UI stored offsets as summed floats (-0.00020000000000000004)
|
|
83
|
+
return round(float(value or 0), 12) + 0.0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def legacy_connection(entry: Mapping[str, Any], driver: str) -> ConnectionSpec:
|
|
87
|
+
adapter = str(entry.get("adapter") or "SOCKET").upper()
|
|
88
|
+
kind = LEGACY_KINDS.get(adapter)
|
|
89
|
+
if kind is None:
|
|
90
|
+
raise StationError(f"Unknown legacy adapter {adapter!r}")
|
|
91
|
+
host = entry.get("host") or None
|
|
92
|
+
port = int(entry["port"]) if str(entry.get("port") or "").isdigit() else None
|
|
93
|
+
gpib = entry.get("gpib")
|
|
94
|
+
if driver == ScontelSisBlock.driver_key:
|
|
95
|
+
return ScontelSisBlock.connection(host=host, port=port or 9876)
|
|
96
|
+
if driver == SumitomoF70Rest.driver_key:
|
|
97
|
+
return SumitomoF70Rest.connection(host=host, port=port or 21024)
|
|
98
|
+
if kind == HTTP:
|
|
99
|
+
# The grid answers after the move, give it time
|
|
100
|
+
timeout = 30.0 if driver == ArduinoGrid.driver_key else 2.0
|
|
101
|
+
return ConnectionSpec(HTTP, host=host, port=port, timeout=timeout)
|
|
102
|
+
if kind == PROLOGIX:
|
|
103
|
+
return ConnectionSpec(PROLOGIX, host=host, port=port, gpib=int(gpib))
|
|
104
|
+
if kind == PROLOGIX_USB:
|
|
105
|
+
serial_port = entry.get("port") or host
|
|
106
|
+
return ConnectionSpec(
|
|
107
|
+
PROLOGIX_USB, serial_port=str(serial_port), gpib=int(gpib)
|
|
108
|
+
)
|
|
109
|
+
if kind == SERIAL:
|
|
110
|
+
return ConnectionSpec(SERIAL, serial_port=str(entry.get("port") or host))
|
|
111
|
+
return ConnectionSpec(SOCKET, host=host, port=port)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _entry_name(section: str, entry: Mapping[str, Any]) -> str:
|
|
115
|
+
name = entry.get("name") or entry.get("_name") or section
|
|
116
|
+
if name == section and entry.get("cid") is not None:
|
|
117
|
+
name = f"{section} {entry['cid']}"
|
|
118
|
+
return str(name)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _state_defaults() -> Dict[str, InstrumentConfig]:
|
|
122
|
+
"""Instruments the legacy app addressed through hard-coded state constants."""
|
|
123
|
+
return {
|
|
124
|
+
"power_meter_1": InstrumentConfig(
|
|
125
|
+
driver=RohdeSchwarzNRX.driver_key,
|
|
126
|
+
connection=RohdeSchwarzNRX.connection(),
|
|
127
|
+
# The legacy driver configured the meter like this on every connect
|
|
128
|
+
options={"aperture_time": 0.05, "averaging": False},
|
|
129
|
+
title="R&S NRX",
|
|
130
|
+
),
|
|
131
|
+
"yig_filter_1": InstrumentConfig(
|
|
132
|
+
driver=NiYigFilter.driver_key,
|
|
133
|
+
connection=NiYigFilter.connection(),
|
|
134
|
+
title="NI YIG",
|
|
135
|
+
),
|
|
136
|
+
"chopper_1": InstrumentConfig(
|
|
137
|
+
driver=ModbusChopper.driver_key,
|
|
138
|
+
connection=ModbusChopper.connection(),
|
|
139
|
+
title="Chopper",
|
|
140
|
+
),
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def station_from_legacy(
|
|
145
|
+
configs: Mapping[str, List[Mapping[str, Any]]],
|
|
146
|
+
*,
|
|
147
|
+
name: str = "legacy",
|
|
148
|
+
include_state_defaults: bool = True,
|
|
149
|
+
) -> Tuple[StationConfig, List[str]]:
|
|
150
|
+
"""Returns the profile and notes about skipped or merged entries."""
|
|
151
|
+
instruments: "OrderedDict[str, InstrumentConfig]" = OrderedDict()
|
|
152
|
+
notes: List[str] = []
|
|
153
|
+
sis_blocks: Dict[tuple, str] = {}
|
|
154
|
+
sis_count = 0
|
|
155
|
+
|
|
156
|
+
for section, entries in configs.items():
|
|
157
|
+
if section in LEGACY_ADAPTER_SECTIONS:
|
|
158
|
+
notes.append(f"skipped adapter section {section!r}")
|
|
159
|
+
continue
|
|
160
|
+
driver = LEGACY_DRIVERS.get(section)
|
|
161
|
+
if driver is None:
|
|
162
|
+
notes.append(f"skipped unknown section {section!r}")
|
|
163
|
+
continue
|
|
164
|
+
for entry in entries or []:
|
|
165
|
+
title = _entry_name(section, entry)
|
|
166
|
+
connection = legacy_connection(entry, driver)
|
|
167
|
+
if driver == ScontelSisBlock.driver_key:
|
|
168
|
+
key = connection.link_key
|
|
169
|
+
instrument_id = sis_blocks.get(key)
|
|
170
|
+
if instrument_id is None:
|
|
171
|
+
instrument_id = next_id("sis_block", instruments)
|
|
172
|
+
sis_blocks[key] = instrument_id
|
|
173
|
+
instruments[instrument_id] = InstrumentConfig(
|
|
174
|
+
driver=driver,
|
|
175
|
+
connection=connection,
|
|
176
|
+
title=f"Scontel SIS block {len(sis_blocks)}",
|
|
177
|
+
)
|
|
178
|
+
else:
|
|
179
|
+
notes.append(f"{title!r} merged into {instrument_id}")
|
|
180
|
+
# One legacy SIS config = a bias and a coil device on the block
|
|
181
|
+
sis_count += 1
|
|
182
|
+
devices = instruments[instrument_id].devices
|
|
183
|
+
devices[f"sis_bias_{sis_count}"] = DeviceConfig(
|
|
184
|
+
kind="sis_bias",
|
|
185
|
+
channel=entry.get("bias_dev", "DEV4"),
|
|
186
|
+
title=title,
|
|
187
|
+
settings={
|
|
188
|
+
"offset_voltage": _offset(entry.get("offset_voltage")),
|
|
189
|
+
"offset_current": _offset(entry.get("offset_current")),
|
|
190
|
+
},
|
|
191
|
+
metadata=(
|
|
192
|
+
{CALIBRATION_KEY: entry["calibration_coefficients"]}
|
|
193
|
+
if entry.get("calibration_coefficients")
|
|
194
|
+
else {}
|
|
195
|
+
),
|
|
196
|
+
)
|
|
197
|
+
devices[f"sis_coil_{sis_count}"] = DeviceConfig(
|
|
198
|
+
kind="sis_coil",
|
|
199
|
+
channel=entry.get("ctrl_dev", "DEV3"),
|
|
200
|
+
title=f"{title} coil",
|
|
201
|
+
)
|
|
202
|
+
continue
|
|
203
|
+
instrument_id = next_id(instrument_type(driver), instruments)
|
|
204
|
+
instruments[instrument_id] = InstrumentConfig(
|
|
205
|
+
driver=driver, connection=connection, title=title
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
if include_state_defaults:
|
|
209
|
+
for instrument_id, spec in _state_defaults().items():
|
|
210
|
+
if instrument_id not in instruments:
|
|
211
|
+
instruments[instrument_id] = spec
|
|
212
|
+
notes.append(f"added {instrument_id} from legacy state defaults")
|
|
213
|
+
|
|
214
|
+
config = StationConfig(
|
|
215
|
+
name=name,
|
|
216
|
+
instruments=dict(instruments),
|
|
217
|
+
description="Imported from the legacy settings.ini",
|
|
218
|
+
).validate()
|
|
219
|
+
return config, notes
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def read_legacy_settings(path: str) -> Dict[str, List[Dict[str, Any]]]:
|
|
223
|
+
"""Read [Configs] of a legacy settings.ini (QVariant encoded, needs PySide6)."""
|
|
224
|
+
try:
|
|
225
|
+
from PySide6.QtCore import QSettings
|
|
226
|
+
except ImportError as e:
|
|
227
|
+
raise StationError("Reading legacy settings.ini needs PySide6") from e
|
|
228
|
+
settings = QSettings(str(path), QSettings.IniFormat)
|
|
229
|
+
settings.beginGroup("Configs")
|
|
230
|
+
configs: Dict[str, List[Dict[str, Any]]] = {}
|
|
231
|
+
for key in settings.childKeys():
|
|
232
|
+
value = settings.value(key)
|
|
233
|
+
if isinstance(value, dict):
|
|
234
|
+
value = [value]
|
|
235
|
+
configs[key] = [dict(entry) for entry in (value or [])]
|
|
236
|
+
settings.endGroup()
|
|
237
|
+
return configs
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def import_legacy_settings(
|
|
241
|
+
path: str, *, name: Optional[str] = None, include_state_defaults: bool = True
|
|
242
|
+
) -> Tuple[StationConfig, List[str]]:
|
|
243
|
+
return station_from_legacy(
|
|
244
|
+
read_legacy_settings(path),
|
|
245
|
+
name=name or "legacy",
|
|
246
|
+
include_state_defaults=include_state_defaults,
|
|
247
|
+
)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""The bench's library of its own measurements (the Library port): the Python
|
|
2
|
+
scripts of a folder, measurements/ of the program's. A script's text is
|
|
3
|
+
described by running it with DESCRIBE_ENV: its declarations go to a file,
|
|
4
|
+
and it ends after the last one, before it touches the bench
|
|
5
|
+
(thzdaq_api.custom). The text runs from a hidden copy next to the scripts:
|
|
6
|
+
it imports what they import, and a text not saved yet is described alike.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import hashlib
|
|
10
|
+
import os
|
|
11
|
+
import subprocess
|
|
12
|
+
import tempfile
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Callable, Sequence, Tuple
|
|
15
|
+
|
|
16
|
+
import msgspec
|
|
17
|
+
from thzdaq_api.custom import DESCRIBE_ENV, INPUTS_ENV, SUFFIX, Declarations
|
|
18
|
+
from thzdaq_api.host import apart, script_python
|
|
19
|
+
from thzdaq_api.scripts import OUTPUT_ENV
|
|
20
|
+
|
|
21
|
+
from thzdaq_core.application.ports import DescribeFailed, LibraryFile
|
|
22
|
+
|
|
23
|
+
DESCRIBE_S = 15.0 # a script describes itself within this, or is ended
|
|
24
|
+
TOLD = 4000 # characters of what a failed description printed, at most
|
|
25
|
+
SKIPPED = ("_", ".") # files that are no measurements: helpers, hidden ones
|
|
26
|
+
DECLARED = "declared.json"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def told(text: str) -> str:
|
|
30
|
+
"""The end of what a process printed: its traceback."""
|
|
31
|
+
return text[-TOLD:].strip()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class FolderLibrary:
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
folder: Path,
|
|
38
|
+
python: Callable[[], Sequence[str]] = script_python,
|
|
39
|
+
timeout: float = DESCRIBE_S,
|
|
40
|
+
):
|
|
41
|
+
"""python: how a script is run (this program's Python)."""
|
|
42
|
+
self.folder = Path(folder)
|
|
43
|
+
self.python = python
|
|
44
|
+
self.timeout = timeout
|
|
45
|
+
|
|
46
|
+
def files(self) -> Tuple[LibraryFile, ...]:
|
|
47
|
+
if not self.folder.is_dir():
|
|
48
|
+
return ()
|
|
49
|
+
found = []
|
|
50
|
+
for path in sorted(self.folder.glob(f"*{SUFFIX}")):
|
|
51
|
+
if path.name.startswith(SKIPPED) or not path.is_file():
|
|
52
|
+
continue
|
|
53
|
+
data = path.read_bytes()
|
|
54
|
+
found.append(self._file(path, data))
|
|
55
|
+
return tuple(found)
|
|
56
|
+
|
|
57
|
+
@staticmethod
|
|
58
|
+
def _file(path: Path, data: bytes) -> LibraryFile:
|
|
59
|
+
# Text uses the editor's newlines; the fingerprint identifies file bytes.
|
|
60
|
+
return LibraryFile(
|
|
61
|
+
path.stem,
|
|
62
|
+
str(path),
|
|
63
|
+
data.decode("utf-8", errors="replace")
|
|
64
|
+
.replace("\r\n", "\n")
|
|
65
|
+
.replace("\r", "\n"),
|
|
66
|
+
hashlib.sha256(data).hexdigest(),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def file(self, name: str, text: str) -> LibraryFile:
|
|
70
|
+
return self._file(self.folder / f"{name}{SUFFIX}", text.encode("utf-8"))
|
|
71
|
+
|
|
72
|
+
def write(self, name: str, text: str) -> LibraryFile:
|
|
73
|
+
self.folder.mkdir(parents=True, exist_ok=True)
|
|
74
|
+
path = self.folder / f"{name}{SUFFIX}"
|
|
75
|
+
data = text.encode("utf-8")
|
|
76
|
+
path.write_bytes(data)
|
|
77
|
+
return self._file(path, data)
|
|
78
|
+
|
|
79
|
+
def describe(self, file: LibraryFile) -> Declarations:
|
|
80
|
+
self.folder.mkdir(parents=True, exist_ok=True)
|
|
81
|
+
with tempfile.NamedTemporaryFile(
|
|
82
|
+
"w", encoding="utf-8", newline="", dir=self.folder,
|
|
83
|
+
prefix=f".{file.name}.", suffix=SUFFIX, delete=False,
|
|
84
|
+
) as copy: # fmt: skip
|
|
85
|
+
copy.write(file.text)
|
|
86
|
+
try:
|
|
87
|
+
return self._described(Path(copy.name))
|
|
88
|
+
finally:
|
|
89
|
+
Path(copy.name).unlink(missing_ok=True)
|
|
90
|
+
|
|
91
|
+
def _described(self, copy: Path) -> Declarations:
|
|
92
|
+
with tempfile.TemporaryDirectory() as folder:
|
|
93
|
+
declared = Path(folder) / DECLARED
|
|
94
|
+
environment = {
|
|
95
|
+
key: value for key, value in os.environ.items() if key != INPUTS_ENV
|
|
96
|
+
}
|
|
97
|
+
environment[DESCRIBE_ENV] = str(declared)
|
|
98
|
+
environment.update(OUTPUT_ENV)
|
|
99
|
+
try:
|
|
100
|
+
done = subprocess.run(
|
|
101
|
+
[*self.python(), str(copy)],
|
|
102
|
+
cwd=self.folder,
|
|
103
|
+
env=environment,
|
|
104
|
+
stdin=subprocess.DEVNULL,
|
|
105
|
+
capture_output=True,
|
|
106
|
+
timeout=self.timeout,
|
|
107
|
+
**apart(),
|
|
108
|
+
)
|
|
109
|
+
except subprocess.TimeoutExpired:
|
|
110
|
+
raise DescribeFailed(
|
|
111
|
+
f"not described in {self.timeout:g} s: declarations come"
|
|
112
|
+
" first, before anything that takes long"
|
|
113
|
+
) from None
|
|
114
|
+
except OSError as e:
|
|
115
|
+
raise DescribeFailed(f"{self.python()[0]} did not start: {e}") from None
|
|
116
|
+
output = (done.stdout + done.stderr).decode("utf-8", errors="replace")
|
|
117
|
+
if done.returncode != 0:
|
|
118
|
+
raise DescribeFailed(told(output) or f"exit code {done.returncode}")
|
|
119
|
+
if not declared.exists():
|
|
120
|
+
return Declarations()
|
|
121
|
+
try:
|
|
122
|
+
return msgspec.json.decode(declared.read_bytes(), type=Declarations)
|
|
123
|
+
except msgspec.DecodeError as e:
|
|
124
|
+
raise DescribeFailed(f"its declarations are not read: {e}") from None
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Processes the daemon starts apart (the Launcher port): a script's process
|
|
2
|
+
leaves the daemon's session and terminal, its output goes to its log."""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Mapping, Optional, Sequence
|
|
8
|
+
|
|
9
|
+
from thzdaq_api.host import apart
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Process:
|
|
13
|
+
def __init__(self, popen: subprocess.Popen):
|
|
14
|
+
self.popen = popen
|
|
15
|
+
self.pid = popen.pid
|
|
16
|
+
|
|
17
|
+
def wait(self, timeout: Optional[float] = None) -> Optional[int]:
|
|
18
|
+
try:
|
|
19
|
+
return self.popen.wait(timeout)
|
|
20
|
+
except subprocess.TimeoutExpired:
|
|
21
|
+
return None
|
|
22
|
+
|
|
23
|
+
def terminate(self) -> None:
|
|
24
|
+
self.popen.kill()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Processes:
|
|
28
|
+
def launch(
|
|
29
|
+
self,
|
|
30
|
+
command: Sequence[str],
|
|
31
|
+
folder: Path,
|
|
32
|
+
environment: Mapping[str, str],
|
|
33
|
+
log: Path,
|
|
34
|
+
removed: Sequence[str] = (),
|
|
35
|
+
) -> Process:
|
|
36
|
+
log.parent.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
inherited = {k: v for k, v in os.environ.items() if k not in removed}
|
|
38
|
+
with open(log, "ab") as output:
|
|
39
|
+
return Process(
|
|
40
|
+
subprocess.Popen(
|
|
41
|
+
list(command),
|
|
42
|
+
cwd=folder,
|
|
43
|
+
env={**inherited, **environment},
|
|
44
|
+
stdin=subprocess.DEVNULL,
|
|
45
|
+
stdout=output,
|
|
46
|
+
stderr=subprocess.STDOUT,
|
|
47
|
+
close_fds=True,
|
|
48
|
+
**apart(),
|
|
49
|
+
)
|
|
50
|
+
)
|