-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathsave_pvlib_module_inverter_database.py
139 lines (127 loc) · 4.82 KB
/
save_pvlib_module_inverter_database.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
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
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
"""
This is a script for saving the database to be used by PVLib for
modules and inverters models. This was necessary to keep the
database up to date with the latest database version from SAM
while updating the out-dated original database from PVLib.
This script uses the tabulate package: pip install tabulate
"""
import bz2
import pathlib
import pickle as cPickle
import pandas as pd
import pvlib
from tabulate import tabulate
from emhass.utils import get_logger, get_root
# the root folder
root = pathlib.Path(str(get_root(__file__, num_parent=2)))
emhass_conf = {}
emhass_conf["data_path"] = root / "src/emhass/data/" # NOTE CUSTOM DATA PATH
emhass_conf["root_path"] = root / "src/emhass/"
emhass_conf["docs_path"] = root / "docs/"
emhass_conf["config_path"] = root / "config.json"
emhass_conf["defaults_path"] = emhass_conf["root_path"] / "data/config_defaults.json"
emhass_conf["associations_path"] = emhass_conf["root_path"] / "data/associations.csv"
# create logger
logger, ch = get_logger(__name__, emhass_conf, save_to_file=False)
if __name__ == "__main__":
save_new_files = True
logger.info("Reading original outdated database from PVLib")
cec_modules_0 = pvlib.pvsystem.retrieve_sam("CECMod")
cec_inverters_0 = pvlib.pvsystem.retrieve_sam("cecinverter")
logger.info("Reading the downloaded database from SAM")
cec_modules = pvlib.pvsystem.retrieve_sam(
path=str(emhass_conf["data_path"] / "CEC Modules.csv")
)
cec_modules = cec_modules.loc[
:, ~cec_modules.columns.duplicated()
] # Drop column duplicates
cec_inverters = pvlib.pvsystem.retrieve_sam(
path=str(emhass_conf["data_path"] / "CEC Inverters.csv")
)
cec_inverters = cec_inverters.loc[
:, ~cec_inverters.columns.duplicated()
] # Drop column duplicates
logger.info("Reading custom EMHASS database")
cec_modules_emhass = pvlib.pvsystem.retrieve_sam(
path=str(emhass_conf["data_path"] / "emhass_modules.csv")
)
cec_inverters_emhass = pvlib.pvsystem.retrieve_sam(
path=str(emhass_conf["data_path"] / "emhass_inverters.csv")
)
strait_str = "================="
logger.info(strait_str)
logger.info(strait_str)
logger.info("Updating and saving databases")
# Modules
cols_to_keep_modules = [
elem
for elem in list(cec_modules_0.columns)
if elem not in list(cec_modules.columns)
]
cec_modules = pd.concat([cec_modules, cec_modules_0[cols_to_keep_modules]], axis=1)
logger.info(
f"Number of elements from old database copied in new database for modules = {len(cols_to_keep_modules)}:"
)
cols_to_keep_modules = [
elem
for elem in list(cec_modules_emhass.columns)
if elem not in list(cec_modules.columns)
]
cec_modules = pd.concat(
[cec_modules, cec_modules_emhass[cols_to_keep_modules]], axis=1
)
logger.info(
f"Number of elements from custom EMHASS database copied in new database for modules = {len(cols_to_keep_modules)}:"
)
print(
tabulate(
cec_modules_emhass[cols_to_keep_modules].head(20).iloc[:, :5],
headers="keys",
tablefmt="psql",
)
)
logger.info(strait_str)
logger.info(strait_str)
# Inverters
cols_to_keep_inverters = [
elem
for elem in list(cec_inverters_0.columns)
if elem not in list(cec_inverters.columns)
]
cec_inverters = pd.concat(
[cec_inverters, cec_inverters_0[cols_to_keep_inverters]], axis=1
)
logger.info(
f"Number of elements from old database copied in new database for inverters = {len(cols_to_keep_inverters)}"
)
cols_to_keep_inverters = [
elem
for elem in list(cec_inverters_emhass.columns)
if elem not in list(cec_inverters.columns)
]
cec_inverters = pd.concat(
[cec_inverters, cec_inverters_emhass[cols_to_keep_inverters]], axis=1
)
logger.info(
f"Number of elements from custom EMHASS database copied in new database for inverters = {len(cols_to_keep_inverters)}"
)
print(
tabulate(
cec_inverters_emhass[cols_to_keep_inverters].head(20).iloc[:, :5],
headers="keys",
tablefmt="psql",
)
)
logger.info(strait_str)
logger.info(strait_str)
logger.info("Modules databases")
print(tabulate(cec_modules.head(20).iloc[:, :5], headers="keys", tablefmt="psql"))
logger.info("Inverters databases")
print(tabulate(cec_inverters.head(20).iloc[:, :3], headers="keys", tablefmt="psql"))
if save_new_files:
with bz2.BZ2File(emhass_conf["data_path"] / "cec_modules.pbz2", "w") as f:
cPickle.dump(cec_modules, f)
if save_new_files:
with bz2.BZ2File(emhass_conf["data_path"] / "cec_inverters.pbz2", "w") as f:
cPickle.dump(cec_inverters, f)