gofioDRS.py 5.67 KB
Newer Older
Monica Rainer's avatar
Monica Rainer committed
1
2
3
4
5
6
"""
GOFIO DRS
Written by Monica Rainer

Usage:
    gofioDRS.py -h
7
8
9
    gofioDRS.py [-g=<config>]
    gofioDRS.py <date> [-g=<config>]
    gofioDRS.py <date> <calib_date> [-g=<config>]
Monica Rainer's avatar
Monica Rainer committed
10
11
    gofioDRS.py <date> [--dark --flat --une --fp --all_calib --only_calib --use_flat=<flag> --s1d=<s1d> --group]
    gofioDRS.py <date> <calib_date> [--dark --flat --une --fp --all_calib --only_calib --use_flat=<flag> --s1d=<s1d> --group]
Monica Rainer's avatar
Monica Rainer committed
12
13
14

Options:
    -h,--help           : show this screen
15
16
    -g,--cfg=<config>   : path of the configuration file (optional)
    date                : date to be reduced (always first input)
Monica Rainer's avatar
Monica Rainer committed
17
18
19
20
21
22
23
24
25
26
27
28
29
    calib_date          : calibration date to be used (always second input)
    --dark              : darks are reduced
    --flat              : flats are reduced
    --une               : U-Ne lamps are reduced
    --fp                : FP lamps are reduced
    --all_calib         : all the calibrations are reduced
    --only_calib        : only calibrations are reduced, no science images
                          (the calibrations to be reduced must be defined
                           using the above parameters)
    --use_flat=<flag>   : flat-field is removed,
                          options are global/order/nor/none [Default: order]
    --s1d=<s1d>         : s1d outputs are created, either normalized or not 
                          options are yes/no/norm [Default: yes]
Monica Rainer's avatar
Monica Rainer committed
30
31
    --group             : group reduction of all the images of the same
                          observing block
Monica Rainer's avatar
Monica Rainer committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

"""

import os, glob, sys, time

from docopt import docopt

from drslib.logger import DrsLogger

from drslib import db, gofio

# use watchdog to monitor file creation
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler



# --------------------------------
#   read docopt args
# --------------------------------
docopt_args = docopt(__doc__)
53
#print docopt_args
Monica Rainer's avatar
Monica Rainer committed
54
55
56
57
58
59
60

def run_gofio(CONFIG, rawlists, dbcalib, working, group, stop, drslogger, dbnight, dbramps):
    do_calib = CONFIG['DO_CALIB']
    rawdir = CONFIG['RAW_DIR']

# check if there are already files in RAW_DIR, create list raws in temporal order
    fits = os.path.join(rawdir,"*.fts")
61
    #global raws
Monica Rainer's avatar
Monica Rainer committed
62
63
64
    raws = glob.glob(fits)
    raws.sort()

65
66
67
68
69
70
71
72
73
74
75
76
# watchdog append fits files to the raw list
    class UpdateRawList(PatternMatchingEventHandler):
        def process(self, event):
            #global raws
            raws.append(event.src_path)
            drslogger.log('File %s has arrived.' % (event.src_path))

        def on_created(self, event):
            ext = os.path.splitext(event.src_path)[1]
            if ext == '.fts':
                self.process(event)

Monica Rainer's avatar
Monica Rainer committed
77
78
79
80
81
82
83
84
85
86
87
88
89
# use watchdog to check when new files are created
    observer = Observer()
    observer.schedule(UpdateRawList(),rawdir)
    observer.start()

    try:
        while True:
            time.sleep(1)

# the raw files are processed
            while raws:
                if CONFIG['OFFLINE']:
                    working, group, go, do_calib, rawlists = gofio.GianoBreduce(raws[0], rawlists, dbcalib, working, group, do_calib, stop, drslogger, dbnight)
90
                    #print rawlists
Monica Rainer's avatar
Monica Rainer committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
                    if go:
                        raws.pop(0)
                    #raws.pop(0)

                else:
                    for n in xrange(len(raws)):
                        if db.check_night(dbnight,os.path.basename(raws[n])):
                            raws.pop(n)
                            break
                        #elif db.check_raw(dbramps,raws[n]):
                        elif db.check_raw(dbramps,os.path.basename(raws[n])):
                            working, group, go, do_calib, rawlists = gofio.GianoBreduce(raws[n], rawlists, dbcalib, working, group, do_calib, stop, drslogger, dbnight)
                            if go:
                                raws.pop(n)
                            break

                        else:
                            time.sleep(1)

            if CONFIG['OFFLINE']:
111
112
113
                if rawlists[CONFIG['SCIENCE']] and group['nodstare'] == CONFIG['STAREVALUE']:
                    gofio.GianoBreduce(None, rawlists, dbcalib, working, group, do_calib, stop, drslogger, dbnight)

Monica Rainer's avatar
Monica Rainer committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                drslogger.log('No more files to reduce.')
                dbcalib.close()
                try: os.remove(CONFIG['MASK_C'])
                except: pass
                sys.exit(0)

    except KeyboardInterrupt:
        observer.stop()
        # reduce any file still pending!
        if working:
            drslogger.log('Processing the last pending files (if any).')
            stop = True
            if CONFIG['OFFLINE']:
                gofio.GianoBreduce(raws[0], rawlists, dbcalib, working, group, do_calib, stop, drslogger, dbnight)
            else:
                for n in xrange(len(raws)):
                    if db.check_raw(dbramps,raws[n]):
                        gofio.GianoBreduce(raws[n], rawlists, dbcalib, working, group, do_calib, stop, drslogger, dbnight)
                        break
133
134
135
136
137

            if not raws:
                if rawlists[CONFIG['SCIENCE']] and group['nodstare'] == CONFIG['STAREVALUE']:
                    gofio.GianoBreduce(None, rawlists, dbcalib, working, group, do_calib, stop, drslogger, dbnight)

Monica Rainer's avatar
Monica Rainer committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        try: os.remove(CONFIG['MASK_C'])
        except: pass
        drslogger.log('Pipeline stopped.')
        dbcalib.close()
        if not CONFIG['OFFLINE']:
            dbramps.close()
            dbnight.close()
        sys.exit(0)
        
    observer.join()


if __name__ == "__main__":
    CONFIG, rawlists, dbcalib, working, group, stop, drslogger, dbnight, dbramps = gofio.gofio_init(docopt_args)
    run_gofio(CONFIG, rawlists, dbcalib, working, group, stop, drslogger, dbnight, dbramps)