{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nProduce a new raster with index stacked in new bands\n=============================================================================\n\nThis example shows how to add to a raster spectral index.\nHere we add LChloC and ACORVI (a modified NDVI).\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import libraries\n----------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom museopheno import sensors,datasets\n\n# to add custom  creation of new raster, import rasterMath\nfrom museotoolbox.processing import RasterMath"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import dataset\n----------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "raster,dates = datasets.Sentinel2_3a_2018(return_dates=True)\nS2 = sensors.Sentinel2(n_bands=10)\n\nprint('Default band order for 10 bands is : '+', '.join(S2.bands_order)+'.')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "List of available index : \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "S2.available_indices.keys()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define a custom function\n---------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def createSITSplusIndex(X):\n    X1 = S2.generate_index(X,S2.get_index_expression('LChloC'),multiply_by=100,dtype=np.int16)\n    X2 = S2.generate_index(X,S2.get_index_expression('ACORVI'),multiply_by=100,dtype=np.int16)\n    \n    return np.hstack((X,X1,X2)).astype(np.int16)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use rasterMath to read and write block per block the raster according to a function\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM = RasterMath(raster)\n\nX = rM.get_random_block()\nprint('Block has {} pixels and {} bands'.format(X.shape[0],X.shape[1]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we can try our function\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "XwithIndex = createSITSplusIndex(X)\nprint('Raster+index produced has {} pixels and {} bands'.format(XwithIndex.shape[0],XwithIndex.shape[1]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we add our function as the test was a success\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM.add_function(createSITSplusIndex,'/tmp/SITSwithIndex.tif')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Produce raster\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM.run()\n\n##################\n# Plot image\nfrom matplotlib import pyplot as plt\nrM = RasterMath('/tmp/SITSwithIndex.tif')\nX=rM.get_random_block() #randomly select a block\nplt.plot(X[:20,:].T)"
      ]
    }
  ],
  "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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}