-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmars.py
60 lines (42 loc) · 1.78 KB
/
mars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from typing import BinaryIO
from cads_adaptors import mapping
from cads_adaptors.adaptors import Request, cds
def execute_mars(request: Request, target="data.grib"):
import subprocess
with open("r", "w") as fp:
print(f"retrieve, target={target}", file=fp)
for key, value in request.items():
if not isinstance(value, (list, tuple)):
value = [value]
print(f", {key}={'/'.join(str(v) for v in value)}", file=fp)
env = dict(**os.environ)
# FIXME: set with the namespace and user_id
namespace = "cads"
user_id = 0
env["MARS_USER"] = f"{namespace}-{user_id}"
subprocess.run(["/usr/local/bin/mars", "r"], check=True, env=env)
return target
class DirectMarsCdsAdaptor(cds.AbstractCdsAdaptor):
resources = {"MARS_CLIENT": 1}
def retrieve(self, request: Request) -> BinaryIO:
result = execute_mars(request)
return open(result) # type: ignore
class MarsCdsAdaptor(DirectMarsCdsAdaptor):
def retrieve(self, request: Request) -> BinaryIO:
from cads_adaptors.tools import download_tools
# Format of data files, grib or netcdf
data_format = request.pop("format", "grib")
# Format of download archive, as_source, zip, tar, list etc.
download_format = request.pop("download_format", "as_source")
mapped_request = mapping.apply_mapping(request, self.mapping) # type: ignore
if data_format not in ["grib"]:
# FIXME: reformat if needed
pass
result = execute_mars(mapped_request)
download_kwargs = {
"base_target": f"{self.collection_id}-{hash(tuple(request))}"
}
return download_tools.DOWNLOAD_FORMATS[download_format](
[result], **download_kwargs
)