Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pvlib/iotools/tmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import re
import pandas as pd

from pvlib.tools import _file_context_manager

# Dictionary mapping TMY3 names to pvlib names
VARIABLE_MAP = {
'GHI (W/m^2)': 'ghi',
Expand All @@ -22,7 +24,7 @@
}


def read_tmy3(filename, coerce_year=None, map_variables=True, encoding=None):
def read_tmy3(filename_or_obj, coerce_year=None, map_variables=True, encoding=None):

Check failure on line 27 in pvlib/iotools/tmy.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (84 > 79 characters)
"""Read a TMY3 file into a pandas dataframe.
Note that values contained in the metadata dictionary are unchanged
Expand All @@ -35,7 +37,7 @@
Parameters
----------
filename : str
filename_or_obj : str, Path, or file-like object
A relative file path or absolute file path.
coerce_year : int, optional
If supplied, the year of the index will be set to ``coerce_year``, except
Expand Down Expand Up @@ -186,7 +188,7 @@
""" # noqa: E501
head = ['USAF', 'Name', 'State', 'TZ', 'latitude', 'longitude', 'altitude']

with open(str(filename), 'r', encoding=encoding) as fbuf:
with _file_context_manager(filename_or_obj, mode="r", encoding=encoding) as fbuf:

Check failure on line 191 in pvlib/iotools/tmy.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (85 > 79 characters)
# header information on the 1st line (0 indexing)
firstline = fbuf.readline()
# use pandas to read the csv file buffer
Expand Down
4 changes: 2 additions & 2 deletions pvlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def normalize_max2one(a):
return res


def _file_context_manager(filename_or_object, mode='r'):
def _file_context_manager(filename_or_object, mode='r', encoding=None):
"""
Open a filename/path for reading, or pass a file-like object
through unchanged.
Expand All @@ -584,5 +584,5 @@ def _file_context_manager(filename_or_object, mode='r'):
context = contextlib.nullcontext(filename_or_object)
else:
# otherwise, assume a filename or path
context = open(str(filename_or_object), mode=mode)
context = open(str(filename_or_object), mode=mode, encoding=encoding)
return context
4 changes: 4 additions & 0 deletions tests/iotools/test_tmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
def test_read_tmy3():
tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)

def test_read_tmy3_buffer():

Check failure on line 21 in tests/iotools/test_tmy.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E302 expected 2 blank lines, found 1
with open(TMY3_TESTFILE) as f:
tmy.read_tmy3(f, map_variables=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tmy.read_tmy3(f, map_variables=False)
tmy.read_tmy3(f, map_variables=False)
assert 'GHI source' in data.columns

I suggest adding a simple assertion to make sure that the dataframe actually contains something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @AdamRJensen (maybe I should've read the test just below 😆). I've added it in 970688e, in addition to a check for the parsed data being 8760 rows, which I believe is correct for TMY3.



def test_read_tmy3_norecolumn():
data, _ = tmy.read_tmy3(TMY3_TESTFILE, map_variables=False)
Expand Down
Loading