|
| 1 | +""" |
| 2 | +timestamp - Plot the GMT timestamp logo. |
| 3 | +""" |
| 4 | +from packaging.version import Version |
| 5 | +from pygmt import __gmt_version__ |
| 6 | +from pygmt.clib import Session |
| 7 | +from pygmt.exceptions import GMTInvalidInput |
| 8 | +from pygmt.helpers import build_arg_string, is_nonstr_iter |
| 9 | + |
| 10 | +__doctest_skip__ = ["timestamp"] |
| 11 | + |
| 12 | + |
| 13 | +def timestamp( |
| 14 | + self, |
| 15 | + text=None, |
| 16 | + label=None, |
| 17 | + justification="BL", |
| 18 | + offset=("-54p", "-54p"), |
| 19 | + font="Helvetica,black", |
| 20 | + timefmt="%Y %b %d %H:%M:%S", |
| 21 | +): |
| 22 | + r""" |
| 23 | + Plot the GMT timestamp logo. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + text : None or str |
| 28 | + If ``None``, the current UNIX timestamp is shown in the GMT timestamp |
| 29 | + logo. Set this parameter to replace the UNIX timestamp with a |
| 30 | + custom text string instead. The text must be less than 64 characters. |
| 31 | + *Requires GMT>=6.5.0*. |
| 32 | + label : None or str |
| 33 | + The text string shown after the GMT timestamp logo. |
| 34 | + justification : str |
| 35 | + Justification of the timestamp. The *justification* is a two-character |
| 36 | + code that is a combination of a horizontal (**L**\ (eft), |
| 37 | + **C**\ (enter), or **R**\ (ight)) and a vertical (**T**\ (op), |
| 38 | + **M**\ (iddle), or **B**\ (ottom)) code. |
| 39 | + offset : str or tuple |
| 40 | + *offset* or (*offset_x*, *offset_y*). |
| 41 | + Offset the anchor point of the timestamp by *offset_x* and *offset_y*. |
| 42 | + If a single value *offset* is given, *offset_y* = *offset_x* = |
| 43 | + *offset*. |
| 44 | + font : str |
| 45 | + Font of the timestamp and the optional label. The parameter can't |
| 46 | + change the font color for GMT<=6.4.0, only the font ID. |
| 47 | + timefmt : str |
| 48 | + Format string for the UNIX timestamp. The format string is parsed by |
| 49 | + the C function ``strftime``, so that virtually any text can be used |
| 50 | + (even not containing any time information). |
| 51 | +
|
| 52 | + Examples |
| 53 | + -------- |
| 54 | + >>> # Plot the GMT timestamp logo. |
| 55 | + >>> import pygmt |
| 56 | + >>> fig = pygmt.Figure() |
| 57 | + >>> fig.timestamp() |
| 58 | + >>> fig.show() |
| 59 | + <IPython.core.display.Image object> |
| 60 | +
|
| 61 | + >>> # Plot the GMT timestamp logo with a custom label. |
| 62 | + >>> fig = pygmt.Figure() |
| 63 | + >>> fig.timestamp(label="Powered by PyGMT") |
| 64 | + >>> fig.show() |
| 65 | + <IPython.core.display.Image object> |
| 66 | + """ |
| 67 | + self._preprocess() # pylint: disable=protected-access |
| 68 | + |
| 69 | + # Build the options passed to the "plot" module |
| 70 | + kwdict = {"T": True, "U": ""} |
| 71 | + if label is not None: |
| 72 | + kwdict["U"] += f"{label}" |
| 73 | + kwdict["U"] += f"+j{justification}" |
| 74 | + |
| 75 | + if is_nonstr_iter(offset): # given a tuple |
| 76 | + kwdict["U"] += "+o" + "/".join(f"{item}" for item in offset) |
| 77 | + else: # given a single value |
| 78 | + if "/" not in offset and Version(__gmt_version__) <= Version("6.4.0"): |
| 79 | + # Giving a single offset doesn't work in GMT <= 6.4.0. |
| 80 | + # See https://github.com/GenericMappingTools/gmt/issues/7107. |
| 81 | + kwdict["U"] += f"+o{offset}/{offset}" |
| 82 | + else: |
| 83 | + kwdict["U"] += f"+o{offset}" |
| 84 | + |
| 85 | + # The +t modifier was added in GMT 6.5.0. |
| 86 | + # See https://github.com/GenericMappingTools/gmt/pull/7127. |
| 87 | + if text is not None: |
| 88 | + if Version(__gmt_version__) < Version("6.5.0"): |
| 89 | + raise GMTInvalidInput("The parameter 'text' requires GMT>=6.5.0.") |
| 90 | + if len(str(text)) > 64: |
| 91 | + raise GMTInvalidInput( |
| 92 | + "The parameter 'text' must be less than 64 characters." |
| 93 | + ) |
| 94 | + kwdict["U"] += f"+t{text}" |
| 95 | + |
| 96 | + with Session() as lib: |
| 97 | + lib.call_module( |
| 98 | + module="plot", |
| 99 | + args=build_arg_string( |
| 100 | + kwdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt} |
| 101 | + ), |
| 102 | + ) |
0 commit comments