|
2 | 2 | tests for :mod:`pvlib.iotools.bsrn`
|
3 | 3 | """
|
4 | 4 |
|
5 |
| - |
6 | 5 | import pandas as pd
|
7 | 6 | import pytest
|
| 7 | +import os |
| 8 | +from pvlib.iotools import read_bsrn, get_bsrn |
| 9 | +from ..conftest import (DATA_DIR, RERUNS, RERUNS_DELAY, assert_index_equal, |
| 10 | + requires_bsrn_credentials) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="module") |
| 14 | +def bsrn_credentials(): |
| 15 | + """Supplies the BSRN FTP credentials for testing purposes. |
8 | 16 |
|
9 |
| -from pvlib.iotools import bsrn |
10 |
| -from ..conftest import DATA_DIR, assert_index_equal |
| 17 | + Users should obtain their own credentials as described in the `read_bsrn` |
| 18 | + documentation.""" |
| 19 | + bsrn_username = os.environ["BSRN_FTP_USERNAME"] |
| 20 | + bsrn_password = os.environ["BSRN_FTP_PASSWORD"] |
| 21 | + return bsrn_username, bsrn_password |
11 | 22 |
|
12 | 23 |
|
13 |
| -@pytest.mark.parametrize('testfile,expected_index', [ |
14 |
| - ('bsrn-pay0616.dat.gz', |
15 |
| - pd.date_range(start='20160601', periods=43200, freq='1min', tz='UTC')), |
16 |
| - ('bsrn-lr0100-pay0616.dat', |
17 |
| - pd.date_range(start='20160601', periods=43200, freq='1min', tz='UTC')), |
| 24 | +@pytest.fixture |
| 25 | +def expected_index(): |
| 26 | + return pd.date_range(start='20160601', periods=43200, freq='1min', |
| 27 | + tz='UTC') |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize('testfile', [ |
| 31 | + ('bsrn-pay0616.dat.gz'), |
| 32 | + ('bsrn-lr0100-pay0616.dat'), |
18 | 33 | ])
|
19 | 34 | def test_read_bsrn(testfile, expected_index):
|
20 |
| - data = bsrn.read_bsrn(DATA_DIR / testfile) |
| 35 | + data, metadata = read_bsrn(DATA_DIR / testfile) |
| 36 | + assert_index_equal(expected_index, data.index) |
| 37 | + assert 'ghi' in data.columns |
| 38 | + assert 'dni_std' in data.columns |
| 39 | + assert 'dhi_min' in data.columns |
| 40 | + assert 'lwd_max' in data.columns |
| 41 | + assert 'relative_humidity' in data.columns |
| 42 | + |
| 43 | + |
| 44 | +def test_read_bsrn_logical_records(expected_index): |
| 45 | + # Test if logical records 0300 and 0500 are correct parsed |
| 46 | + # and that 0100 is not passed when not specified |
| 47 | + data, metadata = read_bsrn(DATA_DIR / 'bsrn-pay0616.dat.gz', |
| 48 | + logical_records=['0300', '0500']) |
| 49 | + assert_index_equal(expected_index, data.index) |
| 50 | + assert 'lwu' in data.columns |
| 51 | + assert 'uva_global' in data.columns |
| 52 | + assert 'uvb_reflected_std' in data.columns |
| 53 | + assert 'ghi' not in data.columns |
| 54 | + |
| 55 | + |
| 56 | +def test_read_bsrn_bad_logical_record(): |
| 57 | + # Test if ValueError is raised if an unsupported logical record is passed |
| 58 | + with pytest.raises(ValueError, match='not in'): |
| 59 | + read_bsrn(DATA_DIR / 'bsrn-lr0100-pay0616.dat', |
| 60 | + logical_records=['dummy']) |
| 61 | + |
| 62 | + |
| 63 | +def test_read_bsrn_logical_records_not_found(): |
| 64 | + # Test if an empty dataframe is returned if specified LRs are not present |
| 65 | + data, metadata = read_bsrn(DATA_DIR / 'bsrn-lr0100-pay0616.dat', |
| 66 | + logical_records=['0300', '0500']) |
| 67 | + assert data.empty # assert that the dataframe is empty |
| 68 | + assert 'uva_global' in data.columns |
| 69 | + assert 'uvb_reflected_std' in data.columns |
| 70 | + assert 'uva_global_max' in data.columns |
| 71 | + assert 'dni' not in data.columns |
| 72 | + assert 'day' not in data.columns |
| 73 | + |
| 74 | + |
| 75 | +@requires_bsrn_credentials |
| 76 | +@pytest.mark.remote_data |
| 77 | +@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) |
| 78 | +def test_get_bsrn(expected_index, bsrn_credentials): |
| 79 | + # Retrieve irradiance data from the BSRN FTP server |
| 80 | + # the TAM station is chosen due to its small file sizes |
| 81 | + username, password = bsrn_credentials |
| 82 | + data, metadata = get_bsrn( |
| 83 | + start=pd.Timestamp(2016, 6, 1), |
| 84 | + end=pd.Timestamp(2016, 6, 29), |
| 85 | + station='tam', |
| 86 | + username=username, |
| 87 | + password=password, |
| 88 | + local_path='') |
21 | 89 | assert_index_equal(expected_index, data.index)
|
22 | 90 | assert 'ghi' in data.columns
|
23 | 91 | assert 'dni_std' in data.columns
|
24 | 92 | assert 'dhi_min' in data.columns
|
25 | 93 | assert 'lwd_max' in data.columns
|
26 | 94 | assert 'relative_humidity' in data.columns
|
| 95 | + |
| 96 | + |
| 97 | +@requires_bsrn_credentials |
| 98 | +@pytest.mark.remote_data |
| 99 | +@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) |
| 100 | +def test_get_bsrn_bad_station(bsrn_credentials): |
| 101 | + # Test if KeyError is raised if a bad station name is passed |
| 102 | + username, password = bsrn_credentials |
| 103 | + with pytest.raises(KeyError, match='sub-directory does not exist'): |
| 104 | + get_bsrn( |
| 105 | + start=pd.Timestamp(2016, 6, 1), |
| 106 | + end=pd.Timestamp(2016, 6, 29), |
| 107 | + station='not_a_station_name', |
| 108 | + username=username, |
| 109 | + password=password) |
| 110 | + |
| 111 | + |
| 112 | +@requires_bsrn_credentials |
| 113 | +@pytest.mark.remote_data |
| 114 | +@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) |
| 115 | +def test_get_bsrn_no_files(bsrn_credentials): |
| 116 | + username, password = bsrn_credentials |
| 117 | + # Test if Warning is given if no files are found for the entire time frame |
| 118 | + with pytest.warns(UserWarning, match='No files'): |
| 119 | + get_bsrn( |
| 120 | + start=pd.Timestamp(1990, 6, 1), |
| 121 | + end=pd.Timestamp(1990, 6, 29), |
| 122 | + station='tam', |
| 123 | + username=username, |
| 124 | + password=password) |
0 commit comments