-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasource.py
78 lines (62 loc) · 2.53 KB
/
datasource.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
import pandas as pd
from datetime import datetime
import temperatures
import co2intensity
import solar_heat
import pgeocode
import os
import requests
from joblib import Memory
memory = Memory("cache", verbose=0)
@memory.cache
def geopos_from_zipcode(country_code, zip_code):
nomi = pgeocode.Nominatim(country_code)
response_df = nomi.query_postal_code(str(zip_code))
if not response_df.empty:
return response_df["latitude"], response_df["longitude"]
return None, None
def load_TRY(selection: str):
"""the selection parameter should be one of : ["Wint", "Jahr", "Somm"]"""
if selection is not None:
if selection not in ["Wint", "Jahr", "Somm"]:
raise ValueError("TRY_dataset must be one of ['Wint', 'Jahr', 'Somm']")
filename = (
f"data/weather/DWD/TRY_488641091042/TRY2045_488641091042_{selection}.dat"
)
trydf = pd.read_csv(filename, delim_whitespace=True, skiprows=34).dropna()
trydf["year"] = 2045
trydf["Date"] = pd.to_datetime(
dict(
year=trydf["year"], month=trydf["MM"], day=trydf["DD"], hour=trydf["HH"]
)
)
trydf = trydf.rename(columns={"t": "T_outside [°C]"})
trydf = trydf.set_index("Date")
return trydf
return None
def fetch_all(country_code, zip_code, start, end, TRY_dataset=None) -> pd.DataFrame:
if start and end and zip_code:
if isinstance(start, str):
start = datetime.fromisoformat(start)
if isinstance(end, str):
end = datetime.fromisoformat(end)
tmpdf = co2intensity.load_all()
lat, lon = geopos_from_zipcode(country_code, zip_code)
if isinstance(TRY_dataset, str):
# TODO: All the years of other data should be shifted to 2045
df = temperatures.load_TRY(TRY_dataset).rename(
columns={"T": "T_outside [°C]", "G": "solar [W/m2]"}
)
delta = df.index[0] - pd.to_datetime(start)
delta = pd.Timedelta(delta.year, 1, 1)
else:
df = temperatures.fetch_all(lat, lon, start, end)
df_sol = solar_heat.fetch_all(lat, lon, start, end)
df["p_solar south [kW/m2]"] = df_sol["p_solar south [kW/m2]"]
df["p_solar east [kW/m2]"] = df_sol["p_solar east [kW/m2]"]
df["p_solar west [kW/m2]"] = df_sol["p_solar west [kW/m2]"]
df = df.join(tmpdf, how="inner")
return df
if __name__ == "__main__":
print(fetch_all("DE", 81829, "2020-01-01", "2022-01-02"))
print(fetch_all("DE", 81829, "2020-01-01", "2022-01-02", "Somm"))