Hello, I am attempting to perform the sunrise and sunset calculations found here (http://www.esrl.noaa.gov/gmd/grad/solcalc/solareqns.PDF) [Thanks Peter!] using just the agent. I have most of it going, however I am not sure if I am understanding the final arccos step correctly. I am getting NaN for the acos step…
`local longitude = -83;
local latitude = 30
local timezone = -5;
local datedata = date();
local dayofyear = datedata.yday;
local hour = datedata.hour;
local min = datedata.min;
local sec = datedata.sec;
server.log("Day of year = " + dayofyear);
server.log("Hour of day = " + hour);
server.log("Minute of day = " + min);
server.log("Second of day = " + sec);
local fryear = ((2PI)/365)(dayofyear-1+((hour-12)/24));
server.log("Fractional year = " + fryear);
local eqtime = 229.18*(0.000075+(0.001868math.cos(fryear))-(0.032077math.sin(fryear))-(0.014615math.cos(2fryear))-(0.040849math.sin(2fryear)));
server.log("Equation time = " + eqtime);
local declination = 0.006918-(0.399912math.cos(fryear))+(0.070257math.sin(fryear))-(0.006758math.cos(2fryear))+(0.000907math.sin(2fryear))-(0.002697math.cos(3fryear))+(0.00148math.sin(3fryear));
server.log("Declination = " + declination);
local timeoffset = eqtime-(4longitude)+(60timezone);
server.log("Time ofset = " + timeoffset);
local tst = hour*60+min+(sec/60)+timeoffset;
server.log("tst = " + tst);
local ha = (tst/4)-(180);
server.log("ha = " + ha);
local zenithang = math.sin(latitude)*math.sin(declination)+math.cos(latitude)*math.cos(declination)*math.cos(ha)
server.log("Zenith angle = " + zenithang);
local sunrise = 720+(4*(longitude-ha)-eqtime);
server.log("Sunrise = " + sunrise);
local ha3 = math.cos((90.8333)/(math.cos(latitude)*math.cos(declination))-math.tan(latitude)*math.tan(declination));
local newha = math.acos(math.cos(90.8333)/(math.cos(latitude)*math.cos(declination))-math.tan(latitude)*math.tan(declination));
server.log("Newha = " + newha);
local sunrise2 = 720+(4*(longitude-ha3)-eqtime);
server.log(sunrise2/60);
local sunset2 = 720-(4*(longitude-ha3)-eqtime);
server.log(sunset2/60);
`
I was wondering if someone could lend me a hand to understand how to correctly get the sunrise and sunset times from the solar equations provided by NOAA. I think I might be “skipping” steps, or doing something out of order (even though I am following the equations from top to bottom. I feel like i’m missing something… Any help is appreciated. Thanks!