{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sensor Utils\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from csmapi import csmapi\n",
    "from knoten import csm, sensor_utils\n",
    "\n",
    "from knoten.shape import Ellipsoid\n",
    "from knoten.illuminator import Illuminator\n",
    "\n",
    "import ale\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a usgscsm sensor model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fileName = \"data/N1573082850_1.cub\"\n",
    "\n",
    "kernels = ale.util.generate_kernels_from_cube(fileName, expand=True)\n",
    "isd_string = ale.loads(fileName, props={'kernels': kernels})\n",
    "csm_isd = os.path.splitext(fileName)[0] + '.json'\n",
    "\n",
    "with open(csm_isd, 'w') as isd_file:\n",
    "    isd_file.write(isd_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run Sensor Utils with usgscsm sensor model and image point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "camera = csm.create_csm(csm_isd)\n",
    "image_pt = csmapi.ImageCoord(511.5, 511.5)\n",
    "shape = Ellipsoid.from_csm_sensor(camera)\n",
    "illuminator = Illuminator()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38.87212509629895"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phaseAngle = sensor_utils.phase_angle(image_pt, camera, shape, illuminator)\n",
    "\n",
    "phaseAngle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49.60309924893989"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emissionAngle = sensor_utils.emission_angle(image_pt, camera, shape)\n",
    "\n",
    "emissionAngle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2903512972.146115"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "slantDistance = sensor_utils.slant_distance(image_pt, camera, shape)\n",
    "\n",
    "slantDistance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2943536048.858226"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "targetCenterDistance = sensor_utils.target_center_distance(image_pt, camera)\n",
    "\n",
    "targetCenterDistance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LatLon(lat=3.2229625890973583, lon=258.6197326526089)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subSpacecraftPoint = sensor_utils.sub_spacecraft_point(image_pt, camera)\n",
    "\n",
    "subSpacecraftPoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "59096282.024265066"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "localRadius = sensor_utils.local_radius(image_pt, camera, shape)\n",
    "\n",
    "localRadius"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(79.34815579474038, -2.7790780986459485)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rightAscDec = sensor_utils.right_ascension_declination(image_pt, camera)\n",
    "\n",
    "rightAscDec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17397.96094194587"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lineResolution = sensor_utils.line_resolution(image_pt, camera, shape)\n",
    "\n",
    "lineResolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17397.93370038153"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sampleResolution = sensor_utils.sample_resolution(image_pt, camera, shape)\n",
    "\n",
    "sampleResolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17397.9473211637"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pixelResolution = sensor_utils.pixel_resolution(image_pt, camera, shape)\n",
    "\n",
    "pixelResolution"
   ]
  }
 ],
 "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.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
