jyotisham / jyotisha

Python tools for the astronomical / astrological vedAnga of Hindus
MIT License
92 stars 54 forks source link

Replace swisseph with Skyfield for Enhanced Astronomical Calculations #144

Open karthikraman opened 11 months ago

karthikraman commented 11 months ago

I propose to replace the swisseph library with Skyfield for astronomical calculations within the jyotisha package. This change aims to enhance the accuracy, ease of use, and maintainability of the package.

Background

The jyotisha package currently relies on swisseph for planetary position calculations and related astronomical data. While swisseph is powerful, Skyfield offers several advantages that could benefit the jyotisha package.

Advantages of Skyfield

Accuracy and Precision: Skyfield provides high-precision astronomical data and is regularly updated. It uses DE (Development Ephemeris) models from NASA's Jet Propulsion Laboratory for calculations, ensuring high accuracy.

Ease of Use: Skyfield has a more Pythonic API, making the code more readable and easier to maintain. This could attract more contributors to the jyotisha project and simplify the onboarding process for new developers.

Active Development and Support: Skyfield is actively maintained and has a growing community. This ensures better long-term support and potential for future enhancements.

(credits to GPT-4 for introducing Skyfield, and also drafting this issue!)

baskar-yahoo commented 11 months ago

The advantages look motivating; it certainly is for accuracy and precision. I'm looking forward to it.

vvasuki commented 11 months ago

How would you substitute this -

swe.rise_trans(
      jd_start=julian_day_start, body=graha._get_swisseph_id(),
      lon=self.longitude, lat=self.latitude,
      rsmi=CALC_SET)[1][0]

?

karthikraman commented 11 months ago

Very good point - we will have to alter the risings_and_settings routine from Skyfield to consider middle part of the disc (easier) and ignore refraction.. Draft code from ChatGPT:

from skyfield.api import Topos, load
from datetime import datetime, timedelta
from skyfield.almanac import find_discrete, risings_and_settings

# Your location (example: New Delhi, India)
latitude = 28.6139   # Replace with your latitude
longitude = 77.2090  # Replace with your longitude
location = Topos(latitude_degrees=latitude, longitude_degrees=longitude)

# Load ephemeris data
ts = load.timescale()
eph = load('de421.bsp')

# Define the date for which you want to calculate sunrise
date = datetime.now()  # or any specific date datetime(year, month, day)
start = ts.utc(date.year, date.month, date.day, 0, 0, 0)
end = ts.utc(date.year, date.month, date.day, 23, 59, 59)

# Get the sunrise time
def hindu_sunrise(observer, eph):
    sun = eph['sun']
    apparent_horizon = -0.833  # Standard astronomical sunrise
    hindu_correction = -0.5  # Approximate angle for the Sun's center
    total_correction = apparent_horizon + hindu_correction
    return risings_and_settings(eph, sun, observer, altitude_degrees=total_correction)

times, events = find_discrete(start, end, hindu_sunrise(location, eph))
for time, event in zip(times, events):
    if event == 1:  # Sunrise
        print("Hindu Sunrise time:", time.utc_datetime())