-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_timings.py
154 lines (119 loc) · 3.74 KB
/
record_timings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import signal
import numpy as np
import datetime as dt
import argparse
import subprocess
import logging
import stationrc.common
import stationrc.remote_control
from stationrc.remote_control.utils import get_channels_for_quad
import os
signal.signal(
signal.SIGINT, signal.SIG_DFL
) # allows to close matplotlib window with CTRL+C from terminal
def get_date_of_calibration_file(station):
directory = os.path.dirname(__file__)
file_name = f"cal_{station.radiant_low_level_interface.board_manager_uid():032x}.json"
calib_file = os.path.join(directory, "calib", file_name)
if os.path.exists(calib_file):
return dt.datetime.fromtimestamp(os.path.getmtime(calib_file)).astimezone(dt.UTC).timestamp()
return None
def get_hostname():
try:
sp = subprocess.run(["hostnamectl", "hostname"], capture_output=True)
sp.check_returncode()
except subprocess.CalledProcessError:
# Necessary for station 24 with old bbb
sp = subprocess.run(["hostnamectl", "| grep hostname", "| awk '{print $3}'"], capture_output=True)
sp.check_returncode()
return sp.stdout.decode("utf-8").strip('\n')
def record_for_quad(station, quad, n_recordings):
station.radiant_calselect(quad=quad)
t = np.squeeze([stationrc.remote_control.get_time_run(
station=station, frequency=args.frequency * 1e6) for _ in range(n_recordings)])
if n_recordings > 1:
# n, channels, samples -> channels, n, samples
t = np.swapaxes(t, 0, 1)
return t
parser = argparse.ArgumentParser()
parser.add_argument(
"-n",
"--num-recordings",
dest="num_recordings",
type=int,
default=1,
help="number of recordings to record",
)
parser.add_argument(
"-f",
"--frequency",
type=float,
default=510,
help="Frequency for on-board signal generator (in MHz). (Default: 510 MHz)",
)
parser.add_argument(
"-b",
"--band",
type=int,
default=2,
help="Filter band for on-board signal generator. (Default: 2)",
)
parser.add_argument(
"--filename",
type=str,
default=None,
nargs="?"
)
parser.add_argument(
"--host",
type=str,
default=None,
nargs="?",
help="Set host",
)
parser.add_argument(
"-d",
"--data_dir",
dest="data_dir",
type=str,
default="./",
nargs="?"
)
parser.add_argument(
"--load_calibration",
action="store_true",
help="Load the calibration. This should be not necessary if you called e.g. bring_up.py before. "
"Default: False"
)
args = parser.parse_args()
stationrc.common.setup_logging()
station = stationrc.remote_control.VirtualStation(load_calibration=args.load_calibration, host=args.host)
if station.rc.run_local:
host = get_hostname()
else:
host = station.remote_host
station.radiant_sig_gen_off()
station.radiant_sig_gen_configure(
pulse=False, band=args.band
)
if args.load_calibration:
station.radiant_pedestal_update()
station.radiant_sig_gen_on()
station.radiant_sig_gen_set_frequency(
frequency=args.frequency
)
station.radiant_pedestal_update() # this seems to be necessary, otherwise the timing will be all 0's ...
timings = {}
logging.info("Start recording timing ... ")
for quad in range(3):
channels = get_channels_for_quad(quad)
t = record_for_quad(station, quad, args.num_recordings)
for ch in channels:
logging.info(f"Channel {ch:02d}: {np.mean(t[ch])} ns")
timings[str(ch)] = t[ch]
station.radiant_sig_gen_off()
station.radiant_calselect(None)
logging.info(" ... finished.")
now = dt.datetime.now(dt.UTC)
timestamp_last_calibration = get_date_of_calibration_file(station)
np.savez(f"{args.data_dir}/timing_{host}_{now.strftime('%Y_%m_%d-%H%M')}.npz", time=now.timestamp(), last_calibration=timestamp_last_calibration, **timings)