Mercurial > repos > climate > cds_essential_variability
annotate 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 |
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 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
37 self.area = area |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
38 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
|
39 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
|
40 else: |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
41 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
|
42 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
|
43 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
|
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.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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
53 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
|
54 |
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.retrieve( |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
56 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
|
57 '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
|
58 '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
|
59 '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
|
60 '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
|
61 '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
|
62 '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
|
63 '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
|
64 }, |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
65 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
|
66 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 # 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
81 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
82 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
|
83 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
|
84 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
|
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 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 "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
|
94 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
|
95 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
96 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
|
97 'archive', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
98 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
|
99 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
100 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
|
101 'variable', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
102 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
|
103 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
104 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
|
105 '--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
|
106 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
|
107 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
108 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
|
109 '--year', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
110 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
|
111 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
112 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
|
113 '--month', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
114 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
|
115 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
116 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
|
117 '--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
|
118 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
|
119 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
120 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
|
121 '--area', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
122 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
|
123 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
124 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
|
125 '--format', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
126 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
|
127 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
128 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
|
129 '--output', |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
130 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
|
131 ) |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
132 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
|
133 "-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
|
134 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
|
135 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
|
136 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
|
137 |
5c0ab9932311
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 49926d09bb7d28f07b24050d25c40f2ae875d6f7
climate
parents:
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 # 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
|
144 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
|
145 os.remove(os.path.join(current_pwd, '.cdsapirc')) |