Skip to content

Commit 4d6d777

Browse files
committed
Improve the timestamp function
1 parent e0bb6bb commit 4d6d777

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

pygmt/src/timestamp.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,45 @@
33
"""
44
from packaging.version import Version
55
from pygmt.clib import Session
6+
from pygmt.exceptions import GMTInvalidInput
67
from pygmt.helpers import build_arg_string, is_nonstr_iter
78

89
__doctest_skip__ = ["timestamp"]
910

1011

1112
def timestamp(
12-
self, label=None, justification="BL", offset=("-54p", "-54p"), font="Helvetica"
13+
self,
14+
text=None,
15+
label=None,
16+
justification="BL",
17+
offset=("-54p", "-54p"),
18+
font="Helvetica",
1319
):
1420
"""
1521
Plot the GMT timestamp logo.
1622
1723
Parameters
1824
----------
19-
label : str
25+
text : None or str
26+
If ``None``, the current UNIX time stamp is shown in the GMT timestamp
27+
logo. Setting this parameter to replace the UNIX time stamp with a
28+
custom text string instead. The text must be less than 64 characters.
29+
*Requires GMT>=6.5.0*.
30+
label : None or str
2031
The text string shown after the GMT timestamp logo.
2132
justification : str
2233
Justification of the timestamp. *justification* is a two-character code
2334
that is a combination of a horizontal (**L**(eft), **C**(enter), or
2435
**R**(ight)) and a vertical (**T**(op), **M**(iddle), or **B**(ottom))
2536
code.
26-
offset : str or list
27-
(*offset_x*, *offset_y*) or *offset*.
37+
offset : str or tuple
38+
*offset* or (*offset_x*, *offset_y*).
2839
Offset the anchor point of the timestamp by *offset_x* and *offset_y*.
2940
If a single value *offset* is given, *offset_y*=*offset_x*=*offset*.
3041
font : str
31-
Font name or font number of the timestamp and the optional label .
42+
Font of the timestamp and the optional label.
43+
For GMT<=6.4.0, this parameter can only change the font style.
44+
For GMT>=6.5.0, this parameter can change the font style and font color.
3245
3346
Examples
3447
--------
@@ -37,32 +50,42 @@ def timestamp(
3750
>>> fig = pygmt.Figure()
3851
>>> fig.timestamp()
3952
>>> fig.show()
53+
<IPython.core.display.Image object>
4054
4155
>>> # Plot the GMT timestamp logo with a custom label.
4256
>>> fig = pygmt.Figure()
4357
>>> fig.timestamp(label="Powered by PyGMT")
4458
>>> fig.show()
59+
<IPython.core.display.Image object>
4560
"""
4661
self._preprocess() # pylint: disable=protected-access
4762

48-
kwdict = {}
49-
kwdict["T"] = True # pass the -T option to the plot module
50-
kwdict["U"] = True
63+
# Build the options passed to the "plot" module
64+
kwdict = dict(T=True, U="")
65+
if label is not None:
66+
kwdict["U"] += f"{label}"
67+
kwdict["U"] += f"+j{justification}"
5168

52-
# build the argument for the -U option
53-
label = "" if label is None else f"{label}"
54-
kwdict["U"] = f"{label}+j{justification}"
69+
# Deal with compatibility with different GMT versions
70+
with Session() as lib:
71+
gmt_version = lib.info["version"]
5572

56-
if is_nonstr_iter(offset): # given a list
73+
if is_nonstr_iter(offset): # given a tuple
5774
kwdict["U"] += "+o" + "/".join(f"{item}" for item in offset)
58-
else:
59-
kwdict["U"] += f"+o{offset}"
60-
if "/" not in offset:
75+
else: # given a single value
76+
if "/" not in offset and Version(gmt_version) <= Version("6.4.0"):
6177
# Giving a single offset doesn't work in GMT <= 6.4.0.
6278
# See https://github.com/GenericMappingTools/gmt/issues/7107.
63-
with Session() as lib:
64-
if Version(lib.info["version"]) < Version("6.5.0"):
65-
kwdict["U"] += "/{offset}"
79+
kwdict["U"] += f"+o{offset}/{offset}"
80+
else:
81+
kwdict["U"] += f"+o{offset}"
82+
83+
# The +t modifier was added in GMT 6.5.0.
84+
# See https://github.com/GenericMappingTools/gmt/pull/7127.
85+
if text is not None:
86+
if Version(gmt_version) <= Version("6.4.0"):
87+
raise GMTInvalidInput("The parameter 'text' requires GMT>=6.5.0.")
88+
kwdict["U"] += f"+t{text}"
6689

6790
with Session() as lib:
6891
lib.call_module(

0 commit comments

Comments
 (0)