annotate ecv_retrieve.py @ 4:85ec9e6112df draft

planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit baa781f07f0e18447f1f211c75b1b5b9540d4479
author climate
date Sun, 07 Jul 2019 10:06:33 -0400
parents f47873f1a4b3
children a781bdaaa30e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
edc85cde5f71 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
edc85cde5f71 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)
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
3
edc85cde5f71 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
edc85cde5f71 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
edc85cde5f71 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
edc85cde5f71 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
edc85cde5f71 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
edc85cde5f71 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
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
10
edc85cde5f71 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
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
12
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
13
edc85cde5f71 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 ():
edc85cde5f71 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,
edc85cde5f71 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,
edc85cde5f71 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
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
18 ):
edc85cde5f71 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
edc85cde5f71 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(',')
edc85cde5f71 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 == '':
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
23 else:
edc85cde5f71 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
edc85cde5f71 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 == '':
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
27 else:
edc85cde5f71 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(',')
edc85cde5f71 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 == '':
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
31 else:
edc85cde5f71 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(',')
edc85cde5f71 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 == '':
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
35 else:
edc85cde5f71 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
f47873f1a4b3 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 == '':
f47873f1a4b3 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'
f47873f1a4b3 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:
f47873f1a4b3 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
f47873f1a4b3 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
edc85cde5f71 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 == '':
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
44 else:
edc85cde5f71 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
edc85cde5f71 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 == '':
edc85cde5f71 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
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
48 else:
edc85cde5f71 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
edc85cde5f71 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:
edc85cde5f71 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)
edc85cde5f71 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)
edc85cde5f71 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)
edc85cde5f71 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)
edc85cde5f71 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()
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
56
edc85cde5f71 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):
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
58
edc85cde5f71 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(
edc85cde5f71 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, {
edc85cde5f71 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,
edc85cde5f71 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,
edc85cde5f71 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
f47873f1a4b3 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
edc85cde5f71 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,
edc85cde5f71 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,
edc85cde5f71 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,
edc85cde5f71 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,
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
69 },
edc85cde5f71 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)
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
71
edc85cde5f71 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):
edc85cde5f71 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
edc85cde5f71 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:
edc85cde5f71 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)
edc85cde5f71 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'):
edc85cde5f71 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
edc85cde5f71 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()
edc85cde5f71 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'
edc85cde5f71 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)
edc85cde5f71 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()
edc85cde5f71 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')
edc85cde5f71 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)
edc85cde5f71 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()
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
85
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
86
edc85cde5f71 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__':
edc85cde5f71 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")
edc85cde5f71 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()
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
90
edc85cde5f71 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
edc85cde5f71 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']
edc85cde5f71 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 \
edc85cde5f71 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'):
edc85cde5f71 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:
edc85cde5f71 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")
edc85cde5f71 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(
edc85cde5f71 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'])
edc85cde5f71 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
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
100
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
102 'archive',
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
104 )
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
106 'variable',
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
108 )
edc85cde5f71 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(
edc85cde5f71 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',
edc85cde5f71 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)'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
112 )
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
114 '--year',
edc85cde5f71 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.'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
116 )
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
118 '--month',
edc85cde5f71 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.'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
120 )
edc85cde5f71 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(
edc85cde5f71 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',
edc85cde5f71 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).'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
124 )
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
126 '--area',
edc85cde5f71 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)'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
128 )
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
130 '--format',
edc85cde5f71 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)'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
132 )
edc85cde5f71 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(
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
134 '--output',
edc85cde5f71 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'
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
136 )
edc85cde5f71 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(
edc85cde5f71 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",
edc85cde5f71 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",
edc85cde5f71 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")
edc85cde5f71 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()
edc85cde5f71 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff changeset
142
edc85cde5f71 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,
edc85cde5f71 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,
edc85cde5f71 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)
edc85cde5f71 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()
edc85cde5f71 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()
edc85cde5f71 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
edc85cde5f71 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:
edc85cde5f71 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'))