# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:geoproject]
#     language: python
#     name: conda-env-geoproject-py
# ---

# +
import geopandas as gpd
import pandas_bokeh
import re
import pandas as pd
pandas_bokeh.output_notebook()
from script.function3dto2d import remove_third_dimension

capacity_list = ['conservative estimate Mt','neutral estimate Mt','optimistic estimate Mt']
move = ['COUNTRY','COUNTRYCOD','ID','geometry']

#load data
storage_unit = gpd.read_file('data/storage_unit_map_lite.geojson')
storage_unit.geometry = storage_unit.geometry.apply(remove_third_dimension)
storage_unit.to_file('geodata verfication/storage_unit.geojson', driver = 'GeoJSON')

trap_unit = gpd.read_file('data/trap_map_lite.geojson')
trap_unit.geometry = trap_unit.geometry.apply(remove_third_dimension)

complete_map = gpd.read_file('data/complete_map_37.geojson')
complete_map.geometry = complete_map.geometry.apply(remove_third_dimension)
complete_map.geometry = complete_map.geometry.buffer(0)
# -

# # Prepare Data for Visualization

# +
# prepare data of capacity detail
storage_unit_summary = storage_unit.groupby('COUNTRYCOD').sum()[[i for i in storage_unit.columns if i not in move]]
storage_unit_summary.columns = [x+'storage_unit' for x in storage_unit_summary.columns]

trap_unit_summary = trap_unit.groupby('COUNTRYCOD').sum()[[i for i in trap_unit.columns if i not in move]]
trap_unit_summary.drop(capacity_list,axis=1,inplace=True)

storage_type_detail = storage_unit_summary.merge(trap_unit_summary, 
                                                 left_index= True, 
                                                 right_index= True, 
                                                 how= 'outer')

def view_storage_type_detail(estimation_type = 'conservative'):
    # columns selection
    tmp_df = storage_type_detail[[x for x in storage_type_detail.columns if re.search(estimation_type,x)]].copy()
    tmp_df.columns = ['Storage_unit (normal density, geological formation to store CO2)',
                      'Aquifer (high density storage unit)',
                      'Oil (high density storage unit)',
                      'Gas (high density storage unit)']
    for i in tmp_df.columns:
        tmp_df[i] = tmp_df[i]/1e3
    tmp_df.plot_bokeh.bar(stacked = True,figsize=(900, 500), xlabel = 'country', ylabel = 'capacity Gt',title = estimation_type + ' per country (unit: (Gt) gigaton)')


# +
# total capacity
summary = complete_map.groupby('COUNTRYCOD').sum()[capacity_list]
summary.columns = [x.replace('Mt','') for x in summary.columns]
capacity_list_ = list(summary.columns)
summary = summary.reset_index()
summary = pd.melt(summary, id_vars= ['COUNTRYCOD'],value_vars = capacity_list_)
summary.value = summary.value/1e3 #Mt to Gt


# print
print('+'*20)
print('total capacity of whole Europe unit: Gigaton')
print('+'*20)
print(summary.groupby('variable').sum()['value'])

#---------------------------------------------------

# offshore capacity
offshore = gpd.read_file('data/offshore_shapes.geojson')
only_offshore = gpd.clip(complete_map, offshore)
summary_position_holder = summary.copy()
summary_position_holder.value = 0

summary_2 = only_offshore.groupby('COUNTRYCOD').sum()[capacity_list]
summary_2.columns = [x.replace('Mt','') for x in summary_2.columns]
capacity_list_ = list(summary_2.columns)
summary_2 = summary_2.reset_index()
summary_2 = pd.melt(summary_2, id_vars= ['COUNTRYCOD'],value_vars = capacity_list_)
summary_2.value = summary_2.value/1e3 #Mt to Gt
summary_2 = pd.concat([summary_2, summary_position_holder]).groupby(['COUNTRYCOD','variable']).sum().reset_index()


print('\n\n')
print('+'*20)
print('total offshore capacity of whole Europe unit: Gigaton')
print('+'*20)
print(summary_2.groupby('variable').sum()['value'])
# -

# # Capacity of all European countries split by storage type

view_storage_type_detail('neutral')

# **external GB dataset's capacity estimation (only for United Kingdom):** 
# - total 61.768 Gt | storage_unit + aquifer: 52.749 Gt | oil: 2.678 Gt | gas: 5.997 Gt

# # Total offshore capacity of each country under different estimations
# ## vs
# # Total capacity of each country under different estimations

# +
import seaborn as sns
from matplotlib import pyplot as plt
f,axes = plt.subplots(2,1,figsize = (20,12))
sub1 = sns.barplot(x = 'COUNTRYCOD', y='value', hue = 'variable',data= summary_2, ax = axes[0] )
sub1.set_yscale("log")
sub1.title.set_text('Offshore Capacity')
sub1.set_ylabel('Capacity (Gt) log scale',fontsize = 20)
#sub1.set_xlabel('Country',fontsize = 20)


sub2 = sns.barplot(x = 'COUNTRYCOD', y='value', hue = 'variable',data= summary, ax = axes[1] )
sub2.set_yscale("log")
sub2.title.set_text('Total Capacity')
sub2.set_ylabel('Capacity (Gt) log scale',fontsize = 20)
#sub2.set_xlabel('Country',fontsize = 20)

plt.show()
# y in log scale  change the name of y axis
# -

# # Storage Map

pandas_bokeh.output_notebook()
#pandas_bokeh.output_file("Interactive storage unit.html")
complete_map = complete_map.sort_values('conservative estimate Mt',ascending= True)
complete_map.plot_bokeh(
    figsize=(900, 600),
    simplify_shapes=5000,
    dropdown=capacity_list,
    colormap="Viridis",
    hovertool_columns=capacity_list+['ID'],
    colormap_range = (0,100)
    )







StarcoderdataJupyterScriptsDedupFiltered