This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup_spacepy.py
89 lines (73 loc) · 2.7 KB
/
setup_spacepy.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
import urllib.request
import tarfile
from pathlib import Path
import sys
import shutil
import os
import subprocess
def url_retrieve(url: str, outfile: Path, overwrite: bool = False):
"""
Parameters
----------
url: str
URL to download from
outfile: pathlib.Path
output filepath (including name)
overwrite: bool
overwrite if file exists
"""
outfile = Path(outfile).expanduser().resolve()
if outfile.is_dir():
raise ValueError("Please specify full filepath, including filename")
# need .resolve() in case intermediate relative dir doesn't exist
if overwrite or not outfile.is_file():
outfile.parent.mkdir(parents=True, exist_ok=True)
urllib.request.urlretrieve(url, outfile)
def setup_spacepy():
if sys.version_info < (3, 6):
raise RuntimeError("Python >= 3.6 required")
if os.name == "nt":
raise RuntimeError("Windows can use the PyPi wheel: \n pip install spacepy")
make = shutil.which("make")
if not make:
raise RuntimeError("GNU Make not found.")
R = Path("~").expanduser()
# %% download libcdf
url = "https://spdf.gsfc.nasa.gov/pub/software/cdf/dist/cdf38_0/cdf38_0-dist-all.tar.gz"
ofn = R / url.split("/")[-1]
url_retrieve(url, ofn)
with tarfile.open(ofn, mode="r") as f:
f.extractall(R)
# %% build
if sys.platform == "linux":
cmd = "OS=linux ENV=gnu CURSES=no FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j all".split(
" "
)
elif sys.platform == "darwin":
print(
"CDF Makefiles are obsolete and no longer work with current OSX versions. Suggest CDFlib instead.",
file=sys.stderr,
)
cmd = "OS=macosx ENV=gnu CURSES=no FORTRAN=no UCOPTIONS=-O2 SHARED=yes -j all".split(
" "
)
else:
raise RuntimeError(f"I dont know how to install SpacePy on {sys.platform}")
cwd = list(R.glob("cdf*dist"))[0].resolve()
print(f"\nbuilding CDF in {cwd}\n")
build_cmd = [make, "-C", str(cwd), "--silent"] + cmd
print(" ".join(build_cmd), "\n")
subprocess.check_call(build_cmd)
subprocess.check_call([make, "-C", str(cwd), "--silent", "install"]) # no sudo
# %% install spacepy
# numpy must be installed separately, as spacepy/setup.py isn't handling this as usual
subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "spacepy"])
# %% verify
print("verifying SpacePy CDF read")
subprocess.check_call(
[sys.executable, str(Path(__file__).parent / "test_pycdf_read.py")]
)
if __name__ == "__main__":
setup_spacepy()