-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest_midc.py
88 lines (70 loc) · 2.79 KB
/
test_midc.py
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
import pandas as pd
import pytest
from pvlib.iotools import midc
from ..conftest import DATA_DIR, RERUNS, RERUNS_DELAY
@pytest.fixture
def test_mapping():
return {
'Direct Normal [W/m^2]': 'dni',
'Global PSP [W/m^2]': 'ghi',
'Rel Humidity [%]': 'relative_humidity',
'Temperature @ 2m [deg C]': 'temp_air',
'Non Existant': 'variable',
}
MIDC_TESTFILE = DATA_DIR / 'midc_20181014.txt'
MIDC_RAW_TESTFILE = DATA_DIR / 'midc_raw_20181018.txt'
MIDC_RAW_SHORT_HEADER_TESTFILE = (
DATA_DIR / 'midc_raw_short_header_20191115.txt')
# TODO: not used, remove?
# midc_network_testfile = ('https://midcdmz.nrel.gov/apps/data_api.pl'
# '?site=UAT&begin=20181018&end=20181019')
def test_midc__format_index():
data = pd.read_csv(MIDC_TESTFILE)
data = midc._format_index(data)
start = pd.Timestamp("20181014 00:00")
start = start.tz_localize("MST")
end = pd.Timestamp("20181014 23:59")
end = end.tz_localize("MST")
assert type(data.index) == pd.DatetimeIndex
assert data.index[0] == start
assert data.index[-1] == end
def test_midc__format_index_tz_conversion():
data = pd.read_csv(MIDC_TESTFILE)
data = data.rename(columns={'MST': 'PST'})
data = midc._format_index(data)
assert str(data.index[0].tz) == 'Etc/GMT+8'
def test_midc__format_index_raw():
data = pd.read_csv(MIDC_RAW_TESTFILE)
data = midc._format_index_raw(data)
start = pd.Timestamp('20181018 00:00')
start = start.tz_localize('MST')
end = pd.Timestamp('20181018 23:59')
end = end.tz_localize('MST')
assert data.index[0] == start
assert data.index[-1] == end
def test_read_midc_var_mapping_as_arg(test_mapping):
data = midc.read_midc(MIDC_TESTFILE, variable_map=test_mapping)
assert 'ghi' in data.columns
assert 'temp_air' in data.columns
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_read_midc_raw_data_from_nrel():
start_ts = pd.Timestamp('20181018')
end_ts = pd.Timestamp('20181019')
var_map = midc.MIDC_VARIABLE_MAP['UAT']
data = midc.read_midc_raw_data_from_nrel('UAT', start_ts, end_ts, var_map)
for k, v in var_map.items():
assert v in data.columns
assert data.index.size == 2880
def test_read_midc_header_length_mismatch(mocker):
mock_data = mocker.MagicMock()
with MIDC_RAW_SHORT_HEADER_TESTFILE.open() as f:
mock_data.text = f.read()
mocker.patch('pvlib.iotools.midc.requests.get',
return_value=mock_data)
start = pd.Timestamp('2019-11-15T00:00:00-06:00')
end = pd.Timestamp('2019-11-15T23:59:00-06:00')
data = midc.read_midc_raw_data_from_nrel('', start, end)
assert isinstance(data.index, pd.DatetimeIndex)
assert data.index[0] == start
assert data.index[-1] == end