1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #******************************************************************************* |
---|
4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
5 | # |
---|
6 | # @Date: October 2014 |
---|
7 | # |
---|
8 | # @Change History: |
---|
9 | # |
---|
10 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
11 | # - job submission on ecgate and cca |
---|
12 | # - job templates suitable for twice daily operational dissemination |
---|
13 | # |
---|
14 | # February 2018 - Anne Philipp (University of Vienna): |
---|
15 | # - applied PEP8 style guide |
---|
16 | # - added documentation |
---|
17 | # - minor changes in programming style (for consistence) |
---|
18 | # - changed path names to variables from config file |
---|
19 | # - added option for writing mars requests to extra file |
---|
20 | # additionally,as option without submitting the mars jobs |
---|
21 | # |
---|
22 | # @License: |
---|
23 | # (C) Copyright 2014-2018. |
---|
24 | # |
---|
25 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
26 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
27 | # |
---|
28 | # @Program Functionality: |
---|
29 | # This program is the main program of flex_extract and controls the |
---|
30 | # program flow. |
---|
31 | # If it is supposed to work locally then it works through the necessary |
---|
32 | # functions get_mars_data and prepareFlexpart. Otherwise it prepares |
---|
33 | # a shell job script which will do the necessary work on the |
---|
34 | # ECMWF server and is submitted via ecaccess-job-submit. |
---|
35 | # |
---|
36 | # @Program Content: |
---|
37 | # - main |
---|
38 | # - submit |
---|
39 | # |
---|
40 | #******************************************************************************* |
---|
41 | |
---|
42 | # ------------------------------------------------------------------------------ |
---|
43 | # MODULES |
---|
44 | # ------------------------------------------------------------------------------ |
---|
45 | import os |
---|
46 | import sys |
---|
47 | import subprocess |
---|
48 | import inspect |
---|
49 | import collections |
---|
50 | |
---|
51 | # software specific classes and modules from flex_extract |
---|
52 | import _config |
---|
53 | from mods.tools import (normal_exit, get_cmdline_arguments, |
---|
54 | submit_job_to_ecserver, read_ecenv) |
---|
55 | from mods.get_mars_data import get_mars_data |
---|
56 | from mods.prepare_flexpart import prepare_flexpart |
---|
57 | from classes.ControlFile import ControlFile |
---|
58 | |
---|
59 | # ------------------------------------------------------------------------------ |
---|
60 | # FUNCTIONS |
---|
61 | # ------------------------------------------------------------------------------ |
---|
62 | |
---|
63 | def main(): |
---|
64 | ''' |
---|
65 | @Description: |
---|
66 | Get the arguments from script call and from CONTROL file. |
---|
67 | Decides from the argument "queue" if the local version |
---|
68 | is done "queue=None" or the gateway version with "queue=ecgate" |
---|
69 | or "queue=cca". |
---|
70 | |
---|
71 | @Input: |
---|
72 | <nothing> |
---|
73 | |
---|
74 | @Return: |
---|
75 | <nothing> |
---|
76 | ''' |
---|
77 | |
---|
78 | args = get_cmdline_arguments() |
---|
79 | c = ControlFile(args.controlfile) |
---|
80 | |
---|
81 | env_parameter = read_ecenv(_config.PATH_ECMWF_ENV) |
---|
82 | c.assign_args_to_control(args) |
---|
83 | c.assign_envs_to_control(env_parameter) |
---|
84 | c.check_conditions(args.queue) |
---|
85 | |
---|
86 | # on local side |
---|
87 | # on ECMWF server this would also be the local side |
---|
88 | called_from_dir = os.getcwd() |
---|
89 | if args.queue is None: |
---|
90 | if c.inputdir[0] != '/': |
---|
91 | c.inputdir = os.path.join(called_from_dir, c.inputdir) |
---|
92 | if c.outputdir[0] != '/': |
---|
93 | c.outputdir = os.path.join(called_from_dir, c.outputdir) |
---|
94 | get_mars_data(c) |
---|
95 | if c.request == 0 or c.request == 2: |
---|
96 | prepare_flexpart(args.ppid, c) |
---|
97 | normal_exit(c.mailfail, 'FLEX_EXTRACT IS DONE!') |
---|
98 | else: |
---|
99 | normal_exit(c.mailfail, 'PRINTING MARS_REQUESTS DONE!') |
---|
100 | # send files to ECMWF server and install there |
---|
101 | else: |
---|
102 | submit(args.job_template, c, args.queue) |
---|
103 | |
---|
104 | return |
---|
105 | |
---|
106 | def submit(jtemplate, c, queue): |
---|
107 | ''' |
---|
108 | @Description: |
---|
109 | Prepares the job script and submit it to the specified queue. |
---|
110 | |
---|
111 | @Input: |
---|
112 | jtemplate: string |
---|
113 | Job template file from sub-directory "_templates" for |
---|
114 | submission to ECMWF. It contains all necessary |
---|
115 | module and variable settings for the ECMWF environment as well as |
---|
116 | the job call and mail report instructions. |
---|
117 | Default is "job.temp". |
---|
118 | |
---|
119 | c: instance of class ControlFile |
---|
120 | Contains all the parameters of CONTROL file and |
---|
121 | command line. |
---|
122 | For more information about format and content of the parameter |
---|
123 | see documentation. |
---|
124 | |
---|
125 | queue: string |
---|
126 | Name of queue for submission to ECMWF (e.g. ecgate or cca ) |
---|
127 | |
---|
128 | @Return: |
---|
129 | <nothing> |
---|
130 | ''' |
---|
131 | |
---|
132 | # read template file and get index for CONTROL input |
---|
133 | with open(os.path.join(_config.PATH_TEMPLATES, jtemplate)) as f: |
---|
134 | lftext = f.read().split('\n') |
---|
135 | insert_point = lftext.index('EOF') |
---|
136 | |
---|
137 | if not c.basetime: |
---|
138 | # --------- create on demand job script ------------------------------------ |
---|
139 | if c.maxstep > 24: |
---|
140 | print('---- Pure forecast mode! ----') |
---|
141 | else: |
---|
142 | print('---- On-demand mode! ----') |
---|
143 | job_file = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
144 | jtemplate[:-4] + 'ksh') |
---|
145 | clist = c.to_list() |
---|
146 | |
---|
147 | lftextondemand = lftext[:insert_point] + clist + lftext[insert_point:] |
---|
148 | |
---|
149 | with open(job_file, 'w') as f: |
---|
150 | f.write('\n'.join(lftextondemand)) |
---|
151 | |
---|
152 | submit_job_to_ecserver(queue, job_file) |
---|
153 | |
---|
154 | else: |
---|
155 | # --------- create operational job script ---------------------------------- |
---|
156 | print('---- Operational mode! ----') |
---|
157 | job_file = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
158 | jtemplate[:-5] + 'oper.ksh') |
---|
159 | |
---|
160 | if c.maxstep: |
---|
161 | mt = int(c.maxstep) |
---|
162 | else: |
---|
163 | mt = 0 |
---|
164 | |
---|
165 | c.start_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
166 | c.end_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
167 | c.base_time = '${MSJ_BASETIME}' |
---|
168 | if mt > 24: |
---|
169 | c.time = '${MSJ_BASETIME} {MSJ_BASETIME}' |
---|
170 | |
---|
171 | colist = c.to_list() |
---|
172 | |
---|
173 | lftextoper = lftext[:insert_point] + colist + lftext[insert_point + 2:] |
---|
174 | |
---|
175 | with open(job_file, 'w') as f: |
---|
176 | f.write('\n'.join(lftextoper)) |
---|
177 | |
---|
178 | submit_job_to_ecserver(queue, job_file) |
---|
179 | |
---|
180 | # -------------------------------------------------------------------------- |
---|
181 | print('You should get an email with subject flex.hostname.pid') |
---|
182 | |
---|
183 | return |
---|
184 | |
---|
185 | if __name__ == "__main__": |
---|
186 | main() |
---|