{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Simulation results\n",
    "\n",
    "Once your simulation has finished, you probably want to analyse the results with for example\n",
    "the [ThreediToolbox](https://docs.3di.live/d_qgis_plugin.html) or\n",
    "[threedigrid](https://threedigrid.readthedocs.io/en/latest/readme.html#). In this notebook\n",
    "we'll show you how you can download the result files of a 3Di simulation.\n",
    "\n",
    "Let's first create the client to connect to the threedi-api:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from getpass import getpass\n",
    "from pathlib import Path\n",
    "from threedi_api_client.api import ThreediApi\n",
    "from threedi_api_client.openapi.api.v3_api import V3Api\n",
    "from threedi_api_client.files import download_file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Provide authentication details\n",
    "API_HOST = \"https://api.3di.live\"\n",
    "PERSONAL_API_KEY = getpass(\"Personal API token\")  # https://management.3di.live/personal_api_keys\n",
    "\n",
    "config = {\n",
    "    \"THREEDI_API_HOST\": API_HOST,\n",
    "    \"THREEDI_API_PERSONAL_API_TOKEN\": PERSONAL_API_KEY\n",
    "}\n",
    "\n",
    "api_client: V3Api = ThreediApi(config=config)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Now let's select a simulation we're interested in. Make sure it is finished simulating.\n",
    "We will select the simulation we created in the [previous](./simulation_with_events.ipynb)\n",
    "notebook.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "USERNAME = input(\"username for filtering simulations\")\n",
    "\n",
    "simulation_results = api_client.simulations_list(user__username=USERNAME)\n",
    "\n",
    "if simulation_results.count == 0:\n",
    "    raise Exception(f\"No simulations found for user with username {USERNAME}\")\n",
    "\n",
    "simulation = simulation_results.results[0]\n",
    "\n",
    "status = api_client.simulations_status_list(simulation.id)\n",
    "\n",
    "print(simulation)\n",
    "print(f\"status: {status}\")\n",
    "assert status.name == 'finished'"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Now that we have our finished simulation lets see which result files are available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%"
    }
   },
   "outputs": [],
   "source": [
    "result_files = api_client.simulations_results_files_list(simulation.id)\n",
    "\n",
    "for result in result_files.results:\n",
    "    print(result)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Let's download all the results so we can analyse them locally:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%"
    }
   },
   "outputs": [],
   "source": [
    "download_folder = Path(f'results_{simulation.name}')\n",
    "download_folder.mkdir(exist_ok=True)\n",
    "\n",
    "for file in result_files.results:\n",
    "    download = api_client.simulations_results_files_download(\n",
    "        id=file.id, simulation_pk=simulation.id\n",
    "    )\n",
    "    file_path = download_folder / file.filename\n",
    "    download_file(download.get_url, file_path)\n",
    "    print(f\"Finished downloading {file.filename}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "The ThreediToolbox and threedigrid also require the \"gridadmin.h5\" file of the simulation.\n",
    "This is a model specific file so it's under the threedimodels-api. We'll also download this file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%"
    }
   },
   "outputs": [],
   "source": [
    "threedi_model_id = simulation.threedimodel_id\n",
    "download_url = api_client.threedimodels_gridadmin_download(threedi_model_id)\n",
    "\n",
    "file_path = download_folder / \"gridadmin.h5\"\n",
    "download_file(download.get_url, file_path)\n",
    "\n",
    "print(f\"Finished downloading gridadmin.h5\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
