Calculating Shurouq and Maghrib times
Overview
For Shurouq and Maghrib, we are essentially calculating the apparent sunrise and sunset times—this means the times when the Sun actually looks like it’s rising or setting from the observer’s point of view, not just when it geometrically crosses the horizon.
We start with a basic formula for sunrise and sunset and then make some adjustments to account for various real-world factors that affect what we see.
By definition, sunrise and sunset occur when the solar altitude angle is exactly 0º degrees—meaning the Sun is just on the horizon. But to get accurate times, we need to adjust this angle for several important reasons:
1. Solar Radius Adjustment:
The Sun isn’t just a point in the sky—it has a size. When we say the Sun has “risen” or “set,” we usually mean the top edge of the Sun, not the center. To account for this, we subtract a small angle (about 0.27 degrees) from our calculation. This makes sure we’re timing the moment when the top of the Sun appears or disappears at the horizon.
2. Observer Height Adjustment:
If you’re standing on a hill or at a high elevation, you can see the Sun earlier in the morning and later in the evening than someone at sea level. This is because your higher vantage point lets you see further over the horizon. We adjust for this by calculating how much higher or lower you are compared to the horizon, ensuring the times are accurate for your specific location.
3. Atmospheric Refraction Adjustment:
The Earth’s atmosphere bends light, making the Sun appear slightly higher in the sky than it actually is, especially when it’s near the horizon. This bending is why we can see the Sun even when it’s technically below the horizon. To correct for this, we subtract a small angle (about 0.58 degrees) from our calculation, so the times reflect when the Sun actually appears to rise or set.
4. Horizontal Parallax Adjustment:
The Earth isn’t a perfect sphere, and we’re observing the Sun from the Earth's surface, not from its center. This causes a tiny shift in the Sun’s apparent position, known as parallax, though it’s a very small effect. We subtract this tiny angle (about 0.0024 degrees) fro our calculation to get the most accurate time possible.
Hour Angle Calculation
The adjusted solar altitude angle is then used to calculate the corresponding hour angle, which represents the time difference between solar noon and the desired event (Shurouq or Maghrib). The hour angle is computed using a function, which factors in the observer’s latitude and the Sun’s declination.
Final Shurouq and Maghrib Time Calculation
Finally, the Shurouq or Maghrib time is calculated by adjusting the solar noon by the hour angle:
• Shurouq (Sunrise) Time: Subtract the hour angle from solar noon. • Maghrib (Sunset) Time: Add the hour angle to solar noon.
The result is the local time of sunrise or sunset.
Code Implementation
from math import sqrt
from datetime import datetime, timedelta
from pt_engine.astro_helpers import hour_angle_for_solar_altitude
def compute_shurouq_maghrib_time(pt_type: str, date: datetime, utc_offset: float, latitude: float, longitude: float, declination: float, eot: float, observer_height: int = 0, horizon_height: int = 0) -> datetime:
# Calculate Solar Noon Time
standard_meridian = 15 * utc_offset
local_time_correction = (standard_meridian - longitude) / 15
solar_noon = timedelta(hours=12) + timedelta(hours=local_time_correction) - timedelta(minutes=eot)
# Determine the angle at which sunset and sunrise occur
solar_altitude_angle = 0
# Adjust by Solar radius (half of the sun's angular diameter) to consider upper disk of the sun not the center
solar_radius = 0.26667
solar_altitude_angle = solar_altitude_angle - solar_radius
# Adjust the solar altitude angle based on the observer's height above sea level
height_difference = observer_height - horizon_height
height_adjustment = 2.076 * sqrt(height_difference) / 60 if height_difference > 0 else 0
solar_altitude_angle = solar_altitude_angle - height_adjustment
# Adjust the solar altitude angle based on the observer's atmospheric refraction
atmospheric_refraction = 0.583
solar_altitude_angle = solar_altitude_angle - atmospheric_refraction
# Adjust by Horizontal Parallax correction (Sun's parallax is about 8.794 arcseconds)
horizontal_parallax = 8.794 / 3600
solar_altitude_angle = solar_altitude_angle - horizontal_parallax
# Calculate Hour Angle H for the determined solar_altitude_angle
hour_angle = hour_angle_for_solar_altitude(latitude, declination, solar_altitude_angle)
prayer_time = None
if pt_type == 'shurouq':
# Calculate Shurouq Time
prayer_time = datetime.combine(date, datetime.min.time()) + solar_noon - timedelta(hours=hour_angle / 15)
elif pt_type == 'maghrib':
# Calculate Maghrib Time
prayer_time = datetime.combine(date, datetime.min.time()) + solar_noon + timedelta(hours=hour_angle / 15)
else:
raise ValueError("Invalid type. Must be 'shurouq' or 'maghrib'")
return prayer_time
↗ View in github