#! /usr/bin/python3
import calendar
from datetime import datetime, timedelta, timezone


def get_vega_culmination(year, month, day, lon_deg):
  """Berechnet die UTC-Kulminationszeit von Vega auf ~1s Genauigkeit."""
  # 1. J2000.0 Basisdaten von Vega (RA = 18h 36m 56.34s)
  ra_j2000_hours = 18 + 36 / 60 + 56.34 / 3600

  # 2. Präzessions- & Eigenbewegungskorrektur seit J2000.0
  target_date = datetime(year, month, day)
  days_since_j2000 = (target_date - datetime(2000, 1, 1, 12, 0)).days
  years_since_j2000 = days_since_j2000 / 365.25

  ra_corr_seconds = years_since_j2000 * 0.218
  ra_hours = ra_j2000_hours + (ra_corr_seconds / 3600.0)

  # 3. Greenwich Mean Sidereal Time (GMST) um 0:00 UTC
  d_0utc = (
      datetime(year, month, day, 0, 0) - datetime(2000, 1, 1, 12, 0)
  ).days
  gmst_0utc = (6.697374558 + 0.065709824419 * d_0utc) % 24.0
  if gmst_0utc < 0:
    gmst_0utc += 24.0

  # 4. Lokale Sternzeit (LMST)
  lon_hours = lon_deg / 15.0
  lmst_0utc = (gmst_0utc + lon_hours) % 24.0

  # 5. Fehlende Sternzeit bis Kulmination (LMST = RA)
  st_difference = (ra_hours - lmst_0utc) % 24.0

  # 6. Umrechnung in Solarzeit-Sekunden
  solar_seconds = st_difference * 3590.170412

  # 7. Exakte Kulminationszeit ermitteln
  culmination_utc = datetime(
      year, month, day, 0, 0, 0, tzinfo=timezone.utc
  ) + timedelta(seconds=solar_seconds)

  return culmination_utc


def export_september_table(year, lon_deg, output_filename):
  """Generiert eine Monatstabelle für den September und speichert sie als Textdatei."""
  month = 9
  num_days = calendar.monthrange(year, month)[1]

  header = f"""===================================================================
KULMINATIONSTABELLE VEGA - SEPTEMBER {year}
Standort Längengrad: {lon_deg}° E
===================================================================
Datum        | Tag | Kulmination (UTC) | Kulmination (MESZ)
-------------------------------------------------------------------
"""

  rows = []
  for day in range(1, num_days + 1):
    utc_time = get_vega_culmination(year, month, day, lon_deg)

    # Im September gilt in Mitteleuropa MESZ (UTC + 2 Stunden)
    mesz_time = utc_time + timedelta(hours=2)

    date_str = f"{year}-{month:02d}-{day:02d}"
    weekday_str = utc_time.strftime("%a")
    utc_str = utc_time.strftime("%H:%M:%S UTC")
    mesz_str = mesz_time.strftime("%H:%M:%S MESZ")

    rows.append(f"{date_str} | {weekday_str} | {utc_str}   | {mesz_str}")

  footer = (
      "-------------------------------------------------------------------\n"
  )

  # Datei schreiben
  with open(output_filename, "w", encoding="utf-8") as f:
    f.write(header)
    f.write("\n".join(rows))
    f.write("\n" + footer)

  print(f"Datei erfolgreich gespeichert unter: {output_filename}")


# =====================================================================
# AUSFÜHRUNG
# =====================================================================
YEAR = 2026
LONGITUDE = 11.635  # Längengrad München
FILENAME = f"vega_kulmination_september_{YEAR}.txt"

export_september_table(YEAR, LONGITUDE, FILENAME)
