{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import rasterio\n",
    "from rasterio import plot\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LC08_L1TP_042035_20180603_20180615_01_T1_ANG.txt',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B1.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B10.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B11.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B2.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B3.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B4.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B4_clip.tif',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B5.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B5_clip.tif',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B6.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B7.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B8.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_B9.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_BQA.TIF',\n",
       " 'LC08_L1TP_042035_20180603_20180615_01_T1_MTL.txt']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.listdir('../Landsat8/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "imagePath = '../Landsat8/'\n",
    "band4 = rasterio.open(imagePath+'LC08_L1TP_042035_20180603_20180615_01_T1_B4.TIF')\n",
    "band5 = rasterio.open(imagePath+'LC08_L1TP_042035_20180603_20180615_01_T1_B5.TIF')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7951"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7831"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 91876 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x86f3d68>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot.show(band4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'uint16'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.dtypes[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CRS({'init': 'epsg:32611'})"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.crs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Affine(30.0, 0.0, 164385.0,\n",
       "       0.0, -30.0, 4110315.0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, ..., 0, 0, 0],\n",
       "       [0, 0, 0, ..., 0, 0, 0],\n",
       "       [0, 0, 0, ..., 0, 0, 0],\n",
       "       ...,\n",
       "       [0, 0, 0, ..., 0, 0, 0],\n",
       "       [0, 0, 0, ..., 0, 0, 0],\n",
       "       [0, 0, 0, ..., 0, 0, 0]], dtype=uint16)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band4.read(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 460680 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#multiple band representation\n",
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))\n",
    "plot.show(band4, ax=ax1, cmap='Blues') #red\n",
    "plot.show(band5, ax=ax2, cmap='Blues') #nir\n",
    "fig.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17421., 17139., 17257., 17194., 17027.],\n",
       "       [17624., 17443., 17163., 16776., 16948.],\n",
       "       [17234., 16360., 16528., 16713., 16388.],\n",
       "       [13113., 13077., 12595., 12116., 12814.],\n",
       "       [11915., 11730., 11747., 11562., 11759.]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nir = band5.read(1).astype('float64')\n",
    "red = band4.read(1).astype('float64')\n",
    "\n",
    "nir[4000:4005,4000:4005]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\Software\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:4: RuntimeWarning: invalid value encountered in true_divide\n",
      "  after removing the cwd from sys.path.\n"
     ]
    }
   ],
   "source": [
    "ndvi = np.where(\n",
    "    (nir+read)==0.,\n",
    "    0,\n",
    "    (nir-red)/(nir+red)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "ndviImage = rasterio.open('../Output/ndviImage.tiff','w',driver='Gtiff',\n",
    "                         width=band4.width, height=band4.height,\n",
    "                          count=1,\n",
    "                          crs=band4.crs,\n",
    "                          transform=band4.transform,\n",
    "                          dtype='float64'\n",
    "                         )\n",
    "ndviImage.write(ndvi,1)\n",
    "ndviImage.close()"
   ]
  },
  {
   "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
