{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Writing out a USGSCSM ISD from an ISIS ideal camera image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "\n",
    "import ale\n",
    "from ale.drivers.isis_ideal_drivers import IdealLsIsisLabelIsisSpiceDriver\n",
    "from ale.formatters.usgscsm_formatter import to_usgscsm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Instantiating an ALE driver\n",
    "\n",
    "ALE drivers are objects that define how to acquire common ISD keys from an input image format, in this case we are reading in an ISIS ideal line scan image using updated, attached exterior orientation data. These driver objects are short-lived and are input to a formatter function that consumes the API to create a serializable file format. `ale.formatters` contains available formatter functions.\n",
    "\n",
    "ALE has a two step process for writing out an ISD: 1. Instantiate your driver (in this case `IdealLsIsisLabelIsisSpiceDriver`) within a context and 2. pass the driver object into a formatter (in this case, `to_usgscsm`).  \n",
    "\n",
    "Requirements:\n",
    " * An ISIS ideal line scan image (e.g. a deijjtered HiRISE image, an undistorted HRSC image, or any linescan image processed through [noproj](https://isis.astrogeology.usgs.gov/Application/presentation/Tabbed/noproj/noproj.html))\n",
    " * A conda environment with ALE installed into it usisng the `conda install` command or created using the environment.yml file at the base of ALE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# change to desired PDS3 image path\n",
    "file_name = '/Users/jmapel/ale/ESP_016076_2175_REDmos_hijitreged.balance.noproj.8bit.cub'\n",
    "\n",
    "from ale.transformation import FrameChain, create_rotations\n",
    "# metakernels are furnished when entering the context (with block) with a driver instance\n",
    "# most driver constructors simply accept an image path \n",
    "with IdealLsIsisLabelIsisSpiceDriver(file_name) as driver:\n",
    "    usgscsmString = to_usgscsm(driver)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write ISD to disk \n",
    "\n",
    "ALE formatter functions generally return bytes or a string that can be written out to disk. ALE's USGSCSM formatter function returns a JSON encoded string that can be written out using any JSON library. \n",
    "\n",
    "USGSCSM requires the ISD to be colocated with the image file with a `.json` extension in place of the image extension."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['radii', 'sensor_position', 'sun_position', 'sensor_orientation', 'detector_sample_summing', 'detector_line_summing', 'focal_length_model', 'detector_center', 'starting_detector_line', 'starting_detector_sample', 'focal2pixel_lines', 'focal2pixel_samples', 'optical_distortion', 'image_lines', 'image_samples', 'name_platform', 'name_sensor', 'reference_height', 'name_model', 'interpolation_method', 'line_scan_rate', 'starting_ephemeris_time', 'center_ephemeris_time', 't0_ephemeris', 'dt_ephemeris', 't0_quaternion', 'dt_quaternion'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load the json string into a dict\n",
    "usgscsm_dict = json.loads(usgscsmString)\n",
    "\n",
    "# Write the dict out to the associated file\n",
    "json_file = os.path.splitext(file_name)[0] + '.json'\n",
    "\n",
    "# Save off the json and read it back in to check if\n",
    "# the json exists and was formatted correctly\n",
    "with open(json_file, 'w') as fp:\n",
    "    json.dump(usgscsm_dict, fp)\n",
    "    \n",
    "with open(json_file, 'r') as fp:\n",
    "    usgscsm_dict = json.load(fp)\n",
    "    \n",
    "usgscsm_dict.keys()"
   ]
  },
  {
   "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
