11"""Functions to read NREL MIDC data.
22"""
3- from functools import partial
43import 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
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-
6714def 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 -------
0 commit comments