<jupyter_start><jupyter_text>Bioenergy consumption by feedstock 2002-2018<jupyter_code>import pandas as pd
import os
import pyxlsb
import numpy as np
from scipy.stats import linregress
import zipfile
import requests
tj_ktoe = 41.868
def get_variable(file, variable, col_start, col_end):
    """Reads variable for fuel in multiple sheets and converts 
    the value from ktoe to TJ.
    """
    trajectory = {}
    for year in range(2002,2019):
        df = pd.read_excel(
            file,
            engine='pyxlsb',
            sheet_name=str(year),
            skiprows=[0,1,2,3],
            index_col=1
            )   
        trajectory[year] = sum(df.loc[variable, col_start:col_end]) * tj_ktoe
    return pd.Series(trajectory)
url = 'https://ec.europa.eu/eurostat/documents/38154/4956218/Energy-Balances-April-2020-edition.zip/69da6e9f-bf8f-cd8e-f4ad-50b52f8ce616'

r = requests.get(url)
with open('eurostat_balances_2020.zip', 'wb') as f:
    f.write(r.content)

# Mock of bioenergy

mock = pd.DataFrame()

countries = ('CZ', 'AT', 'DK', 'NL', 'PL', 'SK')

with zipfile.ZipFile('eurostat_balances_2020.zip', 'r') as f:
    for name in f.namelist():
        if name.startswith(countries):
            data = get_variable(f.read(name), 'Gross inland consumption', 'Bioenergy', 'Bioenergy')
            mock[f'{name[:2]}'] = data

mock.to_csv(
    'mock_bioenergy_2002_2018.csv',
    index_label='Rok',
    decimal=',',
    columns=countries,
    )

mock

# Consumption of bioenergy

consumption = pd.DataFrame()

countries = ('CZ', 'AT', 'DK', 'NL', 'PL', 'SK')

for file in os.listdir('Energy-Balances-June-2020-edition'):
    if file.startswith(countries):
        data = get_variable('Energy-Balances-June-2020-edition/' + file, 'Gross inland consumption', 'Bioenergy', 'Bioenergy')
        consumption[f'{file[:2]}'] = data

consumption.to_csv(
    'consumption_bioenergy_2002_2018.csv',
    index_label='Rok',
    decimal=',',
    columns=countries,
    )

consumption
result = linregress(consumption.index, consumption['CZ'])
result
import matplotlib.pyplot as plt

plt.plot(consumption.index, consumption['CZ'], 'o')

fx = np.array([consumption.index.min(), consumption.index.max()])
fy = result.intercept + result.slope * fx
plt.plot(fx, fy, '-')

plt.title(f'Spotřeba bioenergie, pvalue={round(result.pvalue, 3)}')
plt.ylabel('Spotřeva v TJ')
plt.xlabel('Rok')
plt.show()
#  Share of bioenergy on consumption

share = pd.DataFrame()

countries = ('CZ', 'AT', 'DK', 'NL', 'PL', 'SK')

for file in os.listdir('Energy-Balances-June-2020-edition'):
    if file.startswith(countries):
        bioenergy = get_variable('Energy-Balances-June-2020-edition/' + file, 'Gross inland consumption', 'Bioenergy', 'Bioenergy')
        consumption = get_variable('Energy-Balances-June-2020-edition/' + file, 'Gross inland consumption', 'Total', 'Total')
        share[f'{file[:2]}'] = bioenergy / consumption

share.to_csv(
    'share_bioenergy_2002_2018.csv',
    index_label='Rok',
    decimal=',',
    columns=countries,
    )

share
#  Share of bioenergy on RES consumption

share_res = pd.DataFrame()

countries = ('CZ', 'AT', 'DK', 'NL', 'PL', 'SK')

for file in os.listdir('Energy-Balances-June-2020-edition'):
    if file.startswith(countries):
        bioenergy = get_variable('Energy-Balances-June-2020-edition/' + file, 'Gross inland consumption', 'Bioenergy', 'Bioenergy')
        res = get_variable('Energy-Balances-June-2020-edition/' + file, 'Gross inland consumption', 'Renewables and biofuels', 'Renewables and biofuels')
        share_res[f'{file[:2]}'] = bioenergy / res

share_res.to_csv(
    'share_res_bioenergy_2002_2018.csv',
    index_label='Rok',
    decimal=',',
    columns=countries,
    )

share_res
share_res<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15221426677273933, 'fraction_numerical': 0.03553434102360117, 'mean_word_length': 3.898701298701299, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '283969', 'license': 'MIT', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1347, 'n_words': 271, 'path': '2020/selection/old/consumption.ipynb', 'repo_name': 'jandolezal/balances', 'size': '3771'}