Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 7 additions & 12 deletions pygmt/src/solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ def solar(
terminator_datetime : str or datetime object
Set the date and time for the terminator calculation. It can be provided as a
string or any datetime-like object recognized by :func:`pandas.to_datetime`. The
time can be specified in UTC or using a UTC offset. The offset must be an
integer number of hours (e.g., -8 or +5); fractional hours are truncated
towards zero (e.g., -8.5 becomes -8 and +5.5 becomes +5). [Default is the
current UTC date and time].
time can be specified in UTC or with a UTC offset of any precision [Default is
the current UTC date and time].
fill
Set color or pattern for filling terminators [Default is no fill].
pen
Expand Down Expand Up @@ -113,18 +111,16 @@ def solar(
>>> # show the plot
>>> fig.show()
"""
datetime_string, datetime_timezone = None, None
datetime_string = None
if terminator_datetime:
try:
_datetime = pd.to_datetime(terminator_datetime)
datetime_string = _datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")
# GMT's solar module uses the C 'atoi' function to parse the timezone
# offset. Ensure the offset is an integer number of hours (e.g., -8 or +5).
# Fractional hours (e.g., -8.5 or +5.5) are truncated towards zero.
if utcoffset := _datetime.utcoffset():
datetime_timezone = int(utcoffset.total_seconds() / 3600)
except ValueError as verr:
raise GMTValueError(terminator_datetime, description="datetime") from verr
# Convert a timezone-aware datetime to UTC, before passing to GMT.
if _datetime.tzinfo is not None:
_datetime = _datetime.tz_convert("UTC")
Comment on lines +120 to +122

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

With this workaround, we don't need the +z modifier (line 143), so this workaround can simplify the code and is not temporary. So I don't add a # TODO comment to remove it in future versions.

datetime_string = _datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")

aliasdict = AliasSystem(
G=Alias(fill, name="fill"),
Expand All @@ -140,7 +136,6 @@ def solar(
},
),
Alias(datetime_string, name="terminator_datetime", prefix="+d"),
Alias(datetime_timezone, name="terminator_timezone", prefix="+z"),
],
W=Alias(pen, name="pen"),
).add_common(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions pygmt/tests/test_solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def test_solar_terminator_datetime_timezone():
fig = Figure()
fig.basemap(region="d", projection="W0/15c", frame=True)
Comment thread
yvonnefroehlich marked this conversation as resolved.
fig.solar(terminator_datetime="2020-01-01T01:02:03", pen="1p,black")
fig.solar(terminator_datetime="2020-01-01T01:02:03+00:30", pen="1p,orange,-")
fig.solar(terminator_datetime="2020-01-01T01:02:03+01:00", pen="1p,red")
fig.solar(terminator_datetime="2020-01-01T01:02:03-01:00", pen="1p,blue")
fig.solar(
Expand Down
Loading