annotate ecv_retrieve.py @ 2:b9d9fe0c2ce7 draft

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