Skip to content

Commit d59969f

Browse files
committed
Remove default variable mapping from midc parser
1 parent a51db51 commit d59969f

File tree

2 files changed

+35
-81
lines changed

2 files changed

+35
-81
lines changed

pvlib/iotools/midc.py

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
"""Functions to read NREL MIDC data.
22
"""
3-
from functools import partial
43
import pandas as pd
54

6-
# VARIABLE_MAP is a dictionary mapping partial MIDC field names to their
7-
# pvlib names. See docstring of read_midc for description.
8-
9-
VARIABLE_MAP = {
10-
'Direct': 'dni',
11-
'Global': 'ghi',
12-
'Diffuse': 'dhi',
13-
'Airmass': 'airmass',
14-
'Azimuth Angle': 'solar_azimuth',
15-
'Zenith Angle': 'solar_zenith',
16-
'Air Temperature': 'temp_air',
17-
'Temperature': 'temp_air',
18-
'Dew Point Temp': 'temp_dew',
19-
'Relative Humidity': 'relative_humidity',
20-
}
215

226
# Maps problematic timezones to 'Etc/GMT' for parsing.
237

@@ -27,43 +11,6 @@
2711
}
2812

2913

30-
def map_midc_to_pvlib(variable_map, field_name):
31-
"""A mapper function to rename Dataframe columns to their pvlib counterparts.
32-
33-
Parameters
34-
----------
35-
variable_map: Dictionary
36-
A dictionary for mapping MIDC field name to pvlib name. See
37-
VARIABLE_MAP for default value and description of how to construct
38-
this argument.
39-
field_name: string
40-
The Column to map.
41-
42-
Returns
43-
-------
44-
label: string
45-
The pvlib variable name associated with the MIDC field or the input if
46-
a mapping does not exist.
47-
48-
Notes
49-
-----
50-
Will fail if field_name to be mapped matches an entry in VARIABLE_MAP and
51-
does not contain brackets. This should not be an issue unless MIDC file
52-
headers are updated.
53-
54-
"""
55-
new_field_name = field_name
56-
for midc_name, pvlib_name in variable_map.items():
57-
if field_name.startswith(midc_name):
58-
# extract the instrument and units field and then remove units
59-
instrument_units = field_name[len(midc_name):]
60-
units_index = instrument_units.find('[')
61-
instrument = instrument_units[:units_index - 1]
62-
new_field_name = pvlib_name + instrument.replace(' ', '_')
63-
break
64-
return new_field_name
65-
66-
6714
def format_index(data):
6815
"""Create DatetimeIndex for the Dataframe localized to the timezone provided
6916
as the label of the second (time) column.
@@ -114,7 +61,7 @@ def format_index_raw(data):
11461
return data
11562

11663

117-
def read_midc(filename, variable_map=VARIABLE_MAP, raw_data=False):
64+
def read_midc(filename, variable_map={}, raw_data=False):
11865
"""Read in National Renewable Energy Laboratory Measurement and
11966
Instrumentation Data Center [1]_ weather data.
12067
@@ -123,9 +70,9 @@ def read_midc(filename, variable_map=VARIABLE_MAP, raw_data=False):
12370
filename: string
12471
Filename or url of data to read.
12572
variable_map: dictionary
126-
Dictionary for mapping MIDC field names to pvlib names. See variable
127-
`VARIABLE_MAP` for default and Notes section below for a description of
128-
its format.
73+
Dictionary for mapping MIDC field names to pvlib names. Used to rename
74+
the columns of the resulting DataFrame. Does not map names by default.
75+
See Notes for an example.
12976
raw_data: boolean
13077
Set to true to use format_index_raw to correctly format the date/time
13178
columns of MIDC raw data files.
@@ -137,14 +84,18 @@ def read_midc(filename, variable_map=VARIABLE_MAP, raw_data=False):
13784
13885
Notes
13986
-----
140-
Keys of the `variable_map` dictionary should include the first part
141-
of a MIDC field name which indicates the variable being measured.
87+
The `variable_map` argument should map fields from MIDC data to pvlib
88+
names.
89+
90+
e.g. If a MIDC file contains the variable 'Global Horizontal [W/m^2]',
91+
passing the dictionary below will rename the column to 'ghi' in
92+
the returned Dataframe.
14293
143-
e.g. 'Global PSP [W/m^2]' is entered as a key of 'Global'
94+
{
95+
'Global Horizontal [W/m^2]': ghi,
96+
}
14497
145-
The 'PSP' indicating instrument is appended to the pvlib variable name
146-
after mapping to differentiate measurements of the same variable. For a
147-
full list of pvlib variable names see the `Variable Style Rules
98+
For a full list of pvlib variable names see the `Variable Style Rules
14899
<https://pvlib-python.readthedocs.io/en/latest/variables_style_rules.html>`_.
149100
150101
Be sure to check the units for the variables you will use on the
@@ -160,8 +111,7 @@ def read_midc(filename, variable_map=VARIABLE_MAP, raw_data=False):
160111
data = format_index_raw(data)
161112
else:
162113
data = format_index(data)
163-
mapper = partial(map_midc_to_pvlib, variable_map)
164-
data = data.rename(columns=mapper)
114+
data = data.rename(columns=variable_map)
165115
return data
166116

167117

@@ -176,6 +126,10 @@ def read_midc_raw_data_from_nrel(site, start, end, variable_map={}):
176126
Start date for requested data.
177127
end: datetime
178128
End date for requested data.
129+
variable_map: dict
130+
A dictionary mapping MIDC field names to pvlib names. Used to
131+
rename columns of the resulting DataFrame. See Notes of
132+
:py:func:`pvlib.iotools.read_midc` for example.
179133
180134
Returns
181135
-------

pvlib/test/test_midc.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
from pvlib.iotools import midc
1010

1111

12+
@pytest.fixture
13+
def test_mapping():
14+
return {
15+
'Direct Normal [W/m^2]': 'dni',
16+
'Global PSP [W/m^2]': 'ghi',
17+
'Rel Humidity [%]': 'relative_humidity',
18+
'Temperature @ 2m [deg C]': 'temp_air',
19+
'Non Existant': 'variable',
20+
}
21+
22+
1223
test_dir = os.path.dirname(
1324
os.path.abspath(inspect.getfile(inspect.currentframe())))
1425
midc_testfile = os.path.join(test_dir, '../data/midc_20181014.txt')
@@ -17,16 +28,6 @@
1728
'?site=UAT&begin=20181018&end=20181019')
1829

1930

20-
@pytest.mark.parametrize('field_name,expected', [
21-
('Temperature @ 2m [deg C]', 'temp_air_@_2m'),
22-
('Global PSP [W/m^2]', 'ghi_PSP'),
23-
('Temperature @ 50m [deg C]', 'temp_air_@_50m'),
24-
('Other Variable [units]', 'Other Variable [units]'),
25-
])
26-
def test_read_midc_mapper_function(field_name, expected):
27-
assert midc.map_midc_to_pvlib(midc.VARIABLE_MAP, field_name) == expected
28-
29-
3031
def test_midc_format_index():
3132
data = pd.read_csv(midc_testfile)
3233
data = midc.format_index(data)
@@ -57,17 +58,16 @@ def test_midc_format_index_raw():
5758
assert data.index[-1] == end
5859

5960

60-
def test_read_midc_var_mapping_as_arg():
61-
data = midc.read_midc(midc_testfile, variable_map=midc.VARIABLE_MAP)
62-
assert 'ghi_PSP' in data.columns
63-
assert 'temp_air_@_2m' in data.columns
64-
assert 'temp_air_@_50m' in data.columns
61+
def test_read_midc_var_mapping_as_arg(test_mapping):
62+
data = midc.read_midc(midc_testfile, variable_map=test_mapping)
63+
assert 'ghi' in data.columns
64+
assert 'temp_air' in data.columns
6565

6666

6767
@network
6868
def test_read_midc_raw_data_from_nrel():
6969
start_ts = pd.Timestamp('20181018')
7070
end_ts = pd.Timestamp('20181019')
7171
data = midc.read_midc_raw_data_from_nrel('UAT', start_ts, end_ts)
72-
assert 'dni_Normal' in data.columns
72+
assert 'Direct Normal [W/m^2]' in data.columns
7373
assert data.index.size == 2880

0 commit comments

Comments
 (0)