{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import required libraries\n",
    "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.listdir('../Landsat8/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import bands as separate 1 band raster\n",
    "imagePath = '../Landsat8/'\n",
    "band4 = rasterio.open(imagePath+'LC08_L1TP_042035_20180603_20180615_01_T1_B4.TIF') #red\n",
    "band5 = rasterio.open(imagePath+'LC08_L1TP_042035_20180603_20180615_01_T1_B5.TIF') #nir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#number of raster bands\n",
    "band4.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#number of raster columns\n",
    "band4.width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#number of raster rows\n",
    "band4.height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plot band \n",
    "plot.show(band4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#type of raster byte\n",
    "band4.dtypes[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#raster sytem of reference\n",
    "band4.crs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#raster transform parameters\n",
    "band4.transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#raster values as matrix array\n",
    "band4.read(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#multiple band representation\n",
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))\n",
    "plot.show(band4, ax=ax1, cmap='Blues')\n",
    "plot.show(band5, ax=ax2, cmap='Blues')\n",
    "fig.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#generate nir and red objects as arrays in float64 format\n",
    "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#ndvi calculation, empty cells or nodata cells are reporte as 0\n",
    "ndvi = np.where(\n",
    "                (nir+red)==0.,\n",
    "                0,\n",
    "                (nir-red)/(nir+red)\n",
    "               )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export ndvi image\n",
    "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) #ndvi\n",
    "ndviImage.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plot ndvi\n",
    "ndvi = rasterio.open('../Output/ndviImage.tiff')\n",
    "plot.show(ndvi, cmap='Greens')"
   ]
  },
  {
   "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
}
