{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "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 sys\n",
    "import os\n",
    "# Remove any previously loaded 'functions' module\n",
    "if 'functions' in sys.modules:\n",
    "    del sys.modules['functions']\n",
    "\n",
    "# Set correct path to 'c:/Users/youss/Documents/Tesi/Data/dash'\n",
    "correct_path = r'c:\\Users\\youss\\Documents\\Tesi\\Data\\dash'\n",
    "if correct_path not in sys.path:\n",
    "    sys.path.insert(0, correct_path)\n",
    "import functions as functions\n",
    "from plotly.subplots import make_subplots\n",
    "os.chdir(\"c:/Users/youss/Documents/Tesi/Data/Dash\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:60: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_hs_ceuta = Ceuta.groupby(\"Month\")[\"Hs\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:61: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_hs_Gotland = Gotland.groupby(\"Month\")[\"Hs\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:62: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_hs_Gulf_Gabes = Gulf_Gabes.groupby(\"Month\")[\"Hs\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:63: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_hs_Jersey = Jersey.groupby(\"Month\")[\"Hs\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:64: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_hs_Pantelleria = Pantelleria.groupby(\"Month\")[\"Hs\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:65: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_hs_Zadar = Zadar.groupby(\"Month\")[\"Hs\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:83: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_g_ceuta = Ceuta.groupby(\"Month\")[\"G\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:84: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_g_Gotland = Gotland.groupby(\"Month\")[\"G\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:85: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_g_Gulf_Gabes = Gulf_Gabes.groupby(\"Month\")[\"G\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:86: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_g_Jersey = Jersey.groupby(\"Month\")[\"G\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:87: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_g_Pantelleria = Pantelleria.groupby(\"Month\")[\"G\"].mean()\n",
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py:88: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  average_monthly_g_Zadar = Zadar.groupby(\"Month\")[\"G\"].mean()\n"
     ]
    }
   ],
   "source": [
    "[Ceuta,Gotland,Gulf_Gabes,Jersey,Pantelleria,Zadar,Atoll_Maldives,\n",
    " BawahReserve_Indonesia, Buksin_Bay_Korea,Changhua_TAIWAN,\n",
    " Island_Nurai_UAE,Baa_Maldives,Dutch_sea]=functions.get_data_function()\n",
    "[average_monthly, average_monthly_g]=functions.fun_average_monthly(Ceuta,Gotland,Gulf_Gabes,Jersey,Pantelleria,Zadar)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<module 'functions' from 'c:\\\\Users\\\\youss\\\\Documents\\\\Tesi\\\\Data\\\\dash\\\\functions.py'>\n"
     ]
    }
   ],
   "source": [
    "print(functions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "locations = {\n",
    "    \"South Male Atoll, Maldives\": Atoll_Maldives,\n",
    "    \"Bawah Reserve, Indonesia\": BawahReserve_Indonesia,\n",
    "    \"Buksin Bay, Korea\": Buksin_Bay_Korea,\n",
    "    \"Changhua, Taiwan\": Changhua_TAIWAN,\n",
    "    \"Island of Nurai, UAE\": Island_Nurai_UAE,\n",
    "    \"Baa Atoll, Maldives\": Baa_Maldives,\n",
    "    \"Dutch North Sea\": Dutch_sea\n",
    "}\n",
    "\n",
    "# Create an empty list to hold summary data\n",
    "summary_data = []\n",
    "\n",
    "# Loop over each DataFrame\n",
    "for name, df in locations.items():\n",
    "    hs_stats = df['Hs'].describe()  # Get count, mean, std, min, 25%, 50%, 75%, max\n",
    "    summary_data.append({\n",
    "        \"Location\": name,\n",
    "        \"Count\": hs_stats['count'],\n",
    "        \"Mean\": hs_stats['mean'],\n",
    "        \"Median\": df['Hs'].median(),\n",
    "        \"Std Dev\": hs_stats['std'],\n",
    "        \"Min\": hs_stats['min'],\n",
    "        \"Max\": hs_stats['max']\n",
    "    })\n",
    "\n",
    "# Create summary DataFrame\n",
    "summary_df_reference = pd.DataFrame(summary_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(4.03)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dutch_sea['Hs'].quantile(0.978)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 103964 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 630x450 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# Prepara i dati in un DataFrame unico\n",
    "def prepare_data_for_boxplot(locations_dict, class_colors, class_labels, quantile_limits, correction_factors=None):\n",
    "    plot_data = []\n",
    "    for loc, df in locations_dict.items():\n",
    "        class_label = class_labels[loc]\n",
    "        color = class_colors[class_label]\n",
    "        quantile = quantile_limits[loc]\n",
    "        correction = correction_factors.get(loc, 1) if correction_factors else 1\n",
    "        filtered_hs = df[\"Hs\"]\n",
    "        filtered_hs = filtered_hs[filtered_hs <= filtered_hs.quantile(quantile)] * correction\n",
    "        for value in filtered_hs:\n",
    "            plot_data.append({\n",
    "                \"Location\": loc,\n",
    "                \"Hs\": value,\n",
    "                \"Class\": class_label,\n",
    "                \"Color\": color\n",
    "            })\n",
    "    return pd.DataFrame(plot_data)\n",
    "\n",
    "# Dizionari di supporto\n",
    "locations = {\n",
    "    \"South Male Atoll\": Atoll_Maldives,\n",
    "    \"Bawah Reserve\": BawahReserve_Indonesia,\n",
    "    \"Buksin Bay\": Buksin_Bay_Korea,\n",
    "    \"Changhua, Taiwan\": Changhua_TAIWAN,\n",
    "    \"Island of Nurai\": Island_Nurai_UAE,\n",
    "    \"Baa Atoll\": Baa_Maldives,\n",
    "    \"Dutch Sea\": Dutch_sea\n",
    "}\n",
    "\n",
    "class_labels = {\n",
    "    \"South Male Atoll\": \"Class 1\",\n",
    "    \"Bawah Reserve\": \"Class 1\",\n",
    "    \"Buksin Bay\": \"Class 1\",\n",
    "    \"Changhua, Taiwan\": \"Class 2\",\n",
    "    \"Island of Nurai\": \"Class 2\",\n",
    "    \"Baa Atoll\": \"Class 3\",\n",
    "    \"Dutch Sea\": \"Class 3\"\n",
    "}\n",
    "\n",
    "class_colors = {\n",
    "    \"Class 1\": \"green\",\n",
    "    \"Class 2\": \"blue\",\n",
    "    \"Class 3\": \"red\"\n",
    "}\n",
    "\n",
    "quantile_limits = {\n",
    "    \"South Male Atoll\": 0.995,\n",
    "    \"Bawah Reserve\": 0.99,\n",
    "    \"Buksin Bay\": 0.99,\n",
    "    \"Changhua, Taiwan\": 0.99,\n",
    "    \"Island of Nurai\": 0.99,\n",
    "    \"Baa Atoll\": 0.997,\n",
    "    \"Dutch Sea\": 0.978\n",
    "}\n",
    "\n",
    "correction_factors = {\n",
    "    \"Changhua, Taiwan\": 0.35\n",
    "}\n",
    "\n",
    "# Prepara il dataframe finale\n",
    "df_box = prepare_data_for_boxplot(locations, class_colors, class_labels, quantile_limits, correction_factors)\n",
    "\n",
    "# Imposta stile professionale\n",
    "plt.figure(figsize=(12, 6))\n",
    "sns.set(style=\"whitegrid\", font_scale=1.2)\n",
    "\n",
    "# Boxplot\n",
    "ax = sns.boxplot(data=df_box, x=\"Location\", y=\"Hs\", hue=\"Class\", palette=class_colors, dodge=False)\n",
    "\n",
    "# Linee di riferimento\n",
    "ax.axhline(2.08, ls='--', color='green')\n",
    "ax.axhline(1.47, ls='--', color='blue')\n",
    "ax.axhline(4.03, ls='--', color='red')\n",
    "\n",
    "# Layout tesi\n",
    "plt.xlabel(\"Location\")\n",
    "plt.ylabel(\"Significant Wave Height Hs [m]\")\n",
    "plt.xticks(rotation=45)\n",
    "plt.legend(title=\"Platform Class\", loc='upper right')\n",
    "plt.tight_layout()\n",
    "plt.grid(True, linestyle='--', linewidth=0.5)\n",
    "plt.figure(figsize=(6.3, 4.5))\n",
    "# Mostra il grafico\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "locations = {\n",
    "    \"Ceuta\": Ceuta,\n",
    "    \"Gotland\": Gotland,\n",
    "    \"Gulf Gabes\": Gulf_Gabes,\n",
    "    \"Jersey\": Jersey,\n",
    "    \"Pantelleria\": Pantelleria,\n",
    "    \"Zadar\": Zadar\n",
    "}\n",
    "\n",
    "# Create an empty list to hold summary data\n",
    "summary_data = []\n",
    "\n",
    "# Loop over each DataFrame\n",
    "for name, df in locations.items():\n",
    "    hs_stats = df['Hs'].describe()\n",
    "    summary_data.append({\n",
    "        \"Location\": name,\n",
    "        \"Count\": hs_stats['count'],\n",
    "        \"Mean\": hs_stats['mean'],\n",
    "        \"Median\": df['Hs'].median(),\n",
    "        \"Std Dev\": hs_stats['std'],\n",
    "        \"Min\": hs_stats['min'],\n",
    "        \"Max\": hs_stats['max']\n",
    "    })\n",
    "\n",
    "# Create summary DataFrame\n",
    "summary_df = pd.DataFrame(summary_data)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\youss\\AppData\\Local\\Temp\\ipykernel_2960\\4084733314.py:42: FutureWarning: \n",
      "\n",
      "Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same effect.\n",
      "\n",
      "  ax = sns.boxplot(data=df_box_target, x=\"Location\", y=\"Hs\", palette=custom_palette)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 79888 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Prepara i dati in un DataFrame unico per i target\n",
    "def prepare_data_for_boxplot(locations_dict, quantile=0.99):\n",
    "    plot_data = []\n",
    "    for loc, df in locations_dict.items():\n",
    "        filtered_hs = df[\"Hs\"]\n",
    "        filtered_hs = filtered_hs[filtered_hs <= filtered_hs.quantile(quantile)]\n",
    "        for value in filtered_hs:\n",
    "            plot_data.append({\n",
    "                \"Location\": loc,\n",
    "                \"Hs\": value\n",
    "            })\n",
    "    return pd.DataFrame(plot_data)\n",
    "\n",
    "# Dizionario dei target locations\n",
    "locations_target = {\n",
    "    \"Ceuta\": Ceuta,\n",
    "    \"Gotland\": Gotland,\n",
    "    \"Gulf Gabes\": Gulf_Gabes,\n",
    "    \"Jersey\": Jersey,\n",
    "    \"Pantelleria\": Pantelleria,\n",
    "    \"Zadar\": Zadar\n",
    "}\n",
    "\n",
    "# Prepara il dataframe finale\n",
    "df_box_target = prepare_data_for_boxplot(locations_target, quantile=0.99)\n",
    "\n",
    "# Imposta stile professionale\n",
    "# Define custom color palette\n",
    "custom_palette = {\n",
    "    \"Pantelleria\": \"red\",\n",
    "    \"Jersey\": \"red\",\n",
    "    \"Gotland\": \"red\",\n",
    "    \"Ceuta\": \"red\",\n",
    "    \"Gulf Gabes\": \"green\",\n",
    "    \"Zadar\": \"green\"\n",
    "}\n",
    "\n",
    "# Create the boxplot with custom colors\n",
    "plt.figure(figsize=(12, 6))\n",
    "sns.set(style=\"whitegrid\", font_scale=1.2)\n",
    "\n",
    "ax = sns.boxplot(data=df_box_target, x=\"Location\", y=\"Hs\", palette=custom_palette)\n",
    "\n",
    "# Add threshold reference lines\n",
    "ax.axhline(2.08, ls='--', color='green', label='Class 1 limit')\n",
    "ax.axhline(1.47, ls='--', color='blue', label='Class 2 limit')\n",
    "ax.axhline(4.03, ls='--', color='red', label='Class 3 limit')\n",
    "\n",
    "# Style and layout\n",
    "plt.xlabel(\"Location\")\n",
    "plt.ylabel(\"Significant Wave Height Hs [m]\")\n",
    "plt.xticks(rotation=45)\n",
    "plt.legend(loc='upper right')\n",
    "plt.tight_layout()\n",
    "plt.grid(True, linestyle='--', linewidth=0.5)\n",
    "\n",
    "# Show the plot\n",
    "plt.show()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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": 2
}
