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