-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstr_nirspec.py
More file actions
477 lines (375 loc) · 15.8 KB
/
instr_nirspec.py
File metadata and controls
477 lines (375 loc) · 15.8 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
'''
This is the class to handle all the NIRSPEC specific attributes
NIRSPEC specific DR techniques can be added to it in the future
12/14/2017 M. Brown - Created initial file
'''
import instrument
import datetime as dt
from common import *
from math import ceil
class Nirspec(instrument.Instrument):
def __init__(self, instr, utDate, rootDir, log=None):
#call the parent init to get all the shared variables
super().__init__(instr, utDate, rootDir, log)
#set any unique keyword index values here
self.keywordMap['OFNAME'] = 'DATAFILE'
self.keywordMap['FRAMENO'] = 'FRAMENUM'
#other vars that subclass can overwrite
self.endTime = '19:00:00' # 24 hour period start/end time (UT)
#generate the paths to the NIRSPEC datadisk accounts
self.sdataList = self.get_dir_list()
def run_dqa_checks(self, progData):
'''
Run all DQA checks unique to this instrument
'''
ok = True
if ok: ok = self.set_dqa_date()
if ok: ok = self.set_dqa_vers()
if ok: ok = self.set_datlevel(0)
if ok: ok = self.set_instr()
if ok: ok = self.set_dateObs()
if ok: ok = self.set_elaptime()
if ok: ok = self.set_koaimtyp()
if ok: ok = self.set_koaid()
if ok: ok = self.set_frameno()
if ok: ok = self.set_ofName()
if ok: ok = self.set_semester()
if ok: ok = self.set_isao()
if ok: ok = self.set_dispers()
if ok: ok = self.set_slit_values()
if ok: ok = self.set_filter()
if ok: ok = self.set_wavelengths()
if ok: ok = self.set_weather_keywords()
if ok: ok = self.set_image_stats_keywords()
if ok: ok = self.set_gain_and_readnoise()
if ok: ok = self.set_npixsat(self.get_keyword('COADDS') * 25000)
if ok: ok = self.set_oa()
if ok: ok = self.set_prog_info(progData)
if ok: ok = self.set_propint(progData)
return ok
def get_dir_list(self):
'''
Function to generate the paths to all the NIRSPEC accounts, including engineering
Returns the list of paths
'''
dirs = []
path = '/s/sdata60'
for i in range(4):
joinSeq = (path, str(i))
path2 = ''.join(joinSeq)
for j in range(1,10):
joinSeq = (path2, '/nspec', str(j))
path3 = ''.join(joinSeq)
dirs.append(path3)
joinSeq = (path2, '/nspeceng')
path3 = ''.join(joinSeq)
dirs.append(path3)
joinSeq = (path2, 'nirspec')
path3 = ''.join(joinSeq)
dirs.append(path3)
return dirs
def get_prefix(self):
# SCAM = NC, SPEC = NS
instr = self.get_instr()
if instr == 'nirspec' or instr == 'nirspao':
try:
camera = self.get_keyword('CAMERA')
if camera == None:
camera = self.get_keyword('OUTDIR')
camera = camera.lower()
except KeyError:
prefix = ''
else:
if 'scam' in camera:
prefix = 'NC'
elif 'spec' in camera:
prefix = 'NS'
else:
prefix = ''
else:
prefix = ''
return prefix
def set_instr(self):
'''
Overrides instrument.set_instr
'''
ok = False
instrume = self.get_keyword('INSTRUME')
if (instrume.strip() == 'NIRSPAO'): instrume = 'NIRSPEC'
if (self.instr == instrume.strip()): ok = True
return ok
def set_elaptime(self):
'''
Fixes missing ELAPTIME keyword
'''
self.log.info('set_elaptime: determining ELAPTIME from TRUITIME')
#skip it it exists
if self.get_keyword('ELAPTIME', False) != None: return True
#get necessary keywords
itime = self.get_keyword('TRUITIME')
coadds = self.get_keyword('COADDS')
if (itime == None or coadds == None):
self.log.error('set_elaptime: TRUITIME and COADDS values needed to set ELAPTIME')
return False
#update val
elaptime = round(itime * coadds, 5)
if elaptime > 9999:
self.log.warning(f'set_elaptime: ELAPTIME value of {elaptime} is too large. Setting to null.')
return True
self.set_keyword('ELAPTIME', elaptime, 'KOA: Total integration time')
return True
def set_ofName(self):
"""
Adds OFNAME keyword to header
"""
#OFNAME was added as a native NIRSPEC keyword around 20190405
if self.get_keyword('OFNAME', False) != None: return True
self.log.info('set_ofName: setting OFNAME keyword value')
#get value
ofName = self.get_keyword('OFNAME')
if (ofName == None):
self.log.error('set_ofName: cannot find value for OFNAME')
return False
#add *.fits to output if it does not exist (to fix old files)
if (ofName.endswith('.fits') == False) : ofName += '.fits'
#update
self.set_keyword('OFNAME', ofName, 'KOA: Original file name')
return True
def set_koaimtyp(self):
'''
Fixes missing KOAIMTYP keyword.
This is derived from OBSTYPE keyword.
'''
self.log.info('set_koaimtyp: setting KOAIMTYP keyword value from OBSTYPE')
#get obstype value
obstype = self.get_keyword('OBSTYPE')
#map to KOAIMTYP value
koaimtyp = 'undefined'
validValsMap = {
'object' : 'object',
'standard': 'object', #NOTE: old val
'telluric': 'object',
'bias' : 'bias',
'dark' : 'dark',
'domeflat': 'domeflat',
'domearc' : 'domearc',
'arclamp' : 'arclamp',
'flatlamp': 'flatlamp',
# 'astro' : 'object', #NOTE: old val
# 'star' : 'object', #NOTE: old val
# 'calib' : 'undefined' #NOTE: old val
}
#first use OBSTYPE value
if (obstype != None and obstype.lower() in validValsMap):
koaimtyp = validValsMap[obstype.lower()]
#use algorithm
else:
self.log.info('set_koaimtyp: setting KOAIMTYP keyword value from algorithm')
calmpos = self.get_keyword('CALMPOS', default='').lower()
calppos = self.get_keyword('CALPPOS', default='').lower()
#calcpos doesn't exist in header
# calcpos = self.get_keyword('CALCPOS', default='').lower()
xenon = self.get_keyword('XENON', default='').lower()
krypton = self.get_keyword('KRYPTON', default='').lower()
argon = self.get_keyword('ARGON', default='').lower()
neon = self.get_keyword('NEON', default='').lower()
#flat doesn't exist
# flat = self.get_keyword('FLAT')
flimagin = self.get_keyword('FLIMAGIN', default='').lower()
flspectr = self.get_keyword('FLSPECTR', default='').lower()
flat = 0
if flimagin == 'on' or flspectr == 'on':
flat = 1
#arclamp
if argon == 'on' or krypton == 'on' or neon == 'on' or xenon == 'on':
if calmpos == 'in' and calppos == 'out':
koaimtyp = 'arclamp'
else:
koaimtyp = 'undefined'
#flats
elif flat == 0 and calmpos == 'in':
koaimtyp = 'flatlampoff'
elif flat == 1 and calmpos == 'in' and calppos == 'out':
koaimtyp = 'flatlamp'
#darks
elif int(self.get_keyword('ITIME')) == 0:
koaimtyp = 'bias'
#object
elif calmpos == 'out' and calppos == 'out':
koaimtyp = 'object'
else:
koaimtyp = 'undefined'
#warn if undefined
if (koaimtyp == 'undefined'):
self.log.info('set_koaimtyp: Could not determine KOAIMTYP from OBSTYPE value')
#update keyword
self.set_keyword('KOAIMTYP', koaimtyp, 'KOA: Image type')
return True
def set_filter(self):
'''
If FILTER keyword doesn't exist, create from SCIFILT1 and 2
'''
if self.get_keyword('FILTER', False) != None: return True
self.log.info('set_filter: setting FILTER keyword from SCIFILT1/2')
scifilt1 = self.get_keyword('SCIFILT1', default='')
scifilt2 = self.get_keyword('SCIFILT2', default='')
skip = ['thick', 'thin', 'open']
if scifilt1.lower() in skip: scifilt1 = ''
if scifilt2.lower() in skip: scifilt2 = ''
filter = ''.join((scifilt1, scifilt2))
#update keyword
self.set_keyword('FILTER', filter, 'KOA: set from SCIFILT1 and SCIFILT2')
return True
def set_wavelengths(self):
'''
Sets WAVEBLUE, CNTR, RED based on FILTER value
'''
self.log.info('set_wavelengths: setting WAVE keyword values from FILTER')
filters = {}
filters['UNKNOWN'] = {'blue':'null', 'cntr':'null', 'red':'null'}
filters['BLANK'] = {'blue':'null', 'cntr':'null', 'red':'null'}
filters['NIRSPEC-1'] = {'blue':0.9470, 'cntr':1.0340, 'red':1.1210}
filters['NIRSPEC-2'] = {'blue':1.0890, 'cntr':1.1910, 'red':1.2930}
filters['NIRSPEC-3'] = {'blue':1.1430, 'cntr':1.2590, 'red':1.3750}
filters['NIRSPEC-4'] = {'blue':1.2410, 'cntr':1.4170, 'red':1.5930}
filters['NIRSPEC-5'] = {'blue':1.4310, 'cntr':1.6195, 'red':1.8080}
filters['NIRSPEC-6'] = {'blue':1.5580, 'cntr':1.9365, 'red':2.3150}
filters['NIRSPEC-7'] = {'blue':1.8390, 'cntr':2.2345, 'red':2.6300}
filters['Br-Gamma'] = {'blue':2.1550, 'cntr':2.1650, 'red':2.1750}
filters['BR-GAMMA'] = {'blue':2.1550, 'cntr':2.1650, 'red':2.1750}
filters['CO'] = {'blue':2.2810, 'cntr':2.2930, 'red':2.3050}
filters['K-PRIME'] = {'blue':1.9500, 'cntr':2.1225, 'red':2.2950}
filters['KL'] = {'blue':2.1340, 'cntr':3.1810, 'red':4.2280}
filters['K'] = {'blue':1.9960, 'cntr':2.1890, 'red':2.3820}
filters['L-PRIME'] = {'blue':3.4200, 'cntr':3.7700, 'red':4.1200}
filters['M-PRIME'] = {'blue':4.5700, 'cntr':4.6900, 'red':4.8100}
filters['HEI'] = {'blue':1.0776, 'cntr':1.0830, 'red':1.0884}
filters['PA-BETA'] = {'blue':1.2757, 'cntr':1.2823, 'red':1.2888}
filters['FEII'] = {'blue':1.6390, 'cntr':1.6465, 'red':1.6540}
filters['H2'] = {'blue':2.1100, 'cntr':2.1195, 'red':2.1290}
filters['M-WIDE'] = {'blue':4.4200, 'cntr':4.9750, 'red':5.5300}
filter = self.get_keyword('FILTER', default='')
waveblue = wavecntr = wavered = 'null'
for filt, waves in filters.items():
if filt in filter.upper():
waveblue = waves['blue']
wavecntr = waves['cntr']
wavered = waves['red']
break
self.set_keyword('WAVEBLUE', waveblue, 'KOA: Approximate blue end wavelength (u)')
self.set_keyword('WAVECNTR', wavecntr, 'KOA: Approximate central wavelength (u)')
self.set_keyword('WAVERED', wavered, 'KOA: Approximate red end wavelength (u)')
return True
def set_isao(self):
'''
Sets the ISAO keyword value: NIRSPEC = no, NIRSPAO = yes
'''
self.log.info('set_isao: setting ISAO keyword values from INSTRUME')
isao = 'no'
instrume = self.get_keyword('INSTRUME')
if instrume == 'NIRSPAO':
isao = 'yes'
self.set_keyword('ISAO', isao, 'KOA: Is this NIRSPAO data?')
return True
def set_dispers(self):
'''
Sets DISPERS, DISPSCAL and SPATSCAL keyword values
'''
dispers = 'null'
dispscal = 'null'
spatscal = 'null'
# Set SPATSCAL = PSCALE
pscale = self.get_keyword('PSCALE')
if pscale == None: pscale = 'null'
spatscal = pscale
if 'NS' in self.get_keyword('KOAID'):
self.log.info('set_dispers: setting DISPERS and DISPSCAL keyword values')
slitname = self.get_keyword('SLITNAME')
isao = self.get_keyword('ISAO')
if slitname == None: dispers = 'unknown'
elif 'x42' in slitname:
dispers = 'low'
dispscal = 0.129
if isao == 'yes': dispscal = 0.012 # 0.129 / 10.6
else:
dispers = 'high'
dispscal = 0.096
if isao == 'yes': dispscal = 0.009 # 0.096 / 10.6
#update keywords
self.set_keyword('DISPERS', dispers, 'KOA: dispersion level')
self.set_keyword('DISPSCAL', dispscal, 'KOA: pixel scale, dispersion (arcsec/pixel)')
self.set_keyword('SPATSCAL', spatscal, 'KOA: pixel scale, spatial (arcsec/pixel)')
return True
def set_slit_values(self):
'''
Sets keyword values defining the slit dimensions
'''
slitlen = 'null'
slitwidt = 'null'
specres = 'null'
#low resolution slitwidt:specres
#y = 4155.1x^2 - 6578.9x + 4400
lowres = {}
lowres['0.144'] = 3540
lowres['0.288'] = 2850
lowres['0.38'] = 2500
lowres['0.432'] = 2330
lowres['0.57'] = 2000
lowres['0.576'] = 1990
lowres['0.72'] = 1820
lowres['0.76'] = 1800
lowresmap = {}
lowresmap['0.036'] = 0.38;
lowresmap['0.054'] = 0.57;
lowresmap['0.072'] = 0.76;
#high resolution slitwidtAO:slitwidt
#y = 10800 / slitwidt
highresmap = {}
highresmap['0.0136'] = 0.144
highresmap['0.0271'] = 0.288
highresmap['0.0407'] = 0.432
highresmap['0.0543'] = 0.576
highresmap['0.0679'] = 0.720
highresmap['0.0272'] = 0.288
highresmap['0.0407'] = 0.432
highresmap['0.0358'] = 0.380
highresmap['0.0538'] = 0.570
highresmap['0.0717'] = 0.760
if self.prefix == 'NS':
self.log.info('set_slit_values: setting SLITLEN and SLITWIDT keyword values from SLITNAME')
slitname = self.get_keyword('SLITNAME')
if 'x' in slitname:
#SLITNAME = 42x0.380 (low resolution)
slitlen, slitwidt = slitname.split('x')
#SLITNAME = 0.144x12 (high resolution)
if slitwidt > slitlen:
slitlen, slitwidt = slitwidt, slitlen
dispers = self.get_keyword('DISPERS')
width = str(slitwidt)
if dispers == 'low':
if self.get_keyword('ISAO') == 'yes':
width = lowresmap[width]
specres = lowres[str(width.rstrip('0'))]
elif dispers == 'high':
if self.get_keyword('ISAO') == 'yes':
width = highresmap[width]
specres = int(ceil(10800/float(width)/100.0))*100
slitlen = float(slitlen)
slitwidt = float(slitwidt)
self.set_keyword('SLITLEN', slitlen, 'KOA: Slit length projected on sky (arcsec)')
self.set_keyword('SLITWIDT', slitwidt, 'KOA: Slit width projected on sky (arcsec)')
self.set_keyword('SPECRES', specres, 'KOA: Nominal spectral resolution')
return True
def set_gain_and_readnoise(self):
'''
Sets the measured values for gain and read noise
Note from GregD (20190429) - still need to measure RN for SCAM,
leave null for now
'''
gain = 2.85
readnoise = 'null'
if self.prefix == 'NS':
readnoise = 10.8
self.set_keyword('DETGAIN', gain, 'KOA: Detector gain')
self.set_keyword('DETRN', readnoise, 'KOA: Detector read noise')
return True