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