Mercurial > repos > climate > c3s
annotate c3s_retrieve.py @ 2:21e8386cf36f draft default tip
planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit e8074c9f6817bba5ca6cb322434ca5e4412b34cf
author | climate |
---|---|
date | Fri, 27 Sep 2024 19:34:36 +0000 |
parents | 250554670f72 |
children |
rev | line source |
---|---|
0
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
1 import argparse |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
2 import ast |
250554670f72
"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 |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
4 |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
5 import cdsapi |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
6 |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
7 parser = argparse.ArgumentParser() |
250554670f72
"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") |
250554670f72
"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") |
250554670f72
"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() |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
11 |
250554670f72
"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): |
250554670f72
"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") |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
14 req = f.read() |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
15 f.close() |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
16 mapped_chars = { |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
17 '>': '__gt__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
18 '<': '__lt__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
19 "'": '__sq__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
20 '"': '__dq__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
21 '[': '__ob__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
22 ']': '__cb__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
23 '{': '__oc__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
24 '}': '__cc__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
25 '@': '__at__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
26 '#': '__pd__', |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
27 "": '__cn__' |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
28 } |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
29 |
250554670f72
"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) |
250554670f72
"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(): |
250554670f72
"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) |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
33 |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
34 print("req = ", req) |
250554670f72
"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') |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
36 |
250554670f72
"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', '') + '}' |
250554670f72
"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) |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
39 |
250554670f72
"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') |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
41 |
250554670f72
"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") |
250554670f72
"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") |
250554670f72
"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") |
250554670f72
"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) |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
46 f.close() |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
47 |
250554670f72
"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...") |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
49 |
250554670f72
"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') |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
51 |
250554670f72
"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): |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
53 c = cdsapi.Client() |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
54 |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
55 c.retrieve( |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
56 c3s_type, |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
57 c3s_req_dict, |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
58 c3s_output) |
250554670f72
"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/c3s commit 04069ef63198279caf3d97de5a58fb32df12c811"
climate
parents:
diff
changeset
|
59 |
250554670f72
"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") |