{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a117baed-ab98-4499-832c-8c73a8606cc0",
   "metadata": {},
   "source": [
    "# Tutorial: Instantiating a CSM Camera Model from Image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3602c014-53bc-4330-a9b0-0848d4927458",
   "metadata": {},
   "source": [
    "Lessons learned in this tutorial: \n",
    "* How to generate Image Support Data (ISD) for an image\n",
    "* instantiate a CSM camera model\n",
    "* Perform a simple ground to image call \n",
    "\n",
    "\n",
    "### 1. Prerequisites, Install Knoten\n",
    "The `knoten` installation may take a little longer than usual due to the many dependencies (including ALE) involved."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffeccab3-0d5d-4609-9c7f-871bdb69f17a",
   "metadata": {},
   "source": [
    "```\n",
    "conda install -c conda-forge knoten=0.2.1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faed4a43-cd06-45c7-bfa1-793978d41486",
   "metadata": {},
   "source": [
    "### 2. Generate an ISD from a Cube\n",
    "We will use MRO data located in the `data/image_to_ground` folder containing a cube and necessary kernels for ISD (Image Support Data) generation.   \n",
    "*Note*: If your cube already has attached spice data, do you not have to specify kernels in the `props` param and can pass in an empty dict `{}` instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f58cb34-d27f-456d-bfb5-f9075ca575b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/chkim/mambaforge3/envs/test/lib/python3.12/site-packages/osgeo/gdal.py:287: FutureWarning: Neither gdal.UseExceptions() nor gdal.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import ale\n",
    "import json\n",
    "import knoten\n",
    "import os\n",
    "\n",
    "# Set local data directory and paths\n",
    "data_dir = '../data/image_to_ground'\n",
    "cube_file = os.path.join(data_dir, 'B10_013341_1010_XN_79S172W.cub')\n",
    "isd_file = os.path.join(data_dir, 'isd_file.json')\n",
    "\n",
    "# Set local kernel paths\n",
    "props = {\n",
    "    'kernels': [\n",
    "        os.path.join(data_dir, 'B10_013341_1010_XN_79S172W_0.bsp'),\n",
    "        os.path.join(data_dir, 'B10_013341_1010_XN_79S172W_1.bsp'),\n",
    "        os.path.join(data_dir, 'mro_ctx_v11.ti'),\n",
    "        os.path.join(data_dir, 'mro_sc_psp_090526_090601_0_sliced_-74000.bc'),\n",
    "        os.path.join(data_dir, 'mro_sc_psp_090526_090601_1_sliced_-74000.bc'),\n",
    "        os.path.join(data_dir, 'mro_sclkscet_00082_65536.tsc'),\n",
    "        os.path.join(data_dir, 'mro_v16.tf'),\n",
    "        os.path.join(data_dir, 'naif0012.tls'),\n",
    "        os.path.join(data_dir, 'pck00008.tpc')\n",
    "    ]\n",
    "}\n",
    "\n",
    "# Generate the ISD string from the cube's label\n",
    "isd_str = ale.loads(\n",
    "    label=cube_file,\n",
    "    formatter=\"ale\",\n",
    "    props=props,\n",
    "    indent=2,\n",
    "    verbose=False,\n",
    "    only_isis_spice=False,\n",
    "    only_naif_spice=False\n",
    ")\n",
    "\n",
    "# Write the ISD string to file 'isd_file.json'\n",
    "with open(isd_file, \"w\") as file:\n",
    "    file.write(isd_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ed327aa-bffc-4316-b42f-496d9e07465e",
   "metadata": {},
   "source": [
    "### 3. Create a Community Sensor Model\n",
    "We will use Knoten's implementation of CSM as the library supports line scanner types of sensor models in the usgscsm library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0c4dbf84-2986-495b-9e4a-da4c77059e7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sensor_model = knoten.csm.create_csm(isd_file, verbose=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6973fe3-9d4a-4408-9310-50334a52ff58",
   "metadata": {},
   "source": [
    "### 4. Convert image coordinates into ground coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d8f2b155-9803-4a6b-a967-bca1ef35860f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-572485.2147483829, -79884.88742005036, -3326939.6184008163)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create an image coordinate at line = 206 and sample = 206\n",
    "image_coord = knoten.csmapi.ImageCoord(206, 206)\n",
    "\n",
    "# Convert the image coordinates to ground coordinates with desired precision of 0.0\n",
    "ground_coord = sensor_model.imageToGround(image_coord, 0.0)\n",
    "\n",
    "# Output the ground coordinates\n",
    "ground_coord.x, ground_coord.y, ground_coord.z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf87c5a5-b26c-4168-9324-ce5b0004cc7c",
   "metadata": {},
   "source": [
    "### 5. Convert ground coordinates into image coordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0edc0b6d-cdbe-46a8-9fdc-4ebdc4570f1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(205.99991086761267, 206.00000010379927)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Convert the image coordinates to ground coordinates with desired precision of 0.0\n",
    "image_coord = sensor_model.groundToImage(ground_coord, 0.0)\n",
    "\n",
    "# Output the image coordinates\n",
    "image_coord.line, image_coord.samp"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
