Python Forum

Full Version: Sunrise and sunset times from PySwissEph library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using PySwissEph in Python to get planetary positions based on Lahiri Ayanamsha. Planetary positions work fine. But I cannot make the code work for sunrise and sunset times. I am totally new to Python. This is needed for my little project in MS Access(!). Honestly, I have used the code from AI.
The code below doesn't work. If you could help, please.

pip install pyswisseph
import swisseph as swe

# Set the path to Swiss Ephemeris data files
swe.set_ephe_path('/usr/share/ephe')  # Adjust this path based on where you have the ephemeris data files stored

# Function to get sunrise and sunset times
def get_sunrise_sunset(year, month, day, latitude, longitude, timezone):
    # Convert input date to Julian Day
    jd = swe.julday(year, month, day)
    
    # Adjust Julian Day for UT
    jd_ut = jd - (timezone / 24.0)
    
    # Compute sunrise
    sunrise_info = swe.rise_trans(jd_ut, swe.SUN, longitude, latitude, rsmi=swe.CALC_RISE | swe.BIT_DISC_CENTER)
    sunrise = swe.revjul(sunrise_info[1][0])
    
    # Compute sunset
    sunset_info = swe.rise_trans(jd_ut, swe.SUN, longitude, latitude, rsmi=swe.CALC_SET | swe.BIT_DISC_CENTER)
    sunset = swe.revjul(sunset_info[1][0])
    
    return sunrise, sunset

# Example usage
year, month, day = 2025, 1, 27  # Example date
latitude, longitude = 37.7749, -122.4194  # Example coordinates (San Francisco)
timezone = -8  # PST (Pacific Standard Time)

sunrise, sunset = get_sunrise_sunset(year, month, day, latitude, longitude, timezone)

# Print sunrise and sunset times
print(f"Sunrise: {int(sunrise[0])}-{int(sunrise[1]):02d}-{int(sunrise[2]):02d} {int(sunrise[3]):02d}:{int(sunrise[4]):02d}:{int(sunrise[5]):02d}")
print(f"Sunset: {int(sunset[0])}-{int(sunset[1]):02d}-{int(sunset[2]):02d} {int(sunset[3]):02d}:{int(sunset[4]):02d}:{int(sunset[5]):02d}")
This code obviously throws this error;

Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-44-523679a7283f> in <cell line: 0>() 27 timezone = -8 # PST (Pacific Standard Time) 28 ---> 29 sunrise, sunset = get_sunrise_sunset(year, month, day, latitude, longitude, timezone) 30 31 # Print sunrise and sunset times <ipython-input-44-523679a7283f> in get_sunrise_sunset(year, month, day, latitude, longitude, timezone) 13 14 # Compute sunrise ---> 15 sunrise_info = swe.rise_trans(jd_ut, swe.SUN, longitude, latitude, rsmi=swe.CALC_RISE | swe.BIT_DISC_CENTER) 16 sunrise = swe.revjul(sunrise_info[1][0]) 17 TypeError: 'float' object cannot be interpreted as an integer
Skaperen Wrote:if AI made an error then AI should be the one to research why it made an error.
Understood sir...