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