-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrun_metplus.py
executable file
·132 lines (102 loc) · 3.92 KB
/
run_metplus.py
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
#!/usr/bin/env python3
"""
Program Name: run_metplus.py
Contact(s): George McCabe, Julie Prestopnik, Jim Frimel, Minna Win
Abstract: Runs METplus Wrappers scripts
History Log: Initial version
Usage:
Parameters: None
Input Files:
Output Files:
Condition codes:
Developer Note: Please do not use f-strings in this file so that the
Python version check can notify the user of the incorrect version.
Using Python 3.5 or earlier will output the SyntaxError from the
f-string instead of the useful error message.
"""
import os
import sys
import traceback
# add metplus directory to path so the wrappers and utilities can be found
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
os.pardir)))
import produtil.setup
from metplus.util import metplus_check
from metplus.util import pre_run_setup, run_metplus, post_run_cleanup
from metplus import __version__ as metplus_version
'''!@namespace run_metplus
Main script the processes all the tasks in the PROCESS_LIST
'''
def main():
"""!Main program.
METplus script that invokes the necessary Python scripts
to perform various activities, such as series analysis."""
config_inputs = get_config_inputs_from_command_line()
config = pre_run_setup(config_inputs)
# warn if calling master_metplus.py
script_name = os.path.basename(__file__)
if script_name == 'master_metplus.py':
msg = ("master_metplus.py has been renamed to run_metplus.py. "
"This script name will be removed in a future version.")
config.logger.warning(msg)
total_errors = run_metplus(config)
post_run_cleanup(config, 'METplus', total_errors)
def usage():
"""! How to call this script.
"""
filename = os.path.basename(__file__)
print ('''
Usage: %s arg1 arg2 arg3
-h|--help Display this usage statement
Arguments:
/path/to/parmfile.conf -- Specify custom configuration file to use
section.option=value -- override conf options on the command line
'''%(filename))
sys.exit(2)
def get_config_inputs_from_command_line():
"""! Read command line arguments. Pull out configuration
files and configuration variable overrides. Display
usage statement if invalid configuration or if help
statement is requested, i.e. -h. Report error if
invalid flag was provided, i.e. -a.
@returns list of config inputs
"""
# output version that is run to screen
print('Running METplus %s' % metplus_version)
# if not arguments were provided, print usage and exit
if len(sys.argv) < 2:
usage()
# print usage statement and exit if help arg is found
help_args = ('-h', '--help', '-help')
for help_arg in help_args:
if help_arg in sys.argv:
usage()
sys.exit(0)
# pull out command line arguments
config_inputs = []
for arg in sys.argv[1:]:
if arg.startswith('-'):
# ignore -c and --config since they are now optional
if arg == '-c' or arg == '--config' or arg == '-config':
continue
# error/exit if an argument that is not supported was used
print('ERROR: Invalid argument: %s.' % arg)
usage()
# split up comma separated lists into individual items
# and add each to list of arguments
# NOTE: to support lists in a config variable override,
# this logic will have to be enhanced
# i.e. config.PROCESS_LIST=PCPCombine,GridStat
config_inputs.extend(arg.split(','))
# if no valid config_inputs were found, print usage and exit
if not config_inputs:
usage()
return config_inputs
if __name__ == "__main__":
try:
produtil.setup.setup(send_dbn=False, jobname='run-METplus')
main()
except Exception as exc:
print(traceback.format_exc())
print('ERROR: run_metplus failed: %s' % exc)
sys.exit(2)