Skip to content

Fix reverse mapping issue and undesirable column reordering in get_psm3 #1648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 2, 2023
Merged
Changes from 2 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
28 changes: 19 additions & 9 deletions pvlib/iotools/psm3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
TMY_URL = NSRDB_API_BASE + "/api/nsrdb/v2/solar/psm3-tmy-download.csv"
PSM5MIN_URL = NSRDB_API_BASE + "/api/nsrdb/v2/solar/psm3-5min-download.csv"

# 'relative_humidity', 'total_precipitable_water' are not available
ATTRIBUTES = (
'air_temperature', 'dew_point', 'dhi', 'dni', 'ghi', 'surface_albedo',
'surface_pressure', 'wind_direction', 'wind_speed')
PVLIB_PYTHON = 'pvlib python'

# Dictionary mapping PSM3 names to pvlib names
# Dictionary mapping PSM3 response names to pvlib names
VARIABLE_MAP = {
'GHI': 'ghi',
'DHI': 'dhi',
Expand All @@ -40,6 +39,21 @@
'Precipitable Water': 'precipitable_water',
}

# Dictionary mapping pvlib names to PSM3 request names
# Note, PSM3 uses different names for the same variables in the
# response the request
REQUEST_VARIABLE_MAP = {
'temp_air': 'air_temperature',
'ghi_clear': 'clearsky_dhi',
'dni_clear': 'clearsky_dni',
'dhi_clear': 'clearsky_dhi',
'temp_dew': 'dew_point',
'zenith': 'solar_zenith_angle',
'albedo': 'surface_albedo',
'pressure': 'surface_pressure',
'precipitable_water': 'total_precipitable_water',
}


def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60,
attributes=ATTRIBUTES, leap_day=None, full_name=PVLIB_PYTHON,
Expand Down Expand Up @@ -74,7 +88,7 @@ def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60,
meteorological fields to fetch. If not specified, defaults to
``pvlib.iotools.psm3.ATTRIBUTES``. See references [2]_, [3]_, and [4]_
for lists of available fields. Alternatively, pvlib names may also be
used (e.g. 'ghi' rather than 'GHI'); see :const:`VARIABLE_MAP`.
used (e.g. 'ghi' rather than 'GHI'); see :const:`REQUEST_VARIABLE_MAP`.
leap_day : boolean, default False
include leap day in the results. Only used for single-year requests
(i.e., it is ignored for tmy/tgy/tdy requests).
Expand Down Expand Up @@ -158,12 +172,8 @@ def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60,
# convert to string to accomodate integer years being passed in
names = str(names)

# convert pvlib names in attributes to psm3 convention (reverse mapping)
# unlike psm3 columns, attributes are lower case and with underscores
amap = {value: key.lower().replace(' ', '_') for (key, value) in
VARIABLE_MAP.items()}
attributes = [amap.get(a, a) for a in attributes]
attributes = list(set(attributes)) # remove duplicate values
# convert pvlib names in attributes to psm3 convention
attributes = [REQUEST_VARIABLE_MAP.get(a, a) for a in attributes]

if (leap_day is None) and (not names.startswith('t')):
warnings.warn(
Expand Down