Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c8206ec
Add Figure.timestamp to plot the GMT timestamp logo
seisman Nov 24, 2022
2096760
Merge branch 'main' into timestamp
seisman Jan 1, 2023
e0bb6bb
Add Figure.timestamp to the API reference documentation
seisman Jan 1, 2023
4d6d777
Improve the timestamp function
seisman Jan 1, 2023
fffe3cd
Add the timefmt parmater to set time timestamp format
seisman Jan 1, 2023
54f4307
Fix formattingg
seisman Jan 1, 2023
fe8eeac
Merge branch 'main' into timestamp
seisman Jan 29, 2023
97574ac
Merge branch 'main' into timestamp
seisman Jan 30, 2023
1e8de17
Fix the configurations
seisman Jan 31, 2023
79460fb
Add the first test
seisman Jan 31, 2023
7031448
Add the first test
seisman Jan 31, 2023
7ad112c
Merge branch 'main' into timestamp
seisman Feb 4, 2023
b057ad8
Pass GMT parameters directly to the build_arg_string function
seisman Feb 4, 2023
f92d272
fix
seisman Feb 4, 2023
cecf6de
Merge branch 'main' into timestamp
seisman Feb 12, 2023
7dd74d7
Fix the dvc-diff workflow
seisman Feb 12, 2023
06049a6
Update the baseline image
seisman Feb 12, 2023
876d963
Revert "Fix the dvc-diff workflow"
seisman Feb 12, 2023
752273e
Fix the dvc-diff workflow
seisman Feb 12, 2023
f6795a9
Fix linting errors
seisman Feb 12, 2023
9eba2a9
Add more timestamp tests
seisman Feb 12, 2023
92c285a
Add a test for the 'text' parameter
seisman Feb 12, 2023
776981b
Fix docstrings
seisman Feb 12, 2023
c771183
Fix docstrings
seisman Feb 12, 2023
386882e
Add the missing r prefix
seisman Feb 12, 2023
e3580e7
Merge branch 'main' into timestamp
seisman Feb 12, 2023
41f5419
Check if the text string is longer than 64 characters
seisman Feb 13, 2023
4def360
Merge branch 'main' into timestamp
seisman Feb 14, 2023
bc64e8c
Fix docstring
seisman Feb 15, 2023
78c2307
Fix docstring
seisman Feb 15, 2023
e6248c2
Apply suggestions from code review
seisman Feb 15, 2023
d989b22
Merge branch 'main' into timestamp
michaelgrund Feb 15, 2023
960508f
Apply suggestions from code review
seisman Feb 16, 2023
16c0368
Merge branch 'main' into timestamp
seisman Feb 17, 2023
98b6762
Use the package-level variable __gmt_version__
seisman Feb 17, 2023
b8a7fbb
Merge branch 'main' into timestamp
seisman Feb 21, 2023
688f6b0
Fix formatting
seisman Feb 21, 2023
6e168e1
Merge branch 'main' into timestamp
seisman Feb 28, 2023
5857866
Use the __gmt_version__ variable
seisman Feb 28, 2023
01a3cbb
Merge branch 'main' into timestamp
seisman Feb 28, 2023
4fa2e72
Remove unused imports
seisman Mar 1, 2023
0c07a48
Minor changes to the docstrings
seisman Mar 1, 2023
e4bd94c
Update pygmt/src/timestamp.py
seisman Mar 2, 2023
0079643
Fix docstrings for font
seisman Mar 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Plotting map elements
Figure.logo
Figure.solar
Figure.text
Figure.timestamp

Plotting tabular data
~~~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ def _repr_html_(self):
subplot,
ternary,
text,
timestamp,
velo,
wiggle,
)
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from pygmt.src.surface import surface
from pygmt.src.ternary import ternary
from pygmt.src.text import text_ as text # "text" is an argument within "text_"
from pygmt.src.timestamp import timestamp
from pygmt.src.triangulate import triangulate
from pygmt.src.velo import velo
from pygmt.src.which import which
Expand Down
100 changes: 100 additions & 0 deletions pygmt/src/timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
timestamp - Plot the GMT timestamp logo.
"""
from packaging.version import Version
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_string, is_nonstr_iter

__doctest_skip__ = ["timestamp"]


def timestamp(
self,
text=None,
label=None,
justification="BL",
offset=("-54p", "-54p"),
font="Helvetica",
timefmt="%Y %b %d %H:%M:%S",
):
"""
Plot the GMT timestamp logo.

Parameters
----------
text : None or str
If ``None``, the current UNIX time stamp is shown in the GMT timestamp
logo. Setting this parameter to replace the UNIX time stamp with a
custom text string instead. The text must be less than 64 characters.
*Requires GMT>=6.5.0*.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still unsure what's the best way to document features that require new GMT versions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Is there a similar situation for the Julia wrapper, which can serve as an orientation?

label : None or str
The text string shown after the GMT timestamp logo.
justification : str
Justification of the timestamp. *justification* is a two-character code
that is a combination of a horizontal (**L**(eft), **C**(enter), or
**R**(ight)) and a vertical (**T**(op), **M**(iddle), or **B**(ottom))
code.
offset : str or tuple
*offset* or (*offset_x*, *offset_y*).
Offset the anchor point of the timestamp by *offset_x* and *offset_y*.
If a single value *offset* is given, *offset_y*=*offset_x*=*offset*.
font : str
Font of the timestamp and the optional label.
For GMT<=6.4.0, the parameter can only change the font style.
For GMT>=6.5.0, the parameter can change the font style and font color.
timefmt : str
Format of the time information in the UNIX time stamp. This format is
parsed by the C function ``strftime``, so that virtually any text can
be used (even not containing any time information).

Examples
--------
>>> # Plot the GMT timestamp logo.
>>> import pygmt
>>> fig = pygmt.Figure()
>>> fig.timestamp()
>>> fig.show()
<IPython.core.display.Image object>

>>> # Plot the GMT timestamp logo with a custom label.
>>> fig = pygmt.Figure()
>>> fig.timestamp(label="Powered by PyGMT")
>>> fig.show()
<IPython.core.display.Image object>
"""
self._preprocess() # pylint: disable=protected-access

# Build the options passed to the "plot" module
kwdict = dict(T=True, U="")
if label is not None:
kwdict["U"] += f"{label}"
kwdict["U"] += f"+j{justification}"

# Deal with compatibility with different GMT versions
with Session() as lib:
gmt_version = lib.info["version"]

if is_nonstr_iter(offset): # given a tuple
kwdict["U"] += "+o" + "/".join(f"{item}" for item in offset)
else: # given a single value
if "/" not in offset and Version(gmt_version) <= Version("6.4.0"):
# Giving a single offset doesn't work in GMT <= 6.4.0.
# See https://github.com/GenericMappingTools/gmt/issues/7107.
kwdict["U"] += f"+o{offset}/{offset}"
else:
kwdict["U"] += f"+o{offset}"

# The +t modifier was added in GMT 6.5.0.
# See https://github.com/GenericMappingTools/gmt/pull/7127.
if text is not None:
if Version(gmt_version) <= Version("6.4.0"):
raise GMTInvalidInput("The parameter 'text' requires GMT>=6.5.0.")
kwdict["U"] += f"+t{text}"

with Session() as lib:
lib.call_module(
module="plot",
args=build_arg_string(kwdict)
+ f" --FONT_LOGO={font} --FORMAT_TIME_STAMP={timefmt}",
Copy link
Member Author

@seisman seisman Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two thoughts when implementing this function:

  1. Add a new parameter (e.g., confdict) to the build_arg_string function, so that we can pass a dict of GMT defaults to the call_module function, e.g.,

    build_arg_string(kwdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt}
    
  2. The three parameters FONT_LOGO, MAP_LOGO, and MAP_LOGO_POS won't be used after the Figure.timestamp function is used (FORMAT_TIME_STAMP is still used in other places), so they can be removed from the pygmt.config keyword list:

    _keywords = [
    "COLOR_BACKGROUND",
    "COLOR_FOREGROUND",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, point 1 seems ok, but I'm not sure about point 2. I'd prefer to keep the three parameters in the list, because even if you remove it, you're only removing the autocompletion, but users can still manually set those parameters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point 1 was implemented in PR #2324

)