-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Figure.timestamp to plot the GMT timestamp logo #2208
Conversation
pygmt/src/timestamp.py
Outdated
if is_nonstr_iter(offset): # given a list | ||
kwdict["U"] += "+o" + "/".join(f"{item}" for item in offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that we can't use decorator @kwargs_to_strings(offset="sequence")
here, because the decorator can only deal with keyword arguments and doesn't work for the default values. I.e., it doesn't work for Figure.timestamp()
but works for Figure.timestamp(offset=["-54p", "-54p"])
, although offset
defaults to ["-54p", "-54p"]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related issue report at #2361.
Ping @GenericMappingTools/pygmt-maintainers for comments on the current implementation of the |
The libfaketime package can be used to temporarily change the system date/time. It's also available on conda-forge, but it's a CLI program. There are Python wrapper (https://github.com/simon-weber/python-libfaketime) and pytest plugin (https://github.com/touilleMan/pytest-libfaketime), but both are not actively maintained. |
pygmt/src/timestamp.py
Outdated
lib.call_module( | ||
module="plot", | ||
args=build_arg_string(kwdict) | ||
+ f" --FONT_LOGO={font} --FORMAT_TIME_STAMP={timefmt}", |
There was a problem hiding this comment.
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:
-
Add a new parameter (e.g.,
confdict
) to thebuild_arg_string
function, so that we can pass a dict of GMT defaults to thecall_module
function, e.g.,build_arg_string(kwdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt}
-
The three parameters
FONT_LOGO
,MAP_LOGO
, andMAP_LOGO_POS
won't be used after theFigure.timestamp
function is used (FORMAT_TIME_STAMP
is still used in other places), so they can be removed from thepygmt.config
keyword list:
Lines 26 to 28 in 9253af3
_keywords = [ "COLOR_BACKGROUND", "COLOR_FOREGROUND",
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Maybe use |
I just realized that it's possible to specify a fixed timestamp by passing a text to the Here is a CLI example:
I think we can trust the timestamp string is always correct and focus on testing the function parameters. |
Summary of changed imagesThis is an auto-generated report of images that have changed on the DVC remote
Image diff(s)Added images
Modified images
Report last updated at commit 0079643 |
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
Ping @GenericMappingTools/pygmt-maintainers for reviews. |
def test_timestamp_justification(): | ||
""" | ||
Check if the "justification" parameter works. | ||
|
||
Only a subset of justification codes are tested to avoid overlapping | ||
timestamps. | ||
""" | ||
fig = Figure() | ||
fig.basemap(projection="X10c/5c", region=[0, 10, 0, 5], frame=0) | ||
for just in ["BL", "BR", "TL", "TR"]: | ||
fig.timestamp(justification=just, timefmt=just) | ||
return fig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike other embellishments (e.g., inset, legend or direction rose), the timestamp logo is always located at the lower left corner of the map. I believe this is GMT's design.
See https://docs.generic-mapping-tools.org/dev/gmt.conf.html#term-MAP_LOGO_POS
Sets the justification and the position of the logo/timestamp box relative to the current plot’s lower left corner (i.e., map origin)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting design choice 😅 Actually, I just realized I was thinking of position
rather than justification
. Maybe we should document the default offset="-54p/-54p"
under the offset docstring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default values are clear from the function definition:
Figure.timestamp((text=None, label=None, justification='BL', offset=('-54p', '-54p'), font='Helvetica,black', timefmt='%Y %b %d %H:%M:%S')
I'm hesitate to document the defaults in the docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, was thinking of adding [Default is ('-54p', '-54p')]
, but it's not really needed I guess.
pygmt/src/timestamp.py
Outdated
Font of the timestamp and the optional label. The parameter can't | ||
change the font color for GMT<=6.4.0. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should a GMTInvalidInput error be raised when users pass font
as a parameter with GMT 6.3.0? Or is the error clear enough from GMT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The font
parameter works for any GMT versions.
For GMT>6.4.0, it can change both the font ID and font color, but for GMT<=6.4.0, the font color is always black so it can only change the font ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, maybe reword it into something like this?
Font of the timestamp and the optional label. The parameter can't | |
change the font color for GMT<=6.4.0. | |
Font of the timestamp and the optional label. The parameter can't | |
change the font color for GMT<=6.4.0, only the font label. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid "font label" is incorrect here. It should be "font ID" or "font name".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Owh, you mean like font style (e.g. Helvetica)? Feel free to edit the suggested changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added "only the font ID" in 0079643.
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only a very minor suggestion as mentioned in https://github.com/GenericMappingTools/pygmt/pull/2208/files#r1122590265 (up to you to apply), but otherwise wait for second reviewer to approve.
Would you also like to add a gallery example or should we do this in a separate PR @seisman ? |
It's not on my todo list, so better to wait for someone else to work on the gallery example in another PR. |
Description of proposed changes
In this PR, I add the
Figure.timestamp
method to plot the GMT timestamp logo, just like what GMT's-U
option does. The final goal is to remove thetimestamp
alias from all the plotting methods (c.f. #2135 and #924 (comment)).Here are some notes:
-U
option is:-U[<label>][+c][+j<just>][+o<dx>[/<dy>]]
.-U+c
can be used to append the raw GMT command (e.g.,gmt coast -R... -J...
) after the timestamp logo. It's not implemented because (1) It's rarely used. A figure is usually produced by multiple commands, so it makes no sense to show the command string of a single command; (2) It makes little sense to show the raw GMT command string for a figure produced by a Python script; (3) It's also impossible to implement it in theFigure.timestamp()
function.Figure.timestamp
method is implemented by callinggmt plot -T -U...
FONT_LOGO
,MAP_LOGO
,MAP_LOGO_POS
, andFORMAT_TIME_STAMP
) that can affect the timestamp logo.MAP_LOGO
controls if a timestamp logo should be plotted. It makes no sense here, so ignored.MAP_LOGO_POS
defaults toBL/-54p/-54p
. It's equivalent to setting the+j
and+o
modifiers. So it's ignored.FONT_LOGO
can control the font ID of the text strings. It's implemented by passing--FONT_LOGO=xxx
to the GMT C API.FORMAT_TIME_STAMP
can control the format of the timestamp. It's implemented by passing--FORMAT_TIME_STAMP
to the GMT C API.+o
modifier for GMT <=6.4.0 (-U doesn't work with a single offset value gmt#7107).FONT_LOGO
can only change the font ID, but since GMT 6.5.0, it can also change both the font ID and color (but not the font size). See Allow FONT_LOGO to change font color gmt#7125+t
will be added. See Allow FONT_LOGO to change font color gmt#7125.FORMAT_TIME_STAMP
(e.g.,--FORMAT_TIME_STAMP="9999-99-99T99:99:99"
).Examples:
Documentation preview: https://pygmt-dev--2208.org.readthedocs.build/en/2208/api/generated/pygmt.Figure.timestamp.html#pygmt.Figure.timestamp
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash commands are:
/format
: automatically format and lint the code/test-gmt-dev
: run full tests on the latest GMT development version