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

Usage:
Monica Rainer's avatar
Monica Rainer committed
6
7
8
9
    gofioDRS.py -h | --help
    gofioDRS.py [(-g <config> | --cfg=<config>)]
    gofioDRS.py <date> [(-g <config> | --cfg=<config>)]
    gofioDRS.py <date> <calib_date> [(-g <config> | --cfg=<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
Monica Rainer's avatar
   
Monica Rainer committed
15
16
    --cfg=<config>      : path of the configuration file (optional)
                          'filepath/filename'
Monica Rainer's avatar
Monica Rainer committed
17
18
    -g <config>         : path of the configuration file (optional)
                          'filepath/filename'
19
    date                : date to be reduced (always first input)
Monica Rainer's avatar
Monica Rainer committed
20
21
22
23
24
25
26
27
28
29
30
31
32
    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
33
34
    --group             : group reduction of all the images of the same
                          observing block
Monica Rainer's avatar
Monica Rainer committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

"""

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__)
56
#print docopt_args
Monica Rainer's avatar
Monica Rainer committed
57
58
59
60
61
62
63

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")
64
    #global raws
Monica Rainer's avatar
Monica Rainer committed
65
66
67
    raws = glob.glob(fits)
    raws.sort()

68
69
70
71
72
73
74
75
76
77
78
79
# 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
80
# use watchdog to check when new files are created
Andrea Bignamini's avatar
Andrea Bignamini committed
81
82
83
84
85
    if not CONFIG['OFFLINE']:
        observer = Observer()
        observer.schedule(UpdateRawList(),rawdir)
        observer.start()

Monica Rainer's avatar
Monica Rainer committed
86
87
88
89
90
91
92
93
94

    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)
95
                    #print rawlists
Monica Rainer's avatar
Monica Rainer committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                    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']:
116
117
118
                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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
                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
138
139
140
141
142

            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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        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)