Computing Solar Elevation and Azimuth Angles Using Python
Computing Solar Elevation and Azimuth for Location (109.2, 35.1) on 2021/12/22 at 09:00:00
Five approaches are used to determine the solar elevation and azimuth angles. The fifth method's results are adopted for practical use. Readers may skip directly to Method 5.
Method 1: Using the ephem Python Library
Result: Sun altitude: 13:33:46.2, Sun azimuth: 267:57:01.4
import ephem
def get_sun_position(lat, lon, date):
observer = ephem.Observer()
observer.lat, observer.lon = lat, lon
observer.date = ephem.Date(date)
sun = ephem.Sun()
sun.compute(observer)
return sun.alt, sun.az
latitude = 35.1
longitude = 109.2
date = '2021/12/22 9:00:00'
sun_alt, sun_az = get_sun_position(latitude, longitude, date)
print(f"Sun altitude: {sun_alt}, Sun azimuth: {sun_az}")
Method 2: Using the pysolar Python Library
Result: Sun altitude: 10.52866037565445, Sun azimuth: 128.60185059293036
import datetime
import pysolar.solar
from pytz import timezone
from timezonefinder import TimezoneFinder
latitude = 35.1
longitude = 109.2
elevation = 1000
tf = TimezoneFinder(in_memory=True)
timezone_name = tf.timezone_at(lat=latitude, lng=longitude)
timezone_obj = timezone(timezone_name)
date_time = datetime.datetime(2021, 12, 22, 9, 0, 0, 0, timezone_obj)
sun_alt = pysolar.solar.get_altitude(latitude, longitude, date_time, elevation)
sun_az = pysolar.solar.get_azimuth(latitude, longitude, date_time, elevation)
print(f"Sun altitude: {sun_alt}, Sun azimuth: {sun_az}")
Method 3: Online Web Calculator 1
Result: Sun altitude: 20.15858896258514, Sun azimuth: 140.893388236611
Method 4: Online Web Calculator 2
Result: Sun altitude: 11.39, Sun azimuth: 50.40
Method 5: Manual Calculation Based on Formulas
Result: Sun altitude: 11.13924683296049, Sun azimuth: 50.66025597336174
Using the principle of solar elevation contours, the solar altitude angle for 30N, 120E on summer solstice at 10:00 was calculated as approximately 62.5°. This method's outcome aligns close with Method 4’s result. Below is the corresponding Python implementation:
import math
from datetime import datetime
ASIN = math.asin
SIN = math.sin
COS = math.cos
RADIANS = math.radians
DEGREES = math.degrees
# Time difference
def time_difference(lon):
t = (lon - 120) / 15
return t
# True solar time
def true_solar_time(T, t):
ST = T + t
return ST
# Hour angle
def hour_angle(ST):
h = 15 * (ST - 12)
return h
# Solar declination (N is day of year)
def solar_declination(N):
DEC = -23.443 * COS(RADIANS(round(360/365, 8) * (N + 10)))
return DEC
# Day of year
def day_of_year(time):
return time.timetuple().tm_yday
# Compute hour angle
def compute_hour_angle(lon, T):
t = time_difference(lon)
ST = true_solar_time(T, t)
H = hour_angle(ST)
return H
# Compute solar declination
def compute_solar_declination(time):
N = day_of_year(time)
DEC = solar_declination(N)
return DEC
# Main function to compute sun position
def compute_sun_position(lat, lon, time):
T = time.hour + round(1/60, 8) * time.minute + round(1/3600, 8) * time.second
print("Beijing Time: ", T)
H = compute_hour_angle(lon, T)
print("Hour Angle: ", H)
DEC = compute_solar_declination(time)
print("DEC: ", DEC)
alt_rad = ASIN(SIN(RADIANS(lat)) * SIN(RADIANS(DEC)) + COS(RADIANS(DEC)) * COS(RADIANS(lat)) * COS(RADIANS(H)))
sun_alt = DEGREES(alt_rad)
az_rad = ASIN(round((-COS(RADIANS(DEC)) * SIN(RADIANS(H)) / COS(RADIANS(sun_alt))), 8))
sun_az = DEGREES(az_rad)
print("altitude_angle_deg:", sun_alt)
print("azimuth_angle_deg:", sun_az)
return sun_alt, sun_az
# Execution
input_time_str = '2021-12-22 9:00:00'
input_time = datetime.strptime(input_time_str, '%Y-%m-%d %H:%M:%S')
sun_altitude, sun_azimuth = compute_sun_position(35.1, 109.2, input_time)
Output:
Beijing Time: 9.0 Hour Angle: -55.80000000000001 DEC: -23.4395266662262 altitude_angle_deg: 11.13924683296049 azimuth_angle_deg: 50.66025597336174
The azimuth angle is measured from the north direction; thus, it should be corrected to 129.34 for consistency with Method 2.
Mathematical Formulas
Solar Hour Angle h
Time differance: (lon: longitude)
True solar time: (t: Beijing time)
Solar hour angle:
Solar Declination δ
(N represents the day of the year)
Solar Elevation Engle
Solar Azimuth Angle
(x = sin(y) has multiple solutions, but only one is correct; e.g., noon azimuth is 180°, before noon less than 180°, after noon greater than 180°)