{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nrasterMath with custom window/block size (and with 3 dimensions)\n=================================================================\n\nTips to use rasterMath by defining its block size and to receive\na full block (not a array with one pixel per row.)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import librairies\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from museotoolbox.processing import RasterMath\nfrom museotoolbox import datasets\nfrom matplotlib import pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load HistoricalMap dataset\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "raster,vector = datasets.load_historical_data()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize rasterMath with raster\n------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Set return3d to True to have full block size (not one pixel per row)\n\nrM = RasterMath(raster,return_3d=True)\n\nprint(rM.get_random_block().shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Comparing different block size (%, fixed, full block)\n-------------------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can define block by percentage of the whole width/height\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM.custom_block_size(1/2,1/2) \nprint(rM.get_random_block().shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Or by fixed window \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM.custom_block_size(50,100) # width divided every 50 pixel and height every 100\nprint(rM.get_random_block().shape)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To have the full image (one block)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM.custom_block_size(-1,-1) # to have the full image"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To have block width divided by 4 and height by 2\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rM.custom_block_size(1/4,1/2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define block size for output raster\n-------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "raster_parameters = rM.get_raster_parameters()\n\nprint('Default parameters are '+str(raster_parameters))\n\n\n# to do before adding the function\n\nrM.custom_block_size(256,256) # custom for reading AND writing the output\n#raster_parameters = ['COMPRESS=DEFLATE']\n#rM.customRasterParameters(raster_parameters)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "now add a function to just return the same raster\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "returnSameImage  = lambda x : x\nrM.add_function(returnSameImage,'/tmp/testcustomblock.tif')\nrM.run()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "check block size of new raster\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rMblock = RasterMath('/tmp/testcustomblock.tif')\nprint(rMblock.block_sizes)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot blocks\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_row,n_col = 2,4\nrM.custom_block_size(1/n_col,1/n_row)\n\nfig=plt.figure(figsize=(12,6),dpi=150)\n\nfor idx,tile in enumerate(rM.read_block_per_block()):\n    fig.add_subplot(n_row,n_col,idx+1)\n    plt.title('block %s' %(idx+1))\n    plt.imshow(tile)\nplt.show()"
      ]
    }
  ],
  "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
}