{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nQuantitative Precipitation Estimate (QPE)\n====================================================\n\nThis example demonstrates how to perform QPE,\nusing raingauge data from Manila.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport pandas\nimport numpy as np\nfrom swirlspy.qpe.rfmap import RainGauge\nfrom swirlspy.qpe.rfmap import to_map_coordinates\nfrom swirlspy.qpe.rfmap import epsilon\nfrom swirlspy.qpe.rfmap import rbf_interpolation\nfrom swirlspy.qpe.rfmap import save_fig"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read files and declare raingauge object\n---------------------------------------\n\nRead files in consecutive timesteps to obtain accumulated rainfall,\nreturn as a raingauge object.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "THIS_DIR = '../tests/qpe'\nos.chdir(THIS_DIR)\n    \nfilelist = [\"rf60m_201808102340_qced\", \"rf60m_201808102350_qced\", \n            \"rf60m_201808110000_qced\", \"rf60m_201808110010_qced\", \n            \"rf60m_201808110020_qced\", \"rf60m_201808110030_qced\",\n            \"rf60m_201808110040_qced\"]\ntable = pandas.read_csv(filepath_or_buffer=filelist[0], header=None, \n\t\t\t\t\t\tdelim_whitespace=True, skiprows=[0], \n\t\t\t\t\t\tusecols=[1,2,3], dtype=float, na_values=3276.7, \n\t\t\t\t\t\tlow_memory=False) #override nan values\nnrow, ncol = table.shape\naccu_rf = np.zeros(nrow)\n#accumulate rainfall\nfor i in range (0,len(filelist)):\n    file = open(filelist[i])\n    if i == 0:\n        start_time = file.readline()\n    elif i == len(filelist)-1:\n        end_time = file.readline()\n    table = pandas.read_csv(filepath_or_buffer=filelist[i], header=None,  \n\t\t\t\t\t\t\tdelim_whitespace=True,  skiprows=[0],\n\t\t\t\t\t\t\tusecols=[1,2,3], dtype=float, na_values=3276.7, \n\t\t\t\t\t\t\tlow_memory=False) #override nan values\n    table = table.dropna()\n    gauge_lat = table[1].values\n    gauge_lon = table[2].values\n    accu_rf = accu_rf + table[3].values\n    \n#Declare raingauge object\nrg_object = RainGauge(start_time, end_time, gauge_lat, \n\t\t\t\t\t    gauge_lon, accu_rf)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Interpolation\n-------------\n\nPerform RBF interpolation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "lllon = 119.8\nurlon = 122.0\nlllat = 13.6\nurlat = 16.5\nresolution = 1000\nmode= \"rbf\"\nm_rg_lon, m_rg_lat, m_lllon, m_lllat, m_urlon, m_urlat, \\\nrbf_gx, rbf_gy = to_map_coordinates(lllon, urlon, lllat, urlat, \n                                    rg_object.gauge_lon, \n                                    rg_object.gauge_lat,\n                                    resolution) \nepsilon_value = epsilon(urlon, lllon, resolution, rg_object.rg_number)\ninterpolated_rf = rbf_interpolation(m_rg_lon, m_rg_lat, rbf_gx, \n                                    rbf_gy, rg_object.rainfall, \n                                    epsilon_value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate rainfall map\n---------------------\n\nHere, we create a rainfall map with Cartopy basemap and\nshowing raingauge values. Please refer to \n`swirlspy.qpe.rfmap.save_fig` for customization.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "#Options here: showing raingauge values, Cartopy basemap\nsave_fig(mode, rg_object.start_time, rg_object.end_time, lllon, urlon, lllat, \n        urlat, m_rg_lon, m_rg_lat, rg_object.rainfall, \n        rbf_gx, rbf_gy, interpolated_rf, True, True)"
      ]
    }
  ],
  "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.6.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}