{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c165ac4d",
   "metadata": {},
   "source": [
    "# Visual comparison of parameters for images in SAR and geographic coordinates\n",
    "\n",
    "- This is only to check the outputs for geocoded images\n",
    "- Numerical comparison is done in a script with C outputs using reference data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99920dfb",
   "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 refined_lee\n",
    "from polsarpro.util import S_to_T3, boxcar\n",
    "import matplotlib.pyplot as plt\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_slc = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
    "input_alos_geo = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_geocoded_T3_7_look_az.nc\")\n",
    "\n",
    "# output files from C\n",
    "output_test_dir = Path(\"/data/psp/res/h_a_alpha_c\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14bc0335",
   "metadata": {},
   "outputs": [],
   "source": [
    "slc = open_netcdf_beam(input_alos_slc)\n",
    "geo = open_netcdf_beam(input_alos_geo)\n",
    "\n",
    "# uncomment if you want to test for cropped data\n",
    "# geo_crop = geo.isel(lat=slice(0,1000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb995ca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's use persist (retains dask arrays) instead of compute (produces numpy arrays)\n",
    "with ProgressBar():\n",
    "    res_slc = refined_lee(S_to_T3(slc), window_size=7).persist()\n",
    "with ProgressBar():\n",
    "    # data has been multilooked in azimuth so we need less filtering\n",
    "    res_geo = refined_lee(geo, window_size=3, num_looks=7).persist()\n",
    "\n",
    "    # uncomment if you want to test for cropped data\n",
    "    # res_geo = h_a_alpha(geo_crop, boxcar_size=[7 ,7]).compute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9691637e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for var in res_geo.data_vars:\n",
    "    plt.figure(figsize=(10, 10))\n",
    "    plt.suptitle(var)\n",
    "    plt.subplot(121)\n",
    "    plt.imshow(abs(res_slc)[var][::8], interpolation=\"none\", vmax=1)\n",
    "    plt.colorbar(fraction=0.046, pad=0.04)\n",
    "    plt.subplot(122)\n",
    "    plt.imshow(abs(res_geo)[var], interpolation=\"none\", vmax=1)\n",
    "    plt.colorbar(fraction=0.046, pad=0.04)"
   ]
  }
 ],
 "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": 5
}
