24
24
}
25
25
26
26
27
- def read_tmy3 (filename , coerce_year = None , map_variables = None , recolumn = None ):
27
+ def read_tmy3 (filename , coerce_year = None , map_variables = None , recolumn = None ,
28
+ encoding = None ):
28
29
"""Read a TMY3 file into a pandas dataframe.
29
30
30
31
Note that values contained in the metadata dictionary are unchanged
@@ -50,6 +51,11 @@ def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=None):
50
51
If ``True``, apply standard names to TMY3 columns. Typically this
51
52
results in stripping the units from the column name.
52
53
Cannot be used in combination with ``map_variables``.
54
+ encoding : str, optional
55
+ Encoding of the file. For files that contain non-UTF8 characters it may
56
+ be necessary to specify an alternative encoding, e.g., for
57
+ SolarAnywhere TMY3 files the encoding should be 'iso-8859-1'. Users
58
+ may also consider using the 'utf-8-sig' encoding.
53
59
54
60
Returns
55
61
-------
@@ -58,7 +64,7 @@ def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=None):
58
64
data : DataFrame
59
65
A pandas dataframe with the columns described in the table
60
66
below. For more detailed descriptions of each component, please
61
- consult the TMY3 User's Manual ( [1]_) , especially tables 1-1
67
+ consult the TMY3 User's Manual [1]_, especially tables 1-1
62
68
through 1-6.
63
69
64
70
metadata : dict
@@ -187,14 +193,12 @@ def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=None):
187
193
""" # noqa: E501
188
194
head = ['USAF' , 'Name' , 'State' , 'TZ' , 'latitude' , 'longitude' , 'altitude' ]
189
195
190
- try :
191
- with open (str (filename ), 'r' ) as fbuf :
192
- firstline , data = _parse_tmy3 (fbuf )
193
- # SolarAnywhere files contain non-UTF8 characters and may require
194
- # encoding='iso-8859-1' in order to be parsed
195
- except UnicodeDecodeError :
196
- with open (str (filename ), 'r' , encoding = 'iso-8859-1' ) as fbuf :
197
- firstline , data = _parse_tmy3 (fbuf )
196
+ with open (str (filename ), 'r' , encoding = encoding ) as fbuf :
197
+ # header information on the 1st line (0 indexing)
198
+ firstline = fbuf .readline ()
199
+ # use pandas to read the csv file buffer
200
+ # header is actually the second line, but tell pandas to look for
201
+ data = pd .read_csv (fbuf , header = 0 )
198
202
199
203
meta = dict (zip (head , firstline .rstrip ('\n ' ).split ("," )))
200
204
# convert metadata strings to numeric types
@@ -206,8 +210,10 @@ def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=None):
206
210
207
211
# get the date column as a pd.Series of numpy datetime64
208
212
data_ymd = pd .to_datetime (data ['Date (MM/DD/YYYY)' ], format = '%m/%d/%Y' )
213
+ # extract minutes
214
+ minutes = data ['Time (HH:MM)' ].str .split (':' ).str [1 ].astype (int )
209
215
# shift the time column so that midnite is 00:00 instead of 24:00
210
- shifted_hour = data ['Time (HH:MM)' ].str [: 2 ].astype (int ) % 24
216
+ shifted_hour = data ['Time (HH:MM)' ].str . split ( ':' ). str [ 0 ].astype (int ) % 24
211
217
# shift the dates at midnight (24:00) so they correspond to the next day.
212
218
# If midnight is specified as 00:00 do not shift date.
213
219
data_ymd [data ['Time (HH:MM)' ].str [:2 ] == '24' ] += datetime .timedelta (days = 1 ) # noqa: E501
@@ -225,7 +231,8 @@ def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=None):
225
231
data_ymd .iloc [- 1 ] = data_ymd .iloc [- 1 ].replace (year = coerce_year + 1 )
226
232
# NOTE: as of pvlib-0.6.3, min req is pandas-0.18.1, so pd.to_timedelta
227
233
# unit must be in (D,h,m,s,ms,us,ns), but pandas>=0.24 allows unit='hour'
228
- data .index = data_ymd + pd .to_timedelta (shifted_hour , unit = 'h' )
234
+ data .index = data_ymd + pd .to_timedelta (shifted_hour , unit = 'h' ) \
235
+ + pd .to_timedelta (minutes , unit = 'min' )
229
236
# shouldnt' specify both recolumn and map_variables
230
237
if recolumn is not None and map_variables is not None :
231
238
msg = "`map_variables` and `recolumn` cannot both be specified"
@@ -252,15 +259,6 @@ def read_tmy3(filename, coerce_year=None, map_variables=None, recolumn=None):
252
259
return data , meta
253
260
254
261
255
- def _parse_tmy3 (fbuf ):
256
- # header information on the 1st line (0 indexing)
257
- firstline = fbuf .readline ()
258
- # use pandas to read the csv file buffer
259
- # header is actually the second line, but tell pandas to look for
260
- data = pd .read_csv (fbuf , header = 0 )
261
- return firstline , data
262
-
263
-
264
262
def _recolumn (tmy3_dataframe ):
265
263
"""
266
264
Rename the columns of the TMY3 DataFrame.
@@ -328,7 +326,7 @@ def read_tmy2(filename):
328
326
data : DataFrame
329
327
A dataframe with the columns described in the table below. For a
330
328
more detailed descriptions of each component, please consult the
331
- TMY2 User's Manual ( [1]_) , especially tables 3-1 through 3-6, and
329
+ TMY2 User's Manual [1]_, especially tables 3-1 through 3-6, and
332
330
Appendix B.
333
331
334
332
metadata : dict
@@ -430,6 +428,7 @@ def read_tmy2(filename):
430
428
----------
431
429
.. [1] Marion, W and Urban, K. "Wilcox, S and Marion, W. "User's Manual
432
430
for TMY2s". NREL 1995.
431
+ :doi:`10.2172/87130`
433
432
""" # noqa: E501
434
433
# paste in the column info as one long line
435
434
string = '%2d%2d%2d%2d%4d%4d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%4d%1s%1d%2d%1s%1d%2d%1s%1d%4d%1s%1d%4d%1s%1d%3d%1s%1d%4d%1s%1d%3d%1s%1d%3d%1s%1d%4d%1s%1d%5d%1s%1d%10d%3d%1s%1d%3d%1s%1d%3d%1s%1d%2d%1s%1d' # noqa: E501
0 commit comments