-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add variable mapping of psm3 #1374
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
Changes from 1 commit
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
import requests | ||
import pandas as pd | ||
from json import JSONDecodeError | ||
import warnings | ||
from pvlib._deprecation import pvlibDeprecationWarning | ||
|
||
NSRDB_API_BASE = "https://developer.nrel.gov" | ||
PSM_URL = NSRDB_API_BASE + "/api/nsrdb/v2/solar/psm3-download.csv" | ||
|
@@ -20,10 +22,30 @@ | |
'surface_pressure', 'wind_direction', 'wind_speed') | ||
PVLIB_PYTHON = 'pvlib python' | ||
|
||
# Dictionary mapping PSM3 names to pvlib names | ||
PSM3_VARIABLE_MAP = { | ||
'GHI': 'ghi', | ||
'DHI': 'dhi', | ||
'DNI': 'dni', | ||
'Clearsky GHI': 'ghi_clear', | ||
'Clearsky DHI': 'dhi_clear', | ||
'Clearsky DNI': 'dni_clear', | ||
'Solar Zenith Angle': 'solar_zenith', | ||
'Temperature': 'temp_air', | ||
'Relative Humidity': 'relative_humidity', | ||
'Dew point': 'temp_dew', | ||
'Pressure': 'pressure', | ||
'Wind Direction': 'wind_direction', | ||
'Wind Speed': 'wind_speed', | ||
'Latitude': 'latitude', | ||
'Longitude': 'longitude', | ||
'Elevation': 'elevation' | ||
AdamRJensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60, | ||
attributes=ATTRIBUTES, leap_day=False, full_name=PVLIB_PYTHON, | ||
affiliation=PVLIB_PYTHON, timeout=30): | ||
affiliation=PVLIB_PYTHON, map_variables=None, timeout=30): | ||
""" | ||
Retrieve NSRDB PSM3 timeseries weather data from the PSM3 API. The NSRDB | ||
is described in [1]_ and the PSM3 API is described in [2]_, [3]_, and [4]_. | ||
|
@@ -61,6 +83,9 @@ def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60, | |
optional | ||
affiliation : str, default 'pvlib python' | ||
optional | ||
map_variables: bool | ||
When true, renames columns of the Dataframe to pvlib variable names | ||
where applicable. See variable PSM3_VARIABLE_MAP. | ||
AdamRJensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timeout : int, default 30 | ||
time in seconds to wait for server response before timeout | ||
|
||
|
@@ -133,6 +158,13 @@ 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 | ||
PSM3_VARIABLE_MAP.items()} | ||
attributes = [a if a not in amap.keys() else amap[a] for a in attributes] | ||
attributes = list(set(attributes)) # remove duplicate values | ||
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. 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: 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. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. @AdamRJensen should we add a sentence to the
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. If we're referencing the iotools variable maps via |
||
|
||
# required query-string parameters for request to PSM3 API | ||
params = { | ||
'api_key': api_key, | ||
|
@@ -167,10 +199,10 @@ def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60, | |
# the CSV is in the response content as a UTF-8 bytestring | ||
# to use pandas we need to create a file buffer from the response | ||
fbuf = io.StringIO(response.content.decode('utf-8')) | ||
return parse_psm3(fbuf) | ||
return parse_psm3(fbuf, map_variables) | ||
|
||
|
||
def parse_psm3(fbuf): | ||
def parse_psm3(fbuf, map_variables): | ||
AdamRJensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Parse an NSRDB PSM3 weather file (formatted as SAM CSV). The NSRDB | ||
is described in [1]_ and the SAM CSV format is described in [2]_. | ||
|
@@ -184,6 +216,9 @@ def parse_psm3(fbuf): | |
---------- | ||
fbuf: file-like object | ||
File-like object containing data to read. | ||
map_variables: bool | ||
When true, renames columns of the Dataframe to pvlib variable names | ||
where applicable. See variable PSM3_VARIABLE_MAP. | ||
|
||
Returns | ||
------- | ||
|
@@ -296,10 +331,20 @@ def parse_psm3(fbuf): | |
tz = 'Etc/GMT%+d' % -metadata['Time Zone'] | ||
data.index = pd.DatetimeIndex(dtidx).tz_localize(tz) | ||
|
||
if map_variables is None: | ||
AdamRJensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
warnings.warn( | ||
'PSM3 variable names will be renamed to pvlib conventions by ' | ||
'default starting in pvlib 0.11.0. Specify map_variables=True ' | ||
'to enable that behavior now, or specify map_variables=False ' | ||
'to hide this warning.', pvlibDeprecationWarning) | ||
map_variables = False | ||
if map_variables: | ||
data = data.rename(columns=PSM3_VARIABLE_MAP) | ||
|
||
return data, metadata | ||
|
||
|
||
def read_psm3(filename): | ||
def read_psm3(filename, map_variables=None): | ||
""" | ||
Read an NSRDB PSM3 weather file (formatted as SAM CSV). The NSRDB | ||
is described in [1]_ and the SAM CSV format is described in [2]_. | ||
|
@@ -313,6 +358,9 @@ def read_psm3(filename): | |
---------- | ||
filename: str | ||
Filename of a file containing data to read. | ||
map_variables: bool | ||
When true, renames columns of the Dataframe to pvlib variable names | ||
where applicable. See variable PSM3_VARIABLE_MAP. | ||
|
||
Returns | ||
------- | ||
|
@@ -334,5 +382,5 @@ def read_psm3(filename): | |
<https://rredc.nrel.gov/solar/old_data/nsrdb/2005-2012/wfcsv.pdf>`_ | ||
""" | ||
with open(str(filename), 'r') as fbuf: | ||
content = parse_psm3(fbuf) | ||
content = parse_psm3(fbuf, map_variables) | ||
return content |
Uh oh!
There was an error while loading. Please reload this page.