-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_nsfrb_tc.py
More file actions
executable file
·239 lines (190 loc) · 7.87 KB
/
run_nsfrb_tc.py
File metadata and controls
executable file
·239 lines (190 loc) · 7.87 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/home/ubuntu/anaconda3/envs/casa38/bin/python
"""
run_nsfrb_tc.py
Same as run_nsfrb.py, but the 'move' command applies per-antenna temperature
correction using pre-computed linear fit solutions, and waits for antennas
to settle before proceeding.
Temperature correction:
corrected_el = requested_el - (motor_temp * slope + intercept)
where slope and intercept come from fitsolutions.npy per antenna.
If no temperature data is available or the correction exceeds 1 degree
(indicating a bad fit), the uncorrected position is used.
"""
import sys
import os
import time as pytime
from time import sleep
from datetime import datetime
import numpy as np
from dsautils import dsa_store
from dsautils.dsa_store import DsaStore
from dsautils.cnf import Conf
# ---------------------------------------------------------------------------
# Initialisation
# ---------------------------------------------------------------------------
ETCD = DsaStore()
ANTENNAS = list(Conf(use_etcd=True).get('corr')['antenna_order'].values())
print("Antenna list:", ANTENNAS)
# Load temperature-correction fit solutions relative to this script
fitsolns = np.load("/home/ubuntu/proj/websrv/temp-clone/fitsolutions.npy",allow_pickle=True)
fitsolns_antorder = np.load("/home/ubuntu/proj/websrv/temp-clone/fitsolutions_antorder.npy",allow_pickle=True)
# ---------------------------------------------------------------------------
# Helper functions
# ---------------------------------------------------------------------------
def get_datestring():
val = datetime.now()
datestring = (str(val.year) + '_' + str(val.month) + '_' + str(val.day)
+ '_' + str(val.hour) + '_' + str(val.minute) + '_' + str(val.second))
return datestring
def pause_until(time):
"""
Pause your program until a specific end time.
'time' is either a valid datetime object or unix timestamp in seconds
(i.e. seconds since Unix epoch).
"""
end = time
# Convert datetime to unix timestamp
if isinstance(time, datetime):
end = time.timestamp()
# Type check
if not isinstance(end, (int, float)):
raise Exception('The time parameter is not a number or datetime object')
# Now we wait
while True:
now = datetime.utcnow().timestamp()
diff = end - now
print('waiting: ', diff)
if diff <= 0:
break
else:
# 'logarithmic' sleeping to minimise loop iterations
sleep(diff / 2)
def move_with_tempcorrection(newposition, timeout=30, tol=0.9):
"""Move every antenna to *newposition* with per-antenna temperature
correction, then wait for them to settle.
For each antenna the motor temperature is read from ETCD and a linear
correction is applied::
correction = motor_temp * slope + intercept
set_el = newposition - correction
If no temperature data is available or the absolute correction exceeds
1 degree (indicating a bad fit), the antenna is moved to the raw
*newposition* instead.
Parameters
----------
newposition : float
Requested elevation in degrees.
timeout : float
Maximum time in seconds to wait for antennas to settle.
tol : float
Fraction of antennas that must report settled (drv_state == 2)
before the function returns success.
Returns
-------
int
0 on success (enough antennas settled), 1 on timeout.
"""
elapsed = 0.0
for ant in ANTENNAS:
# Look up this antenna in the fit-solution arrays
antidx = list(fitsolns_antorder).index(ant)
# Read current motor temperature from ETCD
anttemp = ETCD.get_dict("/mon/ant/" + str(ant))
if anttemp is None:
print("Ant " + str(ant) + ": no temp data, using uncorrected position "
+ str(newposition))
setposition = newposition
elif np.abs((anttemp["motor_temp"] * fitsolns[antidx, 0])
+ fitsolns[antidx, 1]) > 1:
print("Ant " + str(ant) + ": bad solution, using uncorrected position "
+ str(newposition))
setposition = newposition
else:
correction = (anttemp["motor_temp"] * fitsolns[antidx, 0]
+ fitsolns[antidx, 1])
setposition = newposition - correction
print("Ant " + str(ant) + " revised elevation from "
+ str(newposition) + " to " + str(setposition))
ETCD.put_dict(f'/cmd/ant/{ant}', {'cmd': 'move', 'val': setposition})
pytime.sleep(1e-3)
elapsed += 1e-3
# Wait for antennas to settle (drv_state == 2)
antenna_moved = np.zeros(len(ANTENNAS))
while elapsed < timeout:
for i, ant in enumerate(ANTENNAS):
if not antenna_moved[i]:
antdict = ETCD.get_dict(f'/mon/ant/{ant}')
if antdict is not None and antdict['drv_state'] == 2:
antenna_moved[i] = 1
if antenna_moved.mean() > tol:
print(f'Antennas settled at position {newposition} (temp-corrected)')
return 0
pytime.sleep(3)
elapsed += 3.
print(f'Timeout moving to {newposition} (temp-corrected)')
return 1
def exec_action(a, d):
"""Execute a single schedule action.
The 'move' command uses per-antenna temperature-corrected positioning
via `move_with_tempcorrection`. All other commands are unchanged from
the original run_nsfrb.py.
"""
if a['cmd'] == 'move':
move_with_tempcorrection(float(a['val']))
if a['cmd'] == 'start':
d.put_dict('/cmd/corr/docopy', 'True')
for i in range(17, 21):
d.put_dict('/cmd/corr/' + str(i), {'cmd': 'start', 'val': a['val']})
pytime.sleep(5)
for i in range(1, 17):
d.put_dict('/cmd/corr/' + str(i), {'cmd': 'start', 'val': a['val']})
pytime.sleep(600)
os.system('/home/ubuntu/anaconda3/envs/casa38/bin/dsacon corr set')
if a['cmd'] == 'stop':
# edit to avoid missing voltages for previous triggers
# triggers during these 2 min will likely be missed.
lastcmd = d.get_dict("/cmd/corr/0")
if lastcmd['cmd'] == 'trigger':
if 'flush' not in lastcmd['val']:
pytime.sleep(120)
#d.put_dict('/cmd/corr/0', {'cmd': 'trigger', 'val': '0-flush-'})
#pytime.sleep(90)
os.system('/home/ubuntu/anaconda3/envs/casa38/bin/dsacon corr stop')
pytime.sleep(10)
d.put_dict('/cmd/corr/docopy','False')
pytime.sleep(2)
if a['cmd'] == 'faststart':
d.put_dict('/cmd/corr/docopy', 'True')
for i in range(17, 21):
d.put_dict('/cmd/corr/' + str(i), {'cmd': 'start', 'val': a['val']})
pytime.sleep(5)
for i in range(1, 17):
d.put_dict('/cmd/corr/' + str(i), {'cmd': 'start', 'val': a['val']})
pytime.sleep(180)
os.system('/home/ubuntu/anaconda3/envs/casa38/bin/dsacon corr set')
if a['cmd'] == 'test':
d.put_dict('/cmd/corr/100', {'cmd': 'test', 'val': '0'})
pytime.sleep(1)
os.system('echo tested')
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
schedule = '/home/ubuntu/proj/websrv/temp-clone/actions.npy'
d = dsa_store.DsaStore()
# update trig_ct
for i in np.arange(1, 21):
d.put_dict('/mon/corr/' + str(i) + '/voltage_ct', {'n_trigs': 0})
a = np.load(schedule, allow_pickle=True)
for ln in a:
print(ln)
pause_until(ln['time'])
exec_action(ln, d)
pytime.sleep(180)
exec_action({"cmd": "move", "val": "69.04"}, d)
pytime.sleep(180)
exec_action({"cmd": "move", "val": "69.04"}, d)
pytime.sleep(10)
exec_action({"cmd": "start", "val": "16.27"}, d)
while True:
pytime.sleep(3600)
exec_action({"cmd": "stop", "val": "0"}, d)
exec_action({"cmd": "faststart", "val": "16.27"}, d)