comparison ecv_retrieve.py @ 0:5c0ab9932311 draft

planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
author climate
date Fri, 03 May 2019 15:11:01 -0400
parents
children b9d9fe0c2ce7
comparison
equal deleted inserted replaced
-1:000000000000 0:5c0ab9932311
1 # Retrieve Copernicus ECV
2 # (Essential climate Variables)
3
4 import argparse
5 import os
6 import shutil
7 import tarfile
8 import tempfile
9 import warnings
10
11 import cdsapi
12
13
14 class ECV ():
15 def __init__(self, archive, variable, product_type, year,
16 month, time_aggregation, area, format, output,
17 verbose=False
18 ):
19 self.archive = archive
20 self.variable = variable.split(',')
21 if product_type == '':
22 self.product_type = 'climatology'
23 else:
24 self.product_type = product_type
25 if year == '':
26 self.year = '2019'
27 else:
28 self.year = year.split(',')
29 if month == '':
30 self.month = '01'
31 else:
32 self.month = month.split(',')
33 if time_aggregation == '':
34 self.time_aggregation = '1_month'
35 else:
36 self.time_aggregation = time_aggregation
37 self.area = area
38 if format == '':
39 self.format = 'tgz'
40 else:
41 self.format = format
42 if output == '':
43 self.outputfile = "donwload." + self.format
44 else:
45 self.outputfile = output
46 if verbose:
47 print("archive: ", self.archive)
48 print("variable: ", self.variable)
49 print("year: ", self.year)
50 print("month: ", self.month)
51 self.cdsapi = cdsapi.Client()
52
53 def retrieve(self):
54
55 self.cdsapi.retrieve(
56 self.archive, {
57 'variable': self.variable,
58 'year': self.year,
59 'month': self.month,
60 'area': self.area,
61 'format': self.format,
62 'product_type': self.product_type,
63 'time_aggregation': self.time_aggregation,
64 },
65 self.outputfile)
66
67 def checktar(self):
68 is_grib = False
69 with open(self.outputfile, 'rb') as ofile:
70 is_grib = ofile.read(4)
71 if (is_grib == b'GRIB' and self.format == 'tgz'):
72 # we create a tgz to be consistent
73 newfilename = tempfile.NamedTemporaryFile()
74 gribfile = os.path.basename(newfilename.name) + '.grib'
75 shutil.copyfile(self.outputfile, gribfile)
76 newfilename.close()
77 tar = tarfile.open(self.outputfile, 'w:gz')
78 tar.add(gribfile)
79 tar.close()
80
81
82 if __name__ == '__main__':
83 warnings.filterwarnings("ignore")
84 parser = argparse.ArgumentParser()
85
86 remove_apikey = False
87 current_pwd = os.environ['HOME']
88 if 'GALAXY_COPERNICUS_CDSAPIRC_KEY' in os.environ and \
89 not os.path.isfile('.cdsapirc'):
90 with open(".cdsapirc", "w+") as apikey:
91 apikey.write("url: https://cds.climate.copernicus.eu/api/v2\n")
92 apikey.write(
93 "key: " + os.environ['GALAXY_COPERNICUS_CDSAPIRC_KEY'])
94 remove_apikey = True
95
96 parser.add_argument(
97 'archive',
98 help='Archive name'
99 )
100 parser.add_argument(
101 'variable',
102 help='Specify which variable to retrieve'
103 )
104 parser.add_argument(
105 '--product_type',
106 help='Type of product (climatology or anomaly)'
107 )
108 parser.add_argument(
109 '--year',
110 help='Year(s) to retrieve.'
111 )
112 parser.add_argument(
113 '--month',
114 help='List of months to retrieve.'
115 )
116 parser.add_argument(
117 '--time_aggregation',
118 help='Time range over which data is aggregated (monthly/yearly).'
119 )
120 parser.add_argument(
121 '--area',
122 help='Desired sub-area to extract (North/West/South/East)'
123 )
124 parser.add_argument(
125 '--format',
126 help='Output file format (GRIB or netCDF or tgz)'
127 )
128 parser.add_argument(
129 '--output',
130 help='output filename'
131 )
132 parser.add_argument(
133 "-v", "--verbose",
134 help="switch on verbose mode",
135 action="store_true")
136 args = parser.parse_args()
137
138 p = ECV(args.archive, args.variable, args.product_type,
139 args.year, args.month, args.time_aggregation, args.area,
140 args.format, args.output, args.verbose)
141 p.retrieve()
142 p.checktar()
143 # remove api key file if it was created
144 if remove_apikey and os.getcwd() == current_pwd:
145 os.remove(os.path.join(current_pwd, '.cdsapirc'))