-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_data.py
200 lines (166 loc) · 4.79 KB
/
record_data.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import signal
import numpy as np
signal.signal(
signal.SIGINT, signal.SIG_DFL
) # allows to close matplotlib window with CTRL+C from terminal
import argparse
import matplotlib.pyplot as plt
import stationrc.common
import stationrc.remote_control
parser = argparse.ArgumentParser()
parser.add_argument(
"-n",
"--num-events",
dest="num_events",
type=int,
default=1,
help="number of events to record",
)
parser.add_argument(
"-c",
"--channel",
type=int,
default=None,
nargs="*",
help="Plot certain channel. If None plot all channels. (Default: None)",
)
parser.add_argument(
"-u",
"--use_UART",
action="store_true",
help="Use UART, not GPIO to check for events",
)
parser.add_argument(
"-l",
"--line",
action="store_true",
help="Plot vertical lines each 128 samples",
)
parser.add_argument(
"-m",
"--marker",
type=str,
default="",
const="o",
nargs="?",
help="Set marker",
)
parser.add_argument(
"-r",
"--range",
type=int,
nargs="*",
default=None,
metavar="int or [int, int]",
help="Set range (in samples) to plot. Single int: lenght to plot centered around the middle. Two ints: xlow, xup. Default: None -> full range",
)
parser.add_argument(
"-s",
"--save",
action="store_true",
help="Store plots"
)
parser.add_argument(
"--save_data",
action="store_true",
help="Store plots"
)
parser.add_argument(
"--filename",
type=str,
default=None,
nargs="?"
)
parser.add_argument(
"--label",
type=str,
default="",
nargs="?",
help="Label for the filename of the waveform plot."
)
parser.add_argument(
"--not_read_windows",
dest="read_windows",
action="store_false",
help="If set store not readout windows. "
"Those are needed to apply the voltage calibration. "
"But reading them will increase the latency."
)
parser.add_argument(
"--all",
action="store_true",
help="Plot all channels in an individual axis"
)
parser.add_argument(
"--host",
type=str,
default=None,
nargs="?",
help="Set host",
)
args = parser.parse_args()
stationrc.common.setup_logging()
station = stationrc.remote_control.VirtualStation(host=args.host)
data = station.daq_record_data(
num_events=args.num_events, force_trigger=True, use_uart=args.use_UART,
read_header=args.read_windows,
)
if args.save or args.save_data:
uid = station.get_radiant_board_mcu_uid()
if args.label != "":
args.label = f"_{args.label}"
for idx, ev in enumerate(data["data"]["WAVEFORM"]):
if args.all:
fig, axs = plt.subplots(4, 6, figsize=(12, 8),
gridspec_kw=dict(hspace=0.05, wspace=0.05, top=0.97, right=0.99, left=0.08, bottom=0.08),
sharex=True, sharey=True)
for ch, (wvf, ax) in enumerate(zip(ev["radiant_waveforms"], axs.flatten())):
ax.plot(wvf, marker=args.marker, label=f"ch. {ch}")
ax.legend()
if args.line:
for i in range(16):
ax.axvline(i * 128, color="k", lw=1, zorder=0)
fig.supxlabel("samples")
fig.supylabel("ADC counts")
else:
fig, ax = plt.subplots()
if args.channel is None:
for ch, wvf in enumerate(ev["radiant_waveforms"]):
ax.plot(wvf, marker=args.marker, label=f"ch. {ch}")
else:
for channel in args.channel:
ax.plot(ev["radiant_waveforms"][channel], marker=args.marker, label=f"ch. {channel}", lw=1)
if args.line:
for i in range(16):
ax.axvline(i * 128, color="k", lw=1, zorder=0)
ax.legend()
ax.set_title(f"Event: {ev['event_number']}")
ax.set_xlabel("samples")
ax.set_ylabel("ADC counts")
if args.range is not None:
if len(args.range) == 1:
c = np.diff(ax.get_xlim())[0] // 2
ax.set_xlim(c - args.range[0] // 2, c + args.range[0] // 2)
elif len(args.range) == 2:
ax.set_xlim(*args.range)
else:
raise ValueError("Wrong length for args.range (only 1 or 2 are allowed)")
if args.save:
if args.all:
plt.savefig(f"waveform_all_{idx}_{uid:032x}{args.label}", transparent=False)
else:
fig.tight_layout()
plt.savefig(f"waveform_ch{args.channel}_{idx}_{uid:032x}{args.label}", transparent=False)
if not args.save and not args.save_data:
plt.show()
if args.save_data:
import json
filename = args.filename or f"waveform_ch_{uid:032x}"
if not filename.endswith(".json"):
filename += ".json"
if args.read_windows:
with open(filename, "w") as f:
json.dump(data["data"], f)
else:
with open(filename, "w") as f:
json.dump(data["data"]["WAVEFORM"], f)