Skip to content

Commit 15d0911

Browse files
authored
Deprecate parse_bsrn (#2466)
* deprecate parse_bsrn * tests * whatsnew * fixes * lint
1 parent 389e3d1 commit 15d0911

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ Breaking Changes
1717
Deprecations
1818
~~~~~~~~~~~~
1919
* The following ``parse_`` functions in :py:mod:`pvlib.iotools` are deprecated,
20-
with the corresponding ``read_`` functions taking their place: (:issue:`2444`, :pull:`2458`)
20+
with the corresponding ``read_`` functions taking their place: (:issue:`2444`, :pull:`2458`, :pull:`2466`)
2121

2222
- :py:func:`~pvlib.iotools.parse_psm3`
2323
- :py:func:`~pvlib.iotools.parse_cams`
24+
- :py:func:`~pvlib.iotools.parse_bsrn`
2425

2526
* The ``server`` parameter in :py:func:`~pvlib.iotools.get_cams` has been renamed
2627
to ``url`` to be consistent with the other iotools.

pvlib/iotools/bsrn.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import io
1010
import os
1111

12+
from pvlib.tools import _file_context_manager
13+
from pvlib._deprecation import deprecated
14+
1215
BSRN_FTP_URL = "ftp.bsrn.awi.de"
1316

1417
BSRN_LR0100_COL_SPECS = [(0, 3), (4, 9), (10, 16), (16, 22), (22, 27),
@@ -136,7 +139,7 @@ def get_bsrn(station, start, end, username, password,
136139
137140
See Also
138141
--------
139-
pvlib.iotools.read_bsrn, pvlib.iotools.parse_bsrn
142+
pvlib.iotools.read_bsrn
140143
141144
References
142145
----------
@@ -191,7 +194,7 @@ def get_bsrn(station, start, end, username, password,
191194
bio.seek(0) # reset buffer to start of file
192195
gzip_file = io.TextIOWrapper(gzip.GzipFile(fileobj=bio),
193196
encoding='latin1')
194-
dfi, metadata = parse_bsrn(gzip_file, logical_records)
197+
dfi, metadata = _parse_bsrn(gzip_file, logical_records)
195198
dfs.append(dfi)
196199
# FTP client raises an error if the file does not exist on server
197200
except ftplib.error_perm as e:
@@ -217,7 +220,7 @@ def get_bsrn(station, start, end, username, password,
217220
return data, metadata
218221

219222

220-
def parse_bsrn(fbuf, logical_records=('0100',)):
223+
def _parse_bsrn(fbuf, logical_records=('0100',)):
221224
"""
222225
Parse a file-like buffer of a BSRN station-to-archive file.
223226
@@ -382,7 +385,7 @@ def read_bsrn(filename, logical_records=('0100',)):
382385
Parameters
383386
----------
384387
filename: str or path-like
385-
Name or path of a BSRN station-to-archive data file
388+
Name, path, or in-memory buffer of a BSRN station-to-archive data file
386389
logical_records: list or tuple, default: ('0100',)
387390
List of the logical records (LR) to parse. Options include: '0100',
388391
'0300', and '0500'.
@@ -439,7 +442,7 @@ def read_bsrn(filename, logical_records=('0100',)):
439442
440443
See Also
441444
--------
442-
pvlib.iotools.parse_bsrn, pvlib.iotools.get_bsrn
445+
pvlib.iotools.get_bsrn
443446
444447
References
445448
----------
@@ -457,7 +460,11 @@ def read_bsrn(filename, logical_records=('0100',)):
457460
if str(filename).endswith('.gz'): # check if file is a gzipped (.gz) file
458461
open_func, mode = gzip.open, 'rt'
459462
else:
460-
open_func, mode = open, 'r'
463+
open_func, mode = _file_context_manager, 'r'
461464
with open_func(filename, mode) as f:
462-
content = parse_bsrn(f, logical_records)
465+
content = _parse_bsrn(f, logical_records)
463466
return content
467+
468+
469+
parse_bsrn = deprecated(since="0.13.0", name="parse_bsrn",
470+
alternative="read_bsrn")(read_bsrn)

tests/iotools/test_bsrn.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
import os
88
import tempfile
9-
from pvlib.iotools import read_bsrn, get_bsrn
9+
from pvlib.iotools import read_bsrn, get_bsrn, parse_bsrn
1010
from tests.conftest import (
1111
TESTS_DATA_DIR,
1212
RERUNS,
@@ -15,6 +15,8 @@
1515
requires_bsrn_credentials,
1616
)
1717

18+
from pvlib._deprecation import pvlibDeprecationWarning
19+
1820

1921
@pytest.fixture(scope="module")
2022
def bsrn_credentials():
@@ -33,6 +35,12 @@ def expected_index():
3335
tz='UTC')
3436

3537

38+
def test_parse_bsrn_deprecated():
39+
with pytest.warns(pvlibDeprecationWarning, match='Use read_bsrn instead'):
40+
with open(TESTS_DATA_DIR / 'bsrn-lr0100-pay0616.dat') as fbuf:
41+
data, metadata = parse_bsrn(fbuf)
42+
43+
3644
@pytest.mark.parametrize('testfile', [
3745
('bsrn-pay0616.dat.gz'),
3846
('bsrn-lr0100-pay0616.dat'),
@@ -47,6 +55,17 @@ def test_read_bsrn(testfile, expected_index):
4755
assert 'relative_humidity' in data.columns
4856

4957

58+
def test_read_bsrn_buffer(expected_index):
59+
with open(TESTS_DATA_DIR / 'bsrn-lr0100-pay0616.dat') as fbuf:
60+
data, metadata = read_bsrn(fbuf)
61+
assert_index_equal(expected_index, data.index)
62+
assert 'ghi' in data.columns
63+
assert 'dni_std' in data.columns
64+
assert 'dhi_min' in data.columns
65+
assert 'lwd_max' in data.columns
66+
assert 'relative_humidity' in data.columns
67+
68+
5069
def test_read_bsrn_logical_records(expected_index):
5170
# Test if logical records 0300 and 0500 are correct parsed
5271
# and that 0100 is not passed when not specified

0 commit comments

Comments
 (0)