Skip to content

Commit 48e6505

Browse files
seismanweiji14
andcommitted
GMT_DATASET: Add workaround for None values in trailing text (#3174)
Co-authored-by: Wei Ji <[email protected]>
1 parent 97c3cc9 commit 48e6505

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

.github/workflows/cache_data.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
with:
8181
name: gmt-cache
8282
path: |
83-
~/.gmt/cache
84-
~/.gmt/server
85-
~/.gmt/gmt_data_server.txt
86-
~/.gmt/gmt_hash_server.txt
83+
~/.gmt/cache
84+
~/.gmt/server
85+
~/.gmt/gmt_data_server.txt
86+
~/.gmt/gmt_hash_server.txt

pygmt/datatypes/dataset.py

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import ctypes as ctp
6+
import warnings
67
from collections.abc import Mapping
78
from typing import Any, ClassVar
89

@@ -153,6 +154,17 @@ def to_strings(self) -> np.ndarray[Any, np.dtype[np.str_]]:
153154
for segment in table.contents.segment[: table.contents.n_segments]:
154155
if segment.contents.text:
155156
textvector.extend(segment.contents.text[: segment.contents.n_rows])
157+
if None in textvector:
158+
# Workaround for upstream GMT bug reported in
159+
# https://github.com/GenericMappingTools/pygmt/issues/3170.
160+
msg = (
161+
"The trailing text column contains `None' values and has been replaced"
162+
"with empty strings to avoid TypeError exceptions. "
163+
"It's likely caused by an upstream GMT API bug. "
164+
"Please consider reporting to us."
165+
)
166+
warnings.warn(msg, category=RuntimeWarning, stacklevel=1)
167+
textvector = [item if item is not None else b"" for item in textvector]
156168
return np.char.decode(textvector) if textvector else np.array([], dtype=str)
157169

158170
def to_dataframe(

pygmt/tests/test_datatypes_dataset.py

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pandas as pd
88
import pytest
9+
from pygmt import which
910
from pygmt.clib import Session
1011
from pygmt.helpers import GMTTempFile
1112

@@ -81,3 +82,28 @@ def test_dataset_empty():
8182
assert df.empty # Empty DataFrame
8283
expected_df = dataframe_from_pandas(tmpfile.name)
8384
pd.testing.assert_frame_equal(df, expected_df)
85+
86+
87+
def test_dataset_to_strings_with_none_values():
88+
"""
89+
Test that None values in the trailing text doesn't raise an excetion.
90+
91+
Due to a likely upstream bug, the trailing texts sometimes can be ``None`` when
92+
downloading tiled grids. The temporary workaround is to replace any None values with
93+
an empty string.
94+
95+
See the bug report at https://github.com/GenericMappingTools/pygmt/issues/3170.
96+
"""
97+
tiles = ["@N30W120.earth_relief_15s_p.nc", "@N00E000.earth_relief_15s_p.nc"]
98+
paths = which(fname=tiles, download="a")
99+
assert len(paths) == 2
100+
# 'paths' may contain an empty string or not, depending on if the tiles are cached.
101+
if "" not in paths: # Contains two valid paths.
102+
# Delete the cached tiles and try again.
103+
for path in paths:
104+
Path(path).unlink()
105+
with pytest.warns(expected_warning=RuntimeWarning) as record:
106+
paths = which(fname=tiles, download="a")
107+
assert len(record) == 1
108+
assert len(paths) == 2
109+
assert "" in paths

0 commit comments

Comments
 (0)