@@ -91,7 +91,7 @@ def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60,
91
91
92
92
See Also
93
93
--------
94
- pvlib.iotools.read_psm3
94
+ pvlib.iotools.parse_psm3
95
95
96
96
References
97
97
----------
@@ -142,17 +142,17 @@ def get_psm3(latitude, longitude, api_key, email, names='tmy', interval=60,
142
142
# the CSV is in the response content as a UTF-8 bytestring
143
143
# to use pandas we need to create a file buffer from the response
144
144
fbuf = io .StringIO (response .content .decode ('utf-8' ))
145
- return read_psm3 (fbuf )
145
+ return parse_psm3 (fbuf )
146
146
147
147
148
- def read_psm3 ( filename ):
148
+ def parse_psm3 ( fbuf ):
149
149
"""
150
- Read an NSRDB [1]_ PSM3 weather file (formatted as SAM CSV [2]_).
150
+ Parse an NSRDB [1]_ PSM3 weather file (formatted as SAM CSV [2]_).
151
151
152
152
Parameters
153
153
----------
154
- filename: string or file-like object
155
- Filename or file -like object of data to read.
154
+ fbuf: file-like object
155
+ File -like object containing data to read.
156
156
157
157
Returns
158
158
-------
@@ -215,6 +215,12 @@ def read_psm3(filename):
215
215
216
216
The second item is a dataframe with the PSM3 timeseries data.
217
217
218
+ Examples
219
+ --------
220
+ >>> # Read a local PSM3 file:
221
+ >>> with open(filename, 'r') as f: # doctest: +SKIP
222
+ ... metadata, df = iotools.parse_psm3(f) # doctest: +SKIP
223
+
218
224
See Also
219
225
--------
220
226
pvlib.iotools.get_psm3
@@ -226,48 +232,36 @@ def read_psm3(filename):
226
232
.. [2] `Standard Time Series Data File Format
227
233
<https://rredc.nrel.gov/solar/old_data/nsrdb/2005-2012/wfcsv.pdf>`_
228
234
"""
229
- if hasattr (filename , 'readline' ):
230
- # if passed a file-like object, not our job to close it
231
- close = False
232
- fbuf = filename
233
- else :
234
- close = True
235
- fbuf = open (filename , 'r' )
236
-
237
- try :
238
- # The first 2 lines of the response are headers with metadata
239
- header_fields = fbuf .readline ().split (',' )
240
- header_fields [- 1 ] = header_fields [- 1 ].strip () # strip trailing newline
241
- header_values = fbuf .readline ().split (',' )
242
- header_values [- 1 ] = header_values [- 1 ].strip () # strip trailing newline
243
- header = dict (zip (header_fields , header_values ))
244
- # the response is all strings, so set some header types to numbers
245
- header ['Local Time Zone' ] = int (header ['Local Time Zone' ])
246
- header ['Time Zone' ] = int (header ['Time Zone' ])
247
- header ['Latitude' ] = float (header ['Latitude' ])
248
- header ['Longitude' ] = float (header ['Longitude' ])
249
- header ['Elevation' ] = int (header ['Elevation' ])
250
- # get the column names so we can set the dtypes
251
- columns = fbuf .readline ().split (',' )
252
- columns [- 1 ] = columns [- 1 ].strip () # strip trailing newline
253
- # Since the header has so many columns, excel saves blank cols in the
254
- # data below the header lines.
255
- columns = [col for col in columns if col != '' ]
256
- dtypes = dict .fromkeys (columns , float ) # all floats except datevec
257
- dtypes .update (Year = int , Month = int , Day = int , Hour = int , Minute = int )
258
- dtypes ['Cloud Type' ] = int
259
- dtypes ['Fill Flag' ] = int
260
- data = pd .read_csv (
261
- fbuf , header = None , names = columns , usecols = columns , dtype = dtypes ,
262
- delimiter = ',' , lineterminator = '\n ' ) # skip carriage returns \r
263
- # the response 1st 5 columns are a date vector, convert to datetime
264
- dtidx = pd .to_datetime (
265
- data [['Year' , 'Month' , 'Day' , 'Hour' , 'Minute' ]])
266
- # in USA all timezones are integers
267
- tz = 'Etc/GMT%+d' % - header ['Time Zone' ]
268
- data .index = pd .DatetimeIndex (dtidx ).tz_localize (tz )
269
- finally :
270
- if close :
271
- fbuf .close ()
235
+ # The first 2 lines of the response are headers with metadata
236
+ header_fields = fbuf .readline ().split (',' )
237
+ header_fields [- 1 ] = header_fields [- 1 ].strip () # strip trailing newline
238
+ header_values = fbuf .readline ().split (',' )
239
+ header_values [- 1 ] = header_values [- 1 ].strip () # strip trailing newline
240
+ header = dict (zip (header_fields , header_values ))
241
+ # the response is all strings, so set some header types to numbers
242
+ header ['Local Time Zone' ] = int (header ['Local Time Zone' ])
243
+ header ['Time Zone' ] = int (header ['Time Zone' ])
244
+ header ['Latitude' ] = float (header ['Latitude' ])
245
+ header ['Longitude' ] = float (header ['Longitude' ])
246
+ header ['Elevation' ] = int (header ['Elevation' ])
247
+ # get the column names so we can set the dtypes
248
+ columns = fbuf .readline ().split (',' )
249
+ columns [- 1 ] = columns [- 1 ].strip () # strip trailing newline
250
+ # Since the header has so many columns, excel saves blank cols in the
251
+ # data below the header lines.
252
+ columns = [col for col in columns if col != '' ]
253
+ dtypes = dict .fromkeys (columns , float ) # all floats except datevec
254
+ dtypes .update (Year = int , Month = int , Day = int , Hour = int , Minute = int )
255
+ dtypes ['Cloud Type' ] = int
256
+ dtypes ['Fill Flag' ] = int
257
+ data = pd .read_csv (
258
+ fbuf , header = None , names = columns , usecols = columns , dtype = dtypes ,
259
+ delimiter = ',' , lineterminator = '\n ' ) # skip carriage returns \r
260
+ # the response 1st 5 columns are a date vector, convert to datetime
261
+ dtidx = pd .to_datetime (
262
+ data [['Year' , 'Month' , 'Day' , 'Hour' , 'Minute' ]])
263
+ # in USA all timezones are integers
264
+ tz = 'Etc/GMT%+d' % - header ['Time Zone' ]
265
+ data .index = pd .DatetimeIndex (dtidx ).tz_localize (tz )
272
266
273
267
return header , data
0 commit comments