-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add variable mapping of psm3 #1374
Changes from all commits
5474a03
04a1fb2
80ac779
9692da8
c96387b
12f42e8
31a7772
4b749bc
b6d58b6
fb74ba7
e775358
1b04360
cebb770
b616015
9bad509
ad2e9de
f34494e
82af8be
d92055a
7033c94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,14 @@ | |
|
||
import os | ||
from pvlib.iotools import psm3 | ||
from ..conftest import DATA_DIR, RERUNS, RERUNS_DELAY | ||
from ..conftest import DATA_DIR, RERUNS, RERUNS_DELAY, assert_index_equal | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
from requests import HTTPError | ||
from io import StringIO | ||
import warnings | ||
from pvlib._deprecation import pvlibDeprecationWarning | ||
|
||
TMY_TEST_DATA = DATA_DIR / 'test_psm3_tmy-2017.csv' | ||
YEAR_TEST_DATA = DATA_DIR / 'test_psm3_2017.csv' | ||
|
@@ -76,7 +77,8 @@ def assert_psm3_equal(data, metadata, expected): | |
def test_get_psm3_tmy(nrel_api_key): | ||
"""test get_psm3 with a TMY""" | ||
data, metadata = psm3.get_psm3(LATITUDE, LONGITUDE, nrel_api_key, | ||
PVLIB_EMAIL, names='tmy-2017') | ||
PVLIB_EMAIL, names='tmy-2017', | ||
map_variables=False) | ||
expected = pd.read_csv(TMY_TEST_DATA) | ||
assert_psm3_equal(data, metadata, expected) | ||
|
||
|
@@ -86,7 +88,8 @@ def test_get_psm3_tmy(nrel_api_key): | |
def test_get_psm3_singleyear(nrel_api_key): | ||
"""test get_psm3 with a single year""" | ||
data, metadata = psm3.get_psm3(LATITUDE, LONGITUDE, nrel_api_key, | ||
PVLIB_EMAIL, names='2017', interval=30) | ||
PVLIB_EMAIL, names='2017', | ||
map_variables=False, interval=30) | ||
expected = pd.read_csv(YEAR_TEST_DATA) | ||
assert_psm3_equal(data, metadata, expected) | ||
|
||
|
@@ -96,7 +99,8 @@ def test_get_psm3_singleyear(nrel_api_key): | |
def test_get_psm3_5min(nrel_api_key): | ||
"""test get_psm3 for 5-minute data""" | ||
data, metadata = psm3.get_psm3(LATITUDE, LONGITUDE, nrel_api_key, | ||
PVLIB_EMAIL, names='2019', interval=5) | ||
PVLIB_EMAIL, names='2019', interval=5, | ||
map_variables=False) | ||
assert len(data) == 525600/5 | ||
first_day = data.loc['2019-01-01'] | ||
expected = pd.read_csv(YEAR_TEST_DATA_5MIN) | ||
|
@@ -108,7 +112,7 @@ def test_get_psm3_5min(nrel_api_key): | |
def test_get_psm3_check_leap_day(nrel_api_key): | ||
data_2012, _ = psm3.get_psm3(LATITUDE, LONGITUDE, nrel_api_key, | ||
PVLIB_EMAIL, names="2012", interval=60, | ||
leap_day=True) | ||
leap_day=True, map_variables=False) | ||
assert len(data_2012) == (8760 + 24) | ||
|
||
|
||
|
@@ -133,7 +137,7 @@ def test_get_psm3_tmy_errors( | |
""" | ||
with pytest.raises(HTTPError) as excinfo: | ||
psm3.get_psm3(latitude, longitude, api_key, PVLIB_EMAIL, | ||
names=names, interval=interval) | ||
names=names, interval=interval, map_variables=False) | ||
# ensure the HTTPError caught isn't due to overuse of the API key | ||
assert "OVER_RATE_LIMIT" not in str(excinfo.value) | ||
|
||
|
@@ -149,13 +153,49 @@ def io_input(request): | |
|
||
def test_parse_psm3(io_input): | ||
"""test parse_psm3""" | ||
data, metadata = psm3.parse_psm3(io_input) | ||
data, metadata = psm3.parse_psm3(io_input, map_variables=False) | ||
expected = pd.read_csv(YEAR_TEST_DATA) | ||
assert_psm3_equal(data, metadata, expected) | ||
|
||
|
||
def test_read_psm3(): | ||
"""test read_psm3""" | ||
data, metadata = psm3.read_psm3(MANUAL_TEST_DATA) | ||
data, metadata = psm3.read_psm3(MANUAL_TEST_DATA, map_variables=False) | ||
expected = pd.read_csv(YEAR_TEST_DATA) | ||
assert_psm3_equal(data, metadata, expected) | ||
|
||
|
||
def test_read_psm3_map_variables(): | ||
"""test read_psm3 map_variables=True""" | ||
data, metadata = psm3.read_psm3(MANUAL_TEST_DATA, map_variables=True) | ||
columns_mapped = ['Year', 'Month', 'Day', 'Hour', 'Minute', 'dhi', 'dni', | ||
'ghi', 'dhi_clear', 'dni_clear', 'ghi_clear', | ||
'Cloud Type', 'Dew Point', 'apparent_zenith', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AdamRJensen this test is failing locally for me because of an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes indeed. Sorry for the slow response. Thanks for the fix! |
||
'Fill Flag', 'albedo', 'wind_speed', | ||
'precipitable_water', 'wind_direction', | ||
'relative_humidity', 'temp_air', 'pressure'] | ||
data, metadata = psm3.read_psm3(MANUAL_TEST_DATA, map_variables=True) | ||
assert_index_equal(data.columns, pd.Index(columns_mapped)) | ||
|
||
|
||
@pytest.mark.remote_data | ||
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) | ||
def test_get_psm3_attribute_mapping(nrel_api_key): | ||
"""Test that pvlib names can be passed in as attributes and get correctly | ||
reverse mapped to PSM3 names""" | ||
data, meta = psm3.get_psm3(LATITUDE, LONGITUDE, nrel_api_key, PVLIB_EMAIL, | ||
names=2019, interval=60, | ||
attributes=['ghi', 'wind_speed'], | ||
map_variables=True) | ||
assert 'ghi' in data.columns | ||
assert 'wind_speed' in data.columns | ||
assert 'latitude' in meta.keys() | ||
assert 'longitude' in meta.keys() | ||
assert 'altitude' in meta.keys() | ||
|
||
|
||
@pytest.mark.remote_data | ||
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) | ||
def test_psm3_variable_map_deprecation_warning(nrel_api_key): | ||
with pytest.warns(pvlibDeprecationWarning, match='names will be renamed'): | ||
_ = psm3.read_psm3(MANUAL_TEST_DATA) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting. Do we do "reverse mapping" in any other iotools functions? It is unfortunate that the PSM3 API's input parameter names are different from the output column names.
Maybe clearer for the second line, up to you:
attributes = [amap.get(a, a) for a in attributes]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we keep this, it should probably be mentioned in the docstring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot immediately think of any other function where reverse mapping would make sense. Pretty nifty though! And I think it is in line with the spirit of the iotools, in that it conforms the data access interface with pvlib conventions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AdamRJensen should we add a sentence to the
attributes
docstring description for this? Something like (feel free to edit):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're referencing the iotools variable maps via
:const:
then we should think about giving them their own entries in the docs. Something like what we did forpvlib.temperature.TEMPERATURE_MODEL_PARAMETERS
maybe? Let's do that in a separate issue though.