Skip to content

Instantly share code, notes, and snippets.

@wyfinger
Last active November 3, 2017 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyfinger/8e21623a9c4422f6a2f546d55183ef24 to your computer and use it in GitHub Desktop.
Save wyfinger/8e21623a9c4422f6a2f546d55183ef24 to your computer and use it in GitHub Desktop.
Calculate time of Sunrise and Sunset by geo-coordinates and current day of year
day=308; # day of year
longitude=131.9551; # lonDegree + lonMinutes/60 + lonSeconds/3600
latitude=43.1096; # latDegree + latMinutes/60 + latSeconds/3600
zenith=90.8; # angle of sunrise or sunset, with correction by the interference of light in the atmosphere
# sunrise calculation
lngHour = longitude / 15;
trise = day + ((6-lngHour)/24);
Mrise = (0.9856*trise)-3.289;
Lrise = Mrise+(1.916*sin(pi*Mrise/180))+(0.020*sin(2*pi*Mrise/180))+282.634;
Lrise = Lrise >= 360 ? Lrise - 360 : (Lrise < 0 ? Lrise + 360 : Lrise);
RA = 180*atan(0.91764*tan(Lrise*pi/180))/pi;
LQuadrant = 90*floor(Lrise/90);
RQuadrant = 90*floor(RA/90);
RA = RA+(LQuadrant-RQuadrant);
RArise = RA/15;
sinDecrise = 0.39782*sin(Lrise*pi/180);
cosDecrise = cos(asin(sinDecrise));
cosHrise = (cos(zenith*pi/180))-sinDecrise*sin(latitude*pi/180)/(cosDecrise*cos(latitude*pi/180));
Hrise = (360-180*acos(cosHrise)/pi)/15;
Trise = Hrise+RArise-(0.06571*trise)-6.622;
UTriseUTC = Trise-lngHour+10; # Vladivostok, delete "+10" for UTC time
UTriseUTC = UTriseUTC < 0 ? UTriseUTC + 24 : (UTriseUTC >= 24 ? UTriseUTC - 24 : UTriseUTC )
riseHourUTC = floor(UTriseUTC ); riseMin = floor((UTriseUTC-riseHourUTC )*60);
SunRriseUTC = concat(string(riseHourUTC), ":", string(riseMin))
# sunset calculation
lngHour = longitude / 15;
tset = day + ((18-lngHour)/24);
Mset = (0.9856*tset)-3.289;
Lset = Mrise+(1.916*sin(pi*Mset/180))+(0.020*sin(2*pi*Mset/180))+282.634;
Lset = Lset >= 360 ? Lset - 360 : (Lset < 0 ? Lset + 360 : Lset);
RA = 180*atan(0.91764*tan(Lset*pi/180))/pi;
LQuadrant = 90*floor(Lset/90);
RQuadrant = 90*floor(RA/90);
RA = RA+(LQuadrant-RQuadrant);
RAset = RA/15;
sinDecrise = 0.39782*sin(Lset*pi/180);
cosDecrise = cos(asin(sinDecrise));
cosHset = (cos(zenith*pi/180))-sinDecrise*sin(latitude*pi/180)/(cosDecrise*cos(latitude*pi/180));
Hset = (180*acos(cosHset)/pi)/15;
Tset = Hset+RAset-(0.06571*tset)-6.622;
UTsetUTC = Tset-lngHour+10; # Vladivostok, delete "+10" for UTC time
UTsetUTC = UTsetUTC < 0 ? UTsetUTC + 24 : (UTsetUTC >= 24 ? UTsetUTC - 24 : UTsetUTC );
setHourUTC = floor(UTsetUTC); setMin = floor((UTsetUTC-setHourUTC )*60);
SunSetUTC = concat(string(setHourUTC), ":", string(setMin))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment