__copyright__ = "Zentrum für nachhaltige Energiesysteme Flensburg"
__license__   = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__       = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__    = "wolfbunke"

"""
Explanation: <img src="http://193.175.187.164/static/OEP_logo_2_no_text.svg" alt="OpenEnergy Platform" height="100" width="100"  align="left"/>
OpenEnergy Platform
<br>
End of explanation
"""


import requests
import pandas as pd
from IPython.core.display import HTML

# oedb
oep_url= 'http://oep.iks.cs.ovgu.de/'

# token
your_token = ''

"""
Explanation: Tutorial - How to work with the OpenEnergy Platform (OEP)
<br>
<div class="alert alert-block alert-danger">
This is an important information!
</div>
<div class="alert alert-block alert-info">
This is an information!
</div>
<div class="alert alert-block alert-success">
This is your task!
</div>

This tutorial gives you an overview of the OpenEnergy Platform and how you can work with the REST-full-HTTP API in Python. <br>
The full API documentaion can be found on ReadtheDocs.io.
Part IV
0 Setup token <br>
1 Select data <br>
2 Make a pandas dataframe <br>
3 Make calculations <br>
4 Save results as csv and excel files <br>
Part  IV
0. Setup token
<br>
<div class="alert alert-block alert-danger">
Do not push your token to GitHub!
</div>
End of explanation
"""


# select powerplant data
schema = 'supply'
table = 'ego_dp_conv_powerplant'
where = 'version=v0.2.10'

conv_powerplants = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where='+where, )
conv_powerplants.status_code

"""
Explanation: 1. Select data
End of explanation
"""


df_pp = pd.DataFrame(conv_powerplants.json())

"""
Explanation: <div class="alert alert-block alert-info">
**200** succesfully selected data! <br>
</div>

2. Make a pandas dataframe
End of explanation
"""


df_pp.info()

"""
Explanation: 3. Make calculations
Get an overview of your DataFrame:
End of explanation
"""


results = df_pp[['capacity','fuel']].groupby('fuel').sum()
results['units'] = 'MW'
results

# Write DataFrame as csv
results.to_csv('Conventional_powerplants_germany.csv',
                        sep=',',
                        float_format='%.3f',
                        decimal='.',
                        date_format='%Y-%m-%d',
                        encoding='utf-8',
                        if_exists="replace")


# Write the results as xlsx file
writer = pd.ExcelWriter('Conventional_powerplants_germany.xlsx', engine='xlsxwriter')

# write results of installed Capacity by fuels
results.to_excel(writer, index=False, sheet_name='Installed Capacities by fuel')

# write orgininal data in second sheet
df_pp.to_excel(writer, index=False, sheet_name='Conventional Powerplants')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

"""
Explanation: Sum the installed Capacity by fuels and add the Unit MW to a new column.
End of explanation
"""


