cliopts.py 23.3 KB
Newer Older
Lab's avatar
Update  
Lab committed
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
from optparse import OptionParser   # from version 2.3 to 2.7
from argparse import ArgumentParser
from . import standardhandle
from . import options
##############################################################################
#
# closercliparsing
#
##############################################################################
def closer_argparser():
    tag = standardhandle.gettag()
    parser = ArgumentParser()
    parser.add_argument("-c", "--config", action = "store", dest = "configfile", default = None,
                      help = "use specific config file [default cfg/osa.cfg]")
    parser.add_argument("-d", "--date", action = "store", type = str, dest = "date",
                      help = "observation ending date YYYY_MM_DD [default today]")
    parser.add_argument("-n", "--usenightsummary", action = "store_true", dest = "nightsum", default = False,
                      help = "rely on existing nightsumary file")
    parser.add_argument("-o", "--outputdir", action = "store", type = str, dest = "directory",
                      help = "analysis output directory")
    parser.add_argument("-r", "--reason", action = "store", type = str, dest = "reason",
                      choices = ['moon', 'weather', 'other'],
                      help = "reason for closing without data: (moon, weather, other)")
    parser.add_argument("-s", "--simulate", action = "store_true", dest = "simulate", default = False,
                      help = "do not run, just show what would happen")
    parser.add_argument("-y", "--yes", action = "store_true", dest = "noninteractive", default = False,
                      help = "assume yes to all questions")
    parser.add_argument("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                      help = "make lots of noise for debugging")
    parser.add_argument("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                      help = "show useful warnings")
    parser.add_argument("--stderr", action = "store", type = str, dest = "stderr",
                      help = "file for standard error")
    parser.add_argument("--stdout", action = "store", type = str, dest = "stdout",
                      help = "file for standard output")
    parser.add_argument("--seq", action = "store", type = str, dest = "seqtoclose",
                      help="If you only want to close a certain sequence")
    parser.add_argument('tel_id', choices=['ST', 'LST1', 'LST2'])
    return parser

def closercliparsing():
    tag = standardhandle.gettag()

    # Parse the command line
    opts = closer_argparser().parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout
    options.date = opts.date
    options.directory = opts.directory
    options.nightsum = opts.nightsum
    options.noninteractive = opts.noninteractive
    options.simulate = opts.simulate
    options.verbose = opts.verbose
    options.warning = opts.warning
    options.reason = opts.reason
    options.seqtoclose = opts.seqtoclose
    options.tel_id = opts.tel_id

    standardhandle.verbose(tag, "the options are {0}".format(opts))

    # Setting the default date and directory if needed
    options.configfile = set_default_configfile_if_needed('closer.py')
    options.date = set_default_date_if_needed()
    options.directory = set_default_directory_if_needed()

    # Setting on the usage of night summary
    options.nightsum = True
##############################################################################
#
# calibrationsequencecliparsing
#
##############################################################################
def calibrationsequencecliparsing(command):
    tag = standardhandle.gettag()
    message = "usage: %prog [-vw] [-c CONFIGFILE] [-d DATE] [-o OUTPUTDIR] [-z] <CAL_RUN_ID> <PED_RUN_ID>  <TEL_ID>"
    parser = OptionParser(usage = message)
    parser.add_option("-c", "--config", action = "store", dest = "configfile", default = None,
                      help = "use specific config file [default cfg/osa.cfg]")
    parser.add_option("-d", "--date", action = "store", type = "string", dest = "date",
                      help = "observation ending date YYYY_MM_DD [default today]")
    parser.add_option("-o", "--outputdir", action = "store", type = "string", dest = "directory",
                      help = "write files to output directory")
    parser.add_option("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                      help = "make lots of noise for debugging")
    parser.add_option("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                      help = "show useful warnings")
    parser.add_option("-z", "--rawzip", action = "store_true", dest = "compressed", default = False,
                      help = "Use input as compressed raw.gz files")
    parser.add_option("--stderr", action = "store", type = "string", dest = "stderr",
                      help = "file for standard error")
    parser.add_option("--stdout", action = "store", type = "string", dest = "stdout",
                      help = "file for standard output")

    # Parse the command line
    (opts, args) = parser.parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout
    options.date = opts.date
    options.directory = opts.directory
    options.verbose = opts.verbose
    options.warning = opts.warning
    options.compressed = opts.compressed

    # The standardhandle has to be declared here, since verbose and warnings are options from the cli
    standardhandle.verbose(tag, "the options are {0}".format(opts))
    standardhandle.verbose(tag, "the argument is {0}".format(args))

    # Mapping the telescope argument to an option parameter (it might become an option in the future)
    if len(args) != 3:
        standardhandle.error(tag, "incorrect number of arguments, type -h for help", 2)
    elif args[1] == 'ST':
        standardhandle.error(tag, "not yet ready for telescope {0}".format(options.options.tel_id), 2)
    elif args[1] != 'LST1' and args[1] != 'LST2':
        standardhandle.error(tag, "wrong telescope id, use 'LST1', 'LST2' or 'ST'", 2)

    options.tel_id = args[1]

    # Setting the default date and directory if needed
    options.configfile = set_default_configfile_if_needed(command)
    options.date = set_default_date_if_needed()
    options.directory = set_default_directory_if_needed()

    return args
##############################################################################
#
# datasequencecliparsing
#
##############################################################################
def datasequencecliparsing(command):
    tag = standardhandle.gettag()
Lab's avatar
Lab committed
137
    message = "usage: %prog  [-vw] [--stderr=FILE] [--stdout=FILE] [-c CONFIGFILE] [-d DATE] [-o OUTPUTDIR] [-z] <calibrationfile> <pedestalfile> <drivefile> <timecalibration> <RUN> <TEL_ID>"
Lab's avatar
Update  
Lab committed
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
    parser = OptionParser(usage = message)
    parser.add_option("-c", "--config", action = "store", dest = "configfile", default = None,
                      help = "use specific config file [default cfg/osa.cfg]")
    parser.add_option("-d", "--date", action = "store", type = "string", dest = "date",
                      help = "observation ending date YYYY_MM_DD [default today]")
    parser.add_option("-o", "--outputdir", action = "store", type = "string", dest = "directory",
                          help = "analysis output directory")
    parser.add_option("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                      help = "make lots of noise for debugging")
    parser.add_option("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                      help = "show useful warnings")
    parser.add_option("-z", "--rawzip", action = "store_true", dest = "compressed", default = False,
                      help = "Use input as compressed raw.gz files")
    parser.add_option("--stderr", action = "store", type = "string", dest = "stderr",
                      help = "file for standard error")
    parser.add_option("--stdout", action = "store", type = "string", dest = "stdout",
                      help = "file for standard output")

    # Parse the command line
    (opts, args) = parser.parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout    
    options.date = opts.date
    options.directory = opts.directory
    options.verbose = opts.verbose
    options.warning = opts.warning
    options.compressed = opts.compressed

    # The standardhandle has to be declared here, since verbose and warnings are options from the cli
    standardhandle.verbose(tag, "the options are {0}".format(opts))
    standardhandle.verbose(tag, "the argument is {0}".format(args))

    # Checking arguments
Lab's avatar
Lab committed
174
    if len(args) != 6:
Lab's avatar
Update  
Lab committed
175
176
        standardhandle.error(tag, "incorrect number of arguments, type -h for help", 2)
    # Mapping the telescope argument to an option parameter (it might become an option in the future)
Lab's avatar
Lab committed
177
    elif args[5] == 'ST':
Lab's avatar
Update  
Lab committed
178
        standardhandle.error(tag, "not yet ready for telescope {0}".format(options.options.tel_id), 2)
Lab's avatar
Lab committed
179
    elif args[5] != 'LST1' and args[5] != 'LST2':
Lab's avatar
Update  
Lab committed
180
        standardhandle.error(tag, "wrong telescope id, use 'LST1', 'LST2' or 'ST'", 2)
Lab's avatar
Lab committed
181
    options.tel_id = args[5]
Lab's avatar
Update  
Lab committed
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

    # Setting the default date and directory if needed
    options.configfile = set_default_configfile_if_needed(command)
    options.date = set_default_date_if_needed()
    options.directory = set_default_directory_if_needed()

    return args
##############################################################################
#
# stereosequencecliparsing
#
##############################################################################
def stereosequencecliparsing(command):
    tag = standardhandle.gettag()
    message = "usage: %prog  [-vw] [--stderr=FILE] [--stdout=FILE] [-c CONFIGFILE] [-d DATE] [-o OUTPUTDIR] [-z] <RUN>"
    parser = OptionParser(usage = message)
    parser.add_option("-c", "--config", action = "store", dest = "configfile", default = None,
                      help = "use specific config file [default cfg/osa.cfg]")
    parser.add_option("-d", "--date", action = "store", type = "string", dest = "date",
                      help = "observation ending date YYYY_MM_DD [default today]")
    parser.add_option("-o", "--outputdir", action = "store", type = "string", dest = "directory",
                          help = "analysis output directory")
    parser.add_option("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                      help = "make lots of noise for debugging")
    parser.add_option("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                      help = "show useful warnings")
    parser.add_option("-z", "--rawzip", action = "store_true", dest = "compressed", default = False,
                      help = "Use input as compressed raw.gz files")
    parser.add_option("--stderr", action = "store", type = "string", dest = "stderr",
                      help = "file for standard error")
    parser.add_option("--stdout", action = "store", type = "string", dest = "stdout",
                      help = "file for standard output")

    # Parse the command line
    (opts, args) = parser.parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout    
    options.date = opts.date
    options.directory = opts.directory
    options.verbose = opts.verbose
    options.warning = opts.warning
    options.compressed = opts.compressed

    # The standardhandle has to be declared here, since verbose and warnings are options from the cli
    standardhandle.verbose(tag, "the options are {0}".format(opts))
    standardhandle.verbose(tag, "the argument is {0}".format(args))

    # Checking arguments
    if len(args) != 1:
        standardhandle.error(tag, "incorrect number of arguments, type -h for help", 2)
    # Mapping the telescope argument to an option parameter (it might become an option in the future)
    options.tel_id = 'ST'

    # Setting the default date and directory if needed
    options.configfile = set_default_configfile_if_needed(command)
    options.date = set_default_date_if_needed()
    options.directory = set_default_directory_if_needed()

    return args
##############################################################################
#
# sequencercliparsing
#
##############################################################################
def sequencer_argparser():
    tag = standardhandle.gettag()
    parser = ArgumentParser()
    """ Options which define variables """
    parser.add_argument("-c", "--config", action = "store", dest = "configfile", default = None,
                        help = "use specific config file [default cfg/osa.cfg]")
    parser.add_argument("-d", "--date", action = "store", type = str, dest = "date",
                        help = "observation ending date YYYY_MM_DD [default today]")
    parser.add_argument("-m", "--mode", action = "store", type = str, dest = "mode", choices = ['P', 'S', 'T'],
                        help = "mode to run dependant sequences:\n P=parallel [default], S=Sequential, T=temperature-aware")
    """ Boolean options """
    parser.add_argument("-n", "--usenightsummary", action = "store_true", dest = "nightsum", default = False,
                        help = "rely on existing nightsumary file")
    parser.add_argument("-o", "--outputdir", action = "store", type = str, dest = "directory",
                        help = "analysis output directory")
    parser.add_argument("-s", "--simulate", action = "store_true", dest = "simulate", default = False,
                        help = "do not submit sequences as jobs")
    parser.add_argument("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                        help = "make lots of noise for debugging")
    parser.add_argument("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                        help = "show useful warnings")
    parser.add_argument("-z", "--rawzip", action = "store_true", dest = "compressed", default = False,
                        help = "Use input as compressed raw.gz files, compulsory if using -n and raw.gz files")
    parser.add_argument("--stderr", action = "store", type = str, dest = "stderr",
                        help = "file for standard error")
    parser.add_argument("--stdout", action = "store", type = str, dest = "stdout",
                        help = "file for standard output")
    parser.add_argument('tel_id', choices=['ST', 'LST1', 'LST2', 'all'], help = "telescope identifier LST1, LST2, ST or all.")
    return parser

def sequencercliparsing(command):
    tag = standardhandle.gettag()

    # Parse the command line
    opts = sequencer_argparser().parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout
    options.date = opts.date
    options.directory = opts.directory
    options.mode = opts.mode
    options.nightsum = opts.nightsum
    options.simulate = opts.simulate
    options.verbose = opts.verbose
    options.warning = opts.warning
    options.compressed = opts.compressed
    options.tel_id = opts.tel_id

    # The standardhandle has to be declared before here, since verbose and warnings are options from the cli
    standardhandle.verbose(tag, "the options are {0}".format(opts))

    # Set the default value for mode
    if not opts.mode:
        options.mode = 'P'

    # Setting the default date and directory if needed
    options.configfile = set_default_configfile_if_needed('sequencer.py')
    options.date = set_default_date_if_needed()
##############################################################################
#
# monolithcliparsing
#
##############################################################################
def monolithcliparsing(command):
    tag = standardhandle.gettag()
    message = "usage: %prog [-syvw] [--stderr=FILE] [--stdout=FILE] [-c CONFIGFILE] [-t] TEL_ID"
    parser = OptionParser(usage = message)
    # Options which define variables
    parser = OptionParser(usage = message)
    parser.add_option("-c", "--config", action = "store", dest = "configfile", default = None,
                      help = "use specific config file [default cfg/osa.cfg]")
    parser.add_option("-t", "--telescope", action = "store", type = "choice", dest = "tel_id",
                      choices = ['LST1', 'LST2', 'ST'], help = "telescope identifier LST1, LST2 or ST [default all]")
    parser.add_option("-s", "--simulate", action = "store_true", dest = "simulate", default = False,
                      help = "do not run, just show what would happen")
    parser.add_option("-y", "--yes", action = "store_true", dest = "noninteractive", default = False,
                      help = "assume yes to all questions")
    parser.add_option("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                      help = "make lots of noise for debugging")
    parser.add_option("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                      help = "show useful warnings")
    parser.add_option("--stderr", action = "store", type = "string", dest = "stderr",
                      help = "file for standard error")
    parser.add_option("--stdout", action = "store", type = "string", dest = "stdout",
                      help = "file for standard output")

    # Parse the command line
    (opts, args) = parser.parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout    
    options.tel_id = opts.tel_id
    options.simulate = opts.simulate
    options.noninteractive = opts.noninteractive
    options.verbose = opts.verbose
    options.warning = opts.warning

    # The standardhandle has to be declared here, since verbose and warnings are options from the cli
    standardhandle.verbose(tag, "the options are {0}".format(opts))
    standardhandle.verbose(tag, "the argument is {0}".format(args))

    # Mapping the telescope argument to an option parameter (it might become an option in the future)
    if len(args) > 1:
        standardhandle.error(tag, "incorrect number of arguments, type -h for help", 2)
    elif len(args) == 1:
        if (args[0] != 'LST1' and args[0] != 'LST2' and args[0] != 'ST'):
            standardhandle.error(tag, "wrong telescope id, use 'LST1', 'LST2' or 'ST'", 2)
        options.tel_id = args[0]

    options.configfile = set_default_configfile_if_needed(command)
    return args
##############################################################################
#
# rawcopycliparsing
#
##############################################################################
def rawcopycliparsing(command):
    tag = standardhandle.gettag()
    message = "usage: %prog [-vw] [--stderr=FILE] [--stdout=FILE] [-c CONFIGFILE] [-d DATE] [-z] <TEL_ID>"
    parser = OptionParser(usage = message)
    parser.add_option("-c", "--config", action = "store", dest = "configfile", default = None,
                      help = "use specific config file [default rawcopy.cfg]")
    parser.add_option("-d", "--date", action = "store", type = "string", dest = "date",
                      help = "observation ending date YYYY_MM_DD [default today]")
    parser.add_option("--nocheck", action = "store_true", dest = "nocheck", default = False,
                          help = "Skip checking if the daily activity is set over")
    parser.add_option("-v", "--verbose", action = "store_true", dest = "verbose", default = False,
                      help = "make lots of noise for debugging")
    parser.add_option("-w", "--warnings", action = "store_true", dest = "warning", default = False,
                      help = "show useful warnings")
    parser.add_option("-z", "--rawzip", action = "store_true", dest = "compressed", default = False,
                      help = "compress output into raw.gz files")
    parser.add_option("--stderr", action = "store", type = "string", dest = "stderr",
                      help = "file for standard error")
    parser.add_option("--stdout", action = "store", type = "string", dest = "stdout",
                      help = "file for standard output")

    # Parse the command line
    (opts, args) = parser.parse_args()

    # Set global variables
    options.configfile = opts.configfile
    options.stderr = opts.stderr
    options.stdout = opts.stdout    
    options.date = opts.date 
    options.nocheck = opts.nocheck
    options.verbose = opts.verbose
    options.warning = opts.warning
    options.compressed = opts.compressed

    # The standardhandle has to be declared here, since verbose and warnings are options from the cli
    standardhandle.verbose(tag, "the options are {0}".format(opts))
    standardhandle.verbose(tag, "the argument is {0}".format(args))

    # Mapping the telescope argument to an option parameter (it might become an option in the future)
    if len(args) != 1:
        standardhandle.error(tag, "incorrect number of arguments, type -h for help", 2)
    elif args[0] == 'ST':
        standardhandle.error(tag, "not yet ready for telescope {0}".format(options.options.tel_id), 2)
    elif args[0] != 'LST1' and args[0] != 'M2':
        standardhandle.error(tag, "wrong telescope id, use 'LST1', 'LST2' or 'ST'", 2)
    options.tel_id = args[0]

    options.configfile = set_default_configfile_if_needed(command)
    options.date = set_default_date_if_needed()
    return args
##############################################################################
#
# set_default_date_if_needed
#
##############################################################################
def set_default_date_if_needed():
    tag = standardhandle.gettag()
    from .utils import is_defined
    if is_defined(options.date):
        return options.date
    else:
Lab's avatar
Lab committed
430
431
432
        from .utils import getcurrentdate2
        from osa.configs.config import cfg
        return getcurrentdate2(cfg.get('LST', 'DATESEPARATOR'))
Lab's avatar
Update  
Lab committed
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
##############################################################################
#
# set_default_directory_if_needed
#
##############################################################################
def set_default_directory_if_needed():
    tag = standardhandle.gettag()
    from osa.utils.utils import is_defined, getnightdirectory
    if is_defined(options.directory):
        return options.directory
    else:
        return getnightdirectory()
##############################################################################
#
# set_default_configfile_if_needed
#
##############################################################################
def set_default_configfile_if_needed(command):
    tag = standardhandle.gettag()

    """ The default config will be the name of the program, with suffix .cfg
        and present in the cfg subdir, trivial, isn't it? """ 

    from os.path import abspath, dirname, basename, join
    standardhandle.verbose(tag, "Command is {0}".format(command))
    if not options.configfile:
        command_dirname = dirname(abspath(command))
        command_basename = basename(command)
        config_basename = command_basename.replace('.py', '.cfg')
        options.configfile = join(command_dirname, 'cfg', config_basename)

    standardhandle.verbose(tag, "Setting default config file to {0}".format(options.configfile))
    return options.configfile