import numpy as np
import matplotlib.pyplot as plt

import astropy.units as u
from astropy.time import Time
from sbpy.dynamics import State, SynGenerator

r = [2, 0, 0] * u.au
v = [0, 30, 0] * u.km / u.s
t = Time("2023-12-08")
frame = "heliocentriceclipticiau76"
comet = State(r, v, t, frame=frame)

betas = [1, 0.1, 0.01, 0]
ages = np.linspace(0, 100, 25) * u.day
observer = State(
    r=[0, 2, 2] * u.au,
    v=[0, 0, 0] * u.km / u.s,
    t=comet.t,
    frame="icrs",
)
dust = SynGenerator(comet, betas, ages, observer=observer)

coords0 = observer.observe(comet)
def plot(ax, coords, **kwargs):
    dRA = coords.ra - coords0.ra
    dDec = coords.dec - coords0.dec
    ax.plot(dRA.arcsec, dDec.arcsec, **kwargs)

fig, ax = plt.subplots()

for syndyne in dust.syndynes():
    # don't draw the beta = 0 syndyne
    if syndyne.beta == 0:
        continue
    plot(ax, syndyne.coords, label=f"$\\beta={syndyne.beta:.2g}$")

# plot every 5th synchrone
for synchrone in dust.synchrones()[4::5]:
    plot(
        ax,
        synchrone.coords,
        ls="--",
        label=f"$\Delta t={synchrone.age.to(u.d):.2g}$",
    )

# and plot the orbit
dt = np.linspace(-2, 2) * u.d
states, coords = dust.source_orbit(dt)
plot(ax, coords, color="k", ls=":", label="Orbit")

ax.invert_xaxis()
plt.setp(ax,
         xlim=[100, -10],
         ylim=[-10, 100],
         xlabel="$\Delta$RA (arcsec)",
         ylabel="$\Delta$Dec (arcsec)",
)
plt.legend()
plt.tight_layout()