{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from plio.io.io_gdal import GeoDataset\n",
    "from shapely import wkt\n",
    "import knoten.csm as csm\n",
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26424 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "cub_loc = 'data/J03_045994_1986_XN_18N282W_sorted.cub'\n",
    "old_isd = 'J03_045994_1986_XN_18N282W_sorted.json'\n",
    "new_isd = 'data/J03_045994_1986_XN_18N282W_sorted.json'\n",
    "\n",
    "# load ISDs into dictionaries\n",
    "old_aledict = json.load(open(old_isd))\n",
    "new_aledict = json.load(open(new_isd))\n",
    "\n",
    "# make camera models from the ISDs\n",
    "old_camera = csm.create_csm(old_isd)\n",
    "new_camera = csm.create_csm(new_isd)\n",
    "\n",
    "# get lines and samples from ISDs\n",
    "olines, osamples = old_aledict[\"image_lines\"], old_aledict[\"image_samples\"]\n",
    "nlines, nsamples = new_aledict[\"image_lines\"], new_aledict[\"image_samples\"]\n",
    "\n",
    "# generate boundaries from ISDs\n",
    "old_boundary = csm.generate_boundary((olines, osamples))\n",
    "new_boundary = csm.generate_boundary((nlines, nsamples))\n",
    "\n",
    "# generate lat lon boundaries\n",
    "olons, olats, oalts = csm.generate_latlon_boundary(old_camera, old_boundary, radii=(3396.19,3376.2))\n",
    "nlons, nlats, nalts = csm.generate_latlon_boundary(new_camera, new_boundary, radii=(3396.19,3376.2))\n",
    "\n",
    "# get isis footprint\n",
    "isis_wkt = str((wkt.loads(GeoDataset(cub_loc).footprint.ExportToWkt())))\n",
    "isis_geom = wkt.loads(isis_wkt)\n",
    "isis_lon, isis_lat = isis_geom.geoms[0].exterior.coords.xy\n",
    "\n",
    "# Convert ISIS longitude to -180/180\n",
    "isis_lon_360 = np.asarray(isis_lon)\n",
    "isis_lon_180 = np.mod(isis_lon_360 - 180.0, 360.0) - 180.0\n",
    "\n",
    "# plot footprints\n",
    "plt.rcParams[\"figure.figsize\"] = [5, 5]\n",
    "plt.axes().set_aspect('equal','datalim')\n",
    "plt.xlabel(\"Longitude (deg)\")\n",
    "plt.ylabel(\"Latitude (deg)\")\n",
    "plt.title(\"Footprint Comparisons\")\n",
    "oldale_plot, = plt.plot(olons, olats, 'r')\n",
    "oldale_plot.set_label(\"ALE 0.0.4\")\n",
    "newale_plot, = plt.plot(nlons, nlats, 'b', linewidth=3.0)\n",
    "isis_plot, = plt.plot(isis_lon_180, isis_lat, 'y', linewidth=1.5)\n",
    "\n",
    "isis_plot.set_label('ISIS CUBE')\n",
    "newale_plot.set_label(\"ALE 0.2.0\")\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "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
}
