comparison c3s_retrieve.py @ 0:250554670f72 draft

"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
author climate
date Tue, 13 Apr 2021 18:32:45 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:250554670f72
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 if path.isfile(args.request):
13 f = open(args.request, "r")
14 req = f.read()
15 f.close()
16 mapped_chars = {
17 '>': '__gt__',
18 '<': '__lt__',
19 "'": '__sq__',
20 '"': '__dq__',
21 '[': '__ob__',
22 ']': '__cb__',
23 '{': '__oc__',
24 '}': '__cc__',
25 '@': '__at__',
26 '#': '__pd__',
27 "": '__cn__'
28 }
29
30 # Unsanitize labels (element_identifiers are always sanitized by Galaxy)
31 for key, value in mapped_chars.items():
32 req = req.replace(value, key)
33
34 print("req = ", req)
35 c3s_type = req.split('c.retrieve')[1].split('(')[1].split(',')[0].strip(' "\'\t\r\n')
36
37 c3s_req = '{' + req.split('{')[1].split('}')[0].replace('\n', '') + '}'
38 c3s_req_dict = ast.literal_eval(c3s_req)
39
40 c3s_output = req.split('}')[1].split(',')[1].split(')')[0].strip(' "\'\t\r\n')
41
42 f = open(args.output, "w")
43 f.write("dataset to retrieve: " + c3s_type + "\n")
44 f.write("request: " + c3s_req + "\n")
45 f.write("output filename: " + c3s_output)
46 f.close()
47
48 print("start retrieving data...")
49
50 cdapi_file = path.join(environ.get('HOME'), '.cdsapirc')
51
52 if path.isfile(cdapi_file):
53 c = cdsapi.Client()
54
55 c.retrieve(
56 c3s_type,
57 c3s_req_dict,
58 c3s_output)
59
60 print("data retrieval successful")