{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TSVM decomposition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook shows how to apply the TSVM decomposition to a PolSARpro NetCDF dataset."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import packages and set directories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "from pathlib import Path\n",
    "import xarray as xr\n",
    "from polsarpro.io import open_netcdf_beam\n",
    "from polsarpro.decompositions import tsvm\n",
    "\n",
    "# optional import for progress bar\n",
    "from dask.diagnostics import ProgressBar\n",
    "\n",
    "# change to your data paths\n",
    "# original dataset\n",
    "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
    "output_dir = Path(\"/data/psp/res\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load data\n",
    "\n",
    "We load the SNAP NetCDF-BEAM dataset using the `open_netcdf_beam` function. \n",
    "To obtain such a dataset, please refer to the \"Getting Started\" tutorial or the `quickstart-tutorial.ipynb` notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# uncomment to test on S matrix made with SNAP\n",
    "S = open_netcdf_beam(input_alos_data)\n",
    "\n",
    "# Variables to compute. A list can be found in the API reference documentation.\n",
    "# For the sake of illustration we chose the default parameter and one of the per-eigenvalue outputs\n",
    "flags = (\"alpha_phi_tau_psi\", \"alpha\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Apply the decomposition\n",
    "\n",
    "Let's apply the decomposition and write the result to a NetCDF file.\n",
    "Optionally we can use a progress bar to monitor the progress of the computation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[########################################] | 100% Completed | 85.30 s\n"
     ]
    }
   ],
   "source": [
    "# change to the name of your liking\n",
    "file_out = output_dir / \"psp_tsvm.nc\"\n",
    "\n",
    "# netcdf writer cannot overwrite\n",
    "if os.path.isfile(file_out):\n",
    "    os.remove(file_out)\n",
    "\n",
    "with ProgressBar():\n",
    "    tsvm(S, boxcar_size=[7, 7], flags=flags).to_netcdf(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We open the previously saved dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = xr.open_dataset(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we may plot individual parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 382492 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 490784 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 385496 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 506096 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 388288 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 494948 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 528000 characters]",
      "text/plain": [
       "<Figure size 500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "for var in res.data_vars:\n",
    "    plt.figure(figsize=(5, 5))\n",
    "    res[var].plot.imshow()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "psp",
   "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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
