-
Notifications
You must be signed in to change notification settings - Fork 215
/
__init__.py
225 lines (189 loc) · 6.22 KB
/
__init__.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
PyGMT is a library for processing geospatial and geophysical data and making
publication quality maps and figures. It provides a Pythonic interface for the
Generic Mapping Tools (GMT), a command-line program widely used in the Earth
Sciences. Besides making GMT more accessible to new users, PyGMT aims to
provide integration with the PyData ecosystem as well as support for rich
display in Jupyter notebooks.
Main Features
-------------
Here are just a few of the things that PyGMT does well:
- Easy handling of individual types of data like Cartesian, geographic, or
time-series data.
- Processing of (geo)spatial data including gridding, filtering, and masking
- Allows plotting of a large spectrum of objects on figures including
lines, vectors, polygons, and symbols (pre-defined and customized)
- Generate publication-quality illustrations and make animations
"""
import atexit as _atexit
from pkg_resources import get_distribution
# Import modules to make the high-level GMT Python API
from pygmt import datasets
from pygmt.accessors import GMTDataArrayAccessor
from pygmt.figure import Figure, set_display
from pygmt.io import load_dataarray
from pygmt.session_management import begin as _begin
from pygmt.session_management import end as _end
from pygmt.src import (
GrdMathCalc,
blockmean,
blockmedian,
blockmode,
config,
grd2cpt,
grdclip,
grdcut,
grdfill,
grdfilter,
grdgradient,
grdinfo,
grdlandmask,
grdproject,
grdsample,
grdtrack,
info,
makecpt,
sphdistance,
surface,
which,
x2sys_cross,
x2sys_init,
xyz2grd,
)
# Get semantic version through setuptools-scm
__version__ = f'v{get_distribution("pygmt").version}' # e.g. v0.1.2.dev3+g0ab3cd78
__commit__ = __version__.split("+g")[-1] if "+g" in __version__ else "" # 0ab3cd78
# Start our global modern mode session
_begin()
# Tell Python to run _end when shutting down
_atexit.register(_end)
def print_clib_info():
"""
Print information about the GMT shared library that we can find.
Includes the GMT version, default values for parameters, the path to the
``libgmt`` shared library, and GMT directories.
"""
from pygmt.clib import Session
lines = ["GMT library information:"]
with Session() as ses:
for key in sorted(ses.info):
lines.append(f" {key}: {ses.info[key]}")
print("\n".join(lines))
def show_versions():
"""
Prints various dependency versions useful when submitting bug reports. This
includes information about:
- PyGMT itself
- System information (Python version, Operating System)
- Core dependency versions (Numpy, Pandas, Xarray, etc)
- GMT library information
"""
import importlib
import platform
import subprocess
import sys
def _get_module_version(modname):
"""
Get version information of a Python module.
"""
try:
if modname in sys.modules:
module = sys.modules[modname]
else:
module = importlib.import_module(modname)
try:
return module.__version__
except AttributeError:
return module.version
except ImportError:
return None
def _get_ghostscript_version():
"""
Get ghostscript version.
"""
os_name = sys.platform
if os_name.startswith(("linux", "freebsd", "darwin")):
cmds = ["gs"]
elif os_name == "win32":
cmds = ["gswin64c.exe", "gswin32c.exe"]
else:
return None
for gs_cmd in cmds:
try:
version = subprocess.check_output(
[gs_cmd, "--version"], universal_newlines=True
).strip()
return version
except FileNotFoundError:
continue
return None
def _get_gmt_version():
"""
Get GMT version.
"""
try:
version = subprocess.check_output(
["gmt", "--version"], universal_newlines=True
).strip()
return version
except FileNotFoundError:
return None
sys_info = {
"python": sys.version.replace("\n", " "),
"executable": sys.executable,
"machine": platform.platform(),
}
deps = ["numpy", "pandas", "xarray", "netCDF4", "packaging"]
print("PyGMT information:")
print(f" version: {__version__}")
print("System information:")
for key, val in sys_info.items():
print(f" {key}: {val}")
print("Dependency information:")
for modname in deps:
print(f" {modname}: {_get_module_version(modname)}")
print(f" ghostscript: {_get_ghostscript_version()}")
print(f" gmt: {_get_gmt_version()}")
print_clib_info()
def test(doctest=True, verbose=True, coverage=False, figures=True):
"""
Run the test suite.
Uses `pytest <https://pytest.org/>`__ to discover and run the tests. If you
haven't already, you can install it with `conda
<https://conda.pydata.org/>`__ or `pip <https://pip.pypa.io/en/stable/>`__.
Parameters
----------
doctest : bool
If ``True``, will run the doctests as well (code examples that start
with a ``>>>`` in the docs).
verbose : bool
If ``True``, will print extra information during the test run.
coverage : bool
If ``True``, will run test coverage analysis on the code as well.
Requires ``pytest-cov``.
figures : bool
If ``True``, will test generated figures against saved baseline
figures. Requires ``pytest-mpl`` and ``matplotlib``.
Raises
------
AssertionError
If pytest returns a non-zero error code indicating that some tests have
failed.
"""
import pytest
show_versions()
package = __name__
args = []
if verbose:
args.append("-vv")
if coverage:
args.append(f"--cov={package}")
args.append("--cov-report=term-missing")
if doctest:
args.append("--doctest-modules")
if figures:
args.append("--mpl")
args.append("--pyargs")
args.append(package)
status = pytest.main(args)
assert status == 0, "Some tests have failed."