{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "everyday-problem",
   "metadata": {},
   "source": [
    "# Open multiples datasets\n",
    "\n",
    "Some safe files have multiples subdatasets, like WV or IW_SLC. This notebook show how to handle them, or how to handle multiple datasets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hindu-special",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xsar \n",
    "import xarray as xr\n",
    "import glob\n",
    "import holoviews as hv\n",
    "import geoviews as gv\n",
    "hv.extension('bokeh')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "attractive-giant",
   "metadata": {},
   "source": [
    "SAFES containings multiples subdatasets like WV_SLC cannot be openned with `xsar.open_dataset` given the safe path."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "basic-slovak",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    wv_slc_ds = xsar.open_dataset(xsar.get_test_file('S1A_WV_SLC__1SSV_20160510T101603_20160510T102143_011195_010EA1_Z010.SAFE'))\n",
    "except IndexError as e:\n",
    "    print(str(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "technological-hearing",
   "metadata": {},
   "source": [
    "An `IndexError` is raised, with an advice to use [xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta) to get the available subdatasets list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ea97e1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "wv_slc_meta = xsar.Sentinel1Meta(xsar.get_test_file('S1A_WV_SLC__1SSV_20160510T101603_20160510T102143_011195_010EA1_Z010.SAFE'))\n",
    "wv_slc_meta"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ce757c4",
   "metadata": {},
   "source": [
    "Note that the abose `wv_slc_meta` object is a **multi dataset**\n",
    "\n",
    "Subdatasets list is available with [xsar.Sentinel1Meta.subdatasets](../basic_api.rst#xsar.Sentinel1Meta.subdatasets) property"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8995f4b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "wv_slc_meta.subdatasets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd75a47d",
   "metadata": {},
   "source": [
    "A subdataset can be directly be opened by [xsar.open_dataset](../basic_api.rst#xsar.open_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caroline-decimal",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "wv_slc_ds1 = xsar.open_dataset(wv_slc_meta.subdatasets.index[0])\n",
    "wv_slc_ds1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aed75f31",
   "metadata": {},
   "source": [
    "Or we can get a **single dataset** by using [xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cccbd6d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "wv_slc_meta1 = xsar.Sentinel1Meta(wv_slc_meta.subdatasets.index[0])\n",
    "wv_slc_meta1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e84d8a59",
   "metadata": {},
   "source": [
    "And use this `wv_slc_meta1` object with [xsar.open_dataset](../basic_api.rst#xsar.open_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e85a99b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "wv_slc_ds1 = xsar.open_dataset(wv_slc_meta1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "surprised-thong",
   "metadata": {},
   "source": [
    "## Getting all xsar.Sentinel1Meta  objects from a list of path\n",
    "\n",
    "[xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta) can only handle one path at a time.\n",
    "\n",
    "With [xsar.product_info](../basic_api.rst#xsar.product_info), you can provide a list of path, and get a `pandas.DataFrame` with all `meta` objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "another-corrections",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_info = xsar.product_info([\n",
    "    xsar.get_test_file('S1A_WV_SLC__1SSV_20160510T101603_20160510T102143_011195_010EA1_Z010.SAFE'),\n",
    "    xsar.get_test_file('S1A_IW_SLC__1SDV_20170907T102951_20170907T103021_018268_01EB76_Z010.SAFE')\n",
    "])\n",
    "df_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b079a68",
   "metadata": {},
   "source": [
    "By default,  [xsar.product_info](../basic_api.rst#xsar.product_info) only return a minimal set of columns.\n",
    "\n",
    "Columns list can be given by the `columns=` keyword. \n",
    "\n",
    "Columns name can be any attributes from [xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta), with the addition of the 'meta' column name, who refer to the [xsar.Sentinel1Meta](../basic_api.rst#xsar.Sentinel1Meta) objet itself.\n",
    "\n",
    "If the columns list include 'geometry', the result will by a `geopandas.GeoDataframe`, who can be displayed whith holoviews."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9fc4c32",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf_info = xsar.product_info(\n",
    "    [\n",
    "        xsar.get_test_file('S1A_WV_SLC__1SSV_20160510T101603_20160510T102143_011195_010EA1_Z010.SAFE'),\n",
    "        xsar.get_test_file('S1A_IW_SLC__1SDV_20170907T102951_20170907T103021_018268_01EB76_Z010.SAFE')\n",
    "        \n",
    "    ],\n",
    "    columns=[ 'safe', 'dsid', 'meta', 'geometry' ]\n",
    ")\n",
    "\n",
    "# 'meta' column is dropped for display\n",
    "gv.feature.land * gv.Polygons(gdf_info.drop(['meta'], axis=1)).opts(tools=['hover'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eacfc4f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf_info.iloc[0]['meta']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_xsar",
   "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.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
