{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "77c69989",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py\n"
     ]
    }
   ],
   "source": [
    "from dash import callback, Output, Input, State, html\n",
    "from dash import callback_context\n",
    "import pandas as pd\n",
    "import plotly.graph_objects as go\n",
    "from dash.dependencies import Input, Output\n",
    "import plotly.express as px\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy.interpolate import make_interp_spline\n",
    "import matplotlib.image as mpimg\n",
    "import os \n",
    "import sys\n",
    "# Get the grandparent directory\n",
    "# Add the grandparent directory where the correct functions.py is\n",
    "grandparent_path = os.path.abspath(os.path.join(os.getcwd(), '..'))\n",
    "sys.path.insert(0, grandparent_path)\n",
    "import matplotlib.ticker as ticker\n",
    "\n",
    "# Now re-import the correct functions.py\n",
    "import importlib\n",
    "import functions\n",
    "importlib.reload(functions)  # Force reload in case it's cached\n",
    "from Thesis_plot.functions import irradiance_analysis\n",
    "\n",
    "print(functions.__file__)  # Should now show ...\\Data\\dash\\functions.py\n",
    "from plotly.subplots import make_subplots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "7f27c4c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def design_function(Class ,capacity, sea_depth):\n",
    "        modules_per_platform = {\n",
    "            \"Class1\": 70,   #for 70 modules\n",
    "            \"Class2\": 100,   #for 100 modules\n",
    "            \"Class3\": 80   #for 80 modules\n",
    "        }\n",
    "        N_modules= capacity*10**3/0.4 #Number of modules\n",
    "        N_platforms= int(N_modules/modules_per_platform.get(Class, None)) #Number of pltaforms\n",
    "        Chain_Length=sea_depth*1.4 #Total length of one single chain\n",
    "\n",
    "        return  N_modules, N_platforms,Chain_Length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "14e047d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mooring_cost_function(Class, Chain_Length, N_platforms):\n",
    "    N_lines = {\n",
    "        \"Class1\": 1,   #for 70 modules\n",
    "        \"Class2\": 1,   #for 100 modules\n",
    "        \"Class3\": 2   #for 80 modules\n",
    "    }\n",
    "\n",
    "    #Cost of the mooring system\n",
    "    C_chain=Chain_Length*18.2*2.75*N_platforms*N_lines.get(Class, None) #Cost Chains\n",
    "    C_anchors=736000*0.052/9.81*N_platforms*N_lines.get(Class, None) #Cost anchors\n",
    "    Mooring_Cost=(C_chain+C_anchors)\n",
    "\n",
    "    return  Mooring_Cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "3100d853",
   "metadata": {},
   "outputs": [],
   "source": [
    "def platfrom_cost_function(Class, N_platforms):\n",
    "\n",
    "\n",
    "    class_type_mapping = {\n",
    "        \"Class1\": 11366,   #for 70 modules\n",
    "        \"Class2\": 9620,   #for 100 modules\n",
    "        \"Class3\": 16503    #for 80 modules\n",
    "    }\n",
    "\n",
    "    Platfroms_Cost = class_type_mapping.get(Class, None)*N_platforms\n",
    "\n",
    "    return   Platfroms_Cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "04686ebc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def electrical_cost(Power, distance):\n",
    "    #Power in MW\n",
    "    #Distance in km\n",
    "\n",
    "    C_substation=157360*Power\n",
    "    C_cables=2.18*Power*distance\n",
    "    Panels_Cost=300*10**6*Power/400\n",
    "\n",
    "    Electrical_cost=C_cables+C_substation\n",
    "\n",
    "    return   Panels_Cost, Electrical_cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "568605bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def labour_cost_function(Mooring_Cost,Platfroms_Cost,Electrical_cost,Panels_Cost):\n",
    "  \n",
    "    #Total labour cost, equal to 5% of the total components cost\n",
    "    C_labour=(Mooring_Cost+Platfroms_Cost+Electrical_cost+Panels_Cost)*0.05\n",
    "\n",
    "    return   C_labour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "80f8cb18",
   "metadata": {},
   "outputs": [],
   "source": [
    "def OPEX_function(Class, distance,N_platforms,capacity,location):\n",
    "    class_type_mapping = {\n",
    "        \"Class1\": 5,   #for 70 modules\n",
    "        \"Class2\": 5,   #for 100 modules\n",
    "        \"Class3\": 2    #for 80 modules\n",
    "    }\n",
    "\n",
    "\n",
    "    v_boat=20000 #m/h \n",
    "    c_boat=120 #e/h\n",
    "    c_divers=90*4 #e/h 4 divers\n",
    "    c_workers=50*3 #e/h i consider 3 workers\n",
    "    T_install=0.5 #Time required to install each floating PV structure \n",
    "\n",
    "    if location in ['Ceuta','Jersey', 'Gulf_Gabes','Zadar']:\n",
    "        cost_SPR=N_platforms*(T_install+2*distance/v_boat)*(c_boat+c_divers+c_workers)/class_type_mapping.get(Class, None)\n",
    "    else: cost_SPR=0\n",
    "\n",
    "    #Yearly opertional Cost equal to $15 for each kw of nominal power\n",
    "\n",
    "    Opex=(capacity*15*10**3+cost_SPR)/10**3  #result in kEuro\n",
    "\n",
    "    return Opex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "79fd8bbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def COSTS(Class ,Power, sea_depth, distance):\n",
    "    [N_modules, N_platforms,Chain_Length]=design_function(Class ,Power, sea_depth)\n",
    "    Mooring_Cost=mooring_cost_function(Class, Chain_Length, N_platforms)\n",
    "    Platfroms_Cost=platfrom_cost_function(Class, N_platforms)\n",
    "    [Panels_Cost, Electrical_cost] =electrical_cost(Power, distance)\n",
    "    C_labour=labour_cost_function(Mooring_Cost,Platfroms_Cost,Electrical_cost,Panels_Cost)\n",
    "    return Mooring_Cost, Platfroms_Cost, Panels_Cost, Electrical_cost, C_labour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "4681dcf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sea_depth=40\n",
    "distance=1000\n",
    "power=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "62618135",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32428 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "# Define your inputs\n",
    "classes = ['Class1', 'Class2', 'Class3']\n",
    "\n",
    "# Collect cost breakdowns\n",
    "data = {}\n",
    "for cls in classes:\n",
    "    mooring, platform, panels, electrical, labour = COSTS(cls, power, sea_depth, distance)\n",
    "    data[cls] = [mooring, platform, panels, electrical, labour]\n",
    "\n",
    "# Create DataFrame: rows = components, columns = classes\n",
    "components = ['Mooring', 'Platform', 'Panels', 'Electrical', 'Labour']\n",
    "\n",
    "df = pd.DataFrame(data, index=components)\n",
    "\n",
    "# Convert to k€\n",
    "df = df / 1000\n",
    "# Plot grouped bar chart (components on x-axis, classes in legend)\n",
    "ax = df.plot(kind='bar', figsize=(12, 6))\n",
    "\n",
    "plt.ylabel('Cost [k€]')\n",
    "plt.xticks(rotation=0)\n",
    "plt.legend(title='Platform Class')\n",
    "plt.grid(axis='y')\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "05e42666",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9506.599999999999"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "112*18.2*2.75+3901"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "7d47705a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 78344 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "# Collect cost breakdowns\n",
    "data = {}\n",
    "for cls in classes:\n",
    "    mooring, platform, panels, electrical, labour = COSTS(cls, power, sea_depth, distance)\n",
    "    data[cls] = {\n",
    "        'Mooring': mooring,\n",
    "        'Platform': platform,\n",
    "        'Panels': panels,\n",
    "        'Electrical': electrical,\n",
    "        'Labour': labour\n",
    "    }\n",
    "\n",
    "# Plot pie charts\n",
    "fig, axes = plt.subplots(1, 3, figsize=(12,6 ))\n",
    "\n",
    "for i, cls in enumerate(classes):\n",
    "    costs = data[cls]\n",
    "    labels = list(costs.keys())\n",
    "    values = list(costs.values())\n",
    "    \n",
    "    axes[i].pie(values, labels=labels, autopct='%1.1f%%', startangle=90)\n",
    "    axes[i].set_title(f\"{cls}\")\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "4f609458",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 96248 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Power range: from 0.1 to 2.0 in steps of 0.1\n",
    "power_range = np.arange(0.1, 2.1, 0.1)\n",
    "classes = ['Class1', 'Class2', 'Class3']\n",
    "\n",
    "\n",
    "# Dictionary to store results\n",
    "results = {cls: [] for cls in classes}\n",
    "\n",
    "# Loop over power values\n",
    "for power_value in power_range:\n",
    "    for cls in classes:\n",
    "        mooring, platform, panels, electrical, labour = COSTS(cls, power_value, sea_depth, distance)\n",
    "        total_cost = mooring + platform + panels + electrical + labour\n",
    "        results[cls].append(total_cost)\n",
    "\n",
    "# Create DataFrame for plotting\n",
    "df_sensitivity = pd.DataFrame(results, index=power_range)\n",
    "\n",
    "# Plot\n",
    "plt.figure(figsize=(12, 6))\n",
    "for cls in classes:\n",
    "    plt.plot(df_sensitivity.index, df_sensitivity[cls]/10**3, label=cls, marker='o')\n",
    "plt.gca().get_yaxis().set_major_formatter(plt.matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))\n",
    "\n",
    "plt.xlabel('Installed Power [MW]')\n",
    "plt.ylabel('Total Cost [k€]')\n",
    "plt.grid(True)\n",
    "plt.legend(title='Platform Class')\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "7737ea2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def OPEX_function(distance,N_platforms,capacity,location):\n",
    "\n",
    "    v_boat=20000 #m/h \n",
    "    c_boat=120 #e/h\n",
    "    c_divers=90*4 #e/h 4 divers\n",
    "    c_workers=50*3 #e/h i consider 3 workers\n",
    "    T_install=0.5 #Time required to install each floating PV structure \n",
    "\n",
    "    if location in ['Ceuta','Jersey', 'Gulf_Gabes','Zadar']:\n",
    "        cost_SPR=N_platforms*(T_install+2*distance/v_boat)*(c_boat+c_divers+c_workers)\n",
    "    else: cost_SPR=0\n",
    "\n",
    "    #Yearly opertional Cost equal to $15 for each kw of nominal power\n",
    "\n",
    "    Opex=(capacity*15*10**3+cost_SPR)/10**3  #result in kEuro\n",
    "\n",
    "    return Opex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "374b8830",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Location</th>\n",
       "      <th>Class</th>\n",
       "      <th>Seasonal Removal OPEX [k€/year]</th>\n",
       "      <th>Fixed OPEX [k€/year]</th>\n",
       "      <th>Total OPEX [k€/year]</th>\n",
       "      <th>CAPEX [k€/year]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ceuta</td>\n",
       "      <td>Class1</td>\n",
       "      <td>13.23</td>\n",
       "      <td>15</td>\n",
       "      <td>28.23</td>\n",
       "      <td>1619.094100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Gotland</td>\n",
       "      <td>Class3</td>\n",
       "      <td>0.00</td>\n",
       "      <td>15</td>\n",
       "      <td>15.00</td>\n",
       "      <td>1928.628199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Gulf_Gabes</td>\n",
       "      <td>Class2</td>\n",
       "      <td>9.45</td>\n",
       "      <td>15</td>\n",
       "      <td>24.45</td>\n",
       "      <td>1383.525286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pantelleria</td>\n",
       "      <td>Class3</td>\n",
       "      <td>0.00</td>\n",
       "      <td>15</td>\n",
       "      <td>15.00</td>\n",
       "      <td>1928.628199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Zadar</td>\n",
       "      <td>Class2</td>\n",
       "      <td>9.45</td>\n",
       "      <td>15</td>\n",
       "      <td>24.45</td>\n",
       "      <td>1383.525286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Jersey</td>\n",
       "      <td>Class1</td>\n",
       "      <td>13.23</td>\n",
       "      <td>15</td>\n",
       "      <td>28.23</td>\n",
       "      <td>1619.094100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Location   Class  Seasonal Removal OPEX [k€/year]  Fixed OPEX [k€/year]  \\\n",
       "0        Ceuta  Class1                            13.23                    15   \n",
       "1      Gotland  Class3                             0.00                    15   \n",
       "2   Gulf_Gabes  Class2                             9.45                    15   \n",
       "3  Pantelleria  Class3                             0.00                    15   \n",
       "4        Zadar  Class2                             9.45                    15   \n",
       "5       Jersey  Class1                            13.23                    15   \n",
       "\n",
       "   Total OPEX [k€/year]  CAPEX [k€/year]  \n",
       "0                 28.23      1619.094100  \n",
       "1                 15.00      1928.628199  \n",
       "2                 24.45      1383.525286  \n",
       "3                 15.00      1928.628199  \n",
       "4                 24.45      1383.525286  \n",
       "5                 28.23      1619.094100  "
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "import pandas as pd\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "# Define the class per location\n",
    "location_classes = {\n",
    "    'Ceuta': 'Class1',\n",
    "    'Gotland': 'Class3',\n",
    "    'Gulf_Gabes': 'Class2',\n",
    "    'Pantelleria': 'Class3',\n",
    "    'Zadar': 'Class2',\n",
    "    'Jersey': 'Class1'\n",
    "}\n",
    "\n",
    "print(power)\n",
    "# Store the results\n",
    "opex_results = []\n",
    "\n",
    "# Loop through locations and apply the class rules\n",
    "for location, Class in location_classes.items():\n",
    "    mooring, platform, panels, electrical, labour = COSTS(Class, power, sea_depth, distance)\n",
    "    total_cost = mooring + platform + panels + electrical + labour\n",
    "    N_modules, N_platforms, Chain_Length = design_function(Class, power, sea_depth)\n",
    "    Opex = OPEX_function(distance, N_platforms, power, location)\n",
    "    opex_results.append({\n",
    "        'Location': location,\n",
    "        'Class': Class,\n",
    "        'Seasonal Removal OPEX [k€/year]': Opex-power*15,\n",
    "        'Fixed OPEX [k€/year]': 15,\n",
    "        'Total OPEX [k€/year]': Opex,\n",
    "        'CAPEX [k€/year]': total_cost/1000\n",
    "    })\n",
    "\n",
    "# Convert to DataFrame and show it\n",
    "opex_df = pd.DataFrame(opex_results)\n",
    "opex_df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "2e07a544",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ceuta         & Class1 & 13.23 & 15.00 & 28.23 & 1619.09 \\\\\n",
      "Gotland       & Class3 & 0.00 & 15.00 & 15.00 & 1928.63 \\\\\n",
      "Gulf_Gabes    & Class2 & 9.45 & 15.00 & 24.45 & 1383.53 \\\\\n",
      "Pantelleria   & Class3 & 0.00 & 15.00 & 15.00 & 1928.63 \\\\\n",
      "Zadar         & Class2 & 9.45 & 15.00 & 24.45 & 1383.53 \\\\\n",
      "Jersey        & Class1 & 13.23 & 15.00 & 28.23 & 1619.09 \\\\\n"
     ]
    }
   ],
   "source": [
    "for _, row in opex_df.iterrows():\n",
    "    formatted_row = f\"{row['Location']:<13} & {row['Class']} & {row['Seasonal Removal OPEX [k€/year]']:.2f} & {row['Fixed OPEX [k€/year]']:.2f} & {row['Total OPEX [k€/year]']:.2f} & {row['CAPEX [k€/year]']:.2f} \\\\\\\\\"\n",
    "    print(formatted_row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "c095948b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(13.23)"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "opex_df.loc[opex_df['Location'] == 'Ceuta', 'Seasonal Removal OPEX [k€/year]'].iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "d43f0180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>location</th>\n",
       "      <th>Savings</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ceuta</td>\n",
       "      <td>264.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Gotland</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Gulf_Gabes</td>\n",
       "      <td>189.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pantelleria</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Zadar</td>\n",
       "      <td>189.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Jersey</td>\n",
       "      <td>264.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      location  Savings\n",
       "0        Ceuta    264.6\n",
       "1      Gotland      0.0\n",
       "2   Gulf_Gabes    189.0\n",
       "3  Pantelleria      0.0\n",
       "4        Zadar    189.0\n",
       "5       Jersey    264.6"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "opex_savings=[]\n",
    "for loc in opex_df['Location']:\n",
    "    opex_savings.append({\n",
    "        'location': loc,\n",
    "        'Savings': opex_df.loc[opex_df['Location'] == loc, 'Seasonal Removal OPEX [k€/year]'].iloc[0]*20\n",
    "    })\n",
    "\n",
    "pd.DataFrame(opex_savings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "bd326312",
   "metadata": {},
   "outputs": [],
   "source": [
    "#1MW and in MWh\n",
    "Yearly_Energy=pd.read_csv('../PV\\df_yearly_production_1mw.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "9bd3248c",
   "metadata": {},
   "outputs": [],
   "source": [
    "cost_dieslel = pd.Series({\n",
    "    'Ceuta':         0.637, \n",
    "    'Gotland':       0.814,\n",
    "    'Gulf_Gabes':    0.307,\n",
    "    'Pantelleria':   0.767,\n",
    "    'Zadar':         0.744,\n",
    "    'Jersey':        0.600\n",
    "})\n",
    "\n",
    "cost_grid = pd.Series({\n",
    "    'Ceuta':         0.115, \n",
    "    'Gotland':       0.087,\n",
    "    'Gulf_Gabes':    0.184,\n",
    "    'Pantelleria':   0.16,\n",
    "    'Zadar':         0.117,\n",
    "    'Jersey':        0.085\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "076f62ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "cost_energy_per_kwh = pd.Series(dtype=float)\n",
    "for location in cost_grid.index:\n",
    "    cost_energy_per_kwh[location]=cost_grid[location]*0.80+cost_dieslel[location]*0.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f4fc172",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "b339645f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Ceuta          0.2194\n",
       "Gotland        0.2324\n",
       "Gulf_Gabes     0.2086\n",
       "Pantelleria    0.2814\n",
       "Zadar          0.2424\n",
       "Jersey         0.1880\n",
       "dtype: float64"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cost_energy_per_kwh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "7a765024",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3954430.2"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "18957*0.2086*10**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "6311a186",
   "metadata": {},
   "outputs": [],
   "source": [
    "class_location = pd.Series({\n",
    "    'Ceuta':         'Class1',\n",
    "    'Gotland':       'Class3',\n",
    "    'Gulf_Gabes':    'Class2',\n",
    "    'Pantelleria':   'Class3',\n",
    "    'Zadar':         'Class2',\n",
    "    'Jersey':        'Class1'\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "2f6612b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def NPV(production , C_EN , Opex , Capex, life_time):\n",
    "        i=0.064\n",
    "        years = list(range(0, life_time))  # Example: 25 years\n",
    "        CC = [(prod * C_EN*10**3 - Opex) / (1 + i) ** year for year, prod in zip(years, production)]\n",
    "        NPV = [-Capex]\n",
    "        # Compute cumulative NPV\n",
    "        for d in range(1, len(years)):\n",
    "            NPV.append(NPV[d - 1] + CC[d - 1])\n",
    "\n",
    "        # Create a DataFrame with years and NPV\n",
    "        npv_df = pd.DataFrame({'Year': years, 'NPV': NPV})\n",
    "        return npv_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "71e6b9fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "life_time=20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "72586885",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\youss\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\matplotlib\\cbook.py:1762: FutureWarning: Calling float on a single element Series is deprecated and will raise a TypeError in the future. Use float(ser.iloc[0]) instead\n",
      "  return math.isfinite(val)\n",
      "C:\\Users\\youss\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\matplotlib\\cbook.py:1398: FutureWarning: Calling float on a single element Series is deprecated and will raise a TypeError in the future. Use float(ser.iloc[0]) instead\n",
      "  return np.asarray(x, float)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 93948 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = plt.subplots(2, 3, figsize=(12, 6), sharex=True)\n",
    "axes = axes.flatten()\n",
    "locations = ['Ceuta', 'Gotland', 'Gulf_Gabes', 'Pantelleria', 'Zadar', 'Jersey']\n",
    "\n",
    "for i, loc in enumerate(locations):\n",
    "    production = Yearly_Energy[loc]\n",
    "    C_EN = cost_energy_per_kwh[loc]\n",
    "    Opex = opex_df[opex_df.Location == loc]['Total OPEX [k€/year]']*10**3\n",
    "    Capex = opex_df[opex_df.Location == loc]['CAPEX [k€/year]']*10**3\n",
    "    npv_series = NPV(production, C_EN, Opex, Capex, life_time)\n",
    "\n",
    "    axes[i].plot(npv_series.index, npv_series.values, label=loc)\n",
    "    axes[i].set_title(f\"NPV - {loc}\")\n",
    "    axes[i].set_xlabel(\"Year\")\n",
    "    axes[i].set_ylabel(\"Cumulative NPV [€]\")\n",
    "    axes[i].grid(True)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "3cad7d89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Ceuta       Gotland    Gulf_Gabes   Pantelleria         Zadar  \\\n",
      "Year                                                                         \n",
      "0    -1.619094e+06 -1.928628e+06 -1.383525e+06 -1.928628e+06 -1.383525e+06   \n",
      "1    -1.304240e+06 -1.744580e+06 -1.151730e+06 -1.539567e+06 -1.132062e+06   \n",
      "2    -1.014774e+06 -1.575344e+06 -9.386944e+05 -1.181504e+06 -9.009106e+05   \n",
      "3    -7.486584e+05 -1.419734e+06 -7.429091e+05 -8.519737e+05 -6.884399e+05   \n",
      "4    -5.040210e+05 -1.276657e+06 -5.629865e+05 -5.487080e+05 -4.931493e+05   \n",
      "5    -2.791377e+05 -1.145111e+06 -3.976498e+05 -2.696184e+05 -3.136578e+05   \n",
      "6    -7.242228e+04 -1.024169e+06 -2.457247e+05 -1.278222e+04 -1.486953e+05   \n",
      "7     1.175843e+05 -9.129827e+05 -1.061306e+05  2.235707e+05  2.906789e+03   \n",
      "8     2.922247e+05 -8.107682e+05  2.212608e+04  4.410698e+05  1.422236e+05   \n",
      "9     4.527339e+05 -7.168060e+05  1.399596e+05  6.412153e+05  2.702439e+05   \n",
      "10    6.002484e+05 -6.304335e+05  2.482106e+05  8.253882e+05  3.878777e+05   \n",
      "11    7.358133e+05 -5.510412e+05  3.476526e+05  9.948598e+05  4.959615e+05   \n",
      "12    8.603904e+05 -4.780683e+05  4.389967e+05  1.150800e+06  5.952653e+05   \n",
      "13    9.748642e+05 -4.109990e+05  5.228972e+05  1.294288e+06  6.864971e+05   \n",
      "14    1.080049e+06 -3.493585e+05  5.999556e+05  1.426313e+06  7.703080e+05   \n",
      "15    1.176692e+06 -2.927103e+05  6.707253e+05  1.547789e+06  8.472971e+05   \n",
      "16    1.265483e+06 -2.406526e+05  7.357151e+05  1.659557e+06  9.180153e+05   \n",
      "17    1.347055e+06 -1.928159e+05  7.953929e+05  1.762390e+06  9.829692e+05   \n",
      "18    1.421991e+06 -1.488602e+05  8.501889e+05  1.856999e+06  1.042625e+06   \n",
      "19    1.490825e+06 -1.084730e+05  9.004989e+05  1.944042e+06  1.097411e+06   \n",
      "\n",
      "            Jersey  \n",
      "Year                \n",
      "0    -1.619094e+06  \n",
      "1    -1.412573e+06  \n",
      "2    -1.222887e+06  \n",
      "3    -1.048675e+06  \n",
      "4    -8.886855e+05  \n",
      "5    -7.417671e+05  \n",
      "6    -6.068615e+05  \n",
      "7    -4.829955e+05  \n",
      "8    -3.692741e+05  \n",
      "9    -2.648744e+05  \n",
      "10   -1.690399e+05  \n",
      "11   -8.107481e+04  \n",
      "12   -3.397337e+02  \n",
      "13    7.375333e+04  \n",
      "14    1.417449e+05  \n",
      "15    2.041318e+05  \n",
      "16    2.613708e+05  \n",
      "17    3.138816e+05  \n",
      "18    3.620502e+05  \n",
      "19    4.062312e+05  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "locations = ['Ceuta', 'Gotland', 'Gulf_Gabes', 'Pantelleria', 'Zadar', 'Jersey']\n",
    "npv_df = pd.DataFrame()\n",
    "\n",
    "for loc in locations:\n",
    "    production = Yearly_Energy[loc]\n",
    "    C_EN = cost_energy_per_kwh[loc]\n",
    "    Opex = opex_df[opex_df.Location == loc]['Total OPEX [k€/year]'].values[0] * 1e3\n",
    "    Capex = opex_df[opex_df.Location == loc]['CAPEX [k€/year]'].values[0] * 1e3\n",
    "\n",
    "    npv_series = NPV(production, C_EN, Opex, Capex, life_time)\n",
    "\n",
    "    # Assuming npv_series is a DataFrame with columns \"Year\" and \"NPV\"\n",
    "    npv_df[loc] = npv_series[\"NPV\"].values\n",
    "\n",
    "# Add year as index (assuming all npv_series have the same \"Year\" range)\n",
    "npv_df.index = npv_series[\"Year\"]\n",
    "npv_df.index.name = \"Year\"\n",
    "\n",
    "print(npv_df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "a0c7b37b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Year\n",
       "0    -1.619094e+06\n",
       "1    -1.412573e+06\n",
       "2    -1.222887e+06\n",
       "3    -1.048675e+06\n",
       "4    -8.886855e+05\n",
       "5    -7.417671e+05\n",
       "6    -6.068615e+05\n",
       "7    -4.829955e+05\n",
       "8    -3.692741e+05\n",
       "9    -2.648744e+05\n",
       "10   -1.690399e+05\n",
       "11   -8.107481e+04\n",
       "12   -3.397337e+02\n",
       "13    7.375333e+04\n",
       "14    1.417449e+05\n",
       "15    2.041318e+05\n",
       "16    2.613708e+05\n",
       "17    3.138816e+05\n",
       "18    3.620502e+05\n",
       "19    4.062312e+05\n",
       "Name: Jersey, dtype: float64"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npv_df['Jersey']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "592a447c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 118960 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.cm as cm\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(12, 6))\n",
    "locations = ['Ceuta', 'Gotland', 'Gulf_Gabes', 'Pantelleria', 'Zadar', 'Jersey']\n",
    "\n",
    "# Use a qualitative colormap\n",
    "colors = plt.get_cmap('tab10')\n",
    "\n",
    "for i, loc in enumerate(locations):\n",
    "    production = Yearly_Energy[loc]\n",
    "    C_EN = cost_energy_per_kwh[loc]\n",
    "    Opex = opex_df[opex_df.Location == loc]['Total OPEX [k€/year]'].values[0] * 10**3\n",
    "    Capex = opex_df[opex_df.Location == loc]['CAPEX [k€/year]'].values[0] * 10**3\n",
    "    npv_series = NPV(production, C_EN, Opex, Capex, life_time)\n",
    "\n",
    "    ax.plot(npv_series.index, npv_series.values/1000, label=loc, color=colors(i))\n",
    "\n",
    "ax.set_xlabel(\"Year\")\n",
    "ax.set_ylabel(\"Cumulative NPV [k€]\")\n",
    "ax.grid(True)\n",
    "ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))\n",
    "\n",
    "# De-duplicate legend\n",
    "handles, labels = ax.get_legend_handles_labels()\n",
    "unique = dict(zip(labels, handles))\n",
    "ax.legend(unique.values(), unique.keys())\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "eb058c08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "703383.0"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "578983.00 + 389000.00 -264600.00"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "3bc70e6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_lcoe(life_time, capex, opex, production):\n",
    "\n",
    "    years=list(range(0, life_time))\n",
    "    discount_rate=0.06\n",
    "    # Calculate discounted OPEX and production\n",
    "    discounted_opex = [opex / (1 + discount_rate) ** year for year in years]\n",
    "    discounted_production = [production / (1 + discount_rate) ** year for year, production in zip(years, production)]\n",
    "\n",
    "    # Exclude the last year for discounted production sum\n",
    "    discounted_production_sum = sum(discounted_production[:-1])\n",
    "\n",
    "    # Calculate LCOE\n",
    "    lcoe = (capex + sum(discounted_opex)) / discounted_production_sum\n",
    "\n",
    "    return lcoe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "11b49ff6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LCOE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Ceuta</th>\n",
       "      <td>122.229323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gotland</th>\n",
       "      <td>240.069073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gulf_Gabes</th>\n",
       "      <td>133.273122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pantelleria</th>\n",
       "      <td>143.197570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Zadar</th>\n",
       "      <td>143.828083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jersey</th>\n",
       "      <td>153.069945</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   LCOE\n",
       "Ceuta        122.229323\n",
       "Gotland      240.069073\n",
       "Gulf_Gabes   133.273122\n",
       "Pantelleria  143.197570\n",
       "Zadar        143.828083\n",
       "Jersey       153.069945"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lcoe_dict = {}\n",
    "\n",
    "for loc in locations:\n",
    "    row = opex_df[opex_df['Location'] == loc]\n",
    "    if row.empty or loc not in Yearly_Energy:\n",
    "        continue\n",
    "\n",
    "    production = Yearly_Energy[loc]\n",
    "    Opex = row['Total OPEX [k€/year]'].values[0] * 1e3\n",
    "    Capex = row['CAPEX [k€/year]'].values[0] * 1e3\n",
    "\n",
    "    lcoe_value = calculate_lcoe(life_time, Capex, Opex, production)  # this is a float\n",
    "    lcoe_dict[loc] = lcoe_value\n",
    "\n",
    "# Convert to DataFrame (location as index)\n",
    "lcoe_df = pd.DataFrame.from_dict(lcoe_dict, orient='index', columns=['LCOE'])\n",
    "lcoe_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "da62b10a",
   "metadata": {},
   "outputs": [],
   "source": [
    "loc='Ceuta'\n",
    "production = Yearly_Energy[loc]\n",
    "Opex = opex_df[opex_df.Location == loc]['Total OPEX [k€/year]'].values[0] * 1e3\n",
    "Capex = opex_df[opex_df.Location == loc]['CAPEX [k€/year]'].values[0] * 1e3\n",
    "\n",
    "lcoe_series = calculate_lcoe(life_time, Capex, Opex, production)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "fdff3157",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(122.22932300024567)"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lcoe_series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "160de3f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "loc='Ceuta'\n",
    "life_time=20\n",
    "capex= opex_df[opex_df.Location == loc]['CAPEX [k€/year]']*10**3\n",
    "opex=opex_df[opex_df.Location == loc]['Total OPEX [k€/year]']*10**3\n",
    "production=Yearly_Energy['Ceuta']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "ee6d572f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    122.229323\n",
       "dtype: float64"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calculate_lcoe(life_time, capex, opex, production)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b39d7f85",
   "metadata": {},
   "source": [
    "EMISSIONS AVVOIDED"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "3f6cb0ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "CO2_grid = pd.Series({\n",
    "    'Ceuta':         0.115, \n",
    "    'Gotland':       0.036,\n",
    "    'Gulf_Gabes':    0.560,\n",
    "    'Pantelleria':   0.288,\n",
    "    'Zadar':         0.174,\n",
    "    'Jersey':        0.044\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "79beb14f",
   "metadata": {},
   "outputs": [],
   "source": [
    "CO2_combined=pd.Series()\n",
    "for location in CO2_grid.index:\n",
    "    CO2_combined[location]=CO2_grid[location]*0.8+0.2*1.585"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "96866aa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Ceuta          0.4090\n",
       "Gotland        0.3458\n",
       "Gulf_Gabes     0.7650\n",
       "Pantelleria    0.5474\n",
       "Zadar          0.4562\n",
       "Jersey         0.3522\n",
       "dtype: float64"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CO2_combined"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "66678dfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "639.2669999999999"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1563*0.4090"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "61fb337a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Year</th>\n",
       "      <th>Ceuta</th>\n",
       "      <th>Gulf_Gabes</th>\n",
       "      <th>Gotland</th>\n",
       "      <th>Jersey</th>\n",
       "      <th>Zadar</th>\n",
       "      <th>Pantelleria</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1563.737698</td>\n",
       "      <td>1228.403903</td>\n",
       "      <td>856.489400</td>\n",
       "      <td>1248.674913</td>\n",
       "      <td>1138.256313</td>\n",
       "      <td>1435.894584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1532.462944</td>\n",
       "      <td>1203.835825</td>\n",
       "      <td>839.359612</td>\n",
       "      <td>1223.701414</td>\n",
       "      <td>1115.491186</td>\n",
       "      <td>1407.176693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1501.813685</td>\n",
       "      <td>1179.759108</td>\n",
       "      <td>822.572419</td>\n",
       "      <td>1199.227386</td>\n",
       "      <td>1093.181363</td>\n",
       "      <td>1379.033159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1471.777412</td>\n",
       "      <td>1156.163926</td>\n",
       "      <td>806.120971</td>\n",
       "      <td>1175.242838</td>\n",
       "      <td>1071.317735</td>\n",
       "      <td>1351.452496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1442.341863</td>\n",
       "      <td>1133.040647</td>\n",
       "      <td>789.998552</td>\n",
       "      <td>1151.737982</td>\n",
       "      <td>1049.891381</td>\n",
       "      <td>1324.423446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>1413.495026</td>\n",
       "      <td>1110.379835</td>\n",
       "      <td>774.198581</td>\n",
       "      <td>1128.703222</td>\n",
       "      <td>1028.893553</td>\n",
       "      <td>1297.934977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>1385.225126</td>\n",
       "      <td>1088.172238</td>\n",
       "      <td>758.714609</td>\n",
       "      <td>1106.129158</td>\n",
       "      <td>1008.315682</td>\n",
       "      <td>1271.976277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>1357.520623</td>\n",
       "      <td>1066.408793</td>\n",
       "      <td>743.540317</td>\n",
       "      <td>1084.006574</td>\n",
       "      <td>988.149368</td>\n",
       "      <td>1246.536752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>1330.370211</td>\n",
       "      <td>1045.080617</td>\n",
       "      <td>728.669510</td>\n",
       "      <td>1062.326443</td>\n",
       "      <td>968.386381</td>\n",
       "      <td>1221.606017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>1303.762806</td>\n",
       "      <td>1024.179005</td>\n",
       "      <td>714.096120</td>\n",
       "      <td>1041.079914</td>\n",
       "      <td>949.018653</td>\n",
       "      <td>1197.173896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>1277.687550</td>\n",
       "      <td>1003.695425</td>\n",
       "      <td>699.814198</td>\n",
       "      <td>1020.258316</td>\n",
       "      <td>930.038280</td>\n",
       "      <td>1173.230418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>1252.133799</td>\n",
       "      <td>983.621516</td>\n",
       "      <td>685.817914</td>\n",
       "      <td>999.853150</td>\n",
       "      <td>911.437515</td>\n",
       "      <td>1149.765810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>1227.091123</td>\n",
       "      <td>963.949086</td>\n",
       "      <td>672.101556</td>\n",
       "      <td>979.856087</td>\n",
       "      <td>893.208764</td>\n",
       "      <td>1126.770494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>1202.549301</td>\n",
       "      <td>944.670104</td>\n",
       "      <td>658.659525</td>\n",
       "      <td>960.258965</td>\n",
       "      <td>875.344589</td>\n",
       "      <td>1104.235084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>1178.498315</td>\n",
       "      <td>925.776702</td>\n",
       "      <td>645.486334</td>\n",
       "      <td>941.053785</td>\n",
       "      <td>857.837697</td>\n",
       "      <td>1082.150382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>1154.928349</td>\n",
       "      <td>907.261168</td>\n",
       "      <td>632.576607</td>\n",
       "      <td>922.232710</td>\n",
       "      <td>840.680943</td>\n",
       "      <td>1060.507375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>1131.829782</td>\n",
       "      <td>889.115945</td>\n",
       "      <td>619.925075</td>\n",
       "      <td>903.788056</td>\n",
       "      <td>823.867325</td>\n",
       "      <td>1039.297227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>1109.193186</td>\n",
       "      <td>871.333626</td>\n",
       "      <td>607.526574</td>\n",
       "      <td>885.712294</td>\n",
       "      <td>807.389978</td>\n",
       "      <td>1018.511283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>1087.009322</td>\n",
       "      <td>853.906953</td>\n",
       "      <td>595.376042</td>\n",
       "      <td>867.998049</td>\n",
       "      <td>791.242179</td>\n",
       "      <td>998.141057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>1065.269136</td>\n",
       "      <td>836.828814</td>\n",
       "      <td>583.468521</td>\n",
       "      <td>850.638088</td>\n",
       "      <td>775.417335</td>\n",
       "      <td>978.178236</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Year        Ceuta   Gulf_Gabes     Gotland       Jersey        Zadar  \\\n",
       "0      1  1563.737698  1228.403903  856.489400  1248.674913  1138.256313   \n",
       "1      2  1532.462944  1203.835825  839.359612  1223.701414  1115.491186   \n",
       "2      3  1501.813685  1179.759108  822.572419  1199.227386  1093.181363   \n",
       "3      4  1471.777412  1156.163926  806.120971  1175.242838  1071.317735   \n",
       "4      5  1442.341863  1133.040647  789.998552  1151.737982  1049.891381   \n",
       "5      6  1413.495026  1110.379835  774.198581  1128.703222  1028.893553   \n",
       "6      7  1385.225126  1088.172238  758.714609  1106.129158  1008.315682   \n",
       "7      8  1357.520623  1066.408793  743.540317  1084.006574   988.149368   \n",
       "8      9  1330.370211  1045.080617  728.669510  1062.326443   968.386381   \n",
       "9     10  1303.762806  1024.179005  714.096120  1041.079914   949.018653   \n",
       "10    11  1277.687550  1003.695425  699.814198  1020.258316   930.038280   \n",
       "11    12  1252.133799   983.621516  685.817914   999.853150   911.437515   \n",
       "12    13  1227.091123   963.949086  672.101556   979.856087   893.208764   \n",
       "13    14  1202.549301   944.670104  658.659525   960.258965   875.344589   \n",
       "14    15  1178.498315   925.776702  645.486334   941.053785   857.837697   \n",
       "15    16  1154.928349   907.261168  632.576607   922.232710   840.680943   \n",
       "16    17  1131.829782   889.115945  619.925075   903.788056   823.867325   \n",
       "17    18  1109.193186   871.333626  607.526574   885.712294   807.389978   \n",
       "18    19  1087.009322   853.906953  595.376042   867.998049   791.242179   \n",
       "19    20  1065.269136   836.828814  583.468521   850.638088   775.417335   \n",
       "\n",
       "    Pantelleria  \n",
       "0   1435.894584  \n",
       "1   1407.176693  \n",
       "2   1379.033159  \n",
       "3   1351.452496  \n",
       "4   1324.423446  \n",
       "5   1297.934977  \n",
       "6   1271.976277  \n",
       "7   1246.536752  \n",
       "8   1221.606017  \n",
       "9   1197.173896  \n",
       "10  1173.230418  \n",
       "11  1149.765810  \n",
       "12  1126.770494  \n",
       "13  1104.235084  \n",
       "14  1082.150382  \n",
       "15  1060.507375  \n",
       "16  1039.297227  \n",
       "17  1018.511283  \n",
       "18   998.141057  \n",
       "19   978.178236  "
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Yearly_Energy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "01d2126f",
   "metadata": {},
   "outputs": [],
   "source": [
    "kg_CO2_per_location=pd.Series()\n",
    "for location in CO2_combined.index:\n",
    "    kg_CO2_per_location[location]=CO2_combined[location]*Yearly_Energy[location]*10**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "7fc35c00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 122676 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(figsize=(12, 6))\n",
    "locations = ['Ceuta', 'Gotland', 'Gulf_Gabes', 'Pantelleria', 'Zadar', 'Jersey']\n",
    "\n",
    "# Use a qualitative colormap\n",
    "colors = plt.get_cmap('tab10')\n",
    "\n",
    "for i, loc in enumerate(locations):\n",
    "    production = Yearly_Energy[loc] #kwh\n",
    "    EF = CO2_combined[loc]#kg/kwh\n",
    "    co2=production*EF\n",
    "\n",
    "    ax.plot(co2.index, co2.cumsum().values, label=loc, color=colors(i))\n",
    "\n",
    "ax.set_xlabel(\"Year\")\n",
    "ax.set_ylabel(\"Cumulative CO₂ Avoided [tCO₂]\")\n",
    "ax.grid(True)\n",
    "ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))\n",
    "\n",
    "# De-duplicate legend\n",
    "handles, labels = ax.get_legend_handles_labels()\n",
    "unique = dict(zip(labels, handles))\n",
    "ax.legend(unique.values(), unique.keys())\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
