{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]\n",
      "numpy version: 1.14.0\n",
      "matplotlib version: 2.1.2\n",
      "pandas version: 0.22.0\n",
      "flopy version: 3.2.9\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import time\n",
    "import os\n",
    "import sys\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import flopy\n",
    "\n",
    "print(sys.version)\n",
    "print('numpy version: {}'.format(np.__version__))\n",
    "print('matplotlib version: {}'.format(mpl.__version__))\n",
    "print('pandas version: {}'.format(pd.__version__))\n",
    "print('flopy version: {}'.format(flopy.__version__))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "modflownwt = '../bin/MODFLOW-NWT_64.exe'\n",
    "#%ls ..\\model\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_ws = '../model'\n",
    "ml = flopy.modflow.Modflow.load(\"Model1.nam\", model_ws=model_ws, verbose=False,\n",
    "                               check=False, exe_name=modflownwt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "xul:0; yul:15676.50482; rotation:0; proj4_str:None; units:meters; lenuni:2; length_multiplier:1.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ml.dis.sr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "782367.670159734 9242294.06548361 0.0\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "dislines = open('../model/Model1.dis').readlines()\n",
    "#extracting features from the upper left corner\n",
    "upperleft = re.sub('\\(|\\)',',',dislines[1]).split(',')[1:3]\n",
    "modelxul = float(upperleft[0])\n",
    "modelyul = float(upperleft[1])\n",
    "\n",
    "#extracting gridrotation\n",
    "gridangle = float(re.sub('\\\\n',' ',dislines[5]).split(' ')[6])\n",
    "\n",
    "print(modelxul,modelyul,gridangle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flopy.utils.reference import SpatialReference\n",
    "ml.sr = SpatialReference(delr=ml.dis.delr, delc=ml.dis.delc, \n",
    "                         xul=modelxul, yul=modelyul, rotation=gridangle, epsg=32717)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "xul:782367.6702; yul:9242294.065; rotation:0; proj4_str:+proj=utm +zone=17 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs ; units:meters; lenuni:2; length_multiplier:1.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ml.dis.sr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.axes._subplots.AxesSubplot at 0x7e011e5cc0>,\n",
       " <matplotlib.axes._subplots.AxesSubplot at 0x7e01954550>,\n",
       " <matplotlib.axes._subplots.AxesSubplot at 0x7e019cd940>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 55252 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7e7e1fd438>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 55268 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7e018ef080>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 16740 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7e0198a128>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ml.dis.plot(mflay=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrote ../shp\\Model1_dis.shp\n"
     ]
    }
   ],
   "source": [
    "#Remember to have pyshp installed\n",
    "#in mac: pip install pyshp\n",
    "#in windows on anaconda prompt; python -m pip install pyshp\n",
    "ml.dis.export(os.path.join('../shp',\"Model1_dis.shp\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrote ../shp\\Model1_upw.shp\n"
     ]
    }
   ],
   "source": [
    "ml.upw.export(os.path.join('../shp',\"Model1_upw.shp\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 13, 39, 133, 134, 19, 22, 24, 25]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ml."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
