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