Skip to content

Commit a110a69

Browse files
committed
Allows reading TMY data from a Path or file-like object
1 parent 39867da commit a110a69

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

pvlib/iotools/tmy.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import re
55
import pandas as pd
66

7+
from pvlib.tools import _file_context_manager
8+
79
# Dictionary mapping TMY3 names to pvlib names
810
VARIABLE_MAP = {
911
'GHI (W/m^2)': 'ghi',
@@ -22,7 +24,7 @@
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

pvlib/tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def normalize_max2one(a):
562562
return res
563563

564564

565-
def _file_context_manager(filename_or_object, mode='r'):
565+
def _file_context_manager(filename_or_object, mode='r', encoding=None):
566566
"""
567567
Open a filename/path for reading, or pass a file-like object
568568
through unchanged.
@@ -584,5 +584,5 @@ def _file_context_manager(filename_or_object, mode='r'):
584584
context = contextlib.nullcontext(filename_or_object)
585585
else:
586586
# otherwise, assume a filename or path
587-
context = open(str(filename_or_object), mode=mode)
587+
context = open(str(filename_or_object), mode=mode, encoding=encoding)
588588
return context

tests/iotools/test_tmy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
def test_read_tmy3():
1919
tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)
2020

21+
def test_read_tmy3_buffer():
22+
with open(TMY3_TESTFILE) as f:
23+
tmy.read_tmy3(f, map_variables=False)
24+
2125

2226
def test_read_tmy3_norecolumn():
2327
data, _ = tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)

0 commit comments

Comments
 (0)