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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 | class RunSoCalObservingLoop(KPFTranslatorFunction):
'''This script runs a control loop to execute SoCal observations.
When the script is invoked, it puts SoCal in AUTONOMOUS mode. This means
that the SoCal dispatcher number 4 will handle opening the enclosure,
acquiring and tracking the Sun, and will perform a weather safety shutdown
if needed. The AUTONOMOUS mode will respect the CAN_OPEN keyword as well,
so that keyword will lock out SoCal motions if that is desired.
The script takes two required inputs: a start and end time in decimal hours
(in HST). The start time can be after the invocation of this script. This
is in fact the recommended operational strategy as the SoCal AUTONOMOUS
mode will then have time to open and acquire the Sun before observations
start.
If needed, the script will wait until the start time before taking further
actions (beyond setting AUTONOMOUS). Once the start time has passed, the
script will check the `kpfconfig.SCRIPT%` keywords to see if something is
currently running. If so, it will wait for the script keywords to clear
before starting operations.
Next the script will try to determine if SoCal is successfully observing
the Sun by invoking the `WaitForSoCalOnTarget` script.
If SoCal is on target, then a short observation of the Sun is performed.
Some of the parameters can be modified in the `KPFTranslator` configuration
file (`kpf_inst_config.ini`). This observation, as currently configured,
takes about 15 minutes to complete.
If SoCal is not on target (according to the `WaitForSoCalOnTarget` script),
then an Etalon calibration set is taken. This is a way to make use of time
that would otherwise be unproductive. This etalon script also takes around
15 minutes or a bit less to complete.
Once either of the two observations above has completed, the script repeats
the loop as long as there is enough time before the end time to complete a
SoCal observation.
Once the end time has passed, the system will perform basic cleanup of KPF,
then it will park SoCal using `ParkSoCal` if the park flag is set.
## Arguments
* __StartTimeHST__ - `float` The time (in decimal hours HST) to begin observing.
* __EndTimeHST__ - `float` The time (in decimal hours HST) to end observing.
* __park__ - `bool` If True, the script will park SoCal when complete.
* __scheduled__ - `bool` If True, the script will not run if the keyword
`kpfconfig.ALLOWSCHEDULEDCALS` is "No".
'''
@classmethod
def pre_condition(cls, args, logger, cfg):
pass
@classmethod
@add_script_log(Path(__file__).name.replace(".py", ""))
def perform(cls, args, logger, cfg):
if args.get('scheduled', True) is True:
ALLOWSCHEDULED = ktl.cache('kpfconfig', 'ALLOWSCHEDULEDCALS').read()
if ALLOWSCHEDULED == 'No':
return
log.info('-------------------------')
log.info(f"Running {cls.__name__}")
log.info('-------------------------')
socal_ND1 = cfg.get('SoCal', 'ND1', fallback='OD 0.1')
socal_ND2 = cfg.get('SoCal', 'ND2', fallback='OD 0.1')
socal_ExpTime = cfg.getfloat('SoCal', 'ExpTime', fallback=12)
SoCal_observation = {'Template_Name': 'kpf_lamp',
'Template_Version': '1.0',
'TargetName': 'Sun',
'TriggerCaHK': False,
'TimedShutter_CaHK': False,
'TriggerGreen': True,
'TriggerRed': True,
'TriggerExpMeter': False,
'RunAgitator': True,
'CalSource': 'SoCal-SciSky',
'Object': 'SoCal',
'CalND1': socal_ND1,
'CalND2': socal_ND2,
'ExpTime': socal_ExpTime,
'nExp': 15,
'SSS_Science': True,
'SSS_Sky': True,
'TakeSimulCal': True,
'nointensemon': True,
}
readout_red = cfg.getfloat('time_estimates', f'readout_red', fallback=60)
readout_green = cfg.getfloat('time_estimates', f'readout_green', fallback=60)
readout_cahk = cfg.getfloat('time_estimates', 'readout_cahk', fallback=1)
archon_time_shim = cfg.getfloat('times', 'archon_temperature_time_shim',
fallback=2)
readout = max([readout_red, readout_green, readout_cahk])
SoCal_duration = int(SoCal_observation['nExp'])*max([float(SoCal_observation['ExpTime']), archon_time_shim])
SoCal_duration += int(SoCal_observation['nExp'])*readout
log.debug(f"Estimated SoCal observation time = {SoCal_duration}")
SLEWCALFILE = ktl.cache('kpfconfig', 'SLEWCALFILE')
slewcal_argsfile = Path(SLEWCALFILE.read())
with open(slewcal_argsfile, 'r') as file:
slewcal_OB = yaml.safe_load(file)
Etalon_observation = slewcal_OB['SEQ_Calibrations'][0]
Etalon_observation['Template_Name'] = 'kpf_lamp'
Etalon_observation['Template_Version'] = slewcal_OB['Template_Version']
Etalon_observation['TriggerCaHK'] = slewcal_OB['TriggerCaHK']
Etalon_observation['TriggerGreen'] = slewcal_OB['TriggerGreen']
Etalon_observation['TriggerRed'] = slewcal_OB['TriggerRed']
Etalon_observation['TimedShutter_CaHK'] = slewcal_OB.get('TriggerCaHK', False)
Etalon_observation['nExp'] = cfg.get('SoCal', 'Etalon_nExp', fallback=8)
Etalon_observation['nointensemon'] = True
Etalon_duration = int(Etalon_observation['nExp'])*max([float(Etalon_observation['ExpTime']), archon_time_shim])
Etalon_duration += int(Etalon_observation['nExp'])*readout
log.debug(f"Estimated Etalon observation time = {Etalon_duration}")
SoCalStartAutonomous.execute({})
max_wait_per_iteration = 60
start_time = args.get('StartTimeHST', 9)
end_time = args.get('EndTimeHST', 12) - max([SoCal_duration, Etalon_duration])/3600 - 0.05
now = datetime.datetime.now()
now_decimal = (now.hour + now.minute/60 + now.second/3600)
if now_decimal < start_time:
wait = (start_time-now_decimal)*3600
log.info(f'Waiting {wait:.0f}s for SoCal window start time')
time.sleep(wait)
elif now_decimal > end_time:
log.info("End time for today's SoCal window has passed")
return
SCRIPTPID = ktl.cache('kpfconfig', 'SCRIPTPID')
if SCRIPTPID.read(binary=True) >= 0:
SCRIPTNAME = ktl.cache('kpfconfig', 'SCRIPTNAME').read()
waittime = (end_time-now_decimal)*3600 - SoCal_duration - 180
log.warning(f'Script is currently running: {SCRIPTNAME}')
if waittime > 0:
log.info(f'Waiting up to {waittime:.0f}s for running script to end')
SCRIPTPID.waitFor("==-1", timeout=waittime)
time.sleep(10)
check_script_running()
set_script_keywords(Path(__file__).name, os.getpid())
log.info(f'Starting SoCal observation loop')
log.info(f'Start time: {start_time:.2f} HST')
log.info(f'End Time: {end_time:.2f} HST')
check_scriptstop()
nSoCalObs = 0
nEtalonObs = 0
now = datetime.datetime.now()
now_decimal = (now.hour + now.minute/60 + now.second/3600)
while now_decimal >= start_time and now_decimal < end_time:
log.debug('Checking if SoCal is on the Sun')
if args.get('ignorePYRIRRAD', False) is True:
on_target = True
else:
on_target = WaitForSoCalOnTarget.execute({'timeout': max_wait_per_iteration})
observation = {True: SoCal_observation, False: Etalon_observation}[on_target]
log.info(f'SoCal on target: {on_target}')
log.info(f"Executing {observation['Object']}")
try:
check_scriptstop()
ExecuteCal.execute(observation)
if on_target == True:
nSoCalObs += 1
else:
nEtalonObs += 1
except ScriptStopTriggered as e:
raise e
except Exception as e:
log.error("ExecuteCal failed:")
log.error(e)
traceback_text = traceback.format_exc()
log.error(traceback_text)
if not isinstance(e, ScriptStopTriggered):
try:
msg = [f'{type(e)}',
f'{traceback.format_exc()}',
'',
f'{OB}']
SendEmail.execute({'Subject': 'ExecuteCals Failed',
'Message': '\n'.join(msg)})
except Exception as email_err:
log.error(f'Sending email failed')
log.error(email_err)
check_scriptstop()
now = datetime.datetime.now()
now_decimal = (now.hour + now.minute/60 + now.second/3600)
log.info('SoCal observation loop completed')
log.info(f'Executed {nSoCalObs} SoCal sequences')
log.info(f'Executed {nEtalonObs} Etalon sequences')
try:
CleanupAfterCalibrations.execute(Etalon_observation)
except Exception as e:
log.error("CleanupAfterCalibrations failed:")
log.error(e)
traceback_text = traceback.format_exc()
log.error(traceback_text)
clear_script_keywords()
try:
msg = [f'{type(e)}',
f'{traceback_text}',
'',
f'{OB}']
SendEmail.execute({'Subject': 'CleanupAfterCalibrations Failed',
'Message': '\n'.join(msg)})
except Exception as email_err:
log.error(f'Sending email failed')
log.error(email_err)
if args.get('park', False) == True:
ParkSoCal.execute({})
@classmethod
def post_condition(cls, args, logger, cfg):
pass
@classmethod
def add_cmdline_args(cls, parser, cfg=None):
parser.add_argument('StartTimeHST', type=float,
help='Start of daily observing window in decimal hours HST.')
parser.add_argument('EndTimeHST', type=float,
help='End of daily observing window in decimal hours HST.')
parser.add_argument("--park", dest="park",
default=False, action="store_true",
help="Close and park SoCal when done?")
parser.add_argument("--notscheduled", dest="scheduled",
default=True, action="store_false",
help="Do not respect the kpfconfig.ALLOWSCHEDULEDCALS flag.")
parser.add_argument("--ignorePYRIRRAD", dest="ignorePYRIRRAD",
default=False, action="store_true",
help="Ignore the PYRIRRAD value and observe the Sun regardless.")
return super().add_cmdline_args(parser, cfg)
|