Guest User

Untitled

a guest
Dec 5th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. # sunpos.py
  2. # https://levelup.gitconnected.com/python-sun-position-for-solar-energy-and-research-7a4ead801777
  3. # John Clark Craig
  4.  
  5. import math
  6.  
  7. def sunpos(when, location, refraction):
  8.     # Extract the passed data
  9.     year, month, day, hour, minute, second, timezone = when
  10.     latitude, longitude = location
  11.     # Math typing shortcuts
  12.     rad, deg = math.radians, math.degrees
  13.     sin, cos, tan = math.sin, math.cos, math.tan
  14.     asin, atan2 = math.asin, math.atan2
  15.     # Convert latitude and longitude to radians
  16.     rlat = rad(latitude)
  17.     rlon = rad(longitude)
  18.     # Decimal hour of the day at Greenwich
  19.     greenwichtime = hour - timezone + minute / 60 + second / 3600
  20.     # Days from J2000, accurate from 1901 to 2099
  21.     daynum = (
  22.         367 * year
  23.         - 7 * (year + (month + 9) // 12) // 4
  24.         + 275 * month // 9
  25.         + day
  26.         - 730531.5
  27.         + greenwichtime / 24
  28.     )
  29.     # Mean longitude of the sun
  30.     mean_long = daynum * 0.01720279239 + 4.894967873# Mean anomaly of the Sun
  31.     mean_anom = daynum * 0.01720197034 + 6.240040768# Ecliptic longitude of the sun
  32.     eclip_long = (
  33.         mean_long
  34.         + 0.03342305518 * sin(mean_anom)
  35.         + 0.0003490658504 * sin(2 * mean_anom)
  36.     )
  37.     # Obliquity of the ecliptic
  38.     obliquity = 0.4090877234 - 0.000000006981317008 * daynum
  39.     # Right ascension of the sun
  40.     rasc = atan2(cos(obliquity) * sin(eclip_long), cos(eclip_long))
  41.     # Declination of the sun
  42.     decl = asin(sin(obliquity) * sin(eclip_long))
  43.     # Local sidereal time
  44.     sidereal = 4.894961213 + 6.300388099 * daynum + rlon
  45.     # Hour angle of the sun
  46.     hour_ang = sidereal - rasc
  47.     # Local elevation of the sun
  48.     elevation = asin(sin(decl) * sin(rlat) + cos(decl) * cos(rlat) * cos(hour_ang))
  49.     # Local azimuth of the sun
  50.     azimuth = atan2(
  51.         -cos(decl) * cos(rlat) * sin(hour_ang),
  52.         sin(decl) - sin(rlat) * sin(elevation),
  53.     )
  54.     # Convert azimuth and elevation to degrees
  55.     azimuth = into_range(deg(azimuth), 0, 360)
  56.     elevation = into_range(deg(elevation), -180, 180)
  57.     # Refraction correction (optional)
  58.     if refraction:
  59.         targ = rad((elevation + (10.3 / (elevation + 5.11))))
  60.         elevation += (1.02 / tan(targ)) / 60
  61.     # Return azimuth and elevation in degrees
  62.     return (round(azimuth, 2), round(elevation, 2))
  63.  
  64. def into_range(x, range_min, range_max):
  65.     shiftedx = x - range_min
  66.     delta = range_max - range_min
  67.     return (((shiftedx % delta) + delta) % delta) + range_min
  68.  
  69. if __name__ == "__main__":
  70.     # Close Encounters latitude, longitude
  71.     location = (40.602778, -104.741667)
  72.     # Fourth of July, 2022 at 11:20 am MDT (-6 hours)
  73.     when = (2022, 7, 4, 11, 20, 0, -6)
  74.     # Get the Sun's apparent location in the sky
  75.     azimuth, elevation = sunpos(when, location, True)
  76.     # Output the results
  77.     print("\nWhen: ", when)
  78.     print("Where: ", location)
  79.     print("Azimuth: ", azimuth)
  80.     print("Elevation: ", elevation)
  81.  
  82. # When:  (2022, 7, 4, 11, 20, 0, -6)
  83. # Where:  (40.602778, -104.741667)
  84. # Azimuth:  121.38
  85. # Elevation:  61.91
  86.  
Advertisement
Add Comment
Please, Sign In to add comment