Skip to content

Commit 2dff28a

Browse files
authored
Change local_path to save_path in get_bsrn (#1282)
* Change local_path to save_path * Update test_bsrn.py * Update save_path description * Fix spelling mistake * Test that file downloaded with save_path is read correctly * Update save_path description * Save testfile in temporary directory
1 parent 403f65e commit 2dff28a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pvlib/iotools/bsrn.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _empty_dataframe_from_logical_records(logical_records):
6262

6363

6464
def get_bsrn(station, start, end, username, password,
65-
logical_records=('0100',), local_path=None):
65+
logical_records=('0100',), save_path=None):
6666
"""
6767
Retrieve ground measured irradiance data from the BSRN FTP server.
6868
@@ -87,8 +87,8 @@ def get_bsrn(station, start, end, username, password,
8787
logical_records: list or tuple, default: ('0100',)
8888
List of the logical records (LR) to parse. Options include: '0100',
8989
'0300', and '0500'.
90-
local_path: str or path-like, optional
91-
If specified, path (abs. or relative) of where to save files
90+
save_path: str or path-like, optional
91+
If specified, a directory path of where to save each monthly file.
9292
9393
Returns
9494
-------
@@ -178,10 +178,10 @@ def get_bsrn(station, start, end, username, password,
178178
# Check that transfer was successfull
179179
if not response.startswith('226 Transfer complete'):
180180
raise ftplib.Error(response)
181-
# Save file locally if local_path is specified
182-
if local_path is not None:
181+
# Save file locally if save_path is specified
182+
if save_path is not None:
183183
# Create local file
184-
with open(os.path.join(local_path, filename), 'wb') as f:
184+
with open(os.path.join(save_path, filename), 'wb') as f:
185185
f.write(bio.getbuffer()) # Write local file
186186
# Open gzip file and convert to StringIO
187187
bio.seek(0) # reset buffer to start of file

pvlib/tests/iotools/test_bsrn.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66
import pytest
77
import os
8+
import tempfile
89
from pvlib.iotools import read_bsrn, get_bsrn
910
from ..conftest import (DATA_DIR, RERUNS, RERUNS_DELAY, assert_index_equal,
1011
requires_bsrn_credentials)
@@ -78,20 +79,26 @@ def test_read_bsrn_logical_records_not_found():
7879
def test_get_bsrn(expected_index, bsrn_credentials):
7980
# Retrieve irradiance data from the BSRN FTP server
8081
# the TAM station is chosen due to its small file sizes
82+
temp_dir = tempfile.TemporaryDirectory() # create temporary directory
8183
username, password = bsrn_credentials
8284
data, metadata = get_bsrn(
8385
start=pd.Timestamp(2016, 6, 1),
8486
end=pd.Timestamp(2016, 6, 29),
8587
station='tam',
8688
username=username,
8789
password=password,
88-
local_path='')
90+
save_path=temp_dir.name)
8991
assert_index_equal(expected_index, data.index)
9092
assert 'ghi' in data.columns
9193
assert 'dni_std' in data.columns
9294
assert 'dhi_min' in data.columns
9395
assert 'lwd_max' in data.columns
9496
assert 'relative_humidity' in data.columns
97+
# test that a local file was saved and is read correctly
98+
data2, metadata2 = read_bsrn(os.path.join(temp_dir.name, 'tam0616.dat.gz'))
99+
assert_index_equal(expected_index, data2.index)
100+
assert 'ghi' in data2.columns
101+
temp_dir.cleanup() # explicitly remove temporary directory
95102

96103

97104
@requires_bsrn_credentials

0 commit comments

Comments
 (0)