source: flexpart.git/preproc/python/prepareFLEXPART.py @ 0e29ef4

10.4.1_peseiFPv9.3.1FPv9.3.1b_testingFPv9.3.2GFS_025bugfixes+enhancementsdevfp9.3.1-20161214-nc4grib2nc4_repairrelease-10release-10.4.1scaling-bugunivie
Last change on this file since 0e29ef4 was 0e29ef4, checked in by Anne Fouilloux <annefou@…>, 9 years ago

bug fix when looping over months/dates

  • Property mode set to 100755
File size: 3.5 KB
Line 
1#!/usr/bin/env python
2#
3# (C) Copyright 2014 UIO.
4#
5# This software is licensed under the terms of the Apache Licence Version 2.0
6# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7#
8# Creation: October 2014 - Anne Fouilloux - University of Oslo
9#
10# Compute WINDFILEDS from ECMWF GRIB data
11#
12import calendar
13import shutil
14import datetime
15import time
16import os
17from UIOTools import UIOFiles
18from string import strip
19from optparse import OptionParser
20from GribTools import GribTools
21from FlexpartTools import EIFlexpart, daterange
22
23
24def main():
25    usage = """usage: %prog --start_date=YYYYMMDD [--end_date=YYYYMMDD] [--namelist=namelist_for_convert] [--inputdir=input_root_directory] [--outputdir=output_directory] """
26    parser = OptionParser(usage=usage)
27    parser.add_option("--start_date", dest="start_date",
28                      help="start date YYYYMMDD", metavar="start_date" )
29    parser.add_option( "--end_date", dest="end_date",
30                      help="end_date YYYYMMDD", metavar="end_date")
31    parser.add_option("--namelist", dest="namelist",
32                      help="namelist used for converting", metavar="namelist")
33    parser.add_option("--inputdir", dest="inputdir",
34                      help="root directory for reading input files", metavar="inputdir")
35    parser.add_option("--outputdir", dest="outputdir",
36                      help="root directory for storing output files", metavar="outputdir")
37    (options, args) = parser.parse_args()
38
39    if not options.start_date:
40        parser.error("start date must be specified!")
41    else:
42        start_date=options.start_date
43
44    if not options.end_date:
45        end_date=start_date
46    else:
47        end_date=options.end_date
48
49    if not options.namelist:
50        namelist='fort.4'
51    else:
52        namelist=options.namelist
53
54    if not options.inputdir:
55# if WORKDIR is defined, we will use it otherwise files
56# will be stored in the current directory
57        inputdir=os.environ.get("WORKDIR",".")
58    else:
59        inputdir=options.inputdir
60
61    if not options.outputdir:
62# if FLEXPART_WINDS is defined, we will use it otherwise files
63# will be stored in the current directory
64        outputdur=os.environ.get("FLEXPART_WINDS",".")
65    else:
66        outputdir=options.outputdir
67
68
69
70    syear=int(start_date[:4])
71    smonth=int(start_date[4:6])
72    sday=int(start_date[6:])
73    start = datetime.date( year = syear, month = smonth, day = sday )
74    eyear=int(end_date[:4])
75    emonth=int(end_date[4:6])
76    eday=int(end_date[6:])
77
78    end = datetime.date( year = eyear, month = emonth, day = eday )
79
80
81
82    cyear = -1
83    cmont = -1
84    inputfiles=UIOFiles(['.grib', '.grb', '.grib1', '.grib2', '.grb1','.grb2'])
85    if (not os.path.exists('fort.4')):
86        fnamelist=open('fort.4','w')
87        shutil.copyfileobj(open(namelist,'r'), fnamelist)
88        fnamelist.close()
89    for date in daterange( start, end ):
90# data retrieved by year/month
91           if cyear != date.year or cmonth != date.month:
92             print 'Prepare year : ' + str(date.year) + ' month : ', date.month
93             cyear = date.year
94             cmonth = date.month
95
96             # we will make the list of files from the root inputdir
97             if cmonth < 10:
98                 inputfiles.listFiles(inputdir + '/'+str(cyear)+'/0'+str(date.month))
99             else:
100                 inputfiles.listFiles(inputdir + '/'+str(cyear)+'/'+str(date.month))
101                 
102             flexpart = EIFlexpart()
103             flexpart.create(inputfiles, outputdir)
104             
105
106if __name__ == "__main__":
107    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG