{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% Scratch Script for EWR Flume Sensitivity Testing\n"
    }
   },
   "outputs": [],
   "source": [
    "# Alexander Rey, 2021\n",
    "\n",
    "import os\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "import netCDF4 as nc\n",
    "import math\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.cm as cm\n",
    "\n",
    "from scipy.interpolate import LinearNDInterpolator, interp1d\n",
    "\n",
    "from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata\n",
    "from dfm_tools.get_nc_helpers import get_timesfromnc, get_ncfilelist, get_hisstationlist, get_ncvardimlist, get_timesfromnc\n",
    "import cartopy.crs as ccrs\n",
    "import contextily as ctx\n",
    "from dfm_tools.regulargrid import scatter_to_regulargrid\n",
    "import pathlib as pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [],
   "source": [
    "# runLog = pd.read_excel(\"Y:/12828.101 English Wabigoon River/06_Models/00_Delft3D/ModelRuns.xlsx\", \"Sensitivity\")\n",
    "pth = pl.Path(\"//srv-ott3.baird.com/\", \"Projects\", \"12828.101 English Wabigoon River\", \"06_Models\", \"00_Delft3D\", \"ModelRuns.xlsx\")\n",
    "runLog = pd.read_excel(pth.as_posix(), \"Sensitivity\")\n",
    "\n",
    "dataPath = \"FlowFM_map.nc\"\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% Read Model Log\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(               nc_varkeys      shape                       dimensions  \\\n",
      "0            station_name    (3, 20)            (nStations, name_len)   \n",
      "1               station_x       (3,)                     (nStations,)   \n",
      "2               station_y       (3,)                     (nStations,)   \n",
      "3               station_z       (3,)                     (nStations,)   \n",
      "4      nhistory_dlwq_time    (2401,)            (nhistory_dlwq_time,)   \n",
      "5   history_dlwq_time_bnd  (2401, 2)        (nhistory_dlwq_time, two)   \n",
      "6                     IM1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "7                      Hg  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "8                   IM1S1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "9                    HgS1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "10                    Tau  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "11                  Depth  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "12               fResS1DM  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "13              fResS1IM1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "14              fResS2IM1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "15                   DMS1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "16                FrHgIM1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "17               fResS1Hg  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "18               fResS2Hg  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "19               Velocity  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "20                  CHEZY  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "21             TotalDepth  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "22               TauVELOC  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "23              CalVelTau  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "24                  DisHg  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "25                  QHgSS  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "26                 KdHgSS  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "27                ActThS1  (2401, 3)  (nhistory_dlwq_time, nStations)   \n",
      "\n",
      "      dtype            standard_name  \\\n",
      "0       |S1                            \n",
      "1   float64  projection_x_coordinate   \n",
      "2   float64  projection_y_coordinate   \n",
      "3   float64  projection_z_coordinate   \n",
      "4     int32                     time   \n",
      "5     int32                            \n",
      "6   float32                            \n",
      "7   float32                            \n",
      "8   float32                            \n",
      "9   float32                            \n",
      "10  float32                      Tau   \n",
      "11  float32                            \n",
      "12  float32                            \n",
      "13  float32                            \n",
      "14  float32                            \n",
      "15  float32                            \n",
      "16  float32                            \n",
      "17  float32                            \n",
      "18  float32                            \n",
      "19  float32                            \n",
      "20  float32                            \n",
      "21  float32                            \n",
      "22  float32                            \n",
      "23  float32                            \n",
      "24  float32                            \n",
      "25  float32                            \n",
      "26  float32                            \n",
      "27  float32                            \n",
      "\n",
      "                                            long_name  \\\n",
      "0                               monitoring point/area   \n",
      "1                                        x-coordinate   \n",
      "2                                        y-coordinate   \n",
      "3                                        z-coordinate   \n",
      "4                                                time   \n",
      "5                                                       \n",
      "6                     inorganic matter (IM1) (gDM/m3)   \n",
      "7                                 Mercury (Hg) (g/m3)   \n",
      "8                            IM1 in layer S1 (gDM/m2)   \n",
      "9                               Hg in layer S1 (g/m2)   \n",
      "10                   total bottom shear stress (N/m2)   \n",
      "11                               depth of segment (m)   \n",
      "12  total resuspension flux DM from layer S1 (g/m2/d)   \n",
      "13       resuspension flux IM1 from layer S1 (g/m2/d)   \n",
      "14       resuspension flux IM1 from layer S2 (g/m2/d)   \n",
      "15    total amount of dry matter in layer S1 (gDM/m2)   \n",
      "16                    fraction Hg adsorbed to IM1 (-)   \n",
      "17      resuspension flux Hg from layer S1 (gHg/m2/d)   \n",
      "18      resuspension flux Hg from layer S2 (gHg/m2/d)   \n",
      "19                     horizontal flow velocity (m/s)   \n",
      "20                         Chezy coefficient (m0.5/s)   \n",
      "21                       total depth water column (m)   \n",
      "22  bottom shear stress caused by flow velocities ...   \n",
      "23         calculated 'velocity' from total tau (m/s)   \n",
      "24         free dissolved Hg in water column (gHg/m3)   \n",
      "25  overall quality suspended solid for Hg (mgHg/k...   \n",
      "26   overall partition coefficient Hg in SS (m3/kgDM)   \n",
      "27                      actual thickness layer S1 (m)   \n",
      "\n",
      "                                   coordinates  \\\n",
      "0                                                \n",
      "1                                                \n",
      "2                                                \n",
      "3                                                \n",
      "4                                                \n",
      "5                                                \n",
      "6   station_x station_y station_z station_name   \n",
      "7   station_x station_y station_z station_name   \n",
      "8   station_x station_y station_z station_name   \n",
      "9   station_x station_y station_z station_name   \n",
      "10  station_x station_y station_z station_name   \n",
      "11  station_x station_y station_z station_name   \n",
      "12  station_x station_y station_z station_name   \n",
      "13  station_x station_y station_z station_name   \n",
      "14  station_x station_y station_z station_name   \n",
      "15  station_x station_y station_z station_name   \n",
      "16  station_x station_y station_z station_name   \n",
      "17  station_x station_y station_z station_name   \n",
      "18  station_x station_y station_z station_name   \n",
      "19  station_x station_y station_z station_name   \n",
      "20  station_x station_y station_z station_name   \n",
      "21  station_x station_y station_z station_name   \n",
      "22  station_x station_y station_z station_name   \n",
      "23  station_x station_y station_z station_name   \n",
      "24  station_x station_y station_z station_name   \n",
      "25  station_x station_y station_z station_name   \n",
      "26  station_x station_y station_z station_name   \n",
      "27  station_x station_y station_z station_name   \n",
      "\n",
      "                                       units mesh location  ndims  \n",
      "0                                                               2  \n",
      "1                                          m                    1  \n",
      "2                                          m                    1  \n",
      "3                                          m                    1  \n",
      "4   seconds since 2001-01-01 00:00:00 +00:00                    1  \n",
      "5                                                               2  \n",
      "6                                                               2  \n",
      "7                                                               2  \n",
      "8                                                               2  \n",
      "9                                                               2  \n",
      "10                                                              2  \n",
      "11                                                              2  \n",
      "12                                                              2  \n",
      "13                                                              2  \n",
      "14                                                              2  \n",
      "15                                                              2  \n",
      "16                                                              2  \n",
      "17                                                              2  \n",
      "18                                                              2  \n",
      "19                                                              2  \n",
      "20                                                              2  \n",
      "21                                                              2  \n",
      "22                                                              2  \n",
      "23                                                              2  \n",
      "24                                                              2  \n",
      "25                                                              2  \n",
      "26                                                              2  \n",
      "27                                                              2  ,            nc_dimkeys                name  size\n",
      "0           nStations           nStations     3\n",
      "1            name_len            name_len    20\n",
      "2  nhistory_dlwq_time  nhistory_dlwq_time  2401\n",
      "3                 two                 two     2)\n",
      "varname found in long_name attribute\n",
      "reading time dimension: reconstruct array\n",
      "retrieving original timezone succeeded, no conversion to UTC/GMT applied\n"
     ]
    }
   ],
   "source": [
    "modelPlot  = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "modelPlot  = [0]\n",
    "\n",
    "dataOUT = dict()\n",
    "\n",
    "for i in modelPlot:\n",
    "    file_nc_hist = pl.Path(runLog['Run Location'][i],'Water_Quality','output','deltashell_his.nc')\n",
    "\n",
    "    # Print file variables\n",
    "    print(get_ncvardimlist(file_nc_hist.as_posix())\n",
    "    # Print file stations\n",
    "    # print(get_hisstationlist(file_nc_hist, 'Hg'))\n",
    "\n",
    "    # Define extraction variables\n",
    "    dataVars = ['Hg', 'IM1', 'HgS1', 'FrHgIM1', 'DisHg', 'QHgSS']\n",
    "    for v in dataVars:\n",
    "        # Extract data from NetCDF file\n",
    "        dataIN = get_ncmodeldata(file_nc=file_nc_hist.as_posix(), varname=v, timestep='all', station=1, silent=True)\n",
    "\n",
    "        # Convert to Numpy array\n",
    "        if i==modelPlot[0]:\n",
    "            dataOUT[v] = np.array(dataIN)\n",
    "        else:\n",
    "            dataOUT[v] = np.hstack((dataOUT[v], np.array(dataIN)))\n",
    "\n",
    "# Extract time\n",
    "dataTime = get_timesfromnc(file_nc_hist)\n",
    "\n",
    "# Clean up input variables\n",
    "del dataIN\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% Import using DFM functions\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 648x1080 with 6 Axes>",
      "image/png": "[encoded data, 124772 characters]\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(9, 15))\n",
    "fig.patch.set_facecolor('white')\n",
    "fig.tight_layout(pad=3)\n",
    "ax = axes.flat\n",
    "\n",
    "cmap = cm.get_cmap('Paired')\n",
    "\n",
    "dataVars        = ['Hg', 'IM1', 'HgS1', 'FrHgIM1', 'DisHg', 'QHgSS']\n",
    "dataVarsLabel   = ['Hg (g/m3)', 'IM1 (gDM/m3)', 'HgS1 (gDM/m2)', 'FrHgIM1 (-)', 'DisHg (gHg/m3)', 'QHgSS (mgHg/kgDM)']\n",
    "dataVarsLegend  = ['Mercury', ' Inorganic matter in water', 'Inorganic matter in layer S1',\n",
    "                   'Fraction Hg adsorbed to inorganic matter', 'Free dissolved Hg in water column',\n",
    "                   'Overall quality suspended solid for Hg']\n",
    "for vIDX, v in enumerate(dataVars):\n",
    "\n",
    "    # Loop through sensitivity runs\n",
    "    for i in modelPlot:\n",
    "        if i == 0:\n",
    "            ax[vIDX].plot(dataTime, dataOUT[v][:,i], color='k', linewidth=3)\n",
    "        else:\n",
    "            ax[vIDX].plot(dataTime, dataOUT[v][:,i], color=cmap.colors[i-1], linewidth=1)\n",
    "\n",
    "    # Plot settings\n",
    "    ax[vIDX].set_ylabel(dataVarsLabel[vIDX])\n",
    "    ax[vIDX].set_title(dataVarsLegend[vIDX])\n",
    "\n",
    "    if vIDX == 0:\n",
    "        ax[vIDX].legend(runLog['Short Description'], loc='upper right')\n",
    "\n",
    "plt.gcf().autofmt_xdate()\n",
    "# plt.gcf().align_ylabels()\n",
    "plt.show()\n",
    "\n",
    "\n",
    "fig.savefig('C:/Users/arey/files/Projects/Grassy Narrows/Modelling/FlumeTest/SensTest/Figures/SensTestParams.pdf',\n",
    "             bbox_inches='tight')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% Plot Sensitivity Data\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-b2185061",
   "language": "python",
   "display_name": "PyCharm (NTC_DFM)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}