{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Advanced XSAR example\n",
    "\n",
    "open a dataset with [xsar.open_dataset](../basic_api.rst#xsar.open_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xsar\n",
    "import os\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Sentinel1 example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get test file. You can replace with an path to other SAFE\n",
    "filename = xsar.get_test_file('S1A_IW_GRDH_1SDV_20170907T103020_20170907T103045_018268_01EB76_Z010.SAFE')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Open a dataset with a xsar.Sentinel1Meta object\n",
    "A [xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta) object handles all attributes and methods that can't be embdeded in a `xarray.Dataset` object.\n",
    "It can also replace a filename in `xarray.open_dataset`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_meta = xsar.Sentinel1Meta(filename)\n",
    "sar_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "If holoviews extension is loaded, the `<Sentinel1Meta objet>` have a nice representation.\n",
    "(`matplolib` is also a valid extension)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import holoviews as hv\n",
    "hv.extension('bokeh')\n",
    "sar_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`sar_meta` object is an [xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta) object that can be given to `xarray.open_dataset` or [xsar.Sentinel1Dataset](../basic_api.rst#xsar.Sentinel1Dataset) , as if it was a filename:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds = xsar.Sentinel1Dataset(sar_meta)\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Open a dataset at lower resolution\n",
    "\n",
    "`resolution` keyword can be used to open a dataset at lower resolution. \n",
    "\n",
    "It might be:\n",
    "\n",
    " * a dict `{'line': 20, 'sample': 20}`: 20*20 pixels. so if sensor resolution is 10m, the final resolution will be 100m\n",
    " * a string like `'200m'`: Sensor resolution will be automatically used to compute the window size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can instantiate a [xsar.Sentinel1Dataset](../basic_api.rst#xsar.Sentinel1Dataset), with the given resolution. Note that the above pixel size has changed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds = xsar.Sentinel1Dataset(sar_meta, resolution='200m')\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Extract a sub image of 10*10km around a lon/lat point\n",
    "\n",
    "#### Convert (lon,lat) to (line, sample)\n",
    "we can use [sar_meta.ll2coords](../basic_api.rst#xsar.BaseMeta.ll2coords) to convert (lon,lat) to (line, sample):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from a shapely object\n",
    "point_lonlat =  sar_meta.footprint.centroid\n",
    "point_coords = sar_meta.ll2coords(point_lonlat.x, point_lonlat.y)\n",
    "point_coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The result is floating, because it is the position inside the pixel.\n",
    "If real indexes from existing dataset is needed, you'll have to use [sar_ds.ll2coords](../basic_api.rst#xsar.BaseDataset.ll2coords) \n",
    "Result will be the nearest (line, sample) in the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_coords = sar_ds.ll2coords(point_lonlat.x, point_lonlat.y)\n",
    "point_coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Extract the sub-image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "box_size = 10000 # 10km\n",
    "dist = {'line' : int(np.round(box_size / 2 / sar_meta.pixel_line_m)), 'sample': int(np.round(box_size / 2 / sar_meta.pixel_sample_m))}\n",
    "dist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The xarray/dask dataset is available as a property :  [sar_ds.dataset](../basic_api.rst#xsar.Sentinel1Dataset.dataset).\n",
    "This attribute can be set to a new values, so the attributes like pixel spacing and coverage are correctly recomputed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# select 10*10 km around point_coords\n",
    "sar_ds.dataset = sar_ds.dataset.sel(line=slice(point_coords[0] - dist['line'], point_coords[0] + dist['line']), sample=slice(point_coords[1] - dist['sample'], point_coords[1] + dist['sample']))\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds.dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## RadarSat2 example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get test file. You can replace with an path to other SAFE\n",
    "filename = xsar.get_test_file('RS2_OK135107_PK1187782_DK1151894_SCWA_20220407_182127_VV_VH_SGF')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Open a dataset with a xsar.RadarSat2Meta object\n",
    "A [xsar.RadarSat2Meta](../basic_api.rst#xsar.RadarSat2Meta) object handles all attributes and methods that can't be embdeded in a `xarray.Dataset` object.\n",
    "It can also replace a filename in `xarray.open_dataset`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_meta = xsar.RadarSat2Meta(filename)\n",
    "sar_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "If holoviews extension is loaded, the `<RadarSat2Meta objet>` have a nice representation.\n",
    "(`matplolib` is also a valid extension)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`sar_meta` object is an [xsar.RadarSat2Meta](../basic_api.rst#xsar.RadarSat2Meta) object that can be given to `xarray.open_dataset` or [xsar.RadarSat2Dataset](../basic_api.rst#xsar.RadarSat2Dataset) , as if it was a filename:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds = xsar.RadarSat2Dataset(sar_meta)\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Open a dataset at lower resolution\n",
    "\n",
    "`resolution` keyword can be used to open a dataset at lower resolution. \n",
    "\n",
    "It might be:\n",
    "\n",
    " * a dict `{'line': 20, 'sample': 20}`: 20*20 pixels. so if sensor resolution is 10m, the final resolution will be 100m\n",
    " * a string like `'200m'`: Sensor resolution will be automatically used to compute the window size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can instantiate a [xsar.RadarSat2Dataset](../basic_api.rst#xsar.RadarSat2Dataset), with the given resolution. Note that the above pixel size has changed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds = xsar.RadarSat2Dataset(sar_meta, resolution='200m')\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Extract a sub image of 10*10km around a lon/lat point\n",
    "\n",
    "#### Convert (lon,lat) to (line, sample)\n",
    "we can use [sar_meta.ll2coords](../basic_api.rst#xsar.BaseMeta.ll2coords) to convert (lon,lat) to (line, sample):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from a shapely object\n",
    "point_lonlat =  sar_meta.footprint.centroid\n",
    "point_coords = sar_meta.ll2coords(point_lonlat.x, point_lonlat.y)\n",
    "point_coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The result is floating, because it is the position inside the pixel.\n",
    "If real indexes from existing dataset is needed, you'll have to use [sar_ds.ll2coords](../basic_api.rst#xsar.BaseDataset.ll2coords) \n",
    "Result will be the nearest (line, sample) in the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_coords = sar_ds.ll2coords(point_lonlat.x, point_lonlat.y)\n",
    "point_coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Extract the sub-image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "box_size = 10000 # 10km\n",
    "dist = {'line' : int(np.round(box_size / 2 / sar_meta.pixel_line_m)), 'sample': int(np.round(box_size / 2 / sar_meta.pixel_sample_m))}\n",
    "dist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The xarray/dask dataset is available as a property :  [sar_ds.dataset](../basic_api.rst#xsar.RadarSat2Dataset.dataset).\n",
    "This attribute can be set to a new values, so the attributes like pixel spacing and coverage are correctly recomputed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# select 10*10 km around point_coords\n",
    "sar_ds.dataset = sar_ds.dataset.sel(line=slice(point_coords[0] - dist['line'], point_coords[0] + dist['line']), sample=slice(point_coords[1] - dist['sample'], point_coords[1] + dist['sample']))\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds.dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## RCM example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get test file. You can replace with an path to other SAFE\n",
    "filename = xsar.get_test_file('RCM1_OK1050603_PK1050605_1_SC50MB_20200214_115905_HH_HV_Z010')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Open a dataset with a xsar.RcmMeta object\n",
    "A [xsar.RcmMeta](../basic_api.rst#xsar.RcmMeta) object handles all attributes and methods that can't be embdeded in a `xarray.Dataset` object.\n",
    "It can also replace a filename in `xarray.open_dataset`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_meta = xsar.RcmMeta(filename)\n",
    "sar_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "If holoviews extension is loaded, the `<RcmMeta objet>` have a nice representation.\n",
    "(`matplolib` is also a valid extension)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`sar_meta` object is an [xsar.RcmMeta](../basic_api.rst#xsar.RcmMeta) object that can be given to `xarray.open_dataset` or [xsar.RcmDataset](../basic_api.rst#xsar.RcmDataset) , as if it was a filename:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds = xsar.RcmDataset(sar_meta)\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Open a dataset at lower resolution\n",
    "\n",
    "`resolution` keyword can be used to open a dataset at lower resolution. \n",
    "\n",
    "It might be:\n",
    "\n",
    " * a dict `{'line': 20, 'sample': 20}`: 20*20 pixels. so if sensor resolution is 10m, the final resolution will be 100m\n",
    " * a string like `'200m'`: Sensor resolution will be automatically used to compute the window size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can instantiate a [xsar.RcmDataset](../basic_api.rst#xsar.RcmDataset), with the given resolution. Note that the above pixel size has changed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds = xsar.RcmDataset(sar_meta, resolution='200m')\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Extract a sub image of 10*10km around a lon/lat point\n",
    "\n",
    "#### Convert (lon,lat) to (line, sample)\n",
    "we can use [sar_meta.ll2coords](../basic_api.rst#xsar.BaseMeta.ll2coords) to convert (lon,lat) to (line, sample):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from a shapely object\n",
    "point_lonlat =  sar_meta.footprint.centroid\n",
    "point_coords = sar_meta.ll2coords(point_lonlat.x, point_lonlat.y)\n",
    "point_coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The result is floating, because it is the position inside the pixel.\n",
    "If real indexes from existing dataset is needed, you'll have to use [sar_ds.ll2coords](../basic_api.rst#xsar.BaseDataset.ll2coords) \n",
    "Result will be the nearest (line, sample) in the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_coords = sar_ds.ll2coords(point_lonlat.x, point_lonlat.y)\n",
    "point_coords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Extract the sub-image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "box_size = 10000 # 10km\n",
    "dist = {'line' : int(np.round(box_size / 2 / sar_meta.pixel_line_m)), 'sample': int(np.round(box_size / 2 / sar_meta.pixel_sample_m))}\n",
    "dist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The xarray/dask dataset is available as a property :  [sar_ds.dataset](../basic_api.rst#xsar.RcmDataset.dataset).\n",
    "This attribute can be set to a new values, so the attributes like pixel spacing and coverage are correctly recomputed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# select 10*10 km around point_coords\n",
    "sar_ds.dataset = sar_ds.dataset.sel(line=slice(point_coords[0] - dist['line'], point_coords[0] + dist['line']), sample=slice(point_coords[1] - dist['sample'], point_coords[1] + dist['sample']))\n",
    "sar_ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sar_ds.dataset"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
