{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hazard indicators\n", "Physrisk is primarily designed to run 'bottom-up' calculations that model the impact of climate hazards on large numbers of individual assets (including natural) and operations. These calculations can be used to assess financial risks or socio-economic impacts. To do this physrisk collects:\n", "\n", "- hazard indicators and\n", "- models of vulnerability of assets/operations to hazards.\n", "\n", "Hazard indicators – that is, quantities that provide the information about hazards needed by the vulnerability models – are collected from a variety of sources. OS-Climate consolidates public domain hazard indicators and also provides the means to combine these with commercial data.\n", "\n", "We start with public domain indicators: which ones are available and how can these be obtained?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Requesting hazard indicators via the physrisk API\n", "\n", "Using physrisk, hazard indicators can be obtained directly from source, as long as the user has the necessary API keys. Alternatively, the API – which is simply a hosted instance of physrisk – can be used, albeit the number of requests will be restricted to 30,000 latitudes and longitudes.\n", "\n", "We give a walk-through of the hazard indicator API below, as well as examples of how to obtain the inventory of available hazard indicators." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "# pip install nbformat pandas plotly requests" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "import pprint as pp\n", "from IPython.display import Markdown, display\n", "import pandas as pd\n", "import plotly.graph_objs as go\n", "from plotly.subplots import make_subplots\n", "import plotly.io\n", "import requests\n", "plotly.io.renderers.default = \"notebook\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "base_url = \"https://physrisk-api-sandbox.apps.odh-cl1.apps.os-climate.org/api/\"\n", "\n", "request = {\n", " \"items\": [\n", " {\n", " \"longitudes\": [69.4787, 68.71, 20.1047, 19.8936, 19.6359, 0.5407, 6.9366, 6.935, 13.7319, 13.7319],\n", " \"latitudes\": [34.556, 35.9416, 39.9116, 41.6796, 42.0137, 35.7835, 36.8789, 36.88, -12.4706, -12.4706],\n", " \"request_item_id\": \"my_flood_request\",\n", " \"hazard_type\": \"RiverineInundation\",\n", " \"indicator_id\": \"flood_depth\",\n", " \"scenario\": \"historical\",\n", " \"year\": 1980,\n", " },\n", " {\n", " \"longitudes\": [69.4787, 68.71, 20.1047, 19.8936, 19.6359, 0.5407, 6.9366, 6.935, 13.7319, 13.7319],\n", " \"latitudes\": [34.556, 35.9416, 39.9116, 41.6796, 42.0137, 35.7835, 36.8789, 36.88, -12.4706, -12.4706],\n", " \"request_item_id\": \"my_flood_request\",\n", " \"hazard_type\": \"RiverineInundation\",\n", " \"indicator_id\": \"flood_depth\",\n", " \"scenario\": \"rcp8p5\",\n", " \"indicator_model_gcm\": \"NorESM1-M\", # optional: can specify\n", " \"year\": 2050,\n", " },\n", " {\n", " \"longitudes\": [114.089],\n", " \"latitudes\": [22.4781],\n", " \"request_item_id\": \"my_wind_request_ssp585\",\n", " \"hazard_type\": \"Wind\",\n", " \"indicator_id\": \"max_speed\",\n", " \"scenario\": \"historical\",\n", " \"path\": \"wind/iris/v1/max_speed_{scenario}_{year}\",\n", " # if path is specified then that particular data array is used\n", " \"year\": 2010,\n", " },\n", " {\n", " \"longitudes\": [114.089],\n", " \"latitudes\": [22.4781],\n", " \"request_item_id\": \"my_wind_request_histo\",\n", " \"hazard_type\": \"Wind\",\n", " \"indicator_id\": \"max_speed\",\n", " \"scenario\": \"ssp585\",\n", " \"path\": \"wind/iris/v1/max_speed_{scenario}_{year}\",\n", " \"year\": 2050,\n", " },\n", " {\n", " \"longitudes\": [114.089],\n", " \"latitudes\": [22.4781],\n", " \"request_item_id\": \"my_fire_request\",\n", " \"hazard_type\": \"Fire\",\n", " \"indicator_id\": \"fire_probability\",\n", " \"scenario\": \"ssp585\",\n", " \"path\": \"fire/jupiter/v1/fire_probability_{scenario}_{year}\",\n", " \"year\": 2040,\n", " },\n", " ]\n", "}\n", "url = base_url + \"get_hazard_data\"\n", "response = requests.post(url, json=request).json()\n", "flood_results_baseline, flood_results_rcp585 = (\n", " response[\"items\"][0][\"intensity_curve_set\"],\n", " response[\"items\"][1][\"intensity_curve_set\"],\n", ")\n", "wind_results_baseline, wind_results_ssp585 = (\n", " response[\"items\"][2][\"intensity_curve_set\"],\n", " response[\"items\"][3][\"intensity_curve_set\"],\n", ")\n", "fire_results = response[\"items\"][4][\"intensity_curve_set\"]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig1 = make_subplots(rows=1, cols=2)\n", "fig1.add_scatter(x=flood_results_baseline[0][\"index_values\"], y=flood_results_baseline[0][\"intensities\"], name=\"baseline flood\", row=1, col=1)\n", "fig1.add_scatter(x=flood_results_rcp585[0][\"index_values\"], y=flood_results_rcp585[0][\"intensities\"], name=\"flood RCP 8.5 2050\", row=1, col=1)\n", "fig1.update_xaxes(title=\"Return period (years)\", title_font={\"size\": 14}, row=1, col=1, type=\"log\")\n", "fig1.update_yaxes(title=\"Flood depth (m)\", title_font={\"size\": 14}, row=1, col=1)\n", "fig1.add_scatter(x=wind_results_baseline[0][\"index_values\"], y=wind_results_baseline[0][\"intensities\"], name=\"baseline wind\", row=1, col=2)\n", "fig1.add_scatter(x=wind_results_ssp585[0][\"index_values\"], y=wind_results_ssp585[0][\"intensities\"], name=\"wind SSP585 2050\", row=1, col=2)\n", "fig1.update_xaxes(title=\"Return period (years)\", title_font={\"size\": 14}, row=1, col=2, type=\"log\")\n", "fig1.update_yaxes(title=\"Max (1 minute) wind speed (m/s)\", title_font={\"size\": 14}, row=1, col=2)\n", "fig1.update_layout(legend=dict(orientation=\"h\", y=-0.15))\n", "fig1.update_layout(margin=dict(l=20, r=20, t=20, b=20))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Requesting hazard indicators through physrisk directly\n", "\n", "As mentioned above, the API is simply using a hosted version of physrisk and it is possible to run the same calculations using physrisk directly, as long as the necessary API keys are present in a credentials.env file.\n", "\n", "The requests above can be run in physrisk using a Requester object." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# pip install physrisk-lib" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from dotenv import load_dotenv\n", "from physrisk.container import Container\n", "\n", "load_dotenv(\"../../credentials.env\")\n", "# the container is a dependency injection container,\n", "# which allows the calculation to be configured to a particular use-case\n", "container = Container()\n", "# the requester is used to run calculations using the API.\n", "# At this point, we can of course debug into the code and modify as required.\n", "requester = container.requester()\n", "result = requester.get(request_id=\"get_hazard_data\", request_dict=request)" ] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }