From a48a4eef400781815e808c1177a8e4d95c1fcfc4 Mon Sep 17 00:00:00 2001 From: Mohith V Date: Tue, 17 Nov 2026 16:06:22 +0530 Subject: [PATCH 1/4] Move repeated WKT POINT formatting in iotools.psm4 into tools helper --- docs/sphinx/source/whatsnew/v0.16.0.rst | 4 +++ pvlib/iotools/psm4.py | 48 ++++--------------------- pvlib/tools.py | 24 +++++++++++++ tests/test_tools.py | 24 +++++++++++++ 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.16.0.rst b/docs/sphinx/source/whatsnew/v0.16.0.rst index bbb6ed0b32..e84d9b624f 100644 --- a/docs/sphinx/source/whatsnew/v0.16.0.rst +++ b/docs/sphinx/source/whatsnew/v0.16.0.rst @@ -178,6 +178,9 @@ Maintenance * Remove private function :py:func:`pvlib.irradiance._liujordan`. (:issue:`2764`, :pull:`2766`) * Fix documentation builds on runner images lacking link-type timezones. (:issue:`2795`, :pull:`2809`) +* Move the well-known text (WKT) POINT formatting repeated in each + :py:mod:`pvlib.iotools.psm4` function into a shared private helper, + ``pvlib.tools._format_wkt_point``. (:pull:`XXXX`) Contributors @@ -215,4 +218,5 @@ Contributors * Mohammad Kazemi (:ghuser:`mokazemi`) * :ghuser:`FrancoisFEM` * :ghuser:`weixuan-song` +* :ghuser:`MoxOmega88` diff --git a/pvlib/iotools/psm4.py b/pvlib/iotools/psm4.py index 808899362c..e9e7ef9f0d 100644 --- a/pvlib/iotools/psm4.py +++ b/pvlib/iotools/psm4.py @@ -174,12 +174,6 @@ def get_nsrdb_psm4_aggregated(latitude, longitude, api_key, email, .. [2] `NSRDB GOES Aggregated V4.0.0 `_ """ - # The well know text (WKT) representation of geometry notation is strict. - # A POINT object is a string with longitude first, then the latitude, with - # four decimals each, and exactly one space between them. - longitude = ('%9.4f' % longitude).strip() - latitude = ('%8.4f' % latitude).strip() - # TODO: make format_WKT(object_type, *args) in tools.py # convert pvlib names in parameters to PSM4 convention parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters] @@ -192,7 +186,7 @@ def get_nsrdb_psm4_aggregated(latitude, longitude, api_key, email, 'affiliation': affiliation, 'reason': PVLIB_PYTHON, 'mailing_list': 'false', - 'wkt': 'POINT(%s %s)' % (longitude, latitude), + 'wkt': tools._format_wkt_point(latitude, longitude), 'names': year, 'attributes': ','.join(parameters), 'leap_day': str(leap_day).lower(), @@ -314,12 +308,6 @@ def get_nsrdb_psm4_tmy(latitude, longitude, api_key, email, year='tmy', .. [2] `NSRDB GOES Tmy V4.0.0 `_ """ - # The well know text (WKT) representation of geometry notation is strict. - # A POINT object is a string with longitude first, then the latitude, with - # four decimals each, and exactly one space between them. - longitude = ('%9.4f' % longitude).strip() - latitude = ('%8.4f' % latitude).strip() - # TODO: make format_WKT(object_type, *args) in tools.py # convert pvlib names in parameters to PSM4 convention parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters] @@ -332,7 +320,7 @@ def get_nsrdb_psm4_tmy(latitude, longitude, api_key, email, year='tmy', 'affiliation': affiliation, 'reason': PVLIB_PYTHON, 'mailing_list': 'false', - 'wkt': 'POINT(%s %s)' % (longitude, latitude), + 'wkt': tools._format_wkt_point(latitude, longitude), 'names': year, 'attributes': ','.join(parameters), 'leap_day': str(leap_day).lower(), @@ -450,12 +438,6 @@ def get_nsrdb_psm4_conus(latitude, longitude, api_key, email, year, .. [2] `NSRDB GOES Conus V4.0.0 `_ """ - # The well know text (WKT) representation of geometry notation is strict. - # A POINT object is a string with longitude first, then the latitude, with - # four decimals each, and exactly one space between them. - longitude = ('%9.4f' % longitude).strip() - latitude = ('%8.4f' % latitude).strip() - # TODO: make format_WKT(object_type, *args) in tools.py # convert pvlib names in parameters to PSM4 convention parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters] @@ -468,7 +450,7 @@ def get_nsrdb_psm4_conus(latitude, longitude, api_key, email, year, 'affiliation': affiliation, 'reason': PVLIB_PYTHON, 'mailing_list': 'false', - 'wkt': 'POINT(%s %s)' % (longitude, latitude), + 'wkt': tools._format_wkt_point(latitude, longitude), 'names': year, 'attributes': ','.join(parameters), 'leap_day': str(leap_day).lower(), @@ -589,12 +571,6 @@ def get_nsrdb_psm4_full_disc(latitude, longitude, api_key, email, .. [2] `NSRDB GOES Full Disc V4.0.0 `_ """ - # The well know text (WKT) representation of geometry notation is strict. - # A POINT object is a string with longitude first, then the latitude, with - # four decimals each, and exactly one space between them. - longitude = ('%9.4f' % longitude).strip() - latitude = ('%8.4f' % latitude).strip() - # TODO: make format_WKT(object_type, *args) in tools.py # convert pvlib names in parameters to PSM4 convention parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters] @@ -607,7 +583,7 @@ def get_nsrdb_psm4_full_disc(latitude, longitude, api_key, email, 'affiliation': affiliation, 'reason': PVLIB_PYTHON, 'mailing_list': 'false', - 'wkt': 'POINT(%s %s)' % (longitude, latitude), + 'wkt': tools._format_wkt_point(latitude, longitude), 'names': year, 'attributes': ','.join(parameters), 'leap_day': str(leap_day).lower(), @@ -738,12 +714,6 @@ def get_nsrdb_psm4_polar(latitude, longitude, api_key, email, .. [3] `NSRDB Polar V4.0.0 `_ """ - # The well know text (WKT) representation of geometry notation is strict. - # A POINT object is a string with longitude first, then the latitude, with - # four decimals each, and exactly one space between them. - longitude = ('%9.4f' % longitude).strip() - latitude = ('%8.4f' % latitude).strip() - # TODO: make format_WKT(object_type, *args) in tools.py # convert pvlib names in parameters to PSM4 convention parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters] @@ -756,7 +726,7 @@ def get_nsrdb_psm4_polar(latitude, longitude, api_key, email, 'affiliation': affiliation, 'reason': PVLIB_PYTHON, 'mailing_list': 'false', - 'wkt': 'POINT(%s %s)' % (longitude, latitude), + 'wkt': tools._format_wkt_point(latitude, longitude), 'names': year, 'attributes': ','.join(parameters), 'leap_day': str(leap_day).lower(), @@ -887,12 +857,6 @@ def get_nsrdb_psm4_polar_tmy(latitude, longitude, api_key, email, year='tmy', .. [3] `NSRDB Polar Tmy V4.0.0 `_ """ - # The well know text (WKT) representation of geometry notation is strict. - # A POINT object is a string with longitude first, then the latitude, with - # four decimals each, and exactly one space between them. - longitude = ('%9.4f' % longitude).strip() - latitude = ('%8.4f' % latitude).strip() - # TODO: make format_WKT(object_type, *args) in tools.py # convert pvlib names in parameters to PSM4 convention parameters = [REQUEST_VARIABLE_MAP.get(a, a) for a in parameters] @@ -905,7 +869,7 @@ def get_nsrdb_psm4_polar_tmy(latitude, longitude, api_key, email, year='tmy', 'affiliation': affiliation, 'reason': PVLIB_PYTHON, 'mailing_list': 'false', - 'wkt': 'POINT(%s %s)' % (longitude, latitude), + 'wkt': tools._format_wkt_point(latitude, longitude), 'names': year, 'attributes': ','.join(parameters), 'leap_day': str(leap_day).lower(), diff --git a/pvlib/tools.py b/pvlib/tools.py index a81306b60c..bc9ea38eb2 100644 --- a/pvlib/tools.py +++ b/pvlib/tools.py @@ -630,3 +630,27 @@ def _file_context_manager(filename_or_object, mode='r', encoding=None): # otherwise, assume a filename or path context = open(str(filename_or_object), mode=mode, encoding=encoding) return context + + +def _format_wkt_point(latitude, longitude): + """ + Format a location as a well-known text (WKT) POINT string. + + The WKT representation of geometry is strict: a POINT object lists + longitude first, then latitude, each rounded to four decimal places, + with exactly one space between them. This format is required by + the NSRDB PSM4 API, see :py:mod:`pvlib.iotools.psm4`. + + Parameters + ---------- + latitude : float or int + Latitude in decimal degrees, between -90 and 90, north is positive. + longitude : float or int + Longitude in decimal degrees, between -180 and 180, east is positive. + + Returns + ------- + str + The location as a WKT POINT, e.g. ``'POINT(-105.1800 39.7400)'``. + """ + return f'POINT({longitude:.4f} {latitude:.4f})' diff --git a/tests/test_tools.py b/tests/test_tools.py index 4b733ad711..8a45226c21 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -283,3 +283,27 @@ def test__file_context_manager(): buffer = StringIO("test content") with tools._file_context_manager(buffer) as obj: assert obj.read() == "test content" + + +@pytest.mark.parametrize('latitude, longitude, expected', [ + (39.742, -105.18, 'POINT(-105.1800 39.7420)'), + (40, -105, 'POINT(-105.0000 40.0000)'), + (-33.86786, 151.20732, 'POINT(151.2073 -33.8679)'), + (0, 0, 'POINT(0.0000 0.0000)'), + (89.99999, 179.99999, 'POINT(180.0000 90.0000)'), + (np.float64(12.3456789), np.float64(-1.23456789), + 'POINT(-1.2346 12.3457)'), +]) +def test__format_wkt_point(latitude, longitude, expected): + assert tools._format_wkt_point(latitude, longitude) == expected + + +@pytest.mark.parametrize('latitude, longitude', [ + (39.742, -105.18), (-33.86785, 151.20732), (0, 0), (-90, 180), +]) +def test__format_wkt_point_matches_legacy_format(latitude, longitude): + # the helper replaces the formatting previously inlined in + # pvlib.iotools.psm4; it must give the same string + legacy = 'POINT(%s %s)' % (('%9.4f' % longitude).strip(), + ('%8.4f' % latitude).strip()) + assert tools._format_wkt_point(latitude, longitude) == legacy From f632e80d4a96201515b26bf4a4a30b327de727a1 Mon Sep 17 00:00:00 2001 From: Mohith V Date: Tue, 17 Nov 2026 16:15:28 +0530 Subject: [PATCH 2/4] Add PR number to whatsnew --- docs/sphinx/source/whatsnew/v0.16.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/sphinx/source/whatsnew/v0.16.0.rst b/docs/sphinx/source/whatsnew/v0.16.0.rst index e84d9b624f..a5691d90bc 100644 --- a/docs/sphinx/source/whatsnew/v0.16.0.rst +++ b/docs/sphinx/source/whatsnew/v0.16.0.rst @@ -180,7 +180,7 @@ Maintenance * Fix documentation builds on runner images lacking link-type timezones. (:issue:`2795`, :pull:`2809`) * Move the well-known text (WKT) POINT formatting repeated in each :py:mod:`pvlib.iotools.psm4` function into a shared private helper, - ``pvlib.tools._format_wkt_point``. (:pull:`XXXX`) + ``pvlib.tools._format_wkt_point``. (:pull:`2948`) Contributors From d604a9b082d3883cdb92e9f37e897297167f48d4 Mon Sep 17 00:00:00 2001 From: MoxOmega88 Date: Fri, 20 Nov 2026 12:23:47 +0530 Subject: [PATCH 3/4] Update pvlib/tools.py Co-authored-by: Rajiv Daxini <143435106+RDaxini@users.noreply.github.com> --- pvlib/tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvlib/tools.py b/pvlib/tools.py index bc9ea38eb2..67aaa6dd09 100644 --- a/pvlib/tools.py +++ b/pvlib/tools.py @@ -653,4 +653,4 @@ def _format_wkt_point(latitude, longitude): str The location as a WKT POINT, e.g. ``'POINT(-105.1800 39.7400)'``. """ - return f'POINT({longitude:.4f} {latitude:.4f})' + return f'POINT({longitude:.4f} {latitude:.4f})' # noqa: E231 From d658b295c45cd5d199ea156ab537b153201163d0 Mon Sep 17 00:00:00 2001 From: Mohith V Date: Fri, 20 Nov 2026 12:29:06 +0530 Subject: [PATCH 4/4] Removed whatsnew entry as suggested --- docs/sphinx/source/whatsnew/v0.16.0.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.16.0.rst b/docs/sphinx/source/whatsnew/v0.16.0.rst index a5691d90bc..bbb6ed0b32 100644 --- a/docs/sphinx/source/whatsnew/v0.16.0.rst +++ b/docs/sphinx/source/whatsnew/v0.16.0.rst @@ -178,9 +178,6 @@ Maintenance * Remove private function :py:func:`pvlib.irradiance._liujordan`. (:issue:`2764`, :pull:`2766`) * Fix documentation builds on runner images lacking link-type timezones. (:issue:`2795`, :pull:`2809`) -* Move the well-known text (WKT) POINT formatting repeated in each - :py:mod:`pvlib.iotools.psm4` function into a shared private helper, - ``pvlib.tools._format_wkt_point``. (:pull:`2948`) Contributors @@ -218,5 +215,4 @@ Contributors * Mohammad Kazemi (:ghuser:`mokazemi`) * :ghuser:`FrancoisFEM` * :ghuser:`weixuan-song` -* :ghuser:`MoxOmega88`