Mercurial > repos > climate > cds_essential_variability
comparison ecv_retrieve.py @ 5:ee42b7a33dc7 draft default tip
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/essential_climate_variables commit 9a008dba390aef21d4ab61240641bf455f5bb036"
author | climate |
---|---|
date | Tue, 08 Jun 2021 08:58:04 +0000 |
parents | b9d9fe0c2ce7 |
children |
comparison
equal
deleted
inserted
replaced
4:9ee8db6ffba3 | 5:ee42b7a33dc7 |
---|---|
12 | 12 |
13 | 13 |
14 class ECV (): | 14 class ECV (): |
15 def __init__(self, archive, variable, product_type, year, | 15 def __init__(self, archive, variable, product_type, year, |
16 month, time_aggregation, area, format, output, | 16 month, time_aggregation, area, format, output, |
17 verbose=False | 17 climate_reference_period=None, verbose=False |
18 ): | 18 ): |
19 self.archive = archive | 19 self.archive = archive |
20 self.variable = variable.split(',') | 20 self.variable = variable.split(',') |
21 self.climate_reference_period = climate_reference_period | |
21 if product_type == '': | 22 if product_type == '': |
22 self.product_type = 'climatology' | 23 self.product_type = 'climatology' |
23 else: | 24 else: |
24 self.product_type = product_type | 25 self.product_type = product_type |
25 if year == '': | 26 if year != '' and year is not None: |
26 self.year = '2019' | 27 self.year = year.split(',') |
27 else: | 28 else: |
28 self.year = year.split(',') | 29 self.year = None |
29 if month == '': | 30 if month == '' or month is None: |
30 self.month = '01' | 31 self.month = '01' |
31 else: | 32 else: |
32 self.month = month.split(',') | 33 self.month = month.split(',') |
33 if time_aggregation == '': | 34 if time_aggregation == '': |
34 self.time_aggregation = '1_month' | 35 self.time_aggregation = '1_month_mean' |
35 else: | 36 else: |
36 self.time_aggregation = time_aggregation | 37 self.time_aggregation = time_aggregation |
37 if area == '': | 38 if area == '' or area is None: |
38 self.area = 'global' | 39 self.area = 'global' |
39 else: | 40 else: |
40 self.area = area | 41 self.area = area |
41 | 42 |
42 if format == '': | 43 if format == '': |
45 self.format = format | 46 self.format = format |
46 if output == '': | 47 if output == '': |
47 self.outputfile = "donwload." + self.format | 48 self.outputfile = "donwload." + self.format |
48 else: | 49 else: |
49 self.outputfile = output | 50 self.outputfile = output |
51 self.verbose = verbose | |
50 if verbose: | 52 if verbose: |
51 print("archive: ", self.archive) | 53 print("archive: ", self.archive) |
52 print("variable: ", self.variable) | 54 print("variable: ", self.variable) |
53 print("year: ", self.year) | 55 print("year: ", self.year) |
54 print("month: ", self.month) | 56 print("month: ", self.month) |
55 self.cdsapi = cdsapi.Client() | 57 self.cdsapi = cdsapi.Client() |
56 | 58 |
57 def retrieve(self): | 59 def retrieve(self): |
58 | 60 |
59 self.cdsapi.retrieve( | 61 if self.verbose: |
60 self.archive, { | 62 print(self.archive) |
61 'variable': self.variable, | 63 print('variable', self.variable) |
62 'year': self.year, | 64 print('year', self.year) |
63 'month': self.month, | 65 print('month', self.month) |
64 'origin': 'era5', | 66 print('origin', 'era5') |
65 'area': self.area, | 67 print('area', self.area) |
66 'format': self.format, | 68 print('format', self.format) |
67 'product_type': self.product_type, | 69 print('product_type', self.product_type) |
68 'time_aggregation': self.time_aggregation, | 70 print('time_aggregation', self.time_aggregation) |
69 }, | 71 print('climate_reference_period', |
72 self.climate_reference_period) | |
73 print(self.outputfile) | |
74 if self.climate_reference_period is None: | |
75 self.cdsapi.retrieve( | |
76 self.archive, { | |
77 'variable': self.variable, | |
78 'year': self.year, | |
79 'month': self.month, | |
80 'origin': 'era5', | |
81 'area': self.area, | |
82 'format': self.format, | |
83 'product_type': self.product_type, | |
84 'time_aggregation': self.time_aggregation, | |
85 }, | |
86 self.outputfile) | |
87 elif self.year is None: | |
88 self.cdsapi.retrieve( | |
89 self.archive, { | |
90 'variable': self.variable, | |
91 'climate_reference_period': | |
92 self.climate_reference_period, | |
93 'month': self.month, | |
94 'origin': 'era5', | |
95 'format': self.format, | |
96 'product_type': self.product_type, | |
97 'time_aggregation': self.time_aggregation, | |
98 }, | |
99 self.outputfile) | |
100 else: | |
101 self.cdsapi.retrieve( | |
102 self.archive, { | |
103 'variable': self.variable, | |
104 'climate_reference_period': | |
105 self.climate_reference_period, | |
106 'year': self.year, | |
107 'month': self.month, | |
108 'origin': 'era5', | |
109 'format': self.format, | |
110 'product_type': self.product_type, | |
111 'time_aggregation': self.time_aggregation, | |
112 }, | |
70 self.outputfile) | 113 self.outputfile) |
71 | 114 |
72 def checktar(self): | 115 def checktar(self): |
73 is_grib = False | 116 is_grib = False |
74 with open(self.outputfile, 'rb') as ofile: | 117 with open(self.outputfile, 'rb') as ofile: |
90 | 133 |
91 remove_apikey = False | 134 remove_apikey = False |
92 current_pwd = os.environ['HOME'] | 135 current_pwd = os.environ['HOME'] |
93 if 'GALAXY_COPERNICUS_CDSAPIRC_KEY' in os.environ and \ | 136 if 'GALAXY_COPERNICUS_CDSAPIRC_KEY' in os.environ and \ |
94 not os.path.isfile('.cdsapirc'): | 137 not os.path.isfile('.cdsapirc'): |
138 print('GALAXY_COPERNICUS_CDSAPIRC_KEY ') | |
95 with open(".cdsapirc", "w+") as apikey: | 139 with open(".cdsapirc", "w+") as apikey: |
96 apikey.write("url: https://cds.climate.copernicus.eu/api/v2\n") | 140 apikey.write("url: https://cds.climate.copernicus.eu/api/v2\n") |
97 apikey.write( | 141 apikey.write("key: " + os.environ['GALAXY_COPERNICUS_CDSAPIRC_KEY']) |
98 "key: " + os.environ['GALAXY_COPERNICUS_CDSAPIRC_KEY']) | |
99 remove_apikey = True | 142 remove_apikey = True |
100 | 143 |
101 parser.add_argument( | 144 parser.add_argument( |
102 'archive', | 145 'archive', |
103 help='Archive name' | 146 help='Archive name' |
125 parser.add_argument( | 168 parser.add_argument( |
126 '--area', | 169 '--area', |
127 help='Desired sub-area to extract (North/West/South/East)' | 170 help='Desired sub-area to extract (North/West/South/East)' |
128 ) | 171 ) |
129 parser.add_argument( | 172 parser.add_argument( |
173 '--climate_reference_period', | |
174 help='Climate reference period (default is 1981-2010)' | |
175 ) | |
176 parser.add_argument( | |
130 '--format', | 177 '--format', |
131 help='Output file format (GRIB or netCDF or tgz)' | 178 help='Output file format (GRIB or netCDF or tgz)' |
132 ) | 179 ) |
133 parser.add_argument( | 180 parser.add_argument( |
134 '--output', | 181 '--output', |
140 action="store_true") | 187 action="store_true") |
141 args = parser.parse_args() | 188 args = parser.parse_args() |
142 | 189 |
143 p = ECV(args.archive, args.variable, args.product_type, | 190 p = ECV(args.archive, args.variable, args.product_type, |
144 args.year, args.month, args.time_aggregation, args.area, | 191 args.year, args.month, args.time_aggregation, args.area, |
145 args.format, args.output, args.verbose) | 192 args.format, args.output, args.climate_reference_period, args.verbose) |
146 p.retrieve() | 193 p.retrieve() |
147 p.checktar() | 194 p.checktar() |
148 # remove api key file if it was created | 195 # remove api key file if it was created |
149 if remove_apikey and os.getcwd() == current_pwd: | 196 if remove_apikey and os.getcwd() == current_pwd: |
150 os.remove(os.path.join(current_pwd, '.cdsapirc')) | 197 os.remove(os.path.join(current_pwd, '.cdsapirc')) |