{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csmapi, os, pyproj, knoten, gdal\n",
    "import urllib.request\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('data/EN0213110924M.IMG', <http.client.HTTPMessage at 0x1184fcac8>)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_dir = 'data'\n",
    "EN0213023991M = os.path.join(image_dir, 'EN0213023991M.IMG')\n",
    "EN0213110924M = os.path.join(image_dir, 'EN0213110924M.IMG')\n",
    "downloader = urllib.request.URLopener()\n",
    "downloader.retrieve(\"https://pdsimage2.wr.usgs.gov/archive/mess-e_v_h-mdis-2-edr-rawdata-v1.0/MSGRMDS_1001/DATA/2011_124/EN0213023991M.IMG\", EN0213023991M)\n",
    "downloader.retrieve(\"https://pdsimage2.wr.usgs.gov/archive/mess-e_v_h-mdis-2-edr-rawdata-v1.0/MSGRMDS_1001/DATA/2011_125/EN0213110924M.IMG\", EN0213110924M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "EN0213023991M_data = gdal.Open(EN0213023991M)\n",
    "EN0213110924M_data = gdal.Open(EN0213110924M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1185ba550>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 984936 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(20, 15))\n",
    "ax = plt.subplot(1, 2, 1)\n",
    "plt.imshow(EN0213023991M_data.ReadAsArray())\n",
    "ax = plt.subplot(1, 2, 2)\n",
    "plt.imshow(EN0213110924M_data.ReadAsArray())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "EN0213023991M_cam = knoten.csm.create_csm(EN0213023991M)\n",
    "EN0213110924M_cam = knoten.csm.create_csm(EN0213110924M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "EN0213023991M_size = EN0213023991M_cam.getImageSize()\n",
    "EN0213110924M_size = EN0213110924M_cam.getImageSize()\n",
    "EN0213023991M_pix_bound = knoten.csm.generate_boundary([EN0213023991M_size.line, EN0213023991M_size.samp])\n",
    "EN0213110924M_pix_bound = knoten.csm.generate_boundary([EN0213110924M_size.line, EN0213110924M_size.samp], 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x11c0a46d8>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 835108 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "EN0213023991M_lines, EN0213023991M_samples = zip(*EN0213023991M_pix_bound)\n",
    "EN0213110924M_lines, EN0213110924M_samples = zip(*EN0213110924M_pix_bound)\n",
    "plt.figure(figsize=(20, 15))\n",
    "ax = plt.subplot(1, 2, 1)\n",
    "plt.imshow(EN0213023991M_data.ReadAsArray())\n",
    "plt.scatter(EN0213023991M_samples, EN0213023991M_lines, c='b')\n",
    "ax = plt.subplot(1, 2, 2)\n",
    "plt.imshow(EN0213110924M_data.ReadAsArray())\n",
    "plt.scatter(EN0213110924M_samples, EN0213110924M_lines, c='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "semi_major = 2439.4*10e3\n",
    "semi_minor = 2439.4*10e3 # Hard code for now, SettableEllipsoid isn't available via the SWIG wrapper\n",
    "EN0213023991M_lons, EN0213023991M_lats, EN0213023991M_alts = knoten.csm.generate_latlon_boundary(EN0213023991M_cam, EN0213023991M_pix_bound, semi_major, semi_minor)\n",
    "EN0213110924M_lons, EN0213110924M_lats, EN0213110924M_alts = knoten.csm.generate_latlon_boundary(EN0213110924M_cam, EN0213110924M_pix_bound, semi_major, semi_minor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11b69bf60>]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18656 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(EN0213023991M_lons, EN0213023991M_lats, 'b')\n",
    "plt.plot(EN0213110924M_lons, EN0213110924M_lats, 'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "EN0213023991M_gcps = knoten.csm.generate_gcps(EN0213023991M_cam, EN0213023991M_pix_bound)\n",
    "EN0213110924M_gcps = knoten.csm.generate_gcps(EN0213110924M_cam, EN0213110924M_pix_bound)\n",
    "proj_str = '+proj=longlat +a={} +b={} +no_defs'.format(semi_major, semi_minor)\n",
    "EN0213023991M_vrt = knoten.csm.generate_vrt([512, 512], EN0213023991M_gcps, EN0213023991M, proj_str)\n",
    "EN0213110924M_vrt = knoten.csm.generate_vrt([512, 512], EN0213110924M_gcps, EN0213110924M, proj_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "warp_options = gdal.WarpOptions(format='VRT', dstNodata=0)\n",
    "EN0213023991M_lvl2 = 'EN0213023991M_lvl2.tif'\n",
    "EN0213110924M_lvl2 = 'EN0213110924M_lvl2.tif'\n",
    "EN0213023991M_lvl2_data = gdal.Warp(EN0213023991M_lvl2, EN0213023991M_vrt, options=warp_options)\n",
    "EN0213110924M_lvl2_data = gdal.Warp(EN0213110924M_lvl2, EN0213110924M_vrt, options=warp_options)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x11c114390>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 578912 characters]\n",
      "text/plain": [
       "<Figure size 1440x1080 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(20, 15))\n",
    "ax = plt.subplot(1, 2, 1)\n",
    "plt.imshow(EN0213023991M_lvl2_data.ReadAsArray())\n",
    "ax = plt.subplot(1, 2, 2)\n",
    "plt.imshow(EN0213110924M_lvl2_data.ReadAsArray())"
   ]
  },
  {
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
