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 | # |
---|
12 | import calendar |
---|
13 | import shutil |
---|
14 | import datetime |
---|
15 | import time |
---|
16 | import os |
---|
17 | from UIOTools import UIOFiles |
---|
18 | from string import strip |
---|
19 | from optparse import OptionParser |
---|
20 | from GribTools import GribTools |
---|
21 | from FlexpartTools import EIFlexpart, daterange |
---|
22 | |
---|
23 | |
---|
24 | def 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 | |
---|
106 | if __name__ == "__main__": |
---|
107 | main() |
---|