source: flex_extract.git/python/prepareFLEXPART.py @ 64cf353

ctbtodev
Last change on this file since 64cf353 was 64cf353, checked in by Anne Philipp <bscannephilipp@…>, 6 years ago

pep8 changes + documentation added + minor code style changes

  • Property mode set to 100644
File size: 3.4 KB
Line 
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#
25import calendar
26import shutil
27import datetime
28import time
29import os,inspect,sys
30import socket
31from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter
32# add path to submit.py to pythonpath so that python finds its buddies
33localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
34if localpythonpath not in sys.path:
35    sys.path.append(localpythonpath)
36from UIOTools import UIOFiles
37#from string import strip
38from GribTools import GribTools
39from FlexpartTools import EIFlexpart, Control, interpret_args_and_control, cleanup
40
41hostname=socket.gethostname()
42ecapi= 'ecmwf' not in hostname
43
44try:
45    if ecapi:
46        import ecmwfapi
47except ImportError:
48    ecapi = False
49
50
51def 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
104if __name__ == "__main__":
105    args, c = interpret_args_and_control()
106    prepareFLEXPART(args, c)
107    cleanup(c)
108
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG