Mercurial > repos > climate > cads
comparison cads_retrieve.py @ 0:b768e517a7d0 draft default tip
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/cads commit 778688ee8d14e22b00b648858ad9bd1c2f46b1e0"
author | climate |
---|---|
date | Sat, 19 Jun 2021 18:35:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b768e517a7d0 |
---|---|
1 import argparse | |
2 import ast | |
3 from os import environ, path | |
4 | |
5 import cdsapi | |
6 | |
7 parser = argparse.ArgumentParser() | |
8 parser.add_argument("-i", "--request", type=str, help="input API request") | |
9 parser.add_argument("-o", "--output", type=str, help="output API request") | |
10 args = parser.parse_args() | |
11 | |
12 mapped_chars = { | |
13 '>': '__gt__', | |
14 '<': '__lt__', | |
15 "'": '__sq__', | |
16 '"': '__dq__', | |
17 '[': '__ob__', | |
18 ']': '__cb__', | |
19 '{': '__oc__', | |
20 '}': '__cc__', | |
21 '@': '__at__', | |
22 '#': '__pd__', | |
23 "": '__cn__' | |
24 } | |
25 | |
26 with open(args.request) as f: | |
27 req = f.read() | |
28 | |
29 # Unsanitize labels (element_identifiers are always sanitized by Galaxy) | |
30 for key, value in mapped_chars.items(): | |
31 req = req.replace(value, key) | |
32 | |
33 print("req = ", req) | |
34 c3s_type = req.split('c.retrieve')[1].split('(')[1].split(',')[0].strip(' "\'\t\r\n') | |
35 | |
36 c3s_req = '{' + req.split('{', 1)[1].rsplit('}', 1)[0].replace('\n', '') + '}' | |
37 c3s_req_dict = ast.literal_eval(c3s_req) | |
38 | |
39 c3s_output = req.rsplit('}', 1)[1].split(',')[1].split(')')[0].strip(' "\'\t\r\n') | |
40 | |
41 with open(args.output, "w") as f: | |
42 f.write(f'dataset to retrieve: {c3s_type}\nrequest: {c3s_req}\noutput filename: {c3s_output}') | |
43 | |
44 print("start retrieving data...") | |
45 | |
46 cdapi_file = path.join(environ.get('HOME'), '.cdsapirc') | |
47 | |
48 if path.isfile(cdapi_file): | |
49 c = cdsapi.Client() | |
50 | |
51 c.retrieve( | |
52 c3s_type, | |
53 c3s_req_dict, | |
54 c3s_output) | |
55 | |
56 print("data retrieval successful") |