Power production from https://www.energy-charts.de/power/month_2015_04.json
Electricity price from http://www.pfbach.dk/
from __future__ import print_function, division
import pandas as pd, urllib, os, json
import matplotlib.pyplot as plt
import datetime as dt
data_folder = "data"
%matplotlib inline
def pad_number(number,digits):
st = str(number)
n_missing = digits - len(st)
if n_missing > 0:
return "0"*n_missing + st
else:
return st
pad_number(12,3)
years = [2015]
datas = {}
for year in years:
datas[year] = {}
for month in range(1,13):
file_name = "month_{}_{}.json".format(year,pad_number(month,2))
path_name = os.path.join(data_folder,file_name)
if not os.path.isfile(path_name):
print("Downloading",file_name)
url = "https://www.energy-charts.de/power/" + file_name
urllib.urlretrieve (url, path_name)
with open(path_name) as data_file:
datas[year][month] = json.load(data_file)
dt.datetime(1970,1,1) + dt.timedelta(seconds=datas[2015][1][0]["date"][0]/1000)
dt.datetime(1970,1,1) + dt.timedelta(seconds=(datas[2015][1][0]["values"][0][0]/1000+3600))
year = 2015
month = 2
pd.date_range(dt.datetime(year,month,1),dt.datetime(year,month+1,1)-dt.timedelta(hours=1),freq="H")
columns = [d["key"][0]["en"] for d in datas[years[0]][1]]
data = []
for year in years:
for month in range(1,13):
for i in range(len(columns)):
if datas[year][month][i]["key"][0]["en"] != columns[i]:
print("Error")
for j in range(len(datas[year][month][0]["values"])):
data.append([d["values"][j][1] for d in datas[year][month]])
index = pd.date_range("2015-01-01 00:00","2015-12-31 23:00",freq="H")
df = pd.DataFrame(data=data,
columns=columns,
index=index)
df.rename(columns={"Import Balance" : "Imports",
"Hydro Power" : "Hydro",
"Uranium" : "Nuclear"},inplace=True)
df.describe()
df.drop(["Import Balance"],axis=1).plot(kind="area")
fig,ax = plt.subplots(1,1)
fig.set_size_inches(12,6)
start = "2015-01-01"
stop = "2015-01-31"
colors = {"Brown Coal" : "brown",
"Hard Coal" : "k",
"Nuclear" : "r",
"Hydro" : "cyan",
"Wind" : "blue",
"Solar" : "yellow",
"Biomass" : "green",
"Waste" : "orange",
"Gas" : "orange"}
#reorder
cols = ["Nuclear","Brown Coal","Hard Coal","Gas","Hydro","Biomass","Wind","Solar"]
df[cols][start:stop].plot(kind="area",
ax=ax,
colors=[colors[col] for col in cols],
linewidth=4)
ax.set_ylabel("Electricity generation in Germany [GW]")
ax.set_ylim([0,100])
(-df["Imports"][start:stop]).plot(ax=ax,color="magenta",label="Exports",linewidth=3)
ax.legend(loc="upper left",ncol=5)
ax.yaxis.grid(True)
fig.tight_layout()
#fig.savefig("germany_generation.pdf")
price_df = pd.read_excel("data/2015_de_spot_price.xls",skiprows=3)
price = pd.Series(price_df[u"€/MWh"].values,pd.date_range("2015-01-01 00:00","2015-12-31 23:00",freq="H"))
price.plot(grid=True)
fig,axes = plt.subplots(2,1)
fig.set_size_inches(12,8)
ax = axes[0]
start = "2015-01-06"
stop = "2015-01-12"
colors = {"Brown Coal" : "brown",
"Hard Coal" : "k",
"Nuclear" : "r",
"Hydro" : "cyan",
"Wind" : "blue",
"Solar" : "yellow",
"Biomass" : "green",
"Waste" : "orange",
"Gas" : "orange"}
#reorder
cols = ["Nuclear","Brown Coal","Hard Coal","Gas","Hydro","Biomass","Wind","Solar"]
df[cols][start:stop].plot(kind="area",
ax=ax,
colors=[colors[col] for col in cols],
linewidth=1)
ax.set_ylabel("Electricity generation in Germany [GW]")
ax.set_ylim([0,110])
(-df["Imports"][start:stop]).plot(ax=ax,color="magenta",label="Exports",linewidth=1)
ax.legend(loc="upper left",ncol=5)
ax.grid(True)
ax = axes[1]
ax.set_ylabel(u"Spot price [€/MWh]")
price[start:stop].plot(ax=ax)
ax.grid(True)
fig.tight_layout()
fig.savefig("germany_generation-price-{}-{}.pdf".format(start,stop))