{
 "metadata": {
  "name": "",
  "signature": "sha256:ce2824974e6e7b99675d6536670cf6898b444945e803f6586259f45b88676716"
 },
 "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": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = sdb.zeros(5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "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",
      "    array = sdb.wrap_array(array)\n",
      "    array = array.between(toindex(lat_start), toindex(lon_start),\n",
      "                          toindex(lat_end), toindex(lon_end))\n",
      "\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",
      "        \n",
      "    array = array.regrid(regrid_lat, regrid_lon, 'avg(altitude) as altitude')\n",
      "    array = array.subarray(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",
      "    #note that all of this computation has been lazy. \n",
      "    # In most cases, actual database computation is delayed until a call to eval() or toarray()\n",
      "    \n",
      "    #execute query, convert to NumPy array\n",
      "    im = array.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": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "modis_regrid(sdb, lat=32.5, lon=-112, extent=2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "global name 'query' is not defined",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-8-02d586a27d43>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmodis_regrid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msdb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlat\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m32.5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlon\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m112\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mextent\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;32m<ipython-input-7-2c5245cd2223>\u001b[0m in \u001b[0;36mmodis_regrid\u001b[0;34m(sdb, lat, lon, extent, image_height, image_width, array_scale, array)\u001b[0m\n\u001b[1;32m     23\u001b[0m     \u001b[0mregrid_lon\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mceil\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md_lon\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mimage_height\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m     \u001b[0marray\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mregrid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquery\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mregrid_lat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mregrid_lon\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'avg(altitude) as altitude'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     26\u001b[0m     array = array.subarray(int(starting_lat + (lat_start * array_scale - starting_lat) / regrid_lat),\n\u001b[1;32m     27\u001b[0m                            \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstarting_lon\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mlon_start\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0marray_scale\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstarting_lon\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mregrid_lon\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;31mNameError\u001b[0m: global name 'query' is not defined"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}