functionsuncalc(lat, lng, time)
-- sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulaslocalPI = math.pilocalsin = math.sinlocalcos = math.coslocaltan = math.tanlocalasin = math.asinlocalatan = math.atan2localacos = math.acoslocaldeg = math.deglocalrad = PI / 180locale = rad * 23.4397-- obliquity of the Earthlocaldaysec = 60 * 60 * 24localJ1970 = 2440588localJ2000 = 2451545localfunctiontoDays(time)
returntime / daysec - 0.5 + J1970 - J2000endlocalfunctionrightAscension(l, b)
returnatan(sin(l) * cos(e) - tan(b) * sin(e), cos(l))
endlocalfunctiondeclination(l, b)
returnasin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l))
endlocalfunctionazimuth(H, phi, dec)
returnatan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi))
endlocalfunctionaltitude(H, phi, dec)
returnasin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H))
endlocalfunctionsiderealTime(d, lw)
returnrad * (280.16 + 360.9856235 * d) - lwendlocalfunctionastroRefraction(h)
ifh < 0then-- the following formula works for positive altitudes only.h = 0-- if h = -0.08901179 a div/0 would occur.end-- formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.-- 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:return0.0002967 / math.tan(h + 0.00312536 / (h + 0.08901179))
end-- general sun calculationslocalfunctionsolarMeanAnomaly(d)
returnrad * (357.5291 + 0.98560028 * d)
endlocalfunctioneclipticLongitude(M)
localC = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)) -- equation of centerlocalP = rad * 102.9372-- perihelion of the EarthreturnM + C + P + PIendlocalfunctionsunCoords(d)
localM = solarMeanAnomaly(d)
localL = eclipticLongitude(M)
returndeclination(L, 0), rightAscension(L, 0)
endlocallw = rad * -lnglocalphi = rad * latlocald = toDays(timeoros.time())
localdec, ra = sunCoords(d)
localH = siderealTime(d, lw) - ralocalalt, az = altitude(H, phi, dec), azimuth(H, phi, dec)
returndeg(alt), 180 + deg(az)
end
I have used this for a few different applications on SL, and it works. But today I implementet this on a LM5, and i get all wrong values from it.
When I log the returned values I get;
* arg: 1
* number: 383
* arg: 2
* number: 1112
Any ideas why? I even tested to enter it at the SL I use for testing at my office, and then I get the correct values.
The firmware of the LM5 is 20180822.
There are 10 kinds of people in the world; those who can read binary and those who don't
(18.03.2020, 13:27)Daniel. Wrote: do you have time set correctly in LM ?
Yes, first thing I checked.
But anyway, the values should still be within 360 degrees, as that would be the highest possible Azimuth, and highest possible Elevation should be 90 degrees.
There are 10 kinds of people in the world; those who can read binary and those who don't
functionsuncalc(lat, lng, time)
-- sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulaslocalPI = math.pilocalsin = math.sinlocalcos = math.coslocaltan = math.tanlocalasin = math.asinlocalatan = math.atan2localacos = math.acoslocaldeg = math.deglocalrad = PI / 180locale = rad * 23.4397-- obliquity of the Earthlocaldaysec = 60 * 60 * 24localJ1970 = 2440588localJ2000 = 2451545localfunctiontoDays(time)
returntime / daysec - 0.5 + J1970 - J2000endlocalfunctionrightAscension(l, b)
returnatan(sin(l) * cos(e) - tan(b) * sin(e), cos(l))
endlocalfunctiondeclination(l, b)
returnasin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l))
endlocalfunctionazimuth(H, phi, dec)
returnatan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi))
endlocalfunctionaltitude(H, phi, dec)
returnasin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H))
endlocalfunctionsiderealTime(d, lw)
returnrad * (280.16 + 360.9856235 * d) - lwendlocalfunctionastroRefraction(h)
ifh < 0then-- the following formula works for positive altitudes only.h = 0-- if h = -0.08901179 a div/0 would occur.end-- formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.-- 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:return0.0002967 / math.tan(h + 0.00312536 / (h + 0.08901179))
end-- general sun calculationslocalfunctionsolarMeanAnomaly(d)
returnrad * (357.5291 + 0.98560028 * d)
endlocalfunctioneclipticLongitude(M)
localC = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)) -- equation of centerlocalP = rad * 102.9372-- perihelion of the EarthreturnM + C + P + PIendlocalfunctionsunCoords(d)
localM = solarMeanAnomaly(d)
localL = eclipticLongitude(M)
returndeclination(L, 0), rightAscension(L, 0)
endlocallw = rad * -lnglocalphi = rad * latlocald = toDays(timeoros.time())
localdec, ra = sunCoords(d)
localH = siderealTime(d, lw) - ralocalalt, az = altitude(H, phi, dec), azimuth(H, phi, dec)
returndeg(alt), 180 + deg(az)
end
Hi Admin,
On what calculation is suncalc (above) based? The Azimuth is 28 degrees wrong, i just tested function suncalc vs sunposition and these are the results:
Hi Erwin, looks like the sunpositon function is wrong. I've checked several online calculators and they give a result similar to what suncalc produces. Suncalc formulas are taken from here: https://github.com/mourner/suncalc