Skip to content

Commit fae47ae

Browse files
committed
pygmt.which: Use long-name arguments for the 'download' parameter
1 parent a2271a2 commit fae47ae

File tree

10 files changed

+72
-53
lines changed

10 files changed

+72
-53
lines changed

pygmt/datasets/samples.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _load_japan_quakes() -> pd.DataFrame:
2323
The data table. The column names are "year", "month", "day", "latitude",
2424
"longitude", "depth_km", and "magnitude" of the earthquakes.
2525
"""
26-
fname = which("@tut_quakes.ngdc", download="c")
26+
fname = which("@tut_quakes.ngdc", download="cache")
2727
return pd.read_csv(
2828
fname,
2929
header=1,
@@ -49,7 +49,7 @@ def _load_ocean_ridge_points() -> pd.DataFrame:
4949
data
5050
The data table. The column names are "longitude" and "latitude".
5151
"""
52-
fname = which("@ridge.txt", download="c")
52+
fname = which("@ridge.txt", download="cache")
5353
return pd.read_csv(
5454
fname,
5555
sep=r"\s+",
@@ -68,7 +68,7 @@ def _load_baja_california_bathymetry() -> pd.DataFrame:
6868
data
6969
The data table. The column names are "longitude", "latitude", and "bathymetry".
7070
"""
71-
fname = which("@tut_ship.xyz", download="c")
71+
fname = which("@tut_ship.xyz", download="cache")
7272
return pd.read_csv(
7373
fname, sep="\t", header=None, names=["longitude", "latitude", "bathymetry"]
7474
)
@@ -83,7 +83,7 @@ def _load_usgs_quakes() -> pd.DataFrame:
8383
data
8484
The data table. Use ``print(data.describe())`` to see the available columns.
8585
"""
86-
fname = which("@usgs_quakes_22.txt", download="c")
86+
fname = which("@usgs_quakes_22.txt", download="cache")
8787
return pd.read_csv(fname)
8888

8989

@@ -97,7 +97,7 @@ def _load_fractures_compilation() -> pd.DataFrame:
9797
data
9898
The data table. The column names are "length" and "azimuth" of the fractures.
9999
"""
100-
fname = which("@fractures_06.txt", download="c")
100+
fname = which("@fractures_06.txt", download="cache")
101101
data = pd.read_csv(fname, header=None, sep=r"\s+", names=["azimuth", "length"])
102102
return data[["length", "azimuth"]]
103103

@@ -116,7 +116,7 @@ def _load_hotspots() -> pd.DataFrame:
116116
The data table. The column names are "longitude", "latitude", "symbol_size", and
117117
"place_name".
118118
"""
119-
fname = which("@hotspots.txt", download="c")
119+
fname = which("@hotspots.txt", download="cache")
120120
return pd.read_csv(
121121
fname,
122122
sep="\t",
@@ -137,7 +137,7 @@ def _load_mars_shape() -> pd.DataFrame:
137137
data
138138
The data table. The column names are "longitude", "latitude", and "radius_m".
139139
"""
140-
fname = which("@mars370d.txt", download="c")
140+
fname = which("@mars370d.txt", download="cache")
141141
return pd.read_csv(
142142
fname, sep="\t", header=None, names=["longitude", "latitude", "radius_m"]
143143
)
@@ -153,7 +153,7 @@ def _load_rock_sample_compositions() -> pd.DataFrame:
153153
The data table. The column names are "limestone", "water", "air", and
154154
"permittivity".
155155
"""
156-
fname = which("@ternary.txt", download="c")
156+
fname = which("@ternary.txt", download="cache")
157157
return pd.read_csv(
158158
fname,
159159
sep=r"\s+",
@@ -173,7 +173,7 @@ def _load_notre_dame_topography() -> pd.DataFrame:
173173
data
174174
The data table. The column names are "x", "y", and "z".
175175
"""
176-
fname = which("@Table_5_11.txt", download="c")
176+
fname = which("@Table_5_11.txt", download="cache")
177177
return pd.read_csv(fname, sep=r"\s+", header=None, names=["x", "y", "z"])
178178

179179

@@ -186,7 +186,7 @@ def _load_maunaloa_co2() -> pd.DataFrame:
186186
data
187187
The data table. The column names are "date" and "co2_ppm".
188188
"""
189-
fname = which("@MaunaLoa_CO2.txt", download="c")
189+
fname = which("@MaunaLoa_CO2.txt", download="cache")
190190
return pd.read_csv(
191191
fname, header=None, skiprows=1, sep=r"\s+", names=["date", "co2_ppm"]
192192
)
@@ -202,7 +202,7 @@ def _load_earth_relief_holes() -> xr.DataArray:
202202
The Earth relief grid. Coordinates are latitude and longitude in degrees. Relief
203203
is in meters.
204204
"""
205-
fname = which("@earth_relief_20m_holes.grd", download="c")
205+
fname = which("@earth_relief_20m_holes.grd", download="cache")
206206
return xr.load_dataarray(fname, engine="gmt", raster_kind="grid")
207207

208208

pygmt/helpers/caching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ def cache_data() -> None:
134134
"@tut_ship.xyz",
135135
"@usgs_quakes_22.txt",
136136
]
137-
which(fname=datasets, download="a")
137+
which(fname=datasets, download="auto")

pygmt/src/which.py

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,62 @@
66
from typing import Literal
77

88
from pygmt._typing import PathLike
9-
from pygmt.alias import AliasSystem
9+
from pygmt.alias import Alias, AliasSystem
1010
from pygmt.clib import Session
11-
from pygmt.helpers import build_arg_list, fmt_docstring, is_nonstr_iter, use_alias
11+
from pygmt.helpers import build_arg_list, fmt_docstring, is_nonstr_iter
1212

1313

1414
@fmt_docstring
15-
@use_alias(G="download")
1615
def which(
1716
fname: PathLike | Sequence[PathLike],
17+
download: Literal["auto", "cache", "local", "user"] | bool = False,
1818
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
1919
| bool = False,
2020
**kwargs,
2121
) -> str | list[str]:
22-
r"""
22+
"""
2323
Find full path to specified files.
2424
25-
Reports the full paths to the files given through ``fname``. We look
26-
for the file in (1) the current directory, (2) in $GMT_USERDIR (if
27-
defined), (3) in $GMT_DATADIR (if defined), or (4) in $GMT_CACHEDIR
28-
(if defined).
25+
Reports the full paths to the files given through ``fname``. It looks for the file
26+
in (1) the current directory, (2) in $GMT_USERDIR (if defined), (3) in $GMT_DATADIR
27+
(if defined), or (4) in $GMT_CACHEDIR (if defined).
2928
30-
``fname`` can also be a downloadable file (either a complete URL, an
31-
@file for downloading from the GMT data server, or any of the remote
32-
datasets at https://www.pygmt.org/latest/api/index.html#datasets).
33-
In these cases, use the ``download`` parameter to set the desired
34-
behavior. If ``download`` is not used (or ``False``), the file will
35-
not be found.
29+
``fname`` can also be a downloadable file (either a complete URL, an @file for
30+
downloading from the GMT data server, or any of the remote datasets at
31+
:ref:`datasets`. In these cases, use the ``download`` parameter to set the desired
32+
behavior. If ``download`` is not used (or ``False``), the file will not be found.
3633
3734
Full GMT docs at :gmt-docs:`gmtwhich.html`.
3835
39-
$aliases
36+
**Aliases:**
37+
38+
.. hlist::
39+
:columns: 3
40+
41+
- G = download
4042
- V = verbose
4143
4244
Parameters
4345
----------
44-
fname : str or list
45-
One or more file names of any data type (grids, tables, etc.).
46-
download : bool or str
47-
[**a**\|\ **c**\|\ **l**\|\ **u**].
48-
If the ``fname`` argument is a downloadable file (either a complete
49-
URL, an @file for downloading from the GMT data server, or any of
50-
the remote datasets at
51-
https://www.pygmt.org/latest/api/index.html#datasets)
52-
we will try to download the file if it is not found in your local
53-
data or cache directories. If set to ``True`` or **l** is passed
54-
the file is downloaded to the current directory. Use **a** to place
55-
files in the appropriate folder under the user directory (this is
56-
where GMT normally places downloaded files), **c** to place it in
57-
the user cache directory, or **u** for the user data directory
58-
instead (i.e., ignoring any subdirectory structure).
46+
fname
47+
One or more file names to find the full path.
48+
download
49+
Try to download the file if it is not found in your local data or cache
50+
directories and the file is downloadable. Here, downloadable files include:
51+
52+
- a file specified by a complete URL
53+
- a GMT remote file on the GMT data server, specified with a leading ``@``.
54+
- any of the GMT remote datasets at :ref:`datasets`
55+
56+
Valid values are:
57+
58+
- ``False``: Do not download the file.
59+
- ``True`` or ``"local"``: Download the file to the current directory.
60+
- ``"cache"``: Download the file to the user cache directory.
61+
- ``"user"``: Download the file to the user data directory but ignore any
62+
subdirectory structure.
63+
- ``"auto"``: Download the file to appropriate folder under the user directory
64+
(this is where GMT normally places downloaded files).
5965
$verbose
6066
6167
Returns
@@ -68,7 +74,20 @@ def which(
6874
FileNotFoundError
6975
If the file is not found.
7076
"""
71-
aliasdict = AliasSystem().add_common(
77+
aliasdict = AliasSystem(
78+
G=Alias(
79+
download,
80+
name="download",
81+
mapping={
82+
"auto": "a",
83+
"cache": "c",
84+
"local": "l",
85+
"user": "u",
86+
True: True,
87+
False: False,
88+
},
89+
)
90+
).add_common(
7291
V=verbose,
7392
)
7493
aliasdict.merge(kwargs)

pygmt/tests/test_datatypes_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_dataset_to_strings_with_none_values():
160160
# Catch the FileNotFoundError exception so that we can focus on the bug.
161161
tiles = ["@N30E060.earth_age_01m_g.nc", "@N30E090.earth_age_01m_g.nc"]
162162
try:
163-
paths = which(fname=tiles, download="a")
163+
paths = which(fname=tiles, download="auto")
164164
assert len(paths) == 2
165165

166166
# 'paths' may contain an empty string or not, depending on if tiles are cached.
@@ -170,7 +170,7 @@ def test_dataset_to_strings_with_none_values():
170170
Path(path).unlink()
171171
with pytest.warns(expected_warning=RuntimeWarning) as record: # noqa: PT031
172172
try:
173-
paths = which(fname=tiles, download="a")
173+
paths = which(fname=tiles, download="auto")
174174
assert len(record) == 1
175175
assert len(paths) == 2
176176
assert "" in paths

pygmt/tests/test_geopandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def fixture_gdf_ridge():
5252
# Read shapefile into a geopandas.GeoDataFrame
5353
shapefile = which(
5454
fname=["@RidgeTest.shp", "@RidgeTest.shx", "@RidgeTest.dbf", "@RidgeTest.prj"],
55-
download="c",
55+
download="cache",
5656
)
5757
gdf = gpd.read_file(shapefile[0])
5858
# Reproject the geometry

pygmt/tests/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def test_plot_shapefile():
553553
See https://github.com/GenericMappingTools/pygmt/issues/1616.
554554
"""
555555
datasets = ["@RidgeTest" + suffix for suffix in [".shp", ".shx", ".dbf", ".prj"]]
556-
which(fname=datasets, download="a")
556+
which(fname=datasets, download="auto")
557557
fig = Figure()
558558
fig.plot(data="@RidgeTest.shp", pen="1p", frame=True)
559559
return fig

pygmt/tests/test_surface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def fixture_data():
1818
"""
1919
Load Table 5.11 in Davis: Statistics and Data Analysis in Geology.
2020
"""
21-
fname = which("@Table_5_11_mean.xyz", download="c")
21+
fname = which("@Table_5_11_mean.xyz", download="cache")
2222
return pd.read_csv(
2323
fname, sep=r"\s+", header=None, names=["x", "y", "z"], skiprows=1
2424
)

pygmt/tests/test_triangulate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def fixture_dataframe():
1919
"""
2020
Load the table data from the sample bathymetry dataset.
2121
"""
22-
fname = which("@Table_5_11_mean.xyz", download="c")
22+
fname = which("@Table_5_11_mean.xyz", download="cache")
2323
return pd.read_csv(
2424
fname, sep=r"\s+", header=None, names=["x", "y", "z"], skiprows=1
2525
)[:10]

pygmt/tests/test_which.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_which():
1818
Make sure `which` returns file paths for @files correctly without errors.
1919
"""
2020
for fname in ["tut_quakes.ngdc", "tut_bathy.nc"]:
21-
cached_file = which(fname=f"@{fname}", download="c")
21+
cached_file = which(fname=f"@{fname}", download="cache")
2222
assert Path(cached_file).exists()
2323
assert Path(cached_file).name == fname
2424

@@ -29,7 +29,7 @@ def test_which_multiple():
2929
Make sure `which` returns file paths for multiple @files correctly.
3030
"""
3131
filenames = ["ridge.txt", "tut_ship.xyz"]
32-
cached_files = which([f"@{fname}" for fname in filenames], download="c")
32+
cached_files = which([f"@{fname}" for fname in filenames], download="cache")
3333
for cached_file in cached_files:
3434
assert Path(cached_file).exists()
3535
assert Path(cached_file).name in filenames
@@ -68,7 +68,7 @@ def test_which_nonascii_path(monkeypatch):
6868
# Start a new session
6969
begin()
7070
# GMT should download the remote file under the new home directory.
71-
fname = which(fname="@static_earth_relief.nc", download="c")
71+
fname = which(fname="@static_earth_relief.nc", download="cache")
7272
assert fname.startswith(fakehome)
7373
assert fname.endswith("static_earth_relief.nc")
7474
end()

pygmt/tests/test_xarray_accessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_xarray_accessor_sliced_datacube():
122122
try:
123123
fname = which(
124124
"https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc",
125-
download="u",
125+
download="user",
126126
)
127127
with xr.open_dataset(fname, engine="netcdf4") as dataset:
128128
grid = dataset.sel(level=500, month=1, drop=True).z

0 commit comments

Comments
 (0)