diff --git a/pvlib/iotools/bsrn.py b/pvlib/iotools/bsrn.py index 4c02c10f90..b9292b41ae 100644 --- a/pvlib/iotools/bsrn.py +++ b/pvlib/iotools/bsrn.py @@ -62,7 +62,7 @@ def _empty_dataframe_from_logical_records(logical_records): def get_bsrn(station, start, end, username, password, - logical_records=('0100',), local_path=None): + logical_records=('0100',), save_path=None): """ Retrieve ground measured irradiance data from the BSRN FTP server. @@ -87,8 +87,8 @@ def get_bsrn(station, start, end, username, password, logical_records: list or tuple, default: ('0100',) List of the logical records (LR) to parse. Options include: '0100', '0300', and '0500'. - local_path: str or path-like, optional - If specified, path (abs. or relative) of where to save files + save_path: str or path-like, optional + If specified, a directory path of where to save each monthly file. Returns ------- @@ -178,10 +178,10 @@ def get_bsrn(station, start, end, username, password, # Check that transfer was successfull if not response.startswith('226 Transfer complete'): raise ftplib.Error(response) - # Save file locally if local_path is specified - if local_path is not None: + # Save file locally if save_path is specified + if save_path is not None: # Create local file - with open(os.path.join(local_path, filename), 'wb') as f: + with open(os.path.join(save_path, filename), 'wb') as f: f.write(bio.getbuffer()) # Write local file # Open gzip file and convert to StringIO bio.seek(0) # reset buffer to start of file diff --git a/pvlib/tests/iotools/test_bsrn.py b/pvlib/tests/iotools/test_bsrn.py index 412cbd5e8f..815b5d84c9 100644 --- a/pvlib/tests/iotools/test_bsrn.py +++ b/pvlib/tests/iotools/test_bsrn.py @@ -5,6 +5,7 @@ import pandas as pd import pytest import os +import tempfile from pvlib.iotools import read_bsrn, get_bsrn from ..conftest import (DATA_DIR, RERUNS, RERUNS_DELAY, assert_index_equal, requires_bsrn_credentials) @@ -78,6 +79,7 @@ def test_read_bsrn_logical_records_not_found(): def test_get_bsrn(expected_index, bsrn_credentials): # Retrieve irradiance data from the BSRN FTP server # the TAM station is chosen due to its small file sizes + temp_dir = tempfile.TemporaryDirectory() # create temporary directory username, password = bsrn_credentials data, metadata = get_bsrn( start=pd.Timestamp(2016, 6, 1), @@ -85,13 +87,18 @@ def test_get_bsrn(expected_index, bsrn_credentials): station='tam', username=username, password=password, - local_path='') + save_path=temp_dir.name) assert_index_equal(expected_index, data.index) assert 'ghi' in data.columns assert 'dni_std' in data.columns assert 'dhi_min' in data.columns assert 'lwd_max' in data.columns assert 'relative_humidity' in data.columns + # test that a local file was saved and is read correctly + data2, metadata2 = read_bsrn(os.path.join(temp_dir.name, 'tam0616.dat.gz')) + assert_index_equal(expected_index, data2.index) + assert 'ghi' in data2.columns + temp_dir.cleanup() # explicitly remove temporary directory @requires_bsrn_credentials