Skip to content

Commit fbfed86

Browse files
Figure.solar: Add type hints to the 'terminator' parameter and simplify codes (#2881)
Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent 6f26aea commit fbfed86

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

pygmt/src/solar.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""
22
solar - Plot day-night terminators and twilight.
33
"""
4+
from __future__ import annotations
5+
6+
from typing import Literal
7+
48
import pandas as pd
59
from pygmt.clib import Session
610
from pygmt.exceptions import GMTInvalidInput
@@ -22,32 +26,38 @@
2226
t="transparency",
2327
)
2428
@kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence")
25-
def solar(self, terminator="d", terminator_datetime=None, **kwargs):
29+
def solar(
30+
self,
31+
terminator: Literal["astronomical", "civil", "day_night", "nautical"] = "day_night",
32+
terminator_datetime=None,
33+
**kwargs,
34+
):
2635
r"""
2736
Plot day-light terminators or twilights.
2837
29-
This function plots the day-night terminator. Alternatively, it can plot
30-
the terminators for civil twilight, nautical twilight, or astronomical
31-
twilight.
38+
This function plots the day-night terminator. Alternatively, it can plot the
39+
terminators for civil twilight, nautical twilight, or astronomical twilight.
3240
3341
Full option list at :gmt-docs:`solar.html`
3442
3543
{aliases}
3644
3745
Parameters
3846
----------
39-
terminator : str
40-
Set the type of terminator displayed. Valid arguments are
41-
``"day_night"``, ``"civil"``, ``"nautical"``, and ``"astronomical"``,
42-
which can be set with either the full name or the first letter of the
43-
name [Default is ``"day_night"``].
47+
terminator
48+
Set the type of terminator displayed, which can be set with either the full name
49+
or the first letter of the name. Available options are:
4450
45-
Refer to https://en.wikipedia.org/wiki/Twilight for the definitions of
46-
different types of twilight.
51+
- ``"astronomical"``: Astronomical twilight
52+
- ``"civil"``: Civil twilight
53+
- ``"day_night"``: Day/night terminator
54+
- ``"nautical"``: Nautical twilight
55+
56+
Refer to https://en.wikipedia.org/wiki/Twilight for the definitions of different
57+
types of twilight.
4758
terminator_datetime : str or datetime object
48-
Set the UTC date and time of the displayed terminator
49-
[Default is the current UTC date and time]. It can be
50-
passed as a string or Python datetime object.
59+
Set the UTC date and time of the displayed terminator [Default is the current
60+
UTC date and time]. It can be passed as a string or Python datetime object.
5161
{region}
5262
{projection}
5363
{frame}
@@ -86,25 +96,16 @@ def solar(self, terminator="d", terminator_datetime=None, **kwargs):
8696
>>> # show the plot
8797
>>> fig.show()
8898
"""
89-
9099
kwargs = self._preprocess(**kwargs)
91100
if kwargs.get("T") is not None:
101+
msg = "Use 'terminator' and 'terminator_datetime' instead of 'T'."
102+
raise GMTInvalidInput(msg)
103+
104+
valid_terminators = ["day_night", "civil", "nautical", "astronomical"]
105+
if terminator not in valid_terminators and terminator not in "dcna":
92106
raise GMTInvalidInput(
93-
"Use 'terminator' and 'terminator_datetime' instead of 'T'."
94-
)
95-
if terminator not in [
96-
"day_night",
97-
"civil",
98-
"nautical",
99-
"astronomical",
100-
"d",
101-
"c",
102-
"n",
103-
"a",
104-
]:
105-
raise GMTInvalidInput(
106-
f"Unrecognized solar terminator type '{terminator}'. Valid values "
107-
"are 'day_night', 'civil', 'nautical', and 'astronomical'."
107+
f"Unrecognized solar terminator type '{terminator}'. "
108+
f"Valid values are {valid_terminators}."
108109
)
109110
kwargs["T"] = terminator[0]
110111
if terminator_datetime:

0 commit comments

Comments
 (0)