Skip to content
Merged
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
48 changes: 6 additions & 42 deletions pvlib/iotools/psm4.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ def get_nsrdb_psm4_aggregated(latitude, longitude, api_key, email,
.. [2] `NSRDB GOES Aggregated V4.0.0
<https://developer.nlr.gov/docs/solar/nsrdb/nsrdb-GOES-aggregated-v4-0-0-download/>`_
"""
# 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]
Expand All @@ -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(),
Expand Down Expand Up @@ -314,12 +308,6 @@ def get_nsrdb_psm4_tmy(latitude, longitude, api_key, email, year='tmy',
.. [2] `NSRDB GOES Tmy V4.0.0
<https://developer.nlr.gov/docs/solar/nsrdb/nsrdb-GOES-tmy-v4-0-0-download/>`_
"""
# 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]
Expand All @@ -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(),
Expand Down Expand Up @@ -450,12 +438,6 @@ def get_nsrdb_psm4_conus(latitude, longitude, api_key, email, year,
.. [2] `NSRDB GOES Conus V4.0.0
<https://developer.nlr.gov/docs/solar/nsrdb/nsrdb-GOES-conus-v4-0-0-download/>`_
"""
# 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]
Expand All @@ -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(),
Expand Down Expand Up @@ -589,12 +571,6 @@ def get_nsrdb_psm4_full_disc(latitude, longitude, api_key, email,
.. [2] `NSRDB GOES Full Disc V4.0.0
<https://developer.nlr.gov/docs/solar/nsrdb/nsrdb-GOES-full-disc-v4-0-0-download/>`_
"""
# 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]
Expand All @@ -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(),
Expand Down Expand Up @@ -738,12 +714,6 @@ def get_nsrdb_psm4_polar(latitude, longitude, api_key, email,
.. [3] `NSRDB Polar V4.0.0
<https://developer.nlr.gov/docs/solar/nsrdb/nsrdb-polar-v4-0-0-download/>`_
"""
# 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]
Expand All @@ -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(),
Expand Down Expand Up @@ -887,12 +857,6 @@ def get_nsrdb_psm4_polar_tmy(latitude, longitude, api_key, email, year='tmy',
.. [3] `NSRDB Polar Tmy V4.0.0
<https://developer.nlr.gov/docs/solar/nsrdb/nsrdb-polar-tmy-v4-0-0-download/>`_
"""
# 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]
Expand All @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions pvlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})' # noqa: E231
24 changes: 24 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading