{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "\n",
    "from polsarpro.dev.devtools import parse_psp_parameter_string\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.dev.io import read_psp_bin\n",
    "from polsarpro.decompositions import yamaguchi4\n",
    "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n",
    "import shutil\n",
    "\n",
    "# optional import for progress bar\n",
    "from dask.diagnostics import ProgressBar\n",
    "\n",
    "# change to your local C-PolSARpro install dir\n",
    "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
    "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n",
    "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\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",
    "# input files from C (use the same as for h-a-alpha)\n",
    "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n",
    "\n",
    "# output files from C\n",
    "output_test_dir = Path(\"/data/psp/res/yamaguchi4_c/\")\n",
    "if not os.path.isdir(output_test_dir):\n",
    "    os.mkdir(output_test_dir)\n",
    "\n",
    "# uncomment to test the different modes\n",
    "# mode = \"y4o\"\n",
    "# mode = \"y4r\"\n",
    "mode = \"s4r\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run the C-version on some test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_str = f\"\"\"id: {input_test_dir}\n",
    "od: {output_test_dir}\n",
    "iodf: S2\n",
    "mod: {mode.upper()}\n",
    "nwr: 7\n",
    "nwc: 7\n",
    "ofr: 0\n",
    "ofc: 0\n",
    "fnr: 18432\n",
    "fnc: 1248\n",
    "errf: /tmp\"\"\"\n",
    "\n",
    "result = parse_psp_parameter_string(input_str)\n",
    "os.system(f\"yamaguchi_4components_decomposition.exe {result}\")\n",
    "\n",
    "# CPSP does not create another config file in output dir\n",
    "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
   ]
  },
  {
   "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 decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_out = f\"/data/psp/res/test_yamaguchi4_{mode}.nc\"\n",
    "# netcdf writer cannot overwrite\n",
    "if os.path.isfile(file_out):\n",
    "    os.remove(file_out)\n",
    "\n",
    "with ProgressBar():\n",
    "    yamaguchi4(S, boxcar_size=[7, 7], mode=mode).to_netcdf(file_out)\n",
    "print(f\"Wrote to {file_out}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numerical evaluation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# open python output (comment if using compute)\n",
    "out_py = xr.open_dataset(f\"/data/psp/res/test_yamaguchi4_{mode}.nc\")\n",
    "# open C-PolSARPro outputs\n",
    "out_c = {}\n",
    "out_names = (\n",
    "    (\"odd\", f\"Yamaguchi4_{mode.upper()}_Odd\"),\n",
    "    (\"double\", f\"Yamaguchi4_{mode.upper()}_Dbl\"),\n",
    "    (\"volume\", f\"Yamaguchi4_{mode.upper()}_Vol\"),\n",
    "    (\"helix\", f\"Yamaguchi4_{mode.upper()}_Hlx\"),\n",
    ")\n",
    "for name in out_names:\n",
    "    print(f\"Loading {name[1]}\")\n",
    "    file_name = output_test_dir / f\"{name[1]}.bin\"\n",
    "    out_c[name[0]] = read_psp_bin(file_name)\n",
    "\n",
    "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "visualize_errors(out_py=out_py, out_c=out_c)"
   ]
  }
 ],
 "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
}
