python sun tracking

# sunpos.pyimport mathdef sunpos(when, location, refraction):# Extract the passed data    year, month, day, hour, minute, second, timezone = when    latitude, longitude = location# Math typing shortcuts    rad, deg = math.radians, math.degrees    sin, cos, tan = math.sin, math.cos, math.tan    asin, atan2 = math.asin, math.atan2# Convert latitude and longitude to radians    rlat = rad(latitude)    rlon = rad(longitude)# Decimal hour of the day at Greenwich    greenwichtime = hour - timezone + minute / 60 + second / 3600# Days from J2000, accurate from 1901 to 2099    daynum = (        367 * year        - 7 * (year + (month + 9) // 12) // 4        + 275 * month // 9        + day        - 730531.5        + greenwichtime / 24    )# Mean longitude of the sun    mean_long = daynum * 0.01720279239 + 4.894967873# Mean anomaly of the Sun    mean_anom = daynum * 0.01720197034 + 6.240040768# Ecliptic longitude of the sun    eclip_long = (        mean_long        + 0.03342305518 * sin(mean_anom)        + 0.0003490658504 * sin(2 * mean_anom)    )# Obliquity of the ecliptic    obliquity = 0.4090877234 - 0.000000006981317008 * daynum# Right ascension of the sun    rasc = atan2(cos(obliquity) * sin(eclip_long), cos(eclip_long))# Declination of the sun    decl = asin(sin(obliquity) * sin(eclip_long))# Local sidereal time    sidereal = 4.894961213 + 6.300388099 * daynum + rlon# Hour angle of the sun    hour_ang = sidereal - rasc# Local elevation of the sun    elevation = asin(sin(decl) * sin(rlat) + cos(decl) * cos(rlat) * cos(hour_ang))# Local azimuth of the sun    azimuth = atan2(        -cos(decl) * cos(rlat) * sin(hour_ang),        sin(decl) - sin(rlat) * sin(elevation),    )# Convert azimuth and elevation to degrees    azimuth = into_range(deg(azimuth), 0, 360)    elevation = into_range(deg(elevation), -180, 180)# Refraction correction (optional)    if refraction:        targ = rad((elevation + (10.3 / (elevation + 5.11))))        elevation += (1.02 / tan(targ)) / 60# Return azimuth and elevation in degrees    return (round(azimuth, 2), round(elevation, 2))def into_range(x, range_min, range_max):    shiftedx = x - range_min    delta = range_max - range_min    return (((shiftedx % delta) + delta) % delta) + range_minif __name__ == "__main__":# Close Encounters latitude, longitude    location = (40.602778, -104.741667)# Fourth of July, 2022 at 11:20 am MDT (-6 hours)    when = (2022, 7, 4, 11, 20, 0, -6)# Get the Sun's apparent location in the sky    azimuth, elevation = sunpos(when, location, True)# Output the results    print("\nWhen: ", when)    print("Where: ", location)    print("Azimuth: ", azimuth)    print("Elevation: ", elevation)# When:  (2022, 7, 4, 11, 20, 0, -6)# Where:  (40.602778, -104.741667)# Azimuth:  121.38# Elevation:  61.91

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source