-
Notifications
You must be signed in to change notification settings - Fork 2
/
acq_long2pos.py
executable file
·264 lines (228 loc) · 9.67 KB
/
acq_long2pos.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
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
#! @KPYTHON3@
description = '''
acq_long2pos -- Acquire mask nod image sets at two different longslit mask
positions.
This script is used in conjunction with the long2pos CSU mask to obtain spectra
of a target in two longslits which are offset from the field center in the
wavelength direction in order to sample the full range of wavelengths in the
passband.
|x|
| | Upper left position, covers long wavelengths
|x|
| | Alignment position
|x|
| | Lower right, short wavelengths
|x|
The normal behavior, which applies when the CSU is configured with the standard
"long2pos" or "long2pos (align)" masks is to acquire a nod pair in each slit.
If the CSU is configured with the "long2pos_specphot" or
"long2pos_specphot (align)", we acquire spectra in the center of each slit as
well as at the ends. This is typically used when the observer wants both a
spectrum from both the "wide" portion of the slit and the narrow portion. The
spectrum from the "wide" part can be used as a spectrophotometric observation
(no slit loss from the extra-wide slit).
Modification History:
2021-04-23 [JoshW] Initial adaptation from the shell script. Incorporates
changes to improve positioning accuracy.
'''
## Import General Tools
from pathlib import Path
import argparse
import logging
from datetime import datetime
from time import sleep
import numpy as np
import subprocess
import ktl
import mosfire
##-------------------------------------------------------------------------
## Parse Command Line Arguments
##-------------------------------------------------------------------------
## create a parser object for understanding command-line arguments
p = argparse.ArgumentParser(description=description)
## add flags
p.add_argument("-v", "--verbose", dest="verbose",
default=False, action="store_true",
help="Be verbose! (default = False)")
p.add_argument("-i", "--imaging", dest="imaging",
default=False, action="store_true",
help="Take data in imaging mode rather than spectroscopy? (for testing)")
p.add_argument("--mcds", "--mcds16", dest="mcds",
default=False, action="store_true",
help=("Take data using MCDS16 readout mode? If neither --cds or --mcds are "
"set, will use current setting."))
p.add_argument("--cds", dest="cds",
default=False, action="store_true",
help=("Take data using CDS readout mode? If neither --cds or --mcds are "
"set, will use current setting."))
## add options
p.add_argument("-f", "--filter", dest="filter", type=str, default='',
choices=['Y', 'J', 'H', 'K', 'Ks', 'J2', 'J3', 'nb1061', ''],
help="The filter to take data in.")
p.add_argument("-e", "--exptime", dest="exptime", type=float, default=0,
help="The exposure time (defaults to 0 which means use existing setting).")
p.add_argument("-g", "--guidecycles", dest="guidecycles", type=float, default=2,
help="The number of guider cycles to wait after large offsets.")
args = p.parse_args()
##-------------------------------------------------------------------------
## Create logger object
##-------------------------------------------------------------------------
log = logging.getLogger('long2pos')
log.setLevel(logging.DEBUG)
LogFormat = logging.Formatter('%(asctime)s %(levelname)8s: %(message)s')
## Set up console output
LogConsoleHandler = logging.StreamHandler()
LogConsoleHandler.setLevel(logging.DEBUG)
LogConsoleHandler.setFormatter(LogFormat)
log.addHandler(LogConsoleHandler)
## Set up file output
now_str= datetime.utcnow().strftime('%Y%m%d_%H%M%SUT')
LogFileName = Path('~/acq_long2pos_{now_str}.log').expanduser()
LogFileHandler = logging.FileHandler(LogFileName)
LogFileHandler.setLevel(logging.DEBUG)
LogFileHandler.setFormatter(LogFormat)
log.addHandler(LogFileHandler)
##-------------------------------------------------------------------------
## Cache mosfire keyword services
##-------------------------------------------------------------------------
mosfireGS = ktl.cache(service='mosfire')
##-------------------------------------------------------------------------
## acq_long2pos
##-------------------------------------------------------------------------
def acq_long2pos(wide=False, waittime=10, wait_on_slitmov=False):
'''Execute the offset pattern and trigger images for the long2pos sequence.
'''
pixel_scale = mosfireGS['pscale'].read(binary=True)
offsetx = 245.8 * pixel_scale
offsety = -88.0 * pixel_scale
log.info('Starting long2pos acquisition')
log.debug('Setting SCRIPTRUN=1')
mosfire.start_scriptrun()
log.info('Marking base')
mosfire.markbase()
log.debug('Setting PATTERN=long2pos')
mosfireGS['PATTERN'].write('long2pos')
log.info('Offsetting to upper left position')
# mosfire.mxy(250.25*pixel_scale, -89.0*pixel_scale)
mosfire.mxy(offsetx, offsety)
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.debug('Setting XOFFSET=-45 YOFFSET=0')
mosfireGS['XOFFSET'].write(-45.0)
mosfireGS['YOFFSET'].write(0.0)
if wide is True:
log.debug('Setting FRAMEID=A')
mosfireGS['FRAMEID'].write('A')
log.info('Taking wide slit data')
mosfire.take_exposure()
log.info('acquiring slit nod data in upper left position')
mosfire.sltmov(-7)
if wait_on_slitmov == True:
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.debug('Setting YOFFSET=-7 FRAMEID=B')
mosfireGS['YOFFSET'].write(-7.0)
mosfireGS['FRAMEID'].write('B')
mosfire.take_exposure()
mosfire.sltmov(14)
if wait_on_slitmov == True:
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.debug('Setting YOFFSET=+7 FRAMEID=A')
mosfireGS['YOFFSET'].write(7.0)
mosfireGS['FRAMEID'].write('A')
mosfire.take_exposure()
log.info(f'Returning to base before offsetting to lower right position')
mosfire.gotobase()
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.info('Offsetting to lower right position')
# mosfire.mxy(-250.25*pixel_scale, 89.0*pixel_scale)
mosfire.mxy(-offsetx, -offsety)
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.debug('Setting XOFFSET=+45 YOFFSET=0')
mosfireGS['XOFFSET'].write(+45.0)
mosfireGS['YOFFSET'].write(0.0)
if wide is True:
log.debug('Setting FRAMEID=A')
mosfireGS['FRAMEID'].write('A')
log.info('Taking wide slit data')
mosfire.take_exposure()
log.info('acquiring slit nod data in lower right position')
mosfire.sltmov(-7)
if wait_on_slitmov == True:
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.debug('Setting YOFFSET=-7 FRAMEID=B')
mosfireGS['YOFFSET'].write(-7.0)
mosfireGS['FRAMEID'].write('B')
mosfire.take_exposure()
mosfire.sltmov(14)
if wait_on_slitmov == True:
log.info(f'Waiting {waittime} seconds for guider')
sleep(waittime)
log.debug('Setting YOFFSET=+7 FRAMEID=A')
mosfireGS['YOFFSET'].write(7.0)
mosfireGS['FRAMEID'].write('A')
mosfire.take_exposure()
log.info(f'Returning to base')
mosfire.gotobase()
log.debug('Setting XOFFSET=0 YOFFSET=0')
mosfireGS['XOFFSET'].write(0.0)
mosfireGS['YOFFSET'].write(0.0)
log.debug('Setting SCRIPTRUN=0')
mosfire.stop_scriptrun()
def acq_long2pos_specphot():
acq_long2pos(wide=True)
def run_acq_long_2pos():
'''Adopted from acq_long2pos shell script and modified based on advice from
Shui and based on the experience of the KCWI nod and shuffle mode.
- Check exposure time of instrument and guider and decide whether guiding
is advised.
- Go to base between the two positions (posA and posC)
- After offsetting, wait for guider centroid to converge before starting
exposure.
'''
# Check that MOSFIRE is the selected instrument
mosfire.instrument_is_MOSFIRE() # raises exception if not
log.info(f"Science Exposure Parameters:")
# Read exposure time
ITIME = mosfire.exptime()
log.info(f" Exposure Time = {ITIME:.0f} s")
# Read coadds
COADDS = mosfire.coadds()
log.info(f" coadds = {COADDS}")
# Read sampling mode & number of reads
sampmode = mosfire.sampmode()
log.info(f" Sampling Mode = {sampmode}")
# Check the mask
maskname = mosfireGS['maskname'].read()
if maskname[:8] != 'long2pos':
log.error(f"Mask {maskname} is not a long2pos mask")
raise Exception(f"Mask {maskname} is not a long2pos mask")
# Check guider and science exposure times
camparms = mosfire.get_camparms()
for key in camparms.keys():
log.info(f'MAGIQ: {key} = {camparms[key]}')
# Set the specified exposure time
if args.exptime > 0:
log.info(f'Setting exposure time to {exptime}')
mosfire.set_exptime(exptime)
# Set the specified sampling mode
if args.mcds is True:
log.info(f'Setting sampling mode to MCDS16')
mosfire.set_sampmode('MCDS16')
if args.cds is True:
log.info(f'Setting sampling mode to CDS')
mosfire.set_sampmode('CDS')
# Set the specified filter and obsmode
if args.filter != '':
obsmode = f'{args.filter}-spectroscopy' if args.imaging is False\
else f'{args.filter}-imaging'
log.info(f'Setting OBSMODE = {obsmode}')
mosfire.set_obsmode(obsmode)
specphot = (maskname=='long2pos_specphot')
acq_long2pos(wide=specphot, waittime=args.guidecycles*camparms['exptime'])
if __name__ == '__main__':
run_acq_long_2pos()