@@ -26,8 +26,9 @@ def map_midc_to_pvlib(variable_map, field_name):
26
26
Parameters
27
27
----------
28
28
variable_map: Dictionary
29
- A dictionary for mapping MIDC field nameto pvlib name. See VARIABLE_MAP
30
- for default value and description of how to construct this argument.
29
+ A dictionary for mapping MIDC field name to pvlib name. See
30
+ VARIABLE_MAP for default value and description of how to construct
31
+ this argument.
31
32
field_name: string
32
33
The Column to map.
33
34
@@ -36,13 +37,21 @@ def map_midc_to_pvlib(variable_map, field_name):
36
37
label: string
37
38
The pvlib variable name associated with the MIDC field or the input if
38
39
a mapping does not exist.
40
+
41
+ Notes
42
+ -----
43
+ Will fail if field_name to be mapped matches an entry in VARIABLE_MAP and
8000
44
+ does not contain brackets. This should not be an issue unless MIDC file
45
+ headers are updated.
46
+
39
47
"""
40
48
new_field_name = field_name
41
49
for midc_name , pvlib_name in variable_map .items ():
42
50
if field_name .startswith (midc_name ):
43
51
# extract the instument and units field and then remove units
44
52
instrument_units = field_name [len (midc_name ):]
45
- instrument = instrument_units [:instrument_units .find ('[' ) - 1 ]
53
+ units_index = instrument_units .find ('[' )
54
+ instrument = instrument_units [:units_index - 1 ]
46
55
new_field_name = pvlib_name + instrument .replace (' ' , '_' )
47
56
break
48
57
return new_field_name
@@ -51,6 +60,17 @@ def map_midc_to_pvlib(variable_map, field_name):
51
60
def format_index (data ):
52
61
"""Create DatetimeIndex for the Dataframe localized to the timezone provided
53
62
as the label of the second (time) column.
63
+
64
+ Parameters
65
+ ----------
66
+ data: Dataframe
67
+ Must contain 'DATE (MM/DD/YYYY)' column, second column must be labeled
68
+ with the timezone and contain times in 'HH:MM' format.
69
+
70
+ Returns
71
+ -------
72
+ data: Dataframe
73
+ Dataframe with DatetimeIndex localized to the provided timezone.
54
74
"""
55
75
timezone = data .columns [1 ]
56
76
datetime = data ['DATE (MM/DD/YYYY)' ] + data [timezone ]
@@ -60,7 +80,32 @@ def format_index(data):
60
80
return data
61
81
62
82
63
- def read_midc (filename , variable_map = VARIABLE_MAP ):
83
+ def format_index_raw (data ):
84
+ """Create DatetimeIndex for the Dataframe localized to the timezone provided
85
+ as the label of the third column.
86
+
87
+ Parameters
88
+ ----------
89
+ data: Dataframe
90
+ Must contain columns 'Year' and 'DOY'. Timezone must be found as the
91
+ label of the third (time) column.
92
+
93
+ Returns
94
+ -------
95
+ data: Dataframe
96
+ The data with a Datetime index localized to the provided timezone.
97
+ """
98
+ tz = data .columns [3 ]
99
+ year = data .Year .apply (str )
100
+ jday = data .DOY .apply (lambda x : '{:03d}' .format (x ))
101
+ time = data [tz ].apply (lambda x : '{:04d}' .format (x ))
102
+ index = pd .to_datetime (year + jday + time , format = "%Y%j%H%M" )
103
+ data = data .set_index (index )
104
+ data = data .tz_localize (tz )
105
+ return data
106
+
107
+
108
+ def read_midc (filename , variable_map = VARIABLE_MAP , raw_data = False ):
64
109
"""Read in National Renewable Energy Laboratory Measurement and
65
110
Instrumentation Data Center [1]_ weather data.
66
111
@@ -72,6 +117,9 @@ def read_midc(filename, variable_map=VARIABLE_MAP):
72
117
Dictionary for mapping MIDC field names to pvlib names. See variable
73
118
`VARIABLE_MAP` for default and Notes section below for a description of
74
119
its format.
120
+ raw_data: boolean
121
+ Set to true to use format_index_raw to correctly format the date/time
122
+ columns of MIDC raw data files.
75
123
76
124
Returns
77
125
-------
@@ -99,7 +147,41 @@ def read_midc(filename, variable_map=VARIABLE_MAP):
99
147
`https://midcdmz.nrel.gov/ <https://midcdmz.nrel.gov/>`_
100
148
"""
101
149
data = pd .read_csv (filename )
102
- data = format_index (data )
150
+ if raw_data :
151
+ data = format_index_raw (data )
152
+ else :
153
+ data = format_index (data )
103
154
mapper = partial (map_midc_to_pvlib , variable_map )
104
155
data = data .rename (columns = mapper )
105
156
return data
157
+
158
+
159
+ def read_midc_raw_data_from_nrel (site , start , end ):
160
+ """Request and read MIDC data directly from the raw data api.
161
+
162
+ Parameters
163
+ ----------
164
+ site: string
165
+ The MIDC station id.
166
+ start: datetime
167
+ Start date for requested data.
168
+ end: datetime
169
+ End date for requested data.
170
+
171
+ Returns
172
+ -------
173
+ data:
174
+ Dataframe with DatetimeIndex localized to the station location.
175
+
176
+ Notes
177
+ -----
178
+ Requests spanning an instrumentation change will yield an error. See the
179
+ MIDC raw data api page here_ for more details and considerations.
180
+ .. _here: https://midcdmz.nrel.gov/apps/data_api_doc.pl?_idtextlist
181
+ """
182
+ args = {'site' : site ,
183
+ 'begin' : start .strftime ('%Y%m%d' ),
184
+ 'end' : end .strftime ('%Y%m%d' )}
185
+ endpoint = 'https://midcdmz.nrel.gov/apps/data_api.pl?'
186
+ url = endpoint + '&' .join (['{}={}' .format (k , v ) for k , v in args .items ()])
187
+ return read_midc (url , raw_data = True )
0 commit comments