1
1
"""Functions to read NREL MIDC data.
2
2
"""
3
- from functools import partial
4
3
import pandas as pd
5
4
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
- }
21
5
22
6
# Maps problematic timezones to 'Etc/GMT' for parsing.
23
7
27
11
}
28
12
29
13
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
-
67
14
def format_index (data ):
68
15
"""Create DatetimeIndex for the Dataframe localized to the timezone provided
69
16
as the label of the second (time) column.
@@ -114,7 +61,7 @@ def format_index_raw(data):
114
61
return data
115
62
116
63
117
- def read_midc (filename , variable_map = VARIABLE_MAP , raw_data = False ):
64
+ def read_midc (filename , variable_map = {} , raw_data = False ):
118
65
"""Read in National Renewable Energy Laboratory Measurement and
119
66
Instrumentation Data Center [1]_ weather data.
120
67
@@ -123,9 +70,9 @@ def read_midc(filename, variable_map=VARIABLE_MAP, raw_data=False):
123
70
filename: string
124
71
Filename or url of data to read.
125
72
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 .
129
76
raw_data: boolean
130
77
Set to true to use format_index_raw to correctly format the date/time
131
78
columns of MIDC raw data files.
@@ -137,14 +84,18 @@ def read_midc(filename, variable_map=VARIABLE_MAP, raw_data=False):
137
84
138
85
Notes
139
86
-----
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.
142
93
143
- e.g. 'Global PSP [W/m^2]' is entered as a key of 'Global'
94
+ {
95
+ 'Global Horizontal [W/m^2]': ghi,
96
+ }
144
97
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
148
99
<https://pvlib-python.readthedocs.io/en/latest/variables_style_rules.html>`_.
149
100
150
101
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):
160
111
data = format_index_raw (data )
161
112
else :
162
113
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 )
165
115
return data
166
116
167
117
@@ -176,6 +126,10 @@ def read_midc_raw_data_from_nrel(site, start, end, variable_map={}):
176
126
Start date for requested data.
177
127
end: datetime
178
128
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.
179
133
180
134
Returns
181
135
-------
0 commit comments