"""
JANUS FOV during Ganymede flyby
===============================

Example of representation of JANUS field of view
during 2G2 Ganymede flyby.

"""

import matplotlib.pyplot as plt

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


# %%
# Download JUICE timeline from the SOC
# ------------------------------------

# %%
URL = 'https://juicesoc.esac.esa.int/data/DATA/crema_5_0'
TIMELINE = 'mission_timeline_event_file_5_0.csv'

wget(f'{URL}/{TIMELINE}', TIMELINE, skip=True)

# %%
# Get the 2G2 flyby date from the timeline
# ----------------------------------------

# %%
timeline = read_events(TIMELINE)

flyby_2G2 = timeline['FLYBY_GANYMEDE']['2G2']

flyby_2G2


# %%
# Load JUICE trajectory during 2G2
# --------------------------------
tour = TourConfig(mk='5.1 150lb', spacecraft='JUICE', instrument='JANUS',
                  target='Ganymede', version='v400')

janus_flyby = tour.flyby(flyby_2G2)

janus_flyby

# %%
# Keep only the part of the trajectory below 100,000 km
# -----------------------------------------------------

# %%
mask_janus_flyby = janus_flyby.where(janus_flyby.alt < 100_000)

mask_janus_flyby

# %%
# Represent JANUS FOV on the map
# -------------------------------

# %%
fig = plt.figure(figsize=(12, 9))
ax = fig.add_subplot(projection=GANYMEDE)

ax.add_collection(mask_janus_flyby.fovs(facecolors='inc',
                                        vmin=0, vmax=90, sort='inc'))

ax.colorbar(vmin=0, vmax=90, label='inc', extend='max')

ax.set_title('JANUS footprints during 2G2 flyby (Feb. 13th 2032)')

plt.show()

# %%
# .. admonition:: Download
#
#   - :download:`janus_fov.ipynb`
#   - :download:`janus_fov.py`
