"""
NASA JunoCam
============

Example of JunoCam field of view projection during the 2021 Ganymede flyby.

"""

# %%
# In June 2021, the Juno spacecraft made a flyby of Ganymede an recorded
# a series of images of the surface.
#
# .. image:: /images/junocam_10629-rgb.png
#     :alt: JunoCam image 10,629

# %%
import matplotlib.pyplot as plt

from planetary_coverage import GANYMEDE, TourConfig
from planetary_coverage.misc import wget


# %%
# Manually download the required kernels
# --------------------------------------

# %%
remote = 'https://naif.jpl.nasa.gov/pub/naif/JUNO/kernels'

kernels = (
    'lsk/naif0012.tls',
    'pck/pck00010.tpc',
    'fk/juno_v12.tf',
    'ik/juno_junocam_v03.ti',
    'sclk/JNO_SCLKSCET.00114.tsc',
    'spk/de440s.bsp',
    'spk/juno_pred_orbit.bsp',
    'spk/juno_rec_orbit.bsp',
    'spk/juno_struct_v04.bsp',
    'spk/jup363.bsp',
    'spk/spk_pre_210416_210902_210608_btm34_p.bsp',
    'ck/juno_sc_rec_210607_210608_v01.bc',
)

# %%
for kernel in kernels:
    wget(f'{remote}/{kernel}', f'data/JUNO/kernels/{kernel}', skip=True)

# %%
# Load Juno trajectory
# --------------------

# %%
juno = TourConfig(
    kernels=[f'data/JUNO/kernels/{kernel}' for kernel in kernels],
    spacecraft='JUNO',
    target='Ganymede',
)

junocam = juno['2021-06-07T16:57:22.8']  # Frame 6 of image 10,629

junocam

# %%
# Select the different channels of the JunoCam:

# %%
junocam_red = junocam.new_traj(instrument='JUNOCAM_RED')
junocam_green = junocam.new_traj(instrument='JUNOCAM_GREEN')
junocam_blue = junocam.new_traj(instrument='JUNOCAM_BLUE')

# %%
# Project the JunoCam FOVs
# ------------------------

# %%
fig = plt.figure(figsize=(12, 8))

ax = fig.add_subplot(projection=GANYMEDE)

ax.add_collection(junocam_red.fovs(edgecolor='tab:red', lw=3))
ax.add_collection(junocam_green.fovs(edgecolor='tab:green', lw=3))
ax.add_collection(junocam_blue.fovs(edgecolor='tab:blue', lw=3))

ax.set_xticks(range(300, 361, 5))
ax.set_yticks(range(-10, 31, 5))

ax.set_xlim(300, 360)
ax.set_ylim(-10, 31)

ax.set_title('JunoCam RGB footprints (frame 6 of image 10,629)')

plt.show()

# %%
# Comparison with the original image
# ----------------------------------
#
# .. image:: /images/junocam_10629-rgb_frame_6.png
#     :alt: JunoCam image 10,629 - frame 6


# %%
# .. admonition:: Download
#
#   - :download:`07-juno.ipynb`
#   - :download:`07-juno.py`
