{
 "metadata": {
  "name": "",
  "signature": "sha256:1d13fb96ae16797e26ae500d7b704841d6265c5b9b2de882fe602dc50613fb11"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%matplotlib inline\n",
      "import math\n",
      "\n",
      "import numpy as np\n",
      "import numpy.random\n",
      "from scidbpy import connect, SciDBQueryError, SciDBArray\n",
      "import matplotlib.pyplot as plt\n",
      "plt.rcParams['figure.figsize']=(12, 8)\n",
      "\n",
      "sdb = connect('http://localhost:8080')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Example plots of MODIS elevation data only. The example subset of MODIS used here consists of only 3 tiles and is about 4GB.\n",
      "\n",
      "f = sdb.afl  # bindings to SciDB Array Functional Language\n",
      "\n",
      "def modis_regrid(sdb, lat=36.5, lon=-112.5, extent=1.5, image_height=500, image_width=500, array_scale=10000, array=\"all\"):\n",
      "    starting_lat = -900000\n",
      "    starting_lon = -1800000\n",
      "    lat_start = lat - extent\n",
      "    lat_end   = lat + extent\n",
      "    lon_start = lon - extent\n",
      "    lon_end   = lon + extent\n",
      "    \n",
      "    def toindex(x):\n",
      "        return int(x * array_scale)\n",
      "\n",
      "    query = f.between(array, toindex(lat_start), toindex(lon_start), \n",
      "                      toindex(lat_end), toindex(lon_end))\n",
      "    d_lat = (lat_end - lat_start) * array_scale\n",
      "    d_lon = (lon_end - lon_start) * array_scale\n",
      "    regrid_lat = int(math.ceil(d_lat / image_height))\n",
      "    regrid_lon = int(math.ceil(d_lon / image_height))\n",
      "    query = f.regrid(query, regrid_lat, regrid_lon, 'avg(altitude) as altitude')\n",
      "    query = f.subarray(query, int(starting_lat + (lat_start * array_scale - starting_lat) / regrid_lat),\n",
      "                       int(starting_lon + (lon_start * array_scale - starting_lon) / regrid_lon),\n",
      "                       int(starting_lat + (lat_end   * array_scale - starting_lat) / regrid_lat),\n",
      "                       int(starting_lon + (lon_end   * array_scale - starting_lon) / regrid_lon))\n",
      "    \n",
      "    #execute query, convert to NumPy array\n",
      "    im = query.toarray()\n",
      "    \n",
      "    #visualize\n",
      "    plt.imshow(im, origin='lower', cmap='Greens_r', extent=[lon_start, lon_end, lat_start, lat_end])\n",
      "    plt.colorbar()\n",
      "    plt.xlabel(\"Longitude\")\n",
      "    plt.ylabel(\"Latittude\")\n",
      "    plt.title(\"Alititude (Feet)\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "modis_regrid(sdb, lat=32.5, lon=-112, extent=2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}