#!/usr/bin/env python3
import math
from datetime import datetime
from astropy.coordinates import SkyCoord, FK5, FK4
from astropy.time import Time
import astropy.units as u

# Beispiel-Datenstruktur für die 57 Sterne (Erweiterbar)
# Format: (Nr, Name, Bayer, Mag, RA_deg/str, Dec_deg/str, pm_ra_cosdec_mas, pm_dec_mas)
STARS_DATA = [
    (1, "Alpheratz", r"\alpha And", 2.06, "00h08m23.3s", "+29d05m26s", 135.7, -162.9),
    (2, "Ankaa", r"\alpha Phe", 2.39, "00h26m17.1s", "-42d18m22s", 224.9, -353.8),
    (10, "Aldebaran", r"\alpha Tau", 0.85, "04h35m55.2s", "+16d30m33s", 63.5, -189.4),
    (37, "Arktur", r"\alpha Boo", -0.05, "14h15m39.7s", "+19d10m57s", -1093.9, -1999.4),
    (49, "Vega", r"\alpha Lyr", 0.03, "18h36m56.3s", "+38d47m01s", 200.9, 286.2),
]

def calculate_apparent_positions(target_date_str="2026-09-07T00:00:00"):
    """
    Berechnet die scheinbaren Koordinaten (Datumsepoche inkl. Eigenbewegung 
    und Präzession) für einen gegebenen Zeitpunkt.
    """
    target_time = Time(target_date_str, format="isot", scale="utc")
    
    print(f"% Berechnet für Datum: {target_time.iso}")
    print(r"\begin{longtable}{r l r r | r r}")
    print(r"\toprule")
    print(r"\textbf{Nr.} & \textbf{Name} & \textbf{RA (J2000)} & \textbf{Dec (J2000)} & \textbf{RA (des Datums)} & \textbf{Dec (des Datums)} \\")
    print(r"\midrule")
    print(r"\endfirsthead")
    
    for nr, name, bayer, mag, ra_str, dec_str, pm_ra, pm_dec in STARS_DATA:
        # 1. J2000 Koordinate mit Eigenbewegung definieren
        star_j2000 = SkyCoord(
            ra=ra_str,
            dec=dec_str,
            pm_ra_cosdec=pm_ra * u.mas / u.yr,
            pm_dec=pm_dec * u.mas / u.yr,
            frame="icrs",
            obstime=Time("J2000.0")
        )

        # 2. Eigenbewegung auf das Zieldatum anwenden
        star_with_pm = star_j2000.apply_space_motion(new_obstime=target_time)

        # 3. Präzession auf das aktuelle Äquinoktium umrechnen (FK5 des Datums)
        star_apparent = star_with_pm.transform_to(FK5(equinox=target_time))

        # Formatierung der Ausgaben
        ra_j2000 = star_j2000.ra.to_string(unit=u.hour, sep=('h ', 'm ', 's'), precision=1)
        dec_j2000 = star_j2000.dec.to_string(unit=u.degree, sep=('° ', "' ", "''"), precision=0, alwayssign=True)

        ra_app = star_apparent.ra.to_string(unit=u.hour, sep=('h ', 'm ', 's'), precision=0)
        dec_app = star_apparent.dec.to_string(unit=u.degree, sep=('° ', "' ", "''"), precision=0, alwayssign=True)

        print(f"{nr} & {name} & {ra_j2000} & {dec_j2000} & {ra_app} & {dec_app} \\\\")

    print(r"\bottomrule")
    print(r"\end{longtable}")

if __name__ == "__main__":
    calculate_apparent_positions("2026-09-07T21:37:00")
