rawfiles.py 21.8 KB
Newer Older
Monica Rainer's avatar
Monica Rainer committed
1
2
3
4
5
6
7
"""
The FITS files are divided in darks, flats, UNes, FPs and science
using the header keywords (defined in config.py).
If all the images of one group have been taken, the command to
process them is passed to the pipeline.
If a different kind of image arrives, the previous ones are processed
even if the group is incomplete.
Monica Rainer's avatar
Monica Rainer committed
8
9
10
WARNING: the dark files are n times the expected number, because for
each observations the ramp-processor creates n images with different
exposure times (10, 30, 60, 100 seconds, and so on).
Monica Rainer's avatar
Monica Rainer committed
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
"""

from drslib.config import CONFIG
from astropy.io import fits
import os
import drslib.varie as varie
import drslib.db as db

class GBCheckRawFiles():
    def __init__(self, rawfile,lists,working,group,do_calib):
        self.rawfile = rawfile
        self.lists = lists
        self.working = working
        self.group = group
        self.do_calib = do_calib
        self.go = 1
        self.command = {CONFIG['DARK']:False, CONFIG['FLAT']:False, CONFIG['WCAL_UNE']:False, CONFIG['WCAL_FP']:False, CONFIG['NODVALUE']:False, CONFIG['STAREVALUE']:False}
        self.messages = []


    def check_raw_file(self):
        """Divide raw files in darks, flats, wl, fp and science"""

        filename = str(os.path.basename(self.rawfile))

        with fits.open(self.rawfile, mode='readonly') as hdu:
            hdr = hdu[0].header

            try:
                obstype = hdr[CONFIG['KEYS']['OBSTYPE']]
            except KeyError:
                self.messages.append('Missing keyword %s in file %s. File skipped.' % (CONFIG['KEYS']['OBSTYPE'],filename))
                return

            self.messages.append('%s is a %s file.' % (filename,obstype))

# ----------------- Check if the pipeline was working on another OBSTYPE
            if self.working and self.working != obstype:

# ----------------- Check if there was an incomplete nodding pair and discard it


# --------------- Check if there was an incomplete group (stare or nodding) and reduce it
                if self.working == CONFIG['SCIENCE'] and self.group:
                    if self.group['nodstare'] == CONFIG['NODVALUE'] and self.group['pair']:
                        skipped = self.lists[CONFIG['SCIENCE']].pop()
                        self.messages.append('A %s file has arrived. The previous nodding is incomplete, the file %s will not be reduced.' % (obstype,str(os.path.basename(skipped)),))
                        self.group.pop('pair',None)
                        if self.group['ngroup'] == 1:
                            self.group.clear()
                            self.go = 0
                            return
                    self.messages.append('A %s file has arrived. The previous group is incomplete, but it will be reduced anyways.' % (obstype,))
                    self.command[self.group['nodstare']] = 'incomplete_group'
                    #self.group['nodstare'] = False
                    self.working = 0
                    self.go = 0
                    return

# ----------- If the pipeline was working on calibration, reduce them
                self.command[self.working] = True
                self.messages.append('A %s file has arrived. The previous %s files will be reduced even if only %s has/have been taken.' % (obstype,self.working,len(self.lists[self.working]),))
                self.working = 0
                self.go = 0
                return

            try:
                groupn = int(hdr[CONFIG['KEYS']['GROUPN']])
            except:
                groupn = 1

            try:
                groupi = int(hdr[CONFIG['KEYS']['GROUPI']])
            except:
                groupi = 1

            try:
                groupid = int(hdr[CONFIG['KEYS']['GROUPID']])
            except:
                pass


# Check the file, see the type, if the group is complete reduce it

# ---- DARK
            if obstype == CONFIG['DARK'] and self.do_calib['dark']:
# while groupid is still not implemented...
                try:
                    if self.group and self.group['id'] != group_id:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s darks out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[CONFIG['DARK']] = True
                        self.working = 0
                        self.go = 0
                        return
                except:
                    pass

                if self.group:
                    self.group['n'] = groupi

                else:
                    self.working = CONFIG['DARK']
                    try: self.group['id'] = group_id
                    except: pass
                    self.group['ngroup'] = groupn
                    self.group['n'] = groupi

118
119
120
121
122
                if len(self.lists[CONFIG['DARK']]) > groupn*int(CONFIG['NDARK']):
                    self.messages.append('A new group of %s has started, the previous one will now be reduced.' % (obstype))
                    self.command[CONFIG['DARK']] = True
                    self.working = 0
                    return
123

Monica Rainer's avatar
Monica Rainer committed
124
125
126
127
128
129
130
                self.lists[CONFIG['DARK']].append(self.rawfile)
                self.messages.append('%s out of %s %s files observed.' % (len(self.lists[CONFIG['DARK']]),groupn*int(CONFIG['NDARK']),obstype))

                if len(self.lists[CONFIG['DARK']]) == groupn*int(CONFIG['NDARK']):
                    self.command[CONFIG['DARK']] = True
                    self.messages.append('All expected %s have been observed. They will now be reduced.' % (obstype))
                    self.working = 0
131

Monica Rainer's avatar
Monica Rainer committed
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
                return

            elif obstype == CONFIG['DARK']:
                self.messages.append('There is already a masterdark for this night, %s will not be reduced.' % (filename,))
                return

# ---- FLAT
            elif obstype == CONFIG['FLAT'] and self.do_calib['flat']:
# while groupid is still not implemented...
                try:
                    if self.group and self.group['id'] != group_id:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s flat-fields out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[CONFIG['FLAT']] = True
                        self.working = 0
                        self.go = 0
                        return
                except:
                    pass

                if self.group:
                    self.group['n'] = groupi

                else:
                    self.working = CONFIG['FLAT']
                    try: self.group['id'] = group_id
                    except: pass
                    self.group['ngroup'] = groupn
                    self.group['n'] = groupi

161
162
163
164
165
166
                if len(self.lists[CONFIG['FLAT']]) > groupn:
                    self.messages.append('A new group of %s has started, the previous one will now be reduced.' % (obstype))
                    self.command[CONFIG['FLAT']] = True
                    self.working = 0
                    return

Monica Rainer's avatar
Monica Rainer committed
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
                self.lists[CONFIG['FLAT']].append(self.rawfile)
                self.messages.append('%s out of %s %s files observed.' % (len(self.lists[CONFIG['FLAT']]),groupn,obstype))

                if groupi == groupn:
                    self.command[CONFIG['FLAT']] = True
                    self.messages.append('All expected %s have been observed. They will now be reduced.' % (obstype))
                    self.working = 0
                return


            elif obstype == CONFIG['FLAT']:
                self.messages.append('There is already a masterflat for this night, %s will not be reduced.' % (filename,))
                return

# ---- UNe LAMPS
            elif obstype == CONFIG['WCAL_UNE'] and self.do_calib['une']:
# while groupid is still not implemented...
                try:
                    if self.group and self.group['id'] != group_id:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s UNe lamps out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[CONFIG['WCAL_UNE']] = True
                        self.working = 0
                        self.go = 0
                        return
                except:
                    pass

                if self.group:
                    self.group['n'] = groupi

                else:
                    self.working = CONFIG['WCAL_UNE']
                    try: self.group['id'] = group_id
                    except: pass
                    self.group['ngroup'] = groupn
                    self.group['n'] = groupi

204
205
206
207
208
209
                if len(self.lists[CONFIG['WCAL_UNE']]) > groupn:
                    self.messages.append('A new group of %s has started, the previous one will now be reduced.' % (obstype))
                    self.command[CONFIG['WCAL_UNE']] = True
                    self.working = 0
                    return

Monica Rainer's avatar
Monica Rainer committed
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
                self.lists[CONFIG['WCAL_UNE']].append(self.rawfile)
                self.messages.append('%s out of %s %s files observed.' % (len(self.lists[CONFIG['WCAL_UNE']]),groupn,obstype))

                if groupi == groupn:
                    self.command[CONFIG['WCAL_UNE']] = True
                    self.messages.append('All expected %s have been observed. They will now be reduced.' % (obstype))
                    self.working = 0
                return

            elif obstype == CONFIG['WCAL_UNE']:
                self.messages.append('There is already a masterlamp UNe for this night, %s will not be reduced.' % (filename,))
                return

# ---- FP LAMPS
            elif obstype == CONFIG['WCAL_FP'] and self.do_calib['fp']:
# while groupid is still not implemented...
                try:
                    if self.group and self.group['id'] != group_id:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s FP lamps out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[CONFIG['WCAL_FP']] = True
                        self.working = 0
                        self.go = 0
                        return
                except:
                    pass

                if self.group:
                    self.group['n'] = groupi

                else:
                    self.working = CONFIG['WCAL_FP']
                    try: self.group['id'] = group_id
                    except: pass
                    self.group['ngroup'] = groupn
                    self.group['n'] = groupi

246
247
248
249
250
251
                if len(self.lists[CONFIG['WCAL_FP']]) > groupn:
                    self.messages.append('A new group of %s has started, the previous one will now be reduced.' % (obstype))
                    self.command[CONFIG['WCAL_FP']] = True
                    self.working = 0
                    return

Monica Rainer's avatar
Monica Rainer committed
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
                self.lists[CONFIG['WCAL_FP']].append(self.rawfile)
                self.messages.append('%s out of %s %s files observed.' % (len(self.lists[CONFIG['WCAL_FP']]),groupn,obstype))

                if groupi == groupn:
                    self.command[CONFIG['WCAL_FP']] = True
                    self.messages.append('All expected %s have been observed. They will now be reduced.' % (obstype))
                    self.working = 0
                return


# ---- SCIENCE
            elif obstype == CONFIG['SCIENCE']:

                if self.do_calib['only_calib']:
                    return

                try:
                    extmode = hdr[CONFIG['KEYS']['EXTMODE']]
                except:
                    #extmode = CONFIG['EXTPAIR']
                    extmode = CONFIG['EXTDEFAULT']

                try:
                    slitpos = hdr[CONFIG['KEYS']['SLIT']]
                except:
                    self.messages.append('No slit position defined for %s, it will not be reduced.' % (filename,))
                    return

                try:
                    nod_stare = hdr[CONFIG['KEYS']['NODSTARE']]
                except:
                    if slitpos == CONFIG['C']:
                        nod_stare = CONFIG['STAREVALUE']
                    elif slitpos == CONFIG['A'] or slitpos == CONFIG['B']:
                        nod_stare = CONFIG['NODVALUE']
                    else:
                        self.messages.append('Incorrect value %s for slit position: %s will not be reduced.' % (slitpos,filename,))
                        return

                self.working = CONFIG['SCIENCE']
                try: obj_name = hdr[CONFIG['KEYS']['OBJECT']]
                except:
                    self.messages.append('No keyword %s found in frame %s.' % (CONFIG['KEYS']['OBJECT'],filename,))
                    obj_name = 'test'

# ---- Check if it was working on different group (frame with different OBSID):
                try:
                    group_id = hdr[CONFIG['KEYS']['ID']]
                except:
                    self.messages.append('No keyword %s found in frame %s.' % (CONFIG['KEYS']['ID'],filename,))
                    #group_id = nod_stare

                try:
                    if self.group and self.group['id'] != group_id:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s observations out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[self.group['nodstare']] = 'incomplete_group'
                        #self.group['nodstare'] = False
                        self.group.pop('pair', None)
                        self.group['pair'] = False
                        self.working = 0
                        self.go = 0
                        #self.lists[CONFIG['SCIENCE']][:] = []
                        return
                except:
                    if self.group and self.group['object'] != obj_name:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s observations out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[self.group['nodstare']] = 'incomplete_group'
                        #self.group['nodstare'] = False
                        self.group.pop('pair', None)
                        self.group['pair'] = False
                        self.working = 0
                        self.go = 0
                        #self.lists[CONFIG['SCIENCE']][:] = []
                        return

                    elif self.group and self.group['nodstare'] != nod_stare:
                        self.messages.append('A %s file of a new group has arrived. The previous group is incomplete (only %s observations out of %s), but it will be reduced anyways.' % (obstype,self.group['n'],self.group['ngroup'],))
                        self.command[self.group['nodstare']] = 'incomplete_group'
                        #self.group['nodstare'] = False
                        self.group.pop('pair', None)
                        self.group['pair'] = False
                        self.working = 0
                        self.go = 0
                        #self.lists[CONFIG['SCIENCE']][:] = []
                        return
 
# NODDING/STARE
# check if the file is part of a new pair/sequence or not
# if a new pair, discard the lone image of the old pair
# if new sequence, reduce the incomplete sequence

                if nod_stare == CONFIG['NODVALUE']:
                    #print self.group
                    try:
                        groupn = int(hdr[CONFIG['KEYS']['NPAIRS']])
                    except:
                        #groupn = 1
                        pass


                    try:
                        pair_id = hdr[CONFIG['KEYS']['PID']]
                        #print pair_id
                        if self.group and self.group['pair'] and self.group['pair'] != pair_id:
                            skipped = self.lists[CONFIG['SCIENCE']].pop()
                            self.messages.append('A %s file of a new science pair has arrived. The previous pair is incomplete, the file %s will not be reduced.' % (obstype,str(os.path.basename(skipped)),))
                            self.group.pop('pair', None)
                            self.group['pair'] = False
                            if groupn ==1:
                                self.group.clear()
                            self.go = 0
                            return

                    except:
                        pair_id = {'object':obj_name,'pos':slitpos}
                        if self.group and self.group['pair'] and self.group['pair']['object'] != obj_name:
                            skipped = self.lists[CONFIG['SCIENCE']].pop()
                            self.messages.append('A %s file of a new science pair has arrived. The previous pair is incomplete, the file %s will not be reduced.' % (obstype,str(os.path.basename(skipped)),))
                            self.group.pop('pair', None)
                            self.group['pair'] = False
                            if groupn ==1:
                                self.group.clear()
                            self.go = 0
                            return
                        elif self.group and self.group['pair'] and self.group['pair']['pos'] == slitpos:
                            skipped = self.lists[CONFIG['SCIENCE']].pop()
                            self.messages.append('A %s file of a new science pair has arrived. The previous pair is incomplete, the file %s will not be reduced.' % (obstype,str(os.path.basename(skipped)),))
                            self.group.pop('pair', None)
                            self.group['pair'] = False
                            if groupn ==1:
                                self.group.clear()
                            self.go = 0
                            return

                    try: self.group['id'] = group_id
                    except: self.group['id'] = nod_stare
                    self.group['nodstare'] = nod_stare
                    self.group['pair'] = pair_id
                    self.group['object'] = obj_name
                    self.group['ngroup'] = groupn
                    self.group['n'] = groupi
                    if 'noddings' not in self.group:
                        self.group['noddings'] = []


                    self.lists[CONFIG['SCIENCE']].append(self.rawfile)

                    if len(self.lists[CONFIG['SCIENCE']]) == 2:
                        self.command[self.group['nodstare']] = 'pair'
                        self.working = 0
                        self.group.pop('pair', None)
                        self.group['pair'] = False
                        self.messages.append('A complete AB nodding has arrived and it will now be reduced.')

                        if extmode == CONFIG['EXTAVG'] and self.group['n'] == self.group['ngroup']:
                            self.command[self.group['nodstare']] = 'group'
                            self.messages.append('A complete nodding group has arrived and it will now be reduced.')


                    return


# ----- STARE

                elif nod_stare == CONFIG['STAREVALUE']:

                    try:
                        pair_id = hdr[CONFIG['KEYS']['PID']]
                    except:
                        pair_id = obj_name

                    if self.group and self.group['pair'] and self.group['pair'] != pair_id:
                        self.messages.append('A %s file of a new sequence has arrived. The previous Obj-Sky sequence is incomplete, but it will be reduced anyway.' % (obstype))
                        self.command[self.group['nodstare']] = 'incomplete_group'
                        self.group.pop('pair', None)
                        self.group['pair'] = False
                        self.go = 0
                        return

                    try: self.group['id'] = group_id
                    except: self.group['id'] = nod_stare
                    self.group['nodstare'] = nod_stare
                    self.group['pair'] = pair_id
                    self.group['object'] = obj_name
                    self.group['ngroup'] = 2*groupn
                    self.group['n'] = groupi
                    if 'stares' not in self.group:
                        self.group['stares'] = []

                    self.lists[CONFIG['SCIENCE']].append(self.rawfile)

                    #if len(self.lists[CONFIG['SCIENCE']]) == 2*groupn:
                    if len(self.lists[CONFIG['SCIENCE']]) == 100:
                        self.command[self.group['nodstare']] = 'pair'
                        self.working = 0
                        self.group.pop('pair', None)
                        self.group['pair'] = False
                        self.messages.append('A complete Obj-Sky stare sequence has arrived and it will now be reduced.')

                        if extmode == CONFIG['EXTAVG'] and self.group['n'] == self.group['ngroup']:
                            self.command[self.group['nodstare']] = 'group'
                            self.messages.append('A complete Obj-Sky stare group has arrived and it will now be reduced.')

                    return
                  
                else:
                    try:
                        self.messages.append('The %s has an uncorrect keyword value (%s = %s), it will not be reduced.' % (filename,CONFIG['NODSTARE'],hdr[CONFIG['NODSTARE']],))
                    except:
                        self.messages.append('The %s has an uncorrect keyword value (slitpos = %s), it will not be reduced.' % (filename,slitpos,))
                    self.working = 0 

                    return

            else:
                self.messages.append('The %s has an uncorrect keyword value (%s = %s), it will not be reduced.' % (filename,CONFIG['KEYS']['OBSTYPE'],obstype,))

            self.working = 0 

            return