Skip to content

Commit 55e0577

Browse files
Fix reverse mapping issue and undesirable column reordering in get_psm3 (#1648)
* Fix mapping of request variables * I've missed stickler * Add column order check * Fix typo * Update pvlib/tests/iotools/test_psm3.py Co-authored-by: Kevin Anderson <[email protected]> * Add whatsnew entry * Update variable map --------- Co-authored-by: Kevin Anderson <[email protected]>
1 parent 25af865 commit 55e0577

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

docs/sphinx/source/whatsnew/v0.9.5.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Enhancements
2020

2121
Bug fixes
2222
~~~~~~~~~
23+
* Fixed incorrect mapping of requested parameters names when using the ``get_psm3``
24+
function. Also fixed the random reordering of the dataframe columns.
25+
(:issue:`1629`, :issue:`1647`, :pull:`1648`)
2326

2427

2528
Testing
@@ -45,4 +48,5 @@ Contributors
4548
~~~~~~~~~~~~
4649
* Kevin Anderson (:ghuser:`kanderso-nrel`)
4750
* Will Holmgren (:ghuser:`wholmgren`)
51+
* Adam R. Jensen (:ghuser:`adamrjensen`)
4852
* Pratham Chauhan (:ghuser:`ooprathamm`)

pvlib/iotools/psm3.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
TMY_URL = NSRDB_API_BASE + "/api/nsrdb/v2/solar/psm3-tmy-download.csv"
1616
PSM5MIN_URL = NSRDB_API_BASE + "/api/nsrdb/v2/solar/psm3-5min-download.csv"
1717

18-
# 'relative_humidity', 'total_precipitable_water' are not available
1918
ATTRIBUTES = (
2019
'air_temperature', 'dew_point', 'dhi', 'dni', 'ghi', 'surface_albedo',
2120
'surface_pressure', 'wind_direction', 'wind_speed')
2221
PVLIB_PYTHON = 'pvlib python'
2322

24-
# Dictionary mapping PSM3 names to pvlib names
23+
# Dictionary mapping PSM3 response names to pvlib names
2524
VARIABLE_MAP = {
2625
'GHI': 'ghi',
2726
'DHI': 'dhi',
@@ -31,15 +30,36 @@
3130
'Clearsky DNI': 'dni_clear',
3231
'Solar Zenith Angle': 'solar_zenith',
3332
'Temperature': 'temp_air',
34-
'Relative Humidity': 'relative_humidity',
3533
'Dew point': 'temp_dew',
34+
'Relative Humidity': 'relative_humidity',
3635
'Pressure': 'pressure',
37-
'Wind Direction': 'wind_direction',
3836
'Wind Speed': 'wind_speed',
37+
'Wind Direction': 'wind_direction',
3938
'Surface Albedo': 'albedo',
4039
'Precipitable Water': 'precipitable_water',
4140
}
4241

42+
# Dictionary mapping pvlib names to PSM3 request names
43+
# Note, PSM3 uses different names for the same variables in the
44+
# response and the request
45+
REQUEST_VARIABLE_MAP = {
46+
'ghi': 'ghi',
47+
'dhi': 'dhi',
48+
'dni': 'dni',
49+
'ghi_clear': 'clearsky_dhi',
50+
'dhi_clear': 'clearsky_dhi',
51+
'dni_clear': 'clearsky_dni',
52+
'zenith': 'solar_zenith_angle',
53+
'temp_air': 'air_temperature',
54+
'temp_dew': 'dew_point',
55+
'relative_humidity': 'relative_humidity',
56+
'pressure': 'surface_pressure',
57+
'wind_speed': 'wind_speed',
58+
'wind_direction': 'wind_direction',
59+
'albedo': 'surface_albedo',
60+
'precipitable_water': 'total_precipitable_water',
61+
}
62+
4363

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

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

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

pvlib/tests/iotools/test_psm3.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ def test_get_psm3_attribute_mapping(nrel_api_key):
189189
names=2019, interval=60,
190190
attributes=['ghi', 'wind_speed'],
191191
leap_day=False, map_variables=True)
192-
assert 'ghi' in data.columns
193-
assert 'wind_speed' in data.columns
192+
# Check that columns are in the correct order (GH1647)
193+
expected_columns = [
194+
'Year', 'Month', 'Day', 'Hour', 'Minute', 'ghi', 'wind_speed']
195+
pd.testing.assert_index_equal(pd.Index(expected_columns), data.columns)
194196
assert 'latitude' in meta.keys()
195197
assert 'longitude' in meta.keys()
196198
assert 'altitude' in meta.keys()

0 commit comments

Comments
 (0)