From fbfed861018522225b0cae13404aa9428ba24395 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 3 Jan 2024 19:54:22 +0800 Subject: [PATCH] Figure.solar: Add type hints to the 'terminator' parameter and simplify codes (#2881) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/src/solar.py | 61 +++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/pygmt/src/solar.py b/pygmt/src/solar.py index 12ff17ca538..3413214cbd6 100644 --- a/pygmt/src/solar.py +++ b/pygmt/src/solar.py @@ -1,6 +1,10 @@ """ solar - Plot day-night terminators and twilight. """ +from __future__ import annotations + +from typing import Literal + import pandas as pd from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput @@ -22,13 +26,17 @@ t="transparency", ) @kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence") -def solar(self, terminator="d", terminator_datetime=None, **kwargs): +def solar( + self, + terminator: Literal["astronomical", "civil", "day_night", "nautical"] = "day_night", + terminator_datetime=None, + **kwargs, +): r""" Plot day-light terminators or twilights. - This function plots the day-night terminator. Alternatively, it can plot - the terminators for civil twilight, nautical twilight, or astronomical - twilight. + This function plots the day-night terminator. Alternatively, it can plot the + terminators for civil twilight, nautical twilight, or astronomical twilight. Full option list at :gmt-docs:`solar.html` @@ -36,18 +44,20 @@ def solar(self, terminator="d", terminator_datetime=None, **kwargs): Parameters ---------- - terminator : str - Set the type of terminator displayed. Valid arguments are - ``"day_night"``, ``"civil"``, ``"nautical"``, and ``"astronomical"``, - which can be set with either the full name or the first letter of the - name [Default is ``"day_night"``]. + terminator + Set the type of terminator displayed, which can be set with either the full name + or the first letter of the name. Available options are: - Refer to https://en.wikipedia.org/wiki/Twilight for the definitions of - different types of twilight. + - ``"astronomical"``: Astronomical twilight + - ``"civil"``: Civil twilight + - ``"day_night"``: Day/night terminator + - ``"nautical"``: Nautical twilight + + Refer to https://en.wikipedia.org/wiki/Twilight for the definitions of different + types of twilight. terminator_datetime : str or datetime object - Set the UTC date and time of the displayed terminator - [Default is the current UTC date and time]. It can be - passed as a string or Python datetime object. + Set the UTC date and time of the displayed terminator [Default is the current + UTC date and time]. It can be passed as a string or Python datetime object. {region} {projection} {frame} @@ -86,25 +96,16 @@ def solar(self, terminator="d", terminator_datetime=None, **kwargs): >>> # show the plot >>> fig.show() """ - kwargs = self._preprocess(**kwargs) if kwargs.get("T") is not None: + msg = "Use 'terminator' and 'terminator_datetime' instead of 'T'." + raise GMTInvalidInput(msg) + + valid_terminators = ["day_night", "civil", "nautical", "astronomical"] + if terminator not in valid_terminators and terminator not in "dcna": raise GMTInvalidInput( - "Use 'terminator' and 'terminator_datetime' instead of 'T'." - ) - if terminator not in [ - "day_night", - "civil", - "nautical", - "astronomical", - "d", - "c", - "n", - "a", - ]: - raise GMTInvalidInput( - f"Unrecognized solar terminator type '{terminator}'. Valid values " - "are 'day_night', 'civil', 'nautical', and 'astronomical'." + f"Unrecognized solar terminator type '{terminator}'. " + f"Valid values are {valid_terminators}." ) kwargs["T"] = terminator[0] if terminator_datetime: