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 | # Requirements: |
---|
19 | # in addition to a standard python 2.6 or 2.7 installation the following packages need to be installed |
---|
20 | # ECMWF WebMARS, gribAPI with python enabled, emoslib, ecaccess web toolkit, all available from https://software.ecmwf.int/ |
---|
21 | # dateutils |
---|
22 | # matplotlib (optional, for debugging) |
---|
23 | # |
---|
24 | import calendar |
---|
25 | import shutil |
---|
26 | import datetime |
---|
27 | import time |
---|
28 | import os,inspect,sys |
---|
29 | import socket |
---|
30 | from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter |
---|
31 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
32 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
33 | if localpythonpath not in sys.path: |
---|
34 | sys.path.append(localpythonpath) |
---|
35 | from UIOTools import UIOFiles |
---|
36 | #from string import strip |
---|
37 | from GribTools import GribTools |
---|
38 | from FlexpartTools import EIFlexpart, Control,interpret_args_and_control, cleanup |
---|
39 | |
---|
40 | hostname=socket.gethostname() |
---|
41 | ecapi= 'ecmwf' not in hostname |
---|
42 | |
---|
43 | try: |
---|
44 | if ecapi: |
---|
45 | import ecmwfapi |
---|
46 | except ImportError: |
---|
47 | ecapi=False |
---|
48 | |
---|
49 | |
---|
50 | def prepareFLEXPART(args,c): |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | namelist='fort.4' |
---|
55 | |
---|
56 | if not args.ppid: |
---|
57 | c.ppid=str(os.getppid()) |
---|
58 | else: |
---|
59 | c.ppid=args.ppid |
---|
60 | |
---|
61 | c.ecapi=ecapi |
---|
62 | |
---|
63 | syear=int(c.start_date[:4]) |
---|
64 | smonth=int(c.start_date[4:6]) |
---|
65 | sday=int(c.start_date[6:]) |
---|
66 | start = datetime.date( year = syear, month = smonth, day = sday ) |
---|
67 | eyear=int(c.end_date[:4]) |
---|
68 | emonth=int(c.end_date[4:6]) |
---|
69 | eday=int(c.end_date[6:]) |
---|
70 | |
---|
71 | end = datetime.date( year = eyear, month = emonth, day = eday ) |
---|
72 | |
---|
73 | inputfiles=UIOFiles(['.grib', '.grb', '.grib1', '.grib2', '.grb1','.grb2']) |
---|
74 | |
---|
75 | startm1=start- datetime.timedelta(days=1) |
---|
76 | endp1=end + datetime.timedelta(days=1) |
---|
77 | |
---|
78 | inputfiles.listFiles(c.inputdir, '*OG_acc_SL*.'+c.ppid+'.*') |
---|
79 | if not os.path.exists(c.outputdir): |
---|
80 | os.makedirs(c.outputdir) |
---|
81 | |
---|
82 | flexpart = EIFlexpart(c,fluxes=True) |
---|
83 | flexpart.create_namelist(c,'fort.4') |
---|
84 | flexpart.deacc_fluxes(inputfiles, c) |
---|
85 | |
---|
86 | inputfiles=UIOFiles(['.grib', '.grb', '.grib1', '.grib2', '.grb1','.grb2']) |
---|
87 | |
---|
88 | print 'Prepare '+start.strftime("%Y%m%d") + "/to/" + end.strftime("%Y%m%d") |
---|
89 | # we will make the list of files from the root inputdir |
---|
90 | inputfiles.listFiles(c.inputdir, '????__??.*'+c.ppid+'.*') |
---|
91 | |
---|
92 | if c.basetime=='00': |
---|
93 | start=startm1 |
---|
94 | flexpart = EIFlexpart(c) |
---|
95 | flexpart.create(inputfiles, c) # produce FLEXPART-ready GRIB files |
---|
96 | flexpart.process_output(c) # process GRIB files - copy/transfer/interpolate them or make them GRIB2 |
---|
97 | |
---|
98 | if int(c.debug)!=0: |
---|
99 | print('Temporary files left intact') |
---|
100 | else: |
---|
101 | cleanup(c) |
---|
102 | |
---|
103 | if __name__ == "__main__": |
---|
104 | args,c=interpret_args_and_control() |
---|
105 | prepareFLEXPART(args,c) |
---|
106 | cleanup(c) |
---|
107 | |
---|