Skip to content

Commit 2aa17b4

Browse files
authored
Allows reading TMY data from a Path or file-like object (#2544)
* Allows reading TMY data from a Path or file-like object * Reverts "filename" parameter rename for consistency * Fixes linter issue in tests * Updates "What's New" docs for change * Adding a couple of more assertions for TMY3 parsing test
1 parent 816104e commit 2aa17b4

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

docs/sphinx/source/whatsnew/v0.13.1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Enhancements
2929
(:pull:`2500`)
3030
* :py:func:`pvlib.spectrum.spectral_factor_firstsolar` no longer emits warnings
3131
when airmass and precipitable water values fall out of range. (:pull:`2512`)
32+
* Allows reading TMY data from a Path or file-like object in :py:func:`~pvlib.iotools.read_tmy3`.
33+
(:pull:`2544`, :ghuser:`jerluc`)
3234

3335
Documentation
3436
~~~~~~~~~~~~~

pvlib/iotools/tmy.py

Lines changed: 4 additions & 2 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',
@@ -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 : 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, 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ def test_read_tmy3():
1919
tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)
2020

2121

22+
def test_read_tmy3_buffer():
23+
with open(TMY3_TESTFILE) as f:
24+
data, _ = tmy.read_tmy3(f, map_variables=False)
25+
assert 'GHI source' in data.columns
26+
assert len(data) == 8760
27+
28+
2229
def test_read_tmy3_norecolumn():
2330
data, _ = tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)
2431
assert 'GHI source' in data.columns

0 commit comments

Comments
 (0)