"""
Juice cruise trajectory
=======================

Example of interplanetary cruise trajectory.

"""

# sphinx_gallery_thumbnail_number = 2

import numpy as np

import matplotlib.pyplot as plt

from planetary_coverage import TourConfig
from planetary_coverage.ticks import AU, au_ticks, date_ticks, km_ticks


# %%
# Compute the distance to Jupiter during the cruise phase
# -------------------------------------------------------

# %%
tour = TourConfig(
    mk='5.1 150lb_23_1',
    version='v422',
    spacecraft='JUICE',
    target='JUPITER',
)

tour

# %%
traj = tour['2023-04-06':'2031-06-01':'1 day']

traj

# %%
fig = plt.figure(figsize=(12, 4))
ax = fig.add_subplot()

ax.plot(traj.utc, traj.dist)

ax.set_yscale('log')

ax.xaxis.set_major_formatter(date_ticks)
ax.yaxis.set_major_formatter(km_ticks)

ax.set_title('Juice distance to Jupiter')


# %%
# Add the main events to the plot
# -------------------------------

# %%
EVENTS = [
    # date, description
    ('2023-04-14', 'Launch'),
    ('2024-08-20', 'Moon - Earth flyby'),
    ('2025-08-31', 'Venus flyby'),
    ('2026-09-28', '2nd Earth flyby'),
    ('2029-01-17', '3rd Earth flyby'),
    ('2031-06-01', 'Jupiter orbit insertion'),
]

# %%
# The distance is now represented as astronomical units:

# %%
fig = plt.figure(figsize=(12, 4))
ax = fig.add_subplot()

ax.plot(traj.utc, traj.dist, color='tab:red')

for date, desc in EVENTS:
    ax.axvline(np.datetime64(date), color='gray', linestyle=':')
    ax.annotate(
        desc,
        (np.datetime64(date), 2.5 * AU),
        backgroundcolor='w',
        rotation=90,
        ha='center',
        va='center',
    )

ax.xaxis.set_major_formatter(date_ticks)
ax.yaxis.set_major_formatter(au_ticks)

ax.set_ylim(0, 7 * AU)
ax.set_yticks([i * AU for i in range(8)])

ax.set_title('Juice distance to Jupiter')

plt.show()

# %%
# .. admonition:: Download
#
#   - :download:`01-juice_cruise.ipynb`
#   - :download:`01-juice_cruise.py`
