{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "64451b6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dash import callback, Output, Input, State, html\n",
    "from dash import callback_context\n",
    "import seaborn as sns\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": 2,
   "id": "c7da44de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py\n"
     ]
    }
   ],
   "source": [
    "print(functions.__file__)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adbbabf4",
   "metadata": {},
   "source": [
    "Data collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "36c2ca0c",
   "metadata": {},
   "outputs": [],
   "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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "72fb4517",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\Users\\youss\\Documents\\Tesi\\Data\\dash\\functions.py\n"
     ]
    }
   ],
   "source": [
    "print(functions.__file__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "16a3c06b",
   "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": [
    "[average_monthly, average_monthly_g]=functions.fun_average_monthly(Ceuta,Gotland,Gulf_Gabes,Jersey,Pantelleria,Zadar)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dbba8dd",
   "metadata": {},
   "source": [
    "Plot "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c301bd68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 187708 characters]",
      "text/plain": [
       "<Figure size 1800x1000 with 6 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",
    "\n",
    "# Define months\n",
    "months = [\n",
    "    \"January\", \"February\", \"March\", \"April\", \"May\", \"June\",\n",
    "    \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"\n",
    "]\n",
    "\n",
    "# Convert DataFrame to dictionary\n",
    "monthly_data = {\n",
    "    col: average_monthly[col].values.tolist()\n",
    "    for col in average_monthly.columns if col != \"Month\"\n",
    "}\n",
    "\n",
    "# Shaded periods: list of month indices (0-based)\n",
    "shaded_periods = {\n",
    "    \"Ceuta\": [0, 1, 2, 3],          # Jan to Apr\n",
    "    \"Jersey\": [10, 11, 0, 1, 2],    # Nov to Mar\n",
    "    \"Zadar\": [10, 11, 0, 1],        # Nov to Feb\n",
    "    \"Gulf Gabes\": [0, 1, 2, 3]      # Jan to Apr\n",
    "}\n",
    "\n",
    "# Create subplots with shared y-axis per row\n",
    "fig, axes = plt.subplots(2, 3, figsize=(18, 10), sharey='row')\n",
    "axes = axes.flatten()\n",
    "\n",
    "for i, (loc, values) in enumerate(monthly_data.items()):\n",
    "    ax = axes[i]\n",
    "    ax.plot(months, values, marker='o', label=loc)\n",
    "    ax.set_title(loc)\n",
    "    ax.set_xlabel(\"Month\")\n",
    "    ax.set_ylabel(\"Mean $H_s$ [m]\")\n",
    "    ax.set_xticks(range(12))\n",
    "    ax.set_xticklabels(months, rotation=45)\n",
    "\n",
    "    # Shade seasonal removal periods\n",
    "    if loc in shaded_periods:\n",
    "        for m in shaded_periods[loc]:\n",
    "            ax.axvspan(m - 0.5, m + 0.5, color='grey', alpha=0.3)\n",
    "\n",
    "    ax.grid(True)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.figure(figsize=(6.3, 4.5))\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4826c995",
   "metadata": {},
   "outputs": [],
   "source": [
    "Jersey_SR=Jersey.loc[~Jersey[\"Month\"].isin([\"February\", \"January\",'December', 'November','March'])]  # Filtered data for Hs\n",
    "Gulf_Gabes_SR=Gulf_Gabes.loc[~Gulf_Gabes[\"Month\"].isin([\"May\",'March', 'April'])]  # Filtered data for Hs\n",
    "Ceuta_SR=Ceuta.loc[~Ceuta[\"Month\"].isin([\"February\", \"January\",'March', 'April'])]  # Filtered data for Hs\n",
    "Zadar_SR=Zadar.loc[~Zadar[\"Month\"].isin([\"February\", \"January\",'November', 'December','March'])]  # Filtered data for Hs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a08cba8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\youss\\AppData\\Local\\Temp\\ipykernel_13800\\1083238253.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, 68692 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_SR,\n",
    "    \"Gotland\": Gotland,\n",
    "    \"Gulf Gabes\": Gulf_Gabes_SR,\n",
    "    \"Jersey\": Jersey_SR,\n",
    "    \"Pantelleria\": Pantelleria,\n",
    "    \"Zadar\": Zadar_SR\n",
    "}\n",
    "\n",
    "# Define custom color palette\n",
    "custom_palette = {\n",
    "    \"Zadar\": \"blue\",\n",
    "    \"Gulf Gabes\": \"blue\",\n",
    "    \"Jersey\": \"green\",\n",
    "    \"Ceuta\": \"green\",\n",
    "    \"Pantelleria\": \"red\",\n",
    "    \"Gotland\": \"red\"\n",
    "}\n",
    "\n",
    "# Prepare final dataframe\n",
    "df_box_target = prepare_data_for_boxplot(locations_target, quantile=0.99)\n",
    "\n",
    "# Set professional style\n",
    "plt.figure(figsize=(12, 6))\n",
    "sns.set(style=\"whitegrid\")\n",
    "\n",
    "# Boxplot with custom colors\n",
    "ax = sns.boxplot(data=df_box_target, x=\"Location\", y=\"Hs\", palette=custom_palette)\n",
    "\n",
    "# Reference lines for previous classes\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",
    "\n",
    "# Thesis-style 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"
   ]
  }
 ],
 "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
}
