Python Forum
Sunrise and sunset times from PySwissEph library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sunrise and sunset times from PySwissEph library
#1
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
Reply
#2
Skaperen Wrote:if AI made an error then AI should be the one to research why it made an error.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Understood sir...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Information about api.sunrise-sunset please bill_z 20 12,531 Jan-31-2022, 09:19 PM
Last Post: bill_z
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 5,672 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020