1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
4 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
5 | # |
---|
6 | # Functionality provided: Prepare input 3D-wind fields in hybrid coordinates + surface fields for FLEXPART runs |
---|
7 | # |
---|
8 | # Creation: October 2014 - Anne Fouilloux - University of Oslo |
---|
9 | # Extension November 2015 - Leopold Haimberger - University of Vienna for: |
---|
10 | # - using the WebAPI also for general MARS retrievals |
---|
11 | # - job submission on ecgate and cca |
---|
12 | # - job templates suitable for twice daily operational dissemination |
---|
13 | # - dividing retrievals of longer periods into digestable chunks |
---|
14 | # - retrieve also longer term forecasts, not only analyses and short term forecast data |
---|
15 | # - conversion into GRIB2 |
---|
16 | # - conversion into .fp format for faster execution of FLEXPART |
---|
17 | # |
---|
18 | # Further documentation may be obtained from www.flexpart.eu |
---|
19 | # |
---|
20 | # Requirements: |
---|
21 | # in addition to a standard python 2.6 or 2.7 installation the following packages need to be installed |
---|
22 | # ECMWF WebMARS, gribAPI with python enabled, emoslib, ecaccess web toolkit, all available from https://software.ecmwf.int/ |
---|
23 | # dateutils |
---|
24 | # matplotlib (optional, for debugging) |
---|
25 | # |
---|
26 | # |
---|
27 | import calendar |
---|
28 | import shutil |
---|
29 | import datetime |
---|
30 | import time |
---|
31 | import os,sys,glob |
---|
32 | import subprocess |
---|
33 | import inspect |
---|
34 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
35 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
36 | sys.path.append(localpythonpath) |
---|
37 | from UIOTools import UIOFiles |
---|
38 | from string import strip |
---|
39 | from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter |
---|
40 | from GribTools import GribTools |
---|
41 | from FlexpartTools import EIFlexpart, Control, interpret_args_and_control, normalexit,myerror |
---|
42 | from getMARSdata import getMARSdata |
---|
43 | from prepareFLEXPART import prepareFLEXPART |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | def main(): |
---|
48 | |
---|
49 | calledfromdir=os.getcwd() |
---|
50 | # os.chdir(localpythonpath) |
---|
51 | args,c=interpret_args_and_control() |
---|
52 | if args.queue==None: |
---|
53 | if c.inputdir[0]!='/': |
---|
54 | c.inputdir=os.path.join(calledfromdir,c.inputdir) |
---|
55 | if c.outputdir[0]!='/': |
---|
56 | c.outputdir=os.path.join(calledfromdir,c.outputdir) |
---|
57 | getMARSdata(args,c) |
---|
58 | if c.request == '0' or c.request == '2': |
---|
59 | prepareFLEXPART(args,c) |
---|
60 | normalexit(c) |
---|
61 | else: |
---|
62 | normalexit(c) |
---|
63 | else: |
---|
64 | submit(args.job_template,c,args.queue) |
---|
65 | |
---|
66 | |
---|
67 | def submit(jtemplate,c,queue): |
---|
68 | |
---|
69 | f=open(jtemplate) |
---|
70 | lftext=f.read().split('\n') |
---|
71 | insert_point=lftext.index('EOF') |
---|
72 | f.close() |
---|
73 | |
---|
74 | clist=c.tolist() |
---|
75 | colist=[] |
---|
76 | mt=0 |
---|
77 | for elem in clist: |
---|
78 | if 'maxstep' in elem: |
---|
79 | mt=int(elem.split(' ')[1]) |
---|
80 | |
---|
81 | for elem in clist: |
---|
82 | if 'start_date' in elem: |
---|
83 | elem='start_date '+'${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
84 | if 'end_date' in elem: |
---|
85 | elem='start_date '+'${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
86 | if 'base_time' in elem: |
---|
87 | elem='base_time '+'${MSJ_BASETIME}' |
---|
88 | if 'time' in elem and mt>24: |
---|
89 | elem='time '+'${MSJ_BASETIME} {MSJ_BASETIME}' |
---|
90 | colist.append(elem) |
---|
91 | |
---|
92 | lftextondemand=lftext[:insert_point]+clist+lftext[insert_point+2:] |
---|
93 | lftextoper=lftext[:insert_point]+colist+lftext[insert_point+2:] |
---|
94 | |
---|
95 | h=open('job.ksh','w') |
---|
96 | h.write('\n'.join(lftextondemand)) |
---|
97 | h.close() |
---|
98 | |
---|
99 | h=open('joboper.ksh','w') |
---|
100 | h.write('\n'.join(lftextoper)) |
---|
101 | h.close() |
---|
102 | |
---|
103 | try: |
---|
104 | p=subprocess.check_call(['ecaccess-job-submit','-queueName',queue,'job.ksh']) |
---|
105 | except: |
---|
106 | print 'ecaccess-job-submit failed, probably eccert has expired' |
---|
107 | exit(1) |
---|
108 | #pout=p.communicate(input=s)[0] |
---|
109 | print 'You should get an email with subject flex.hostname.pid' |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | if __name__ == "__main__": |
---|
115 | main() |
---|