{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# InSAR time series analysis with HyP3 and MintPy\n",
    "\n",
    "This notebook shows how to do time-series analysis using HyP3 product with MintPy. It requires `hyp3_sdk` and `MintPy`:\n",
    "\n",
    "+ run `conda install --yes -c conda-forge hyp3_sdk ipywidgets` to install `hyp3_sdk`\n",
    "+ check the [installation page](https://github.com/insarlab/MintPy/blob/main/docs/installation.md) to install `MintPy`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Initial setup of the notebook\n",
    "\n",
    "The cell below performs the intial setup of the notebook and must be **run every time the notebook (re)starts**. It imports necessary modules and defines the processing location."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "# verify mintpy install\n",
    "try:\n",
    "    #from mintpy.objects.insar_vs_gps import plot_insar_vs_gps_scatter\n",
    "    #from mintpy.unwrap_error_phase_closure import plot_num_triplet_with_nonzero_integer_ambiguity\n",
    "    #from mintpy import workflow, view, tsview, plot_network, plot_transection, plot_coherence_matrix\n",
    "    from mintpy import view, tsview\n",
    "except ImportError:\n",
    "    raise ImportError(\"Can not import mintpy!\")\n",
    "\n",
    "# utils function\n",
    "def configure_template_file(outName, CONFIG_TXT): \n",
    "    \"\"\"Write configuration files for MintPy to process HyP3 product\"\"\"\n",
    "    if os.path.isfile(outName):\n",
    "        with open(outName, \"w\") as fid:\n",
    "            fid.write(CONFIG_TXT)\n",
    "        print('write configuration to file: {}'.format(outName))\n",
    "\n",
    "    else:\n",
    "        with open(outName, \"a\") as fid:\n",
    "            fid.write(\"\\n\" + CONFIG_TXT)\n",
    "        print('add the following to file: \\n{}'.format(outName))\n",
    "\n",
    "# define the work directory\n",
    "#work_dir = os.path.abspath(os.path.join(os.getcwd(), 'mintpy'))      #OpenSARLab at ASF\n",
    "proj_name = 'Ridgecrest'\n",
    "proj_dir = os.path.join('/media/jzhu4/data/hyp3-mintpy', proj_name)  #Local\n",
    "hyp3_dir = os.path.join(proj_dir, 'hyp3')\n",
    "work_dir = os.path.join(proj_dir, 'mintpy')   #Local\n",
    "\n",
    "if not os.path.isdir(proj_dir):\n",
    "    os.makedirs(proj_dir)\n",
    "    print('Create directory: {}'.format(proj_dir))\n",
    "    \n",
    "if not os.path.isdir(hyp3_dir):\n",
    "    os.makedirs(hyp3_dir)\n",
    "    print('Create directory: {}'.format(hyp3_dir))\n",
    "    \n",
    "if not os.path.isdir(work_dir):\n",
    "    os.makedirs(work_dir)\n",
    "    print('Create directory: {}'.format(work_dir))\n",
    "    \n",
    "os.chdir(work_dir)\n",
    "print('Go to work directory: {}'.format(work_dir))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Run MintPy routine workflow `smallbaselineApp.py`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 Prepare the template file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "CONFIG_TXT = f'''# vim: set filetype=cfg:\n",
    "mintpy.load.processor        = hyp3\n",
    "##---------interferogram datasets:\n",
    "mintpy.load.unwFile          = {hyp3_dir}/*/*unw_phase_clip.tif\n",
    "mintpy.load.corFile          = {hyp3_dir}/*/*corr_clip.tif\n",
    "##---------geometry datasets:\n",
    "mintpy.load.demFile          = {hyp3_dir}/*/*dem_clip.tif\n",
    "mintpy.load.incAngleFile     = {hyp3_dir}/*/*lv_theta_clip.tif\n",
    "mintpy.load.waterMaskFile    = {hyp3_dir}/*/*water_mask_clip.tif\n",
    "'''\n",
    "print(CONFIG_TXT)\n",
    "configName = os.path.join(work_dir, \"{}.txt\".format(proj_name))\n",
    "configure_template_file(configName, CONFIG_TXT)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 Load the data produced from HyP3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The example dataset is from 2019 Ridgecrest, CA earthquake. The dataset can be obtained through either downloading from the stagged server or producing with hyp3-sdk. As far as producing data from hyp3-sdk, we provide the prep_ts_hyp3 notebook at the tutorial directory of (https://github.com/ASFHyP3/hyp3-docs/tree/develop/docs )."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# verify / prepare input dataset\n",
    "\n",
    "os.chdir(hyp3_dir)\n",
    "\n",
    "use_staged_data = True\n",
    "\n",
    "zip_file_name ='Ridgecrest.zip'\n",
    "\n",
    "if all(os.path.isfile(os.path.join(work_dir, 'inputs', i)) for i in ['ifgramStack.h5', 'geometryGeo.h5']):\n",
    "    print(\"Required inputs for mintpy already exists.\")\n",
    "\n",
    "else:\n",
    "    if use_staged_data:\n",
    "        # Check if a stage file from S3 already exist, if not try and download it\n",
    "        zip_file = os.path.join(hyp3_dir, zip_file_name)\n",
    "        if not os.path.isfile(zip_file):\n",
    "            !wget https://jzhu-hyp3-dev.s3.us-west-2.amazonaws.com/hyp3-mintpy/{zip_file_name}\n",
    "            #!aws s3 cp s3://jzhu-hyp3-dev/hyp3-mintpy-example/{zip_file_name} {zip_file_name}\n",
    "        # verify if download was succesfull\n",
    "        if os.path.isfile(zip_file_name):\n",
    "            import zipfile, glob\n",
    "            \n",
    "            with zipfile.ZipFile(zip_file, 'r') as fzip:\n",
    "                fzip.extractall(hyp3_dir)\n",
    "            # unzip zip files extracted from the zip_file\n",
    "            files = glob.glob(\"./????_*.zip\")\n",
    "            for file in files:\n",
    "                with zipfile.ZipFile(file) as f:\n",
    "                    f.extractall(hyp3_dir)\n",
    "                    \n",
    "            print('S3 pre-staged data retrieval was successfull')\n",
    "\n",
    "        else:\n",
    "            msg = 'No staged data. Setting use_staged_data = False and re-run this cell.'\n",
    "            print(msg)\n",
    "\n",
    "    else:\n",
    "        print(\"Using HyP3-sdk to download and prepare the input data for MintPy\")\n",
    "        print(\"please refer the notebook\")\n",
    "        os.chdir(os.path.dirname(work_dir))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 Run Time-series Analysis application"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! smallbaselineApp.py --work-dir {work_dir}  {configName}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 Display the analysis results\n",
    "\n",
    "There are a few scripts used to display the analysis results. There are in the `${MINTPY_HOME}/src/mintpy/cli`. Here we show two majoy disaply scripts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir(proj_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "view.main(['mintpy/velocity.h5'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tsview.main(['mintpy/timeseries.h5'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}