{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Polarimetric Whitening Filter (PWF)\n",
    "\n",
    "This notebook shows how to apply the PWF on the S matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "\n",
    "import numpy as np\n",
    "from pathlib import Path\n",
    "import xarray as xr\n",
    "from polsarpro.io import open_netcdf_beam\n",
    "from polsarpro.speckle_filters import pwf\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",
    "\n",
    "# output files from C\n",
    "output_dir = Path(\"/data/psp/res/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load ALOS data and C outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "S = open_netcdf_beam(input_alos_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Apply the filter\n",
    "**Important note:**  \n",
    "this version differs from the C implementation since it allows to smooth the covariance matrix using the `test_window_size` prior to multiplying it with the inverse covariance estimated using the `train_window size` parameter. Using values `[1, 1]` for the test window is equivalent to the original C PolSARpro implementation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_out = output_dir / \"pwf.nc\"\n",
    "# netcdf writer cannot overwrite\n",
    "if os.path.isfile(file_out):\n",
    "    os.remove(file_out)\n",
    "\n",
    "with ProgressBar():\n",
    "    pwf(S, train_window_size=[15, 15], test_window_size=[3, 3]).to_netcdf(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Open the output file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = xr.open_dataset(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display the result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# clip the intensity to handle the high dynamic range\n",
    "res.pwf.plot.imshow(figsize=(7,9), vmax=10)"
   ]
  }
 ],
 "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
}
