44import re
55import pandas as pd
66
7+ from pvlib .tools import _file_context_manager
8+
79# Dictionary mapping TMY3 names to pvlib names
810VARIABLE_MAP = {
911 'GHI (W/m^2)' : 'ghi' ,
2224}
2325
2426
25- def read_tmy3 (filename , coerce_year = None , map_variables = True , encoding = None ):
27+ def read_tmy3 (filename_or_obj , coerce_year = None , map_variables = True , encoding = None ):
2628 """Read a TMY3 file into a pandas dataframe.
2729
2830 Note that values contained in the metadata dictionary are unchanged
@@ -35,7 +37,7 @@ def read_tmy3(filename, coerce_year=None, map_variables=True, encoding=None):
3537
3638 Parameters
3739 ----------
38- filename : str
40+ filename_or_obj : str, Path, or file-like object
3941 A relative file path or absolute file path.
4042 coerce_year : int, optional
4143 If supplied, the year of the index will be set to ``coerce_year``, except
@@ -186,7 +188,7 @@ def read_tmy3(filename, coerce_year=None, map_variables=True, encoding=None):
186188 """ # noqa: E501
187189 head = ['USAF' , 'Name' , 'State' , 'TZ' , 'latitude' , 'longitude' , 'altitude' ]
188190
189- with open ( str ( filename ), 'r' , encoding = encoding ) as fbuf :
191+ with _file_context_manager ( filename_or_obj , mode = "r" , encoding = encoding ) as fbuf :
190192 # header information on the 1st line (0 indexing)
191193 firstline = fbuf .readline ()
192194 # use pandas to read the csv file buffer
0 commit comments