This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

How to calculate sun position?
#3
(14.12.2015, 08:07)admin Wrote: This function should work, though it was not tested much:
Code:
-- solar altitude, azimuth (degrees)
function sunposition(latitude, longitude, time)
 time = time or os.time()
 if type(time) == 'table' then
   time = os.time(time)
 end

 local date = os.date('*t', time)
 local timezone = (os.time(date) - os.time(os.date('!*t', time))) / 3600

 if date.isdst then
   timezone = timezone + 1
 end

 local utcdate = os.date('*t', time - timezone * 3600)
 local latrad = math.rad(latitude)
 local fd = (utcdate.hour + utcdate.min / 60 + utcdate.sec / 3600) / 24
 local g = (2 * math.pi / 365.25) * (utcdate.yday + fd)
 local d = math.rad(0.396372 - 22.91327 * math.cos(g) + 4.02543 * math.sin(g) - 0.387205 * math.cos(2 * g)
         + 0.051967 * math.sin(2 * g) - 0.154527 * math.cos(3 * g) + 0.084798 * math.sin(3 * g))
 local t = math.rad(0.004297 + 0.107029 * math.cos(g) - 1.837877 * math.sin(g)
         - 0.837378 * math.cos(2 * g) - 2.340475 * math.sin(2 * g))
 local sha = 2 * math.pi * (fd - 0.5) + t + math.rad(longitude)
 local sza = math.acos(math.sin(latrad) * math.sin(d) + math.cos(latrad) * math.cos(d) * math.cos(sha))
 local saa = math.acos((math.sin(d) - math.sin(latrad) * math.cos(sza)) / (math.cos(latrad) * math.sin(sza)))

 return 90 - math.deg(sza), math.deg(saa)
end

It works. Thank you

I create a library with this function in User library and in resident script I add this script:

Code:
latitude = 50.990
longtitude = -23.179
time = nil

altitude, azimuth = sunposition(latitude, longitude, time)

log(altitude, azimuth)

Result is very close to the results from online calculators.
Reply


Messages In This Thread
How to calculate sun position? - by buuuudzik - 13.12.2015, 16:23
RE: How to calculate sun position? - by admin - 14.12.2015, 08:07
RE: How to calculate sun position? - by buuuudzik - 14.12.2015, 09:50
RE: How to calculate sun position? - by admin - 14.12.2015, 10:20
RE: How to calculate sun position? - by admin - 15.12.2015, 13:24
RE: How to calculate sun position? - by admin - 21.12.2015, 07:08
RE: How to calculate sun position? - by admin - 11.05.2017, 07:49
RE: How to calculate sun position? - by admin - 18.04.2018, 07:26
RE: How to calculate sun position? - by admin - 02.07.2020, 12:43

Forum Jump: