Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 251074b

Browse files
committedOct 16, 2018
Add MIDC reader
1 parent 8058272 commit 251074b

File tree

4 files changed

+1552
-0
lines changed

4 files changed

+1552
-0
lines changed
 

‎pvlib/data/midc_20181014.txt

Lines changed: 1441 additions & 0 deletions
Large diffs are not rendered by default.

‎pvlib/iotools/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from pvlib.iotools.srml import read_srml # noqa: F401
44
from pvlib.iotools.srml import read_srml_month_from_solardat # noqa: F401
55
from pvlib.iotools.surfrad import read_surfrad # noqa: F401
6+
from pvlib.iotools.midc import read_midc # noqa: F401

‎pvlib/iotools/midc.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Functions to read NREL MIDC data.
2+
"""
3+
from functools import partial
4+
import pandas as pd
5+
6+
7+
def map_midc_to_pvlib(variable_map, field_name):
8+
"""A mapper function to rename Dataframe columns to their pvlib counterparts.
9+
10+
Parameters
11+
----------
12+
variable_map: Dictionary
13+
A dictionary of "MIDC field name": "pvlib variable name"
14+
15+
Returns
16+
-------
17+
label: string
18+
The pvlib variable name associated with the MIDC field or the input if
19+
a mapping does not exist.
20+
"""
21+
try:
22+
return variable_map[field_name]
23+
except KeyError:
24+
return field_name
25+
26+
27+
def format_index(data):
28+
"""Create DatetimeIndex for the Dataframe localized to the timezone provided
29+
as the label of the second (time) column.
30+
"""
31+
timezone = data.columns[1]
32+
datetime = data['DATE (MM/DD/YYYY)'] + data[timezone]
33+
datetime = pd.to_datetime(datetime, format='%m/%d/%Y%H:%M')
34+
data = data.set_index(datetime)
35+
data = data.tz_localize(timezone)
36+
return data
37+
38+
39+
def read_midc(filename, variable_map={}):
40+
"""Read in NREL MIDC[1]_ weather data.
41+
42+
Paramters
43+
---------
44+
filename: string
45+
Filename or url of data to read.
46+
variable_map: dictionary
47+
Dictionary mapping MIDC field names to pvlib names.
48+
example:
49+
{ 'Temperature @ 2m [deg C]': 'air_temp'}
50+
51+
Returns
52+
-------
53+
data: Dataframe
54+
A dataframe with DatetimeIndex localized to the provided timezone.
55+
56+
References
57+
----------
58+
[1] National Renewable Energy Laboratory: Measurement and Instrumentation Data Center # NOQA
59+
https://midcdmz.nrel.gov/
60+
"""
61+
data = pd.read_csv(filename)
62+
data = format_index(data)
63+
mapper = partial(map_midc_to_pvlib, variable_map)
64+
data = data.rename(mapper, axis='columns')
65+
return data

‎pvlib/test/test_midc.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import inspect
2+
import os
3+
4+
import pandas as pd
5+
import pytest
6+
7+
from pvlib.iotools import midc
8+
9+
10+
test_dir = os.path.dirname(
11+
os.path.abspath(inspect.getfile(inspect.currentframe())))
12+
midc_testfile = os.path.join(test_dir, '../data/midc_20181014.txt')
13+
14+
VARIABLE_MAP = {'Global PSP [W/m^2]': 'ghi',
15+
'Diffuse Horiz [W/m^2]': 'dhi',
16+
'Temperature @ 2m [deg C]': 'temp_air',
17+
'Avg Wind Speed @ 3m [m/s]': 'wind_speed'}
18+
19+
20+
@pytest.mark.paramatrize('field_name,expected', [
21+
('Temperature @ 2m [deg C]', 'temp_air'),
22+
('Global PSP [W/m^2]', 'ghi'),
23+
('Temperature @ 50m [deg C]', 'Temperature @ 50m [deg C]')
24+
])
25+
def test_read_midc_mapper_function(field_name, expected):
26+
assert midc.map_midc_to_pvlib(VARIABLE_MAP, field_name) == expected
27+
28+
29+
def test_read_midc_format_index():
30+
data = pd.read_csv(midc_testfile)
31+
data = midc.format_index(data)
32+
start = pd.Timestamp("20181014 00:00")
33+
start = start.tz_localize("MST")
34+
end = pd.Timestamp("20181014 23:59")
35+
end = end.tz_localize("MST")
36+
assert type(data.index) == pd.DatetimeIndex
37+
assert data.index[0] == start
38+
assert data.index[-1] == end
39+
40+
41+
def test_read_midc_var_mapping_as_arg():
42+
data = midc.read_midc(midc_testfile, variable_map=VARIABLE_MAP)
43+
assert 'ghi' in data.columns
44+
assert 'temp_air' in data.columns
45+
assert 'Temperature @ 50m [deg C]' in data.columns

0 commit comments

Comments
 (0)
Please sign in to comment.