1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # TODO AP |
---|
5 | # |
---|
6 | # - Change History ist nicht angepasst ans File! Original geben lassen |
---|
7 | # - dead code ? what to do? |
---|
8 | # - seperate operational and reanlysis for clarification |
---|
9 | # - divide in two submits , ondemand und operational |
---|
10 | # - |
---|
11 | #************************************************************************ |
---|
12 | |
---|
13 | #******************************************************************************* |
---|
14 | # @Author: Anne Fouilloux (University of Oslo) |
---|
15 | # |
---|
16 | # @Date: October 2014 |
---|
17 | # |
---|
18 | # @Change History: |
---|
19 | # |
---|
20 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
21 | # - job submission on ecgate and cca |
---|
22 | # - job templates suitable for twice daily operational dissemination |
---|
23 | # |
---|
24 | # February 2018 - Anne Philipp (University of Vienna): |
---|
25 | # - applied PEP8 style guide |
---|
26 | # - added documentation |
---|
27 | # - minor changes in programming style (for consistence) |
---|
28 | # |
---|
29 | # @License: |
---|
30 | # (C) Copyright 2014-2018. |
---|
31 | # |
---|
32 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
33 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
34 | # |
---|
35 | # @Program Functionality: |
---|
36 | # This program is the main program of flex_extract and controls the |
---|
37 | # program flow. |
---|
38 | # If it is supposed to work locally then it works through the necessary |
---|
39 | # functions getMARSdata and prepareFlexpart. Otherwise it prepares |
---|
40 | # a shell job script which will do the necessary work on the |
---|
41 | # ECMWF server and is submitted via ecaccess-job-submit. |
---|
42 | # |
---|
43 | # @Program Content: |
---|
44 | # - main |
---|
45 | # - submit |
---|
46 | # |
---|
47 | #******************************************************************************* |
---|
48 | |
---|
49 | # ------------------------------------------------------------------------------ |
---|
50 | # MODULES |
---|
51 | # ------------------------------------------------------------------------------ |
---|
52 | import os |
---|
53 | import sys |
---|
54 | import glob |
---|
55 | import subprocess |
---|
56 | import inspect |
---|
57 | # add path to pythonpath so that python finds its buddies |
---|
58 | localpythonpath = os.path.dirname(os.path.abspath( |
---|
59 | inspect.getfile(inspect.currentframe()))) |
---|
60 | if localpythonpath not in sys.path: |
---|
61 | sys.path.append(localpythonpath) |
---|
62 | |
---|
63 | # software specific classes and modules from flex_extract |
---|
64 | from Tools import interpret_args_and_control, normalexit |
---|
65 | from getMARSdata import getMARSdata |
---|
66 | from prepareFLEXPART import prepareFLEXPART |
---|
67 | |
---|
68 | # ------------------------------------------------------------------------------ |
---|
69 | # FUNCTIONS |
---|
70 | # ------------------------------------------------------------------------------ |
---|
71 | |
---|
72 | def main(): |
---|
73 | ''' |
---|
74 | @Description: |
---|
75 | Get the arguments from script call and from CONTROL file. |
---|
76 | Decides from the argument "queue" if the local version |
---|
77 | is done "queue=None" or the gateway version with "queue=ecgate" |
---|
78 | or "queue=cca". |
---|
79 | |
---|
80 | @Input: |
---|
81 | <nothing> |
---|
82 | |
---|
83 | @Return: |
---|
84 | <nothing> |
---|
85 | ''' |
---|
86 | calledfromdir = os.getcwd() |
---|
87 | args, c = interpret_args_and_control() |
---|
88 | # on local side |
---|
89 | if args.queue is None: |
---|
90 | if c.inputdir[0] != '/': |
---|
91 | c.inputdir = os.path.join(calledfromdir, c.inputdir) |
---|
92 | if c.outputdir[0] != '/': |
---|
93 | c.outputdir = os.path.join(calledfromdir, c.outputdir) |
---|
94 | getMARSdata(args, c) |
---|
95 | prepareFLEXPART(args, c) |
---|
96 | normalexit(c) |
---|
97 | # on ECMWF server |
---|
98 | else: |
---|
99 | submit(args.job_template, c, args.queue) |
---|
100 | |
---|
101 | return |
---|
102 | |
---|
103 | def submit(jtemplate, c, queue): |
---|
104 | ''' |
---|
105 | @Description: |
---|
106 | Prepares the job script and submit it to the specified queue. |
---|
107 | |
---|
108 | @Input: |
---|
109 | jtemplate: string |
---|
110 | Job template file for submission to ECMWF. It contains all necessary |
---|
111 | module and variable settings for the ECMWF environment as well as |
---|
112 | the job call and mail report instructions. |
---|
113 | Default is "job.temp". |
---|
114 | |
---|
115 | c: instance of class ControlFile |
---|
116 | Contains all the parameters of CONTROL file, which are e.g.: |
---|
117 | DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, |
---|
118 | STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, |
---|
119 | LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY, |
---|
120 | OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT, |
---|
121 | ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR, |
---|
122 | MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME |
---|
123 | DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
124 | |
---|
125 | For more information about format and content of the parameter |
---|
126 | see documentation. |
---|
127 | |
---|
128 | queue: string |
---|
129 | Name of queue for submission to ECMWF (e.g. ecgate or cca ) |
---|
130 | |
---|
131 | @Return: |
---|
132 | <nothing> |
---|
133 | ''' |
---|
134 | |
---|
135 | # read template file and split from newline signs |
---|
136 | with open(jtemplate) as f: |
---|
137 | lftext = f.read().split('\n') |
---|
138 | insert_point = lftext.index('EOF') |
---|
139 | |
---|
140 | # put all parameters of ControlFile instance into a list |
---|
141 | clist = c.tolist() |
---|
142 | colist = [] # operational |
---|
143 | mt = 0 |
---|
144 | |
---|
145 | #AP wieso 2 for loops? |
---|
146 | for elem in clist: |
---|
147 | if 'maxstep' in elem: |
---|
148 | mt = int(elem.split(' ')[1]) |
---|
149 | |
---|
150 | for elem in clist: |
---|
151 | if 'start_date' in elem: |
---|
152 | elem = 'start_date ' + '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
153 | if 'end_date' in elem: |
---|
154 | #AP Fehler?! Muss end_date heissen |
---|
155 | elem = 'start_date ' + '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
156 | if 'base_time' in elem: |
---|
157 | elem = 'base_time ' + '${MSJ_BASETIME}' |
---|
158 | if 'time' in elem and mt > 24: |
---|
159 | elem = 'time ' + '${MSJ_BASETIME} {MSJ_BASETIME}' |
---|
160 | colist.append(elem) |
---|
161 | |
---|
162 | lftextondemand = lftext[:insert_point] + clist + lftext[insert_point + 2:] |
---|
163 | lftextoper = lftext[:insert_point] + colist + lftext[insert_point + 2:] |
---|
164 | |
---|
165 | with open('job.ksh', 'w') as h: |
---|
166 | h.write('\n'.join(lftextondemand)) |
---|
167 | |
---|
168 | with open('joboper.ksh', 'w') as h: |
---|
169 | h.write('\n'.join(lftextoper)) |
---|
170 | |
---|
171 | # submit job script to queue |
---|
172 | try: |
---|
173 | p = subprocess.check_call(['ecaccess-job-submit', '-queueName', |
---|
174 | queue, 'job.ksh']) |
---|
175 | except: |
---|
176 | print('ecaccess-job-submit failed, probably eccert has expired') |
---|
177 | exit(1) |
---|
178 | |
---|
179 | print('You should get an email with subject flex.hostname.pid') |
---|
180 | |
---|
181 | return |
---|
182 | |
---|
183 | if __name__ == "__main__": |
---|
184 | main() |
---|