From 97c303811cb1552fe388062d265f8298336e9e54 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 15 Dec 2023 17:59:21 +0800 Subject: [PATCH 1/4] Figure.solar: Add type hints to the 'terminator' parameter and simplify codes --- pygmt/src/solar.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pygmt/src/solar.py b/pygmt/src/solar.py index 7d4344a7081..abbf7a68491 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,7 +26,12 @@ 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["day_night", "civil", "nautical", "astronomical"] = "day_night", + terminator_datetime=None, + **kwargs, +): r""" Plot day-light terminators or twilights. @@ -36,11 +45,11 @@ def solar(self, terminator="d", terminator_datetime=None, **kwargs): Parameters ---------- - terminator : str + terminator 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"``]. + name. Refer to https://en.wikipedia.org/wiki/Twilight for the definitions of different types of twilight. @@ -88,25 +97,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: raise GMTInvalidInput( "Use 'terminator' and 'terminator_datetime' instead of 'T'." ) - if terminator not in [ - "day_night", - "civil", - "nautical", - "astronomical", - "d", - "c", - "n", - "a", - ]: + valid_terminators = ["day_night", "civil", "nautical", "astronomical"] + if terminator not in valid_terminators and terminator not in "dcna": 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: From 840480ac5643a47cb6b25f5d05a5e2efb000b1c2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 15 Dec 2023 21:14:39 +0800 Subject: [PATCH 2/4] Update pygmt/src/solar.py 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/solar.py b/pygmt/src/solar.py index abbf7a68491..3179a153a47 100644 --- a/pygmt/src/solar.py +++ b/pygmt/src/solar.py @@ -28,7 +28,7 @@ @kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence") def solar( self, - terminator: Literal["day_night", "civil", "nautical", "astronomical"] = "day_night", + terminator: Literal["astronomical", "civil", "day_night", "nautical"] = "day_night", terminator_datetime=None, **kwargs, ): From a8def88e194c9120ca79fdad915b402d4f82ed55 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 15 Dec 2023 21:17:59 +0800 Subject: [PATCH 3/4] Improve the description of terminator --- pygmt/src/solar.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pygmt/src/solar.py b/pygmt/src/solar.py index 3179a153a47..b5275b7048f 100644 --- a/pygmt/src/solar.py +++ b/pygmt/src/solar.py @@ -46,10 +46,13 @@ def solar( Parameters ---------- terminator - 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. + 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: + + - ``"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. From 2c2f362d50222c969124d1fb81406242278b3347 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 2 Jan 2024 23:58:22 +0800 Subject: [PATCH 4/4] Rewrap docstrings to 88 characters --- pygmt/src/solar.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/pygmt/src/solar.py b/pygmt/src/solar.py index a84b390b7f3..3413214cbd6 100644 --- a/pygmt/src/solar.py +++ b/pygmt/src/solar.py @@ -35,9 +35,8 @@ def solar( 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` @@ -46,20 +45,19 @@ def solar( Parameters ---------- 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: + 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: - ``"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. + 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} @@ -100,9 +98,9 @@ def solar( """ kwargs = self._preprocess(**kwargs) if kwargs.get("T") is not None: - raise GMTInvalidInput( - "Use 'terminator' and 'terminator_datetime' instead of 'T'." - ) + 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(