diff --git a/Data/EnergyBalance/2019/Niue.csv b/Data/EnergyBalance/2019/Niue.csv index 4b4507c..962c83b 100644 --- a/Data/EnergyBalance/2019/Niue.csv +++ b/Data/EnergyBalance/2019/Niue.csv @@ -1,5 +1,5 @@ Country (2019),Transactions(down)/Commodity(right),Primary Coal and Peat,Coal and Peat Products,Primary Oil,Oil Products,Natural Gas,Biofuels and Waste,Nuclear,Electricity,Heat,Total Energy,memo: Of which Renewables -Niue,Primary production,---,---,---,---,---,1,---,*2,*16,*18,*18 +Niue,Primary production,---,---,---,---,---,1,---,*2,*15,*18,*18 Niue,Imports,---,---,---,*110,---,---,---,---,---,*110,--- Niue,Exports,---,---,---,---,---,---,---,---,---,---,--- Niue,International marine bunkers,---,---,---,---,---,---,---,---,---,---,--- @@ -51,4 +51,4 @@ Niue,Agriculture forestry and fishing,---,---,---,*1,---,---,---,---,---,*1,--- Niue,Commerce and public services,---,---,---,*1,---,---,---,*5,---,*6,--- Niue,Households,---,---,---,*2,---,*0,---,*5,*16,*23,*16 Niue,Other consumption n.e.s,---,---,---,---,---,---,---,---,---,---,--- -Niue,Non-energy use,---,---,---,---,---,---,---,---,---,---,--- \ No newline at end of file +Niue,Non-energy use,---,---,---,---,---,---,---,---,---,---,--- diff --git a/Data/EnergyBalance/2019/all_countries_df.csv b/Data/EnergyBalance/2019/all_countries_df.csv index 2d43f2b..8f3e1f9 100644 --- a/Data/EnergyBalance/2019/all_countries_df.csv +++ b/Data/EnergyBalance/2019/all_countries_df.csv @@ -582,7 +582,7 @@ 580,Micronesia,Households,0,0,0,0,0,4,0,61,0,64,4,0,0,4 581,Micronesia,Other consumption n.e.s,0,0,0,45,0,11,0,0,0,56,11,0,45,56 582,Micronesia,Non-energy use,0,0,0,92,0,0,0,0,0,92,0,0,92,92 -583,Niue,Primary production,0,0,0,0,0,1,0,2,16,18,18,0,0,17 +583,Niue,Primary production,0,0,0,0,0,1,0,2,15,18,18,0,0,17 584,Niue,Imports,0,0,0,110,0,0,0,0,0,110,0,0,110,110 585,Niue,Exports,0,0,0,0,0,0,0,0,0,0,0,0,0,0 586,Niue,International marine bunkers,0,0,0,0,0,0,0,0,0,0,0,0,0,0 diff --git a/Data/SummaryTable.csv b/Data/SummaryTable.csv index 76df746..4798b26 100644 --- a/Data/SummaryTable.csv +++ b/Data/SummaryTable.csv @@ -1,20 +1,20 @@ -Country / Territory,Political Status,Land area (km2),Population,Area per person (m2),Population density (Person/km2),GDP ($Million),GDP Per Capita ($),Geographic Type,No. islands,No. inhab. island -American Samoa,US territory,240,"45,443","5,176",193,658,14480,High islands + atolls,7,6 -Cook Islands,Independent. NZ-affiliated,180,"17,459","10,310",97,300,17183,High islands + atolls,15,13 -Federated States of Micronesia,Independent. US-affiliated,702,"104,468","6,720",149,390,3733,High islands + atolls,607,65 -Fiji,Independent,"18,376","929,276","19,775",51,"12,180",13107,High islands + atolls,330,110 -French Polynesia,French Territory,"3,521","275,918","12,761",78,"5,490",19897,High islands + atolls,118,67 -Guam,US territory,549,"168,801","3,252",307,6,34,High islands,1,1 -Kiribati,Independent,726,"119,000","6,101",164,270,2269,Atolls,33,21 -Marshall Islands,Independent. US-affiliated,720,"58,413","12,326",81,240,4109,Atolls,34,24 -Nauru,Independent,21,"10,670","1,968",508,150,14058,Raised coral island,1,1 -Niue,Independent. NZ-affiliated,258,"1,620","159,259",6,10,6235,Raised coral island,1,1 -Palau,Independent. US-affiliated,475,"17,907","26,526",38,320,17870,High islands + atolls,1,1 -PNG,Independent,"461,690","8,935,000","51,672",19,"38,170",4272,High islands + atolls,600,No data -Solomon Islands,Independent,"29,785","652,857","45,623",22,"1,780",2726,High islands + atolls,992,347 -Tokelau,NZ territory,12,"1,500","8,000",125,8,5333,Atolls,3,3 -Tonga,Independent,696,"100,651","6,915",145,670,6657,High islands,169,36 -Tuvalu,Independent,26,"11,646","2,233",448,50,4293,Atolls,9,8 -Vanuatu,Independent,"12,189","307,815","39,598",25,930,3021,High islands + atolls,83,65 -Samoa,Independent,"2,934","202,506","14,488",69,"1,280",6321,High islands,12,4 -New Caledonia,French Territory,"18,275","297,160","61,499",16,11,37,No Data,55,No data +Country / Territory,Political Status,Land area (km2),Population,Area per person (m2),Population density (Person/km2),GDP Per Capita ($),Geographic Type,No. islands,No. inhab. island +American Samoa,US territory,240,"45,443","5,176",193,14480,High islands + atolls,7,6 +Cook Islands,Independent. NZ-affiliated,180,"17,459","10,310",97,16700,High islands + atolls,15,13 +Micronesia,Independent. US-affiliated,702,"104,468","6,720",149,3500,High islands + atolls,607,65 +Fiji,Independent,"18,376","929,276","19,775",51,11000,High islands + atolls,330,110 +French Polynesia,French Territory,"3,521","275,918","12,761",78,17000,High islands + atolls,118,67 +Guam,US territory,549,"168,801","3,252",307,35600,High islands,1,1 +Kiribati,Independent,726,"119,000","6,101",164,2300,Atolls,33,21 +Marshall Islands,Independent. US-affiliated,720,"58,413","12,326",81,4000,Atolls,34,24 +Nauru,Independent,21,"10,670","1,968",508,13500,Raised coral island,1,1 +Niue,Independent. NZ-affiliated,258,"1,620","159,259",6,5800,Raised coral island,1,1 +Palau,Independent. US-affiliated,475,"17,907","26,526",38,17600,High islands + atolls,1,1 +PNG,Independent,"461,690","8,935,000","51,672",19,4200,High islands + atolls,600,No data +Solomon Islands,Independent,"29,785","652,857","45,623",22,2500,High islands + atolls,992,347 +Tokelau,NZ territory,12,"1,500","8,000",125,60004,Atolls,3,3 +Tonga,Independent,696,"100,651","6,915",145,6400,High islands,169,36 +Tuvalu,Independent,26,"11,646","2,233",448,4400,Atolls,9,8 +Vanuatu,Independent,"12,189","307,815","39,598",25,2800,High islands + atolls,83,65 +Samoa,Independent,"2,934","202,506","14,488",69,6300,High islands,12,4 +New Caledonia,French Territory,"18,275","297,160","61,499",16,31100,No Data,55,No data diff --git a/DecarbonizationFunctions.py b/DecarbonizationFunctions.py index 29fa697..7bdddac 100644 --- a/DecarbonizationFunctions.py +++ b/DecarbonizationFunctions.py @@ -1,18 +1,201 @@ import pandas as pd import functions -def calculate_community_battery_size(country,residential_battery_capacity,demand_scenario="Decarbonization", total_storage_days = 5): - demand = functions.fetch_single_country_demand(Country=country,Year=2019) - if demand_scenario == "World average": - demand = demand * 0.8 - daily_average_demand = demand/365 - total_storage_capacity_GWh = total_storage_days * daily_average_demand - community_storage_capacity = total_storage_capacity_GWh - residential_battery_capacity +from EnergyFlows import Country_List +def calculate_community_battery_size(demand,residential_battery_capacity,technical_pot,total_storage_days = 5): + average_daily_demand = demand/365 #GWH/day + if demand <= technical_pot: + total_storage_capacity_GWh = average_daily_demand * total_storage_days + elif demand > technical_pot: + total_storage_capacity_GWh = (average_daily_demand * total_storage_days)*(technical_pot/demand) + community_battery = max(total_storage_capacity_GWh - residential_battery_capacity,0) - return community_storage_capacity + return community_battery +def calculate_demand(country,demand_scenario): + if demand_scenario == "Decarbonization": + demand = functions.fetch_single_country_demand(Country=country,Year=2019)[0] + elif demand_scenario == "Electrification": + demand = functions.fetch_single_country_demand(Country=country, Year=2019)[1] + elif demand_scenario == "Net_zero": + demand = functions.fetch_single_country_demand(Country=country, Year=2019)[2] + return demand -def run_decarbonization_scenario(): - pass +def calculate_renewable_technical_potential(country,available_land,avaialble_coastline,avaialble_buildings=0.3,PV_size=2.5): + technical_potential_df = functions.calculate_PV_Wind_potential(available_land=available_land, available_coastline=avaialble_coastline) + PV_technical_potential = technical_potential_df[technical_potential_df['Country'] == country]['PV_technical_GWh'].values[0] + Wind_technical_potential = technical_potential_df[technical_potential_df['Country'] == country]['Wind_technical_GWh'].values[0] -calculate_community_battery_size("PNG",5) \ No newline at end of file + rooftop_df = functions.calculate_rooftop_PV_potential(available_buildings=avaialble_buildings,PV_size=PV_size) + rooftop_potential = rooftop_df[rooftop_df['Country'] == country]['Generation_GWh'].values[0] + # print(PV_technical_potential,Wind_technical_potential,rooftop_potential) + + total = PV_technical_potential + Wind_technical_potential + rooftop_potential + return {"PV_tech_GWh":PV_technical_potential,"Wind_tech_GWh":Wind_technical_potential,"Rooftop_GWh":rooftop_potential,"Total":total} + +def calculate_capacity_of_each_technology(country,dic_potential,demand): + + rooftop_PV_GWh = min(dic_potential["Rooftop_GWh"],demand) + rooftop_PV_GWh = max(0,rooftop_PV_GWh) + + large_PV_GWh = min(dic_potential["PV_tech_GWh"],demand-rooftop_PV_GWh) + large_PV_GWh = max(0,large_PV_GWh) + + wind_GWh = min(dic_potential["Wind_tech_GWh"],demand-rooftop_PV_GWh-large_PV_GWh) + wind_GWh = max(0,wind_GWh) + + df_potentials = pd.read_excel('Data/Potentials.xlsx') + PV_pot = df_potentials.iloc[0, 2:] #GWh/MW/year + Wind_pot =df_potentials.iloc[2, 2:] #GWh/MW/year + + PV_pot = PV_pot[country] + Wind_pot = Wind_pot[country] + # print(PV_pot[country]) + + rooftop_MW = rooftop_PV_GWh/PV_pot #MW + large_PV_MW = large_PV_GWh/PV_pot #MW + + wind_MW = wind_GWh/Wind_pot + + total_GWh = wind_GWh + large_PV_GWh + rooftop_PV_GWh + + community_battery = calculate_community_battery_size(demand=demand,residential_battery_capacity = rooftop_MW*2/1000,technical_pot=total_GWh,total_storage_days=5 ) + + return {"Rooftop_MW":rooftop_MW,"Large_PV_MW":large_PV_MW,"Wind_MW":wind_MW,"residential_battery_MWh":rooftop_MW*2,"total_GWh":total_GWh,"Community_battery_GWh":community_battery} + + +def create_yearly_df(country,decarb_year,capacity_dic,cost_dic,diesel_price,inflation_rate, discount_rate): + from datetime import datetime + now = datetime.now().year + number_of_years = decarb_year - 2022 + year_list = [] + installation_df = pd.DataFrame() + + for i in range(0, 31): + now += 1 + year_list.append(now) + + # community_battery = calculate_community_battery_size(demand=) + installation_df['Year'] = year_list + installation_df['rooftop_MW'] = capacity_dic["Rooftop_MW"]/number_of_years + installation_df['resid_battery_MW'] = installation_df['rooftop_MW'] * 2 + installation_df['PV_MW'] = capacity_dic["Large_PV_MW"]/number_of_years + installation_df['wind_MW'] = capacity_dic["Wind_MW"]/number_of_years + installation_df["Community_battery_GWh"] = capacity_dic['Community_battery_GWh']/number_of_years + + installation_df.loc[number_of_years:,'rooftop_MW'] = 0 + installation_df.loc[number_of_years:,'resid_battery_MW'] = 0 + installation_df.loc[number_of_years:,'PV_MW'] = 0 + installation_df.loc[number_of_years:,'wind_MW'] = 0 + installation_df.loc[number_of_years:,"Community_battery_GWh"] = 0 + + # costs are $/W - 1000000/MW + # The output is #$ + installation_df['installation_Cost'] = (installation_df['rooftop_MW'] * cost_dic['rooftop'] + + installation_df['resid_battery_MW'] *cost_dic['resid_battery'] + + installation_df['Community_battery_GWh']*1000 *cost_dic['resid_battery'] + + installation_df['PV_MW'] * cost_dic['large_PV'] +\ + installation_df['wind_MW'] * cost_dic['wind'])*1000000#Convert to $/MW #in the cost dic, are costs are $/W + + + installation_df['avoided_demand_GWh'] = capacity_dic["total_GWh"]/number_of_years + installation_df.loc[number_of_years:,'avoided_demand_GWh'] = 0 + + diesel_efficiency = 0.4 + + diesel_generatio = 2.5 #kWh/Liter + installation_df["avoided_diesel_liter"] = installation_df["avoided_demand_GWh"] / (2.5/1000000) + installation_df["cumulative_avoided_diesel_liter"] = installation_df["avoided_diesel_liter"].cumsum(axis=0) + installation_df["avoided_diesel_savings"] = installation_df["cumulative_avoided_diesel_liter"] * diesel_price + + if country =="New Caledonia": + #coal price is 400 USD/Tonne + # 47% demand is met by diesel, and 53% by coal + # 0.00814 GWh energy in one tonne coal. + installation_df["avoided_diesel_liter"] = installation_df["avoided_diesel_liter"]*0.47 + installation_df["avoided_coal_tonne"] = (installation_df["avoided_demand_GWh"]*0.53)/(0.00814 * 0.35) + + installation_df["cumulative_avoided_diesel_liter"] = installation_df["avoided_diesel_liter"].cumsum(axis=0) + installation_df["cumulative_avoided_coal_tonne"] = installation_df["avoided_coal_tonne"].cumsum(axis=0) + + installation_df["avoided_diesel_savings"] = installation_df["cumulative_avoided_diesel_liter"] * diesel_price + installation_df["avoided_coal_savings"] = installation_df["cumulative_avoided_coal_tonne"] * 400 + installation_df["avoided_diesel_savings"] = installation_df["avoided_diesel_savings"] + installation_df["avoided_coal_savings"] + + installation_df['Cumulative_avoided_cost'] = installation_df['avoided_diesel_savings'].cumsum(axis=0) + + inflation_rate = inflation_rate/100 + discount_rate = discount_rate/100 + + for i, row in installation_df.iterrows(): + installation_df.at[i, 'Cumulative_avoided_cost'] = installation_df.at[i, 'Cumulative_avoided_cost'] * ((1+inflation_rate)/(1+discount_rate))**i + installation_df.at[i, 'installation_Cost'] = installation_df.at[i, 'installation_Cost'] * ((1+inflation_rate)/(1+discount_rate))**i + + installation_df['Annual_Net_saving'] = installation_df['Cumulative_avoided_cost'] - installation_df['installation_Cost'] # $MM + installation_df["Cumulative_net_saving"] = installation_df['Annual_Net_saving'].cumsum(axis=0) + + return installation_df + # demand_df['Net_saving_discounted'] = 0 + # demand_df['Emission_red_saving_discounted'] = 0 + # inflation_rate = inflation_rate/100 + # discount_rate = discount_rate/100 + +def calculate_diesel_price(country): + diesel_df = pd.read_csv("Data/Diesel.csv") + if country in diesel_df["Country"].to_list(): + print("Hi") + diesel_price = diesel_df[diesel_df['Country'] == country]['Tax included'].values[0] + else: + diesel_price = diesel_df['Tax included'].mean() + + diesel_price = diesel_price-20 #20c less than retails price + diesel_price = diesel_price/100 # convert to $ from cents + + print(diesel_price) + return diesel_price + + +def run_decarbonization_scenario(cost_scenario,country_list,demand_scenario="Decarbonization",available_land=0.02, avaialble_coastline=0.1,avaialble_buildings=0.3,PV_size=2.5,decarb_year=2030): + # demand_scenario = ["Decarbonization","Electrification","Net_zero"] + costs= {"optimistic":{"rooftop":3,"resid_battery":4,"large_PV":3,"wind":3},"pessimistic":{"rooftop":4.5,"resid_battery":4,"large_PV":4.5,"wind":6}} + all_countries_result = pd.DataFrame() + all_countries_result['Technology'] =["Rooftop_MW", "Large_PV_MW","Wind_MW","Residential_battery_MWh","Community_battery_GWh","total_GWh",'Payback period (years)'] + # : rooftop_MW, : large_PV_MW, : wind_MW, : total_GWh, : community_battery + # cost_dic = {"rooftop":4.5,"resid_battery":4,"large_PV":4.5,"wind":6}#Pessimistic + # cost_dic = {"rooftop":3,"resid_battery":4,"large_PV":3,"wind":3}#Optimistic + cost_dic = costs[cost_scenario] + for country in country_list: + diesel_price = calculate_diesel_price(country) + pot = calculate_renewable_technical_potential(country, available_land=available_land, avaialble_coastline=avaialble_coastline,avaialble_buildings=avaialble_buildings,PV_size=PV_size) + demand = calculate_demand(country, demand_scenario) + capacity_dic = calculate_capacity_of_each_technology(country, pot, demand) + final_df = create_yearly_df(country=country,decarb_year=decarb_year,capacity_dic=capacity_dic,cost_dic=cost_dic,diesel_price=diesel_price,inflation_rate=3,discount_rate=7) + final_df.to_csv("Results/{}/Simulation_result_{}.csv".format(demand_scenario,country)) + + payback_period = final_df[final_df.Cumulative_net_saving < 0].index.values.max() + + all_countries_result[country] = [capacity_dic["Rooftop_MW"], + capacity_dic["Large_PV_MW"], + capacity_dic["Wind_MW"], + capacity_dic["residential_battery_MWh"], + capacity_dic["Community_battery_GWh"], + capacity_dic["total_GWh"], + payback_period + ] + + all_countries_result.reset_index(drop=True,inplace=True) + # all_countries_result = all_countries_result.pivot(columns="Technology",index=all_countries_result.columns)[all_countries_result.columns] + + all_countries_result.to_excel("Results/{}/{}_simulation_result_{}_wind_{}_PV_{}.xlsx".format(demand_scenario,cost_scenario,demand_scenario,avaialble_coastline,available_land)) + # print(all_countries_result.head()) + return final_df + +for cost_scenario in ["optimistic",'pessimistic']: + for demand_sceanrio in ['Decarbonization',"Electrification","Net_zero"]: + run_decarbonization_scenario(cost_scenario=cost_scenario,country_list=Country_List, + demand_scenario=demand_sceanrio,available_land=0.1, + avaialble_coastline=0,avaialble_buildings=0.3, + PV_size=2.5,decarb_year=2030) + + +#check community battery \ No newline at end of file diff --git a/EnergyFlows.py b/EnergyFlows.py index e1eab46..09c90bd 100644 --- a/EnergyFlows.py +++ b/EnergyFlows.py @@ -200,63 +200,78 @@ def select_flow(): files = os.listdir(path) df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) - input_from = ["Imports","Primary production","Total energy supply"] + input_from = ["Imports","Primary production","Total energy supply","Final consumption","Transformation","Exports"] input_source = ["Primary Coal and Peat","Coal and Peat Products","Primary Oil","Oil Products","Natural Gas","Biofuels and Waste","Nuclear", "Electricity","Heat","Total Energy","All Coal","All Oil","memo: Of which Renewables"] - consumer_list = ['Electricity Plants',"CHP plants","Energy industries own use","Manufacturing const. and mining","Losses", - 'International marine bunkers',"Domestic navigation","International aviation bunkers","Domestic aviation","Road","Rail","Domestic aviation","Transport n.e.s", - "Agriculture forestry and fishing","Commerce and public services","Households","Other consumption n.e.s","Non-energy use", - ] + + consumer_list = df["Transactions(down)/Commodity(right)"].unique() farm_drpdwn_dbc = dbc.FormGroup( [ dbc.Row([ + dbc.Col([dbc.Label("Energy supplied from", style={'margin-right': "5px"})],md=1.5), dbc.Col([ - dbc.Label("From", style={'margin-right': "5px"}), - dbc.Select( + dcc.Dropdown( id="select-flow-provider", options=[ {"label": i, "value": i} for i in input_from ], - value=input_from[0], - style={'width': "50%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', + value=[input_from[0]], + style={'width': "90%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', 'fontSize': 15, 'color': 'black'}, - ),] + multi=True, + searchable=True, + clearable=False, + ),],md=4 ), + dbc.Col([dbc.Label("In the form of", style={'margin-right': "5px"})],md=1.5), + dbc.Col([ - dbc.Label("Energy carrier", style={'margin-right': "5px"}), dbc.Select( id="select-flow-provider-source", options=[ {"label": i, "value": i} for i in input_source ], value="All Oil", - style={'width': "50%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', + style={'width': "80%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', 'fontSize': 15, 'color': 'black'}, + ), - ]) + ],md=4) ]), html.Br(), dbc.Row([ - dbc.Col([dbc.Label("To"),],md=0.5), + dbc.Col([dbc.Label("To")],md=0.5), dbc.Col([ - dcc.Dropdown( id="select-flow-cunsumer", options=[ {"label": i, "value": i} for i in consumer_list ], value=consumer_list[:4], - style={'width': "90%", 'margin-left': "0px", 'fontColor': 'black', 'fontSize': 15, + style={'width': "100%", 'margin-left': "0px", 'fontColor': 'black', 'fontSize': 15, 'color': 'black'}, multi=True, searchable=True, clearable=False, )],md = 6), + dbc.Col([dbc.Label("in the form of")], md=1.5), + + dbc.Col([ + dbc.Select( + id="destination-carrier", + options=[ + {"label": i, "value": i} for i in input_source + ], + value="All Oil", + style={'width': "80%", 'margin-left': "0px", 'fontColor': 'black', 'fontSize': 15, + 'color': 'black'}, + )], md=4), ]), html.Br(), + dbc.Input(placeholder="Y axis title", id = "y_axis_title",className="mb-3",style={"width": '40%', }), dbc.Button("Add Figure", color="danger", id='update-button-cross-country-flow', n_clicks=0, className="me-1"), dbc.Button("Clear Canvas", color="primary", id='update-button-flow-clear', n_clicks=0, className="me-1"), @@ -266,6 +281,79 @@ def select_flow(): style={'marginLeft':35,'marginTop':25,'fontSize':25} ) return farm_drpdwn_dbc + + +def select_breakdown_details(): + path = 'Data/EnergyBalance' + files = os.listdir(path) + df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) + sector_list = df["Transactions(down)/Commodity(right)"].unique() + dbc.Label("Select s sector", style={'margin-right': "5px"}), + + farm_drpdwn_dbc = dbc.FormGroup( + [ + dbc.Label("Select a sector "), + dbc.Select( + id="select-sector-for-breakdown", + options=[ + {"label": i, "value": i} for i in sector_list + ], + value="All Oil", + style={'width': "30%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', + 'fontSize': 15, 'color': 'black'}, + + ), + html.Br(), + dbc.Button("Add Figure", color="danger", id='update-button-sector-breakdown', n_clicks=0, + className="me-1"), + dbc.Button("Clear Canvas", color="primary", id='update-button-sector-breakdown-clear', n_clicks=0, + className="me-1"), + ]) + return farm_drpdwn_dbc +def dynamic_column_components(): + path = 'Data/EnergyBalance' + files = os.listdir(path) + df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) + provider_list = df["Transactions(down)/Commodity(right)"].unique() + dbc.Label("Select s column", style={'margin-right': "5px"}), + columns = df.iloc[:,3:-3] + farm_drpdwn_dbc = dbc.FormGroup( + [ + dbc.Label("Select a sector "), + dcc.Dropdown( + id="select-sector-for-dynamic-column", + options=[ + {"label": i, "value": i} for i in provider_list + ], + value=["Primary production"], + style={'width': "30%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', + 'fontSize': 15, 'color': 'black'}, + multi=True, + searchable=True, + clearable=False, + ), + dbc.Label("Select a column"), + dbc.Select( + id="select-dynamic-column", + options=[ + {"label": i, "value": i} for i in columns + ], + value="Electricity", + style={'width': "30%", 'margin-left': "0px", 'margin-right': "10px", 'fontColor': 'black', + 'fontSize': 15, 'color': 'black'}, + + ), + html.Br(), + dbc.Input(placeholder="Y axis title", id="y_axis_title_dynamic_column", className="mb-3", style={"width": '40%', }), + + html.Br(), + dbc.Button("Add Figure", color="danger", id='update-button-dynamic-column', n_clicks=0, + className="me-1"), + dbc.Button("Clear Canvas", color="primary", id='update-button-dynamic-column-clear', n_clicks=0, + className="me-1"), + ]) + return farm_drpdwn_dbc + Tracing_energy_flows = [ dbc.CardHeader(html.H5("Cross-country comparison of energy flows consumed in different sectors")), @@ -295,6 +383,68 @@ def select_flow(): ), ] + +breakdown_of_source_in_a_sector = [ + + dbc.CardHeader(html.H5("Cross-country comparison of breakdown of sources in one sector (e.g., final consumption, or households)")), + dbc.CardBody( + [ + dcc.Loading( + id="loading-sankey", + children=[ + dbc.Alert( + "Something's gone wrong! Give us a moment, but try loading this page again if problem persists.", + id="no-data-alert-sankey", + color="warning", + style={"display": "none"}, + ), + select_breakdown_details(), + html.Br(), + # html.Div(dcc.Graph(id="cross_country_sankey_figure"),style=figure_border_style), + html.Div(id='Hidden_Div_breakdown_by_source', children=[0, 0], style={'display': 'none'}), + html.Div(id='dynamic_callback_container_energy_breakdown_by_source', children=[], + style={'margin-top': '15px', 'margin-left': '20px','margin-right': '20px'}), + html.Br(), + ], + type="default", + ) + ], + style={"marginTop": 0, "marginBottom": 0}, + ), +] + + +dynamic_column_cross_country = [ + + dbc.CardHeader(html.H5("Cross-country comparison of a column (i.e., Electricity)")), + dbc.CardBody( + [ + dcc.Loading( + id="loading-sankey", + children=[ + dbc.Alert( + "Something's gone wrong! Give us a moment, but try loading this page again if problem persists.", + id="no-data-alert-sankey", + color="warning", + style={"display": "none"}, + ), + dynamic_column_components(), + html.Br(), + # html.Div(dcc.Graph(id="cross_country_sankey_figure"),style=figure_border_style), + html.Div(id='Hidden-Div_dynamic_column', children=[0, 0], style={'display': 'none'}), + html.Div(id='dynamic_callback_container_dynamic_column', children=[], + style={'margin-top': '15px', 'margin-left': '20px','margin-right': '20px'}), + + html.Br(), + + ], + type="default", + ) + ], + style={"marginTop": 0, "marginBottom": 0}, + ), +] + Cross_country_sankey = [ dbc.CardHeader(html.H5("Cross-country comparison of Sankey diagrams")), @@ -333,6 +483,8 @@ def select_flow(): [ dbc.Row([dbc.Col(dbc.Card(Sankey)),], style={"marginTop": 30}), dbc.Row([dbc.Col(dbc.Card(Tracing_energy_flows)),], style={"marginTop": 30}), + dbc.Row([dbc.Col(dbc.Card(breakdown_of_source_in_a_sector)),], style={"marginTop": 30}), + dbc.Row([dbc.Col(dbc.Card(dynamic_column_cross_country)),], style={"marginTop": 30}), dbc.Row([dbc.Col(dbc.Card(Cross_country_sankey)), ], style={"marginTop": 30}), ], diff --git a/callbacks_modeling.py b/callbacks_modeling.py new file mode 100644 index 0000000..e69de29 diff --git a/callbacks_sankey.py b/callbacks_sankey.py index 9421208..151765b 100644 --- a/callbacks_sankey.py +++ b/callbacks_sankey.py @@ -128,18 +128,100 @@ def update_cross_country_comparison(n_clicks,clear_canvas,from_,to_,normalizatio [State("select-flow-provider", "value"), State("select-flow-cunsumer", "value"), State("select-flow-provider-source", "value"), + State("destination-carrier", "value"), State('Hidden_Div_breakdown', "children"), - State('dynamic_callback_container_energy_breakdown', 'children') + State('dynamic_callback_container_energy_breakdown', 'children'), + State("y_axis_title","value") ] ) -def update_cross_country_comparison(n_clicks,clear_canvas,from_,consumer_list,carrier,hidden_div,div_children): +def update_cross_country_comparison(n_clicks,clear_canvas,from_,consumer_list,carrier,carrier_destination,hidden_div,div_children,y_axis_title): if n_clicks != hidden_div[0]: - print("Hereee") + # print("Hereee") - fig = figures.dynamic_breakdown_figure_generation(from_=from_,list_of_consumers=consumer_list,carrier=carrier) + fig = figures.dynamic_breakdown_figure_generation(y_axis_title=y_axis_title,from_=from_,list_of_consumers=consumer_list,carrier=carrier,destination_carrier=carrier_destination) + # print("Here2") + new_child = html.Div( + style={ 'outline': 'thin lightgrey solid', 'padding': 5,'marginLeft': 10, 'marginRight': 10,},#'display': 'inline-block', + children=[ + dcc.Graph( + id={ + 'type': 'dynamic-graph', + 'index': n_clicks + }, + figure=fig, + ), + ] + ) + div_children.append(new_child) + + elif clear_canvas != hidden_div[1]: + div_children = [] + + hidden_div = [n_clicks,clear_canvas] + + return hidden_div,div_children + + + + + +@app.callback( + [ + Output('Hidden_Div_breakdown_by_source', "children"), + Output('dynamic_callback_container_energy_breakdown_by_source', 'children')], + [Input('update-button-sector-breakdown', 'n_clicks'), + Input('update-button-sector-breakdown-clear', 'n_clicks')], + [State("select-sector-for-breakdown", "value"), + State('Hidden_Div_breakdown_by_source', "children"), + State('dynamic_callback_container_energy_breakdown_by_source', 'children'), + ] +) +def update_cross_country_comparison(n_clicks,clear_canvas,sector,hidden_div,div_children): + print("Hereee") + + if n_clicks != hidden_div[0]: + + fig = figures.dynamic_breakdown_of_sectors(sector=sector) print("Here2") new_child = html.Div( - style={'display': 'inline-block', 'outline': 'thin lightgrey solid', 'padding': 5}, + style={ 'outline': 'thin lightgrey solid', 'padding': 5,'marginLeft': 10, 'marginRight': 10,},#'display': 'inline-block', + children=[ + dcc.Graph( + id={ + 'type': 'dynamic-graph', + 'index': n_clicks + }, + figure=fig, + ), + ] + ) + div_children.append(new_child) + + elif clear_canvas != hidden_div[1]: + div_children = [] + + hidden_div = [n_clicks,clear_canvas] + + return hidden_div,div_children + +@app.callback( + [ + Output('Hidden-Div_dynamic_column', "children"), + Output('dynamic_callback_container_dynamic_column', 'children')], + [Input('update-button-dynamic-column', 'n_clicks'), + Input('update-button-dynamic-column-clear', 'n_clicks')], + [State("select-sector-for-dynamic-column", "value"), + State("select-dynamic-column", "value"), + State("y_axis_title_dynamic_column", "value"), + State('Hidden-Div_dynamic_column', "children"), + State('dynamic_callback_container_dynamic_column', 'children'), + ] +) +def update_dynamic_column(n_clicks,clear_canvas,provider,column,y_axis_title,hidden_div,div_children): + if n_clicks != hidden_div[0]: + fig = figures.dynamic_one_column_multiple_source(provider=provider,column=column,y_axis_title=y_axis_title) + new_child = html.Div( + style={ 'outline': 'thin lightgrey solid', 'padding': 5,'marginLeft': 10, 'marginRight': 10,},#'display': 'inline-block', children=[ dcc.Graph( id={ diff --git a/figures.py b/figures.py index fcb0185..9312306 100644 --- a/figures.py +++ b/figures.py @@ -738,7 +738,7 @@ def rooftop_PV_plot(available_buildings,PV_size): fig3 = make_subplots(specs=[[{"secondary_y": False}]]) fig3.add_trace( - go.Bar(x=rooftop_df['Country'], y=rooftop_df['Country'],text=rooftop_df['Country'], name="Potential rooftop PV generation (GWh)",marker_color='forestgreen'), + go.Bar(x=rooftop_df['Country'], y=rooftop_df['Generation_GWh'],text=rooftop_df['Generation_GWh'], name="Potential rooftop PV generation (GWh)",marker_color='forestgreen'), ) fig3.update_yaxes(title_text="Rooftop PV generation (GWh/year)", showline=True, showgrid=True,linecolor=line_color,gridcolor=line_color) fig3.update_xaxes(showgrid=False,showline=True,linecolor=line_color) @@ -1377,11 +1377,13 @@ def mapboxplot(Country,style): from math import sqrt, atan, pi import pyproj - PV_area_non_RE, PV_area_final_demand, PV_area_non_RE_per, PV_area_final_demand_per = functions.PV_area_single_country(Country,2019) + PV_area_non_RE, PV_area_final_demand,PV_area_net_zero,\ + PV_area_non_RE_per, PV_area_final_demand_per, PV_area_net_zero_per = functions.PV_area_single_country(Country,2019) width_decarb = math.sqrt(PV_area_non_RE) * 1000/2 width_final_demand = math.sqrt(PV_area_final_demand) * 1000/2 + width_net_zero = math.sqrt(PV_area_net_zero) * 1000/2 geod = pyproj.Geod(ellps='WGS84') - Coordinates = {"Samoa": [-13.597336, -172.457458], "Nauru": [-0.5228, 166.9315], "Vanuatu": [-15.3767, 166.9592], + Coordinates = {"Samoa": [-13.597336, -172.457458], "Nauru": [-0.5228, 166.9315], "Vanuatu": [-15.245988, 167.008684], "Palau": [7.5150, 134.5825], "Kiribati": [1.780915, -157.304505], "Cook Islands": [-21.2367, -159.7777], "Solomon Islands": [-9.6457, 160.1562], "Tonga": [-21.1790, -175.1982], @@ -1432,7 +1434,7 @@ def mapboxplot(Country,style): fig.add_trace(go.Scattermapbox( mode="lines+text", fill="toself", - marker=dict(size=16, color='grey'), + marker=dict(size=16, color='black'), textposition='top left', # name = "asdsadad" textfont=dict(size=16, color='black'), @@ -1449,6 +1451,43 @@ def mapboxplot(Country,style): showlegend=False, margin={'l': 0, 'r': 0, 'b': 0, 't': 0}, ) + + + + width = width_net_zero # m + height = width_net_zero # m + rect_diag = sqrt(width ** 2 + height ** 2) + center_lon = Coordinates[Country][1] + center_lat = Coordinates[Country][0] + azimuth1 = atan(width / height) + azimuth2 = atan(-width / height) + azimuth3 = atan(width / height) + pi # first point + 180 degrees + azimuth4 = atan(-width / height) + pi # second point + 180 degrees + pt1_lon, pt1_lat, _ = geod.fwd(center_lon, center_lat, azimuth1 * 180 / pi, rect_diag) + pt2_lon, pt2_lat, _ = geod.fwd(center_lon, center_lat, azimuth2 * 180 / pi, rect_diag) + pt3_lon, pt3_lat, _ = geod.fwd(center_lon, center_lat, azimuth3 * 180 / pi, rect_diag) + pt4_lon, pt4_lat, _ = geod.fwd(center_lon, center_lat, azimuth4 * 180 / pi, rect_diag) + + fig.add_trace(go.Scattermapbox( + mode="lines+text", fill="toself", + marker=dict(size=16, color='lightblue'), + textposition='top left', + # name = "asdsadad" + textfont=dict(size=16, color='black'), + hovertemplate="{}

".format(Country) + + "PV area for final demand: {} km2
".format(round(PV_area_net_zero,3)) + + "% of land: {}".format(round(PV_area_net_zero_per,3)), + lon=[pt1_lon, pt2_lon, pt3_lon, pt4_lon, pt1_lon, ], + lat=[pt1_lat, pt2_lat, pt3_lat, pt4_lat, pt1_lat], + )) + + fig.update_layout( + mapbox={'style': style, 'center': {'lon': center_lon, 'lat': center_lat}, 'zoom': 9}, + # add zoom as a slider + showlegend=False, + margin={'l': 0, 'r': 0, 'b': 0, 't': 0}, + ) + return fig @@ -1656,7 +1695,7 @@ def import_export_figure_dynamic(df,product): def Solar_physical_resources(): import plotly.graph_objs as go - df_technical_potential = functions.calculate_PV_Wind_potential() + df_technical_potential = functions.calculate_PV_Wind_potential(available_land=0.02,available_coastline=0.1) @@ -1990,7 +2029,7 @@ def per_capita_comparison(): marker_line_width=0, opacity=1) fig_use_cap.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) fig_use_cap.update_xaxes(showline=True, linecolor=line_color, - title_text="Source: Our world in data ") + title_text="Source: Energy Balances, United Nations") @@ -2027,9 +2066,9 @@ def per_capita_renewables(): fig_use_cap.add_trace(go.Bar(x=countries, y=renewable_electricity,text=renewable_electricity.round(2), name='This research', marker_color='red', )) - fig_use_cap.add_trace(go.Bar(x=wolrd_per_capita_RE_elec['Entity'], y=wolrd_per_capita_RE_elec['Low-carbon electricity per capita (kWh)'],text=wolrd_per_capita_RE_elec['Low-carbon electricity per capita (kWh)'].round(1), name='Our world in data', - marker_color='green', - )) + # fig_use_cap.add_trace(go.Bar(x=wolrd_per_capita_RE_elec['Entity'], y=wolrd_per_capita_RE_elec['Low-carbon electricity per capita (kWh)'],text=wolrd_per_capita_RE_elec['Low-carbon electricity per capita (kWh)'].round(1), name='Our world in data', + # marker_color='green', + # )) fig_use_cap.add_shape( type='line', line=dict(dash='dot', color='red'), x0=0, x1=len(countries), y0=wolrd_average_per_capita_RE_elec, y1=wolrd_average_per_capita_RE_elec @@ -2076,7 +2115,7 @@ def per_capita_renewables(): marker_line_width=0, opacity=1) fig_use_cap.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) fig_use_cap.update_xaxes(showline=True, linecolor=line_color, - title_text="Source: Our world in data ") + title_text="Source: Energy Balances, United Nations") return fig_use_cap @@ -2106,9 +2145,9 @@ def per_capita_intensity(): fig_use_cap.add_trace(go.Bar(x=countries, y=df_GDP['Demand_per_gdp'].round(1),text=df_GDP['Demand_per_gdp'].round(1), name='This research', marker_color='red', )) - fig_use_cap.add_trace(go.Bar(x=wolrd_per_capita_intensity['Entity'], y=wolrd_per_capita_intensity['Energy intensity level of primary energy (MJ/$2017 PPP GDP)'],text=wolrd_per_capita_intensity['Energy intensity level of primary energy (MJ/$2017 PPP GDP)'].round(1), name='Our world in data', - marker_color='green', - )) + # fig_use_cap.add_trace(go.Bar(x=wolrd_per_capita_intensity['Entity'], y=wolrd_per_capita_intensity['Energy intensity level of primary energy (MJ/$2017 PPP GDP)'],text=wolrd_per_capita_intensity['Energy intensity level of primary energy (MJ/$2017 PPP GDP)'].round(1), name='Our world in data', + # marker_color='green', + # )) fig_use_cap.add_shape( type='line', line=dict(dash='dot', color='red'), x0=0, x1=len(countries), y0=wolrd_average_per_capita_intensity, y1=wolrd_average_per_capita_intensity @@ -2155,7 +2194,7 @@ def per_capita_intensity(): marker_line_width=0, opacity=1) fig_use_cap.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) fig_use_cap.update_xaxes(showline=True, linecolor=line_color, - title_text="Source: Our world in data ") + title_text="Source: Energy Balances, United Nations") return fig_use_cap def percentage_of_imports(): @@ -2169,7 +2208,7 @@ def percentage_of_imports(): renewables_in_total = functions.fetch_all_countries_demand(2019,Unit='GWh',Use='Analysis')[7] world_average_demand_non_RE = (world_average_demand - renewables_in_total) * 80/100 # convert to electricity rooftop_PV_df = functions.calculate_rooftop_PV_potential() - wind_PV_df = functions.calculate_PV_Wind_potential() + wind_PV_df = functions.calculate_PV_Wind_potential(available_land=0.02,available_coastline=0.1) remaining_demand_world_average = world_average_demand_non_RE - rooftop_PV_df['Generation_GWh'] - wind_PV_df['PV_technical_GWh'] imports_world_average = 100 * remaining_demand_world_average/world_average_demand_non_RE @@ -2204,7 +2243,12 @@ def percentage_of_imports(): imports_df['Current_GWh'] = net_imports imports_df['Current_GWh'] = imports_df['Current_GWh'].clip(lower=0) + + imports_df.to_csv("imports_for_scenarios.csv") + + + fig_use_cap = make_subplots(specs=[[{"secondary_y": False}]]) fig_use_cap.add_trace(go.Bar(x=imports_df['countries'], y=imports_df['Current_%'].round(1),text=imports_df['Current_%'].round(1), name='Current', marker_color='red', @@ -2244,7 +2288,7 @@ def percentage_of_imports(): marker_line_width=0, opacity=1) fig_use_cap.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) fig_use_cap.update_xaxes(showline=True, linecolor=line_color, - title_text="Source: Our world in data ") + title_text="Source: Energy Balances, United Nations") return fig_use_cap def dependance_on_imports(): @@ -2252,11 +2296,13 @@ def dependance_on_imports(): total_energy_supply = functions.fetch_all_countries_demand(2019,Unit='GWh',Use='SummaryPlot')[1] net_imports = functions.fetch_all_countries_demand(2019,Unit='GWh',Use='Analysis')[15] current_share_of_imports = 100 * net_imports/total_energy_supply - - + int_aviation = functions.fetch_all_countries_demand(2019,Unit='GWh',Use='Analysis')[13] + int_marine = functions.fetch_all_countries_demand(2019,Unit='GWh',Use='Analysis')[12] + total_supply_incl_int_transit = total_energy_supply + abs(int_marine) + abs(int_aviation) + current_share_of_imports_inc_int_transit = 100 * net_imports/total_supply_incl_int_transit fig_use_cap = make_subplots(specs=[[{"secondary_y": False}]]) - fig_use_cap.add_trace(go.Bar(x=countries, y=current_share_of_imports,text=current_share_of_imports.round(0), name='This research', + fig_use_cap.add_trace(go.Bar(x=countries, y=current_share_of_imports_inc_int_transit,text=current_share_of_imports_inc_int_transit.round(0), name='This research', marker_color='green', )) @@ -2277,15 +2323,15 @@ def dependance_on_imports(): 'plot_bgcolor': 'rgba(0,0,0,0)', 'paper_bgcolor': 'rgba(0,0,0,0)', }) - fig_use_cap.update_yaxes(title_text="Dependance on net imports (%)", showline=True,rangemode='tozero',linecolor=line_color,gridcolor=line_color) + fig_use_cap.update_yaxes(title_text="Dependance on net imports (%)", linecolor=line_color,gridcolor=line_color) fig_use_cap.update_xaxes(showline=True,showgrid=False,linecolor=line_color,) fig_use_cap.update_layout( - title="Dependance on net imports (share of net imports in total demand)") + title="Dependence on net imports (share of net imports in total demand)") fig_use_cap.update_traces(marker_line_color=font_color, marker_line_width=0, opacity=1) - fig_use_cap.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) + fig_use_cap.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color,zeroline=False) fig_use_cap.update_xaxes(showline=True, linecolor=line_color, - title_text="Source: Our world in data ") + title_text="Source: Energy Balances, United Nations") return fig_use_cap @@ -2374,7 +2420,7 @@ def biomass_consumption_breakdown(): primary_production = df_1.iloc[0, 2] # print(primary_production) # df_1.loc[:,'primary_production'] = primary_production - df_1.loc[:,'Biofuels and Waste'] = 100 * df_1.loc[:,'Biofuels and Waste']/primary_production#/df_1.loc[:,'primary_production'] + df_1.loc[:,'Biofuels and Waste'] = 100 * abs(df_1.loc[:,'Biofuels and Waste'])/primary_production#/df_1.loc[:,'primary_production'] if df_final.shape[0] ==0: df_final = df_1 else: @@ -2402,7 +2448,7 @@ def biomass_consumption_breakdown(): 'plot_bgcolor': 'rgba(0,0,0,0)', 'paper_bgcolor': 'rgba(0,0,0,0)', }) - fig.update_yaxes(title_text="% biofuel and waste primary production", showline=True,rangemode='tozero',linecolor=line_color,gridcolor=line_color) + fig.update_yaxes(title_text="% biofuel and waste primary production", showline=True,linecolor=line_color,gridcolor=line_color) fig.update_xaxes(showline=True,showgrid=False,linecolor=line_color,) fig.update_layout( title="Breakdown of biofuel and waste consumption") @@ -2410,8 +2456,8 @@ def biomass_consumption_breakdown(): marker_line_width=0, opacity=1) fig.update_layout(legend={'title_text': ''}) - fig.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) - fig.update_xaxes(showline=True,linecolor=line_color, + fig.update_yaxes(linecolor=line_color,gridcolor=line_color,zeroline=False,showline=True) + fig.update_xaxes(showline=True,linecolor=line_color,zeroline = True, title_text="Source: Energy Balances, United Nations"), return fig @@ -2422,6 +2468,7 @@ def navigation_and_int_maritime(): list_of_consumers = ['International marine bunkers',"Domestic navigation"] path = 'Data/EnergyBalance' + path = 'Data/EnergyBalance' files = os.listdir(path) df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) @@ -2474,29 +2521,47 @@ def navigation_and_int_maritime(): -def dynamic_breakdown_figure_generation(from_="Primary production",list_of_consumers = ['International marine bunkers',"Domestic navigation"],carrier = 'Total Energy'): +def dynamic_breakdown_figure_generation(y_axis_title,from_="Primary production",list_of_consumers = ['International marine bunkers',"Domestic navigation"],carrier = 'Total Energy',destination_carrier = 'Total Energy'): import os import plotly.express as px - # list_of_consumers = ['International marine bunkers',"Domestic navigation"] path = 'Data/EnergyBalance' files = os.listdir(path) df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) - df = df[["Country ({})".format(files[-1]),"Transactions(down)/Commodity(right)",carrier]] - df_final = pd.DataFrame() - for country in Country_List: - df_1 = df[df["Country ({})".format(files[-1])]==country] - # total_primary_production = df_1.loc[from_, carrier] - total_primary_production = df_1[df_1["Transactions(down)/Commodity(right)"] == from_][carrier].values[0] - df_1.loc[:,carrier] = 100 * abs(df_1.loc[:,carrier])/total_primary_production# - if df_final.shape[0] == 0: - df_final = df_1 - else: - df_final = pd.concat([df_final,df_1]) + if carrier == destination_carrier: + df = df[["Country ({})".format(files[-1]),"Transactions(down)/Commodity(right)",carrier]] + df_final = pd.DataFrame() + for country in Country_List: + df_1 = df[df["Country ({})".format(files[-1])]==country] + total_source = 0 + for i in from_: + total_source += df_1[df_1["Transactions(down)/Commodity(right)"]==i][carrier].values[0] + df_1.loc[:,carrier] = 100 * abs(df_1.loc[:,carrier])/total_source# + if df_final.shape[0] == 0: + df_final = df_1 + else: + df_final = pd.concat([df_final,df_1]) + else: + df = df[["Country ({})".format(files[-1]),"Transactions(down)/Commodity(right)",carrier,destination_carrier]] + df_final = pd.DataFrame() + for country in Country_List: + df_1 = df[df["Country ({})".format(files[-1])]==country] + total_source = 0 + for i in from_: + total_source += df_1[df_1["Transactions(down)/Commodity(right)"] == i][carrier].values[0] + df_1.loc[:,destination_carrier] = 100 * abs(df_1.loc[:,destination_carrier])/total_source# + if df_final.shape[0] == 0: + df_final = df_1 + else: + df_final = pd.concat([df_final,df_1]) df_final = df_final[df_final["Transactions(down)/Commodity(right)"].isin(list_of_consumers)] + if carrier == destination_carrier: + y_axis = carrier + else: + y_axis = destination_carrier + fig = px.bar(df_final, x="Country ({})".format(files[-1]),color_discrete_sequence = px.colors.qualitative.Alphabet, y=y_axis, color="Transactions(down)/Commodity(right)") - fig = px.bar(df_final, x="Country ({})".format(files[-1]), y=carrier, color="Transactions(down)/Commodity(right)") fig.update_layout( legend=dict(bgcolor='rgba(0,0,0,0)', orientation="h", # yanchor="bottom", @@ -2506,7 +2571,7 @@ def dynamic_breakdown_figure_generation(from_="Primary production",list_of_consu ), font=dict( family="Calibri", - size=15, + size=16, color=font_color ), # hovermode="x" @@ -2517,7 +2582,7 @@ def dynamic_breakdown_figure_generation(from_="Primary production",list_of_consu 'plot_bgcolor': 'rgba(0,0,0,0)', 'paper_bgcolor': 'rgba(0,0,0,0)', }) - fig.update_yaxes(title_text="% of {} {}".format(carrier,from_), showline=True,rangemode='tozero',linecolor=line_color,gridcolor=line_color) + fig.update_yaxes(title_text=y_axis_title, showline=True,rangemode='tozero',linecolor=line_color,gridcolor=line_color) fig.update_xaxes(showline=True,showgrid=False,linecolor=line_color,) fig.update_layout( title="") @@ -2528,4 +2593,107 @@ def dynamic_breakdown_figure_generation(from_="Primary production",list_of_consu fig.update_yaxes(rangemode='tozero',linecolor=line_color,gridcolor=line_color) fig.update_xaxes(showline=True,linecolor=line_color, title_text="Source: Energy Balances, United Nations"), - return fig \ No newline at end of file + return fig + + +def dynamic_breakdown_of_sectors(sector): + import os + path = 'Data/EnergyBalance' + files = os.listdir(path) + df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) + + df = df[df["Transactions(down)/Commodity(right)"] == sector] + df= df.iloc[:,:13]# = df.iloc[:,2:12]/df["Total Energy"] + df.iloc[:,3:]=100*df.iloc[:,3:].div(df['Total Energy'], axis=0) + df.drop(columns=['Total Energy'],inplace=True) + df = df.melt(id_vars=df.columns[:3]) + + fig = px.bar(df, x="Country ({})".format(files[-1]), y='value', color="variable",color_discrete_sequence = px.colors.qualitative.Alphabet) + fig.update_layout( + legend=dict(bgcolor='rgba(0,0,0,0)', orientation="h", + # yanchor="bottom", + y=1.2, + # xanchor="right", + # x=0 + ), + font=dict( + family="Calibri", + size=16, + color=font_color + ), + # hovermode="x" + + ) + + fig.update_layout({ + 'plot_bgcolor': 'rgba(0,0,0,0)', + 'paper_bgcolor': 'rgba(0,0,0,0)', + }) + fig.update_yaxes(title_text="% of {}".format(sector), showline=True, rangemode='tozero', linecolor=line_color, + gridcolor=line_color) + fig.update_xaxes(showline=True, showgrid=False, linecolor=line_color, ) + fig.update_layout( + title="") + fig.update_traces(marker_line_color=font_color, + marker_line_width=0, opacity=1) + fig.update_layout(legend={'title_text': ''}) + + fig.update_yaxes(rangemode='tozero', linecolor=line_color, gridcolor=line_color) + fig.update_xaxes(showline=True, linecolor=line_color, + title_text="Source: Energy Balances, United Nations"), + # print(df.head(20)) + + return fig + + + +def dynamic_one_column_multiple_source(column,provider,y_axis_title): + import os + import plotly.express as px + + path = 'Data/EnergyBalance' + files = os.listdir(path) + + df = pd.read_csv("Data/EnergyBalance/{}/all_countries_df.csv".format(files[-1])) + df = df[["Country ({})".format(files[-1]),"Transactions(down)/Commodity(right)",column]] + # df_final = pd.DataFrame() + + df = df[df["Transactions(down)/Commodity(right)"].isin(provider)] + df[column] = df[column] * 0.277778 + df[column] = df[column].round(1) + fig = px.bar(df, x="Country ({})".format(files[-1]),text=column,color_discrete_sequence = px.colors.qualitative.Alphabet, y=column, color="Transactions(down)/Commodity(right)") + fig.update_layout( + legend=dict(bgcolor='rgba(0,0,0,0)', orientation="h", + # yanchor="bottom", + y=1.2, + # xanchor="right", + # x=0 + ), + font=dict( + family="Calibri", + size=16, + color=font_color + ), + # hovermode="x" + + ) + + fig.update_layout({ + 'plot_bgcolor': 'rgba(0,0,0,0)', + 'paper_bgcolor': 'rgba(0,0,0,0)', + }) + fig.update_yaxes(title_text=y_axis_title, showline=True, rangemode='tozero', linecolor=line_color, + gridcolor=line_color) + fig.update_xaxes(showline=True, showgrid=False, linecolor=line_color, ) + fig.update_layout( + title="") + fig.update_traces(marker_line_color=font_color, + marker_line_width=0, opacity=1) + fig.update_layout(legend={'title_text': ''}) + + fig.update_yaxes(rangemode='tozero', linecolor=line_color, gridcolor=line_color) + fig.update_xaxes(showline=True, linecolor=line_color, + title_text="Source: Energy Balances, United Nations"), + return fig + +# dynamic_breakdown_of_sectors("Primary production") \ No newline at end of file diff --git a/functions.py b/functions.py index f8cb376..c0ebd81 100644 --- a/functions.py +++ b/functions.py @@ -31,30 +31,37 @@ def fetch_single_country_demand(Country,Year,Unit='GWh'): final_demand = non_RE_demand + (final_consumption-final_consumption_electricity-ofWhichRenewable_final_consumption) * (1-electrification_efficiency_improvement) - df_world_per_capita_demand = pd.read_csv("Data/worldinData/per-capita-energy-use.csv") - per_capita_average = df_world_per_capita_demand['Primary energy consumption per capita (kWh/person)'].mean() #kWh/person df_pop = pd.read_csv('Data/Economic Indicators.csv') population = df_pop[df_pop.Country == Country]['Population'].values[0] - print(population) - wolrd_average_demand = per_capita_average * population/1000000 # GWh/year + # print(population) + net_zero_scenario_demand = 10 * population/1000 # GWh/year + net_zero_scenario_demand = net_zero_scenario_demand/0.277778 # to TJ if Unit =='GWh': final_demand = final_demand * 0.277778 non_RE_demand = non_RE_demand * 0.277778 - wolrd_average_demand = wolrd_average_demand * 0.277778 - return non_RE_demand,final_demand + net_zero_scenario_demand = net_zero_scenario_demand * 0.277778 + return non_RE_demand,final_demand,net_zero_scenario_demand def PV_area_single_country(Country,Year): PV_pot, Wind_pot, area, coastline = fetch_wind_PV_potential(Country) - non_RE_demand,final_demand = fetch_single_country_demand(Country,Year) + non_RE_demand,final_demand,net_zero_scenario_demand = fetch_single_country_demand(Country,Year) + PV_non_RE = 1.2 * non_RE_demand / PV_pot # MW PV_final_demand = 1.2 * final_demand / PV_pot # MW + PV_net_zero = 1.2 * net_zero_scenario_demand / PV_pot # MW + PV_area_non_RE = PV_non_RE / (100) # 0.1kw/m2 # Converted to km2 PV_area_non_RE_per = 100 * PV_area_non_RE / area + PV_area_final_demand = PV_final_demand / (100) # 0.1kw/m2 PV_area_final_demand_per = 100 * PV_area_final_demand / area - return PV_area_non_RE,PV_area_final_demand,PV_area_non_RE_per,PV_area_final_demand_per + PV_area_net_zero = PV_net_zero / (100) # 0.1kw/m2 + PV_area_net_zero_per = 100 * PV_area_net_zero / area + + return PV_area_non_RE,PV_area_final_demand,PV_area_net_zero,\ + PV_area_non_RE_per,PV_area_final_demand_per, PV_area_net_zero_per def Wind_area_single_country(Country,Year): @@ -93,6 +100,7 @@ def fetch_all_countries_demand(Year,Unit='GWh',Use="Analysis"): if Use == 'SummaryPlot': total_demand = df[df['Transactions(down)/Commodity(right)']=='Total energy supply']['Total Energy'].values #TJ#Total energy entering the country(oil and renewables) + renewables_in_total = df[df['Transactions(down)/Commodity(right)']=='Total energy supply']['memo: Of which Renewables'].values #TJ renewable_electricity_primary = df[df['Transactions(down)/Commodity(right)']=='Primary production']['Electricity'].values #TJ renewable_electricity = renewable_electricity_primary + transformed_elec_from_renewables @@ -107,7 +115,7 @@ def fetch_all_countries_demand(Year,Unit='GWh',Use="Analysis"): Int_marine_total = df[df['Transactions(down)/Commodity(right)'] == 'International marine bunkers']['Total Energy'].values Int_avi_total = df[df['Transactions(down)/Commodity(right)'] == 'International aviation bunkers']['Total Energy'].values exports_total = df[df['Transactions(down)/Commodity(right)'] == 'Exports']['Total Energy'].values - net_imports_total = all_imports + Int_marine_total + Int_avi_total + exports_total + net_imports_total = all_imports + exports_total #+ Int_marine_total + Int_avi_total @@ -126,7 +134,7 @@ def fetch_all_countries_demand(Year,Unit='GWh',Use="Analysis"): "Population"] * wolrd_average_per_capita_use / 1000000 # GWh/year world_average_demand = df_pop['average_scenario_demand_GWh']/0.277778 # it is calculated on GWh > convert to TJ - + Net_zero_scenario_demand_GWh = (df_pop["Population"] * 10 / 1000)/0.277778 #10MWh/person/year scenario >Tj if Unit == "GWh": non_RE_elec = non_RE_elec * 0.277778 total_demand = total_demand * 0.277778 @@ -143,16 +151,19 @@ def fetch_all_countries_demand(Year,Unit='GWh',Use="Analysis"): Int_avi_total = Int_avi_total * 0.277778 Int_marine_total = Int_marine_total * 0.277778 net_imports_total = net_imports_total * 0.277778 + Net_zero_scenario_demand_GWh = Net_zero_scenario_demand_GWh * 0.277778 df_demand = pd.DataFrame() df_demand['Country'] = Countries df_demand['Non-RE'] = non_RE_elec df_demand['Total'] = total_demand df_demand['World_average_demand'] = world_average_demand.round(0) + df_demand['10MWh/person_GWh'] = Net_zero_scenario_demand_GWh.round(0) + df_demand.to_csv("demand_df_{}.csv".format(Unit)) return [Countries,total_demand,imports_oil,Int_marine_oil,Int_avi_oil,transformation, transformation_losses,renewables_in_total, renewable_electricity,non_RE_elec,all_imports,world_average_demand, - Int_marine_total,Int_avi_total,exports_total,net_imports_total] + Int_marine_total,Int_avi_total,exports_total,net_imports_total,Net_zero_scenario_demand_GWh] def all_countries_cross_comparison_unstats(Year,Unit,Use): summary_df = pd.DataFrame() @@ -304,7 +315,7 @@ def calculate_PV_Wind_potential(available_land = 0.01,available_coastline = 0.1) df_technical_potential['sum_of_wind_and_solar_GWh'] = df_technical_potential['PV_technical_GWh'] + df_technical_potential['Wind_technical_GWh'] - df_technical_potential.to_csv('Wind_and_solar_technical_potential.csv') + # df_technical_potential.to_csv("Wind({})_and_solar({})_technical_potential.csv".format(available_coastline,available_land)) return df_technical_potential @@ -324,14 +335,17 @@ def calculate_rooftop_PV_potential(available_buildings = 0.3,PV_size = 2.5): rooftop_PV_generation_GWh = rooftop_PV_generation_GWh.astype(float) rooftop_PV_generation_GWh = rooftop_PV_generation_GWh.round(decimals=1) - rooftop_df['Generation_GWh'] = rooftop_PV_generation_GWh rooftop_df['Country'] = Countries rooftop_df['Capacity_MW'] = rooftop_capacity_MW rooftop_df['avaialble_homes'] = number_of_homes rooftop_df['Household_size'] = Household_size rooftop_df['Population'] = Population + rooftop_df['Generation_GWh'] = rooftop_PV_generation_GWh rooftop_df.to_csv('rooftop_Pv_potential.csv') return rooftop_df + + +# calculate_PV_Wind_potential(available_land=0.01,available_coastline=0) \ No newline at end of file