{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Polarimetric Refined Lee speckle filter\n",
    "\n",
    "This tutorial shows how to:\n",
    "- Open the ALOS dataset and convert the S matrix to T3 coherency matrix\n",
    "- Pre-process the data by applying a 4-look decimation in azimuth\n",
    "- Apply the Refined Lee speckle filter to the 3x3 coherency matrix\n",
    "- Write the results to a file\n",
    "- Visualize the results as an RGB image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "\n",
    "import xarray as xr\n",
    "\n",
    "from pathlib import Path\n",
    "from polsarpro.io import open_netcdf_beam, polmat_to_netcdf\n",
    "from polsarpro.speckle_filters import refined_lee\n",
    "from polsarpro.util import S_to_T3, multilook, pauli_rgb\n",
    "\n",
    "# optional import for progress bar\n",
    "from dask.diagnostics import ProgressBar\n",
    "\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",
    "\n",
    "# output files from C\n",
    "output_test_dir = Path(\"/data/psp/res/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load ALOS data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "S = open_netcdf_beam(input_alos_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Apply the filter to T3 matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[########################################] | 100% Completed | 27.47 s\n"
     ]
    }
   ],
   "source": [
    "if os.path.exists(output_test_dir / \"T3_refined_lee.nc\"):\n",
    "    os.remove(output_test_dir / \"T3_refined_lee.nc\")\n",
    "\n",
    "with ProgressBar():\n",
    "    # convert S to T3\n",
    "    T3 = S_to_T3(S)\n",
    "    # apply 4-look multilook along lines\n",
    "    T3 = multilook(T3, dim_az=4, dim_rg=1)\n",
    "    # apply the filter\n",
    "    T3_flt = refined_lee(T3, window_size=7, num_looks=4)\n",
    "    # use custom function that writes complex matrices and preserves chunks\n",
    "    polmat_to_netcdf(T3_flt, output_test_dir / f\"T3_refined_lee.nc\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Open the file and visualize the results as an RBG image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7a68100504a0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 765440 characters]",
      "text/plain": [
       "<Figure size 500x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# open the file\n",
    "T3_flt = open_netcdf_beam(output_test_dir / f\"T3_refined_lee.nc\")\n",
    "\n",
    "pauli_rgb(T3_flt).plot.imshow(figsize=(5, 7))"
   ]
  }
 ],
 "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
}
