-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuncalc.html
419 lines (314 loc) · 16.6 KB
/
suncalc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<br>
<p id="demo1"></p>
<br>
<br>
<br>
<p id="demo2"></p>
<!-- <script src="suncalc.js"></script> -->
<script>
/*
(c) 2011-2015, Vladimir Agafonkin
SunCalc is a JavaScript library for calculating sun/moon position and light phases.
https://github.com/mourner/suncalc
*/
(function () { 'use strict';
// shortcuts for easier to read formulas
var PI = Math.PI,
sin = Math.sin,
cos = Math.cos,
tan = Math.tan,
asin = Math.asin,
atan = Math.atan2,
acos = Math.acos,
rad = PI / 180;
// sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulas
// date/time constants and conversions
var dayMs = 1000 * 60 * 60 * 24,
J1970 = 2440588,
J2000 = 2451545;
function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
function toDays(date) { return toJulian(date) - J2000; }
// general calculations for position
var e = rad * 23.4397; // obliquity of the Earth
function rightAscension(l, b) { return atan(sin(l) * cos(e) - tan(b) * sin(e), cos(l)); }
function declination(l, b) { return asin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l)); }
function azimuth(H, phi, dec) { return atan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi)); }
function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H)); }
function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }
function astroRefraction(h) {
if (h < 0) // the following formula works for positive altitudes only.
h = 0; // if h = -0.08901179 a div/0 would occur.
// 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:
return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179));
}
// general sun calculations
function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); }
function eclipticLongitude(M) {
var C = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)), // equation of center
P = rad * 102.9372; // perihelion of the Earth
return M + C + P + PI;
}
function sunCoords(d) {
var M = solarMeanAnomaly(d),
L = eclipticLongitude(M);
return {
dec: declination(L, 0),
ra: rightAscension(L, 0)
};
}
var SunCalc = {};
// calculates sun position for a given date and latitude/longitude
SunCalc.getPosition = function (date, lat, lng) {
var lw = rad * -lng,
phi = rad * lat,
d = toDays(date),
c = sunCoords(d),
H = siderealTime(d, lw) - c.ra;
return {
azimuth: azimuth(H, phi, c.dec),
altitude: altitude(H, phi, c.dec)
};
};
// sun times configuration (angle, morning name, evening name)
var times = SunCalc.times = [
[-0.833, 'sunrise', 'sunset' ],
[ -0.3, 'sunriseEnd', 'sunsetStart' ],
[ -6, 'dawn', 'dusk' ],
[ -12, 'nauticalDawn', 'nauticalDusk'],
[ -18, 'nightEnd', 'night' ],
[ 6, 'goldenHourEnd', 'goldenHour' ]
];
// adds a custom time to the times config
SunCalc.addTime = function (angle, riseName, setName) {
times.push([angle, riseName, setName]);
};
// calculations for sun times
var J0 = 0.0009;
function julianCycle(d, lw) { return Math.round(d - J0 - lw / (2 * PI)); }
function approxTransit(Ht, lw, n) { return J0 + (Ht + lw) / (2 * PI) + n; }
function solarTransitJ(ds, M, L) { return J2000 + ds + 0.0053 * sin(M) - 0.0069 * sin(2 * L); }
function hourAngle(h, phi, d) { return acos((sin(h) - sin(phi) * sin(d)) / (cos(phi) * cos(d))); }
function observerAngle(height) { return -2.076 * Math.sqrt(height) / 60; }
// returns set time for the given sun altitude
function getSetJ(h, lw, phi, dec, n, M, L) {
var w = hourAngle(h, phi, dec),
a = approxTransit(w, lw, n);
return solarTransitJ(a, M, L);
}
// calculates sun times for a given date, latitude/longitude, and, optionally,
// the observer height (in meters) relative to the horizon
SunCalc.getTimes = function (date, lat, lng, height) {
height = height || 0;
var lw = rad * -lng,
phi = rad * lat,
dh = observerAngle(height),
d = toDays(date),
n = julianCycle(d, lw),
ds = approxTransit(0, lw, n),
M = solarMeanAnomaly(ds),
L = eclipticLongitude(M),
dec = declination(L, 0),
Jnoon = solarTransitJ(ds, M, L),
i, len, time, h0, Jset, Jrise;
var result = {
solarNoon: fromJulian(Jnoon),
nadir: fromJulian(Jnoon - 0.5)
};
for (i = 0, len = times.length; i < len; i += 1) {
time = times[i];
h0 = (time[0] + dh) * rad;
Jset = getSetJ(h0, lw, phi, dec, n, M, L);
Jrise = Jnoon - (Jset - Jnoon);
result[time[1]] = fromJulian(Jrise);
result[time[2]] = fromJulian(Jset);
}
return result;
};
// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
function moonCoords(d) { // geocentric ecliptic coordinates of the moon
var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
M = rad * (134.963 + 13.064993 * d), // mean anomaly
F = rad * (93.272 + 13.229350 * d), // mean distance
l = L + rad * 6.289 * sin(M), // longitude
b = rad * 5.128 * sin(F), // latitude
dt = 385001 - 20905 * cos(M); // distance to the moon in km
return {
ra: rightAscension(l, b),
dec: declination(l, b),
dist: dt
};
}
SunCalc.getMoonPosition = function (date, lat, lng) {
var lw = rad * -lng,
phi = rad * lat,
d = toDays(date),
c = moonCoords(d),
H = siderealTime(d, lw) - c.ra,
h = altitude(H, phi, c.dec),
// formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H));
h = h + astroRefraction(h); // altitude correction for refraction
return {
azimuth: azimuth(H, phi, c.dec),
altitude: h,
distance: c.dist,
parallacticAngle: pa
};
};
// calculations for illumination parameters of the moon,
// based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
// Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
SunCalc.getMoonIllumination = function (date) {
var d = toDays(date || new Date()),
s = sunCoords(d),
m = moonCoords(d),
sdist = 149598000, // distance from Earth to Sun in km
phi = acos(sin(s.dec) * sin(m.dec) + cos(s.dec) * cos(m.dec) * cos(s.ra - m.ra)),
inc = atan(sdist * sin(phi), m.dist - sdist * cos(phi)),
angle = atan(cos(s.dec) * sin(s.ra - m.ra), sin(s.dec) * cos(m.dec) -
cos(s.dec) * sin(m.dec) * cos(s.ra - m.ra));
return {
fraction: (1 + cos(inc)) / 2,
phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
angle: angle
};
};
function hoursLater(date, h) {
return new Date(date.valueOf() + h * dayMs / 24);
}
// calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article
SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {
var t = new Date(date);
if (inUTC) t.setUTCHours(0, 0, 0, 0);
else t.setHours(0, 0, 0, 0);
var hc = 0.133 * rad,
h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc,
h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx;
// go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set)
for (var i = 1; i <= 24; i += 2) {
h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc;
h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc;
a = (h0 + h2) / 2 - h1;
b = (h2 - h0) / 2;
xe = -b / (2 * a);
ye = (a * xe + b) * xe + h1;
d = b * b - 4 * a * h1;
roots = 0;
if (d >= 0) {
dx = Math.sqrt(d) / (Math.abs(a) * 2);
x1 = xe - dx;
x2 = xe + dx;
if (Math.abs(x1) <= 1) roots++;
if (Math.abs(x2) <= 1) roots++;
if (x1 < -1) x1 = x2;
}
if (roots === 1) {
if (h0 < 0) rise = i + x1;
else set = i + x1;
} else if (roots === 2) {
rise = i + (ye < 0 ? x2 : x1);
set = i + (ye < 0 ? x1 : x2);
}
if (rise && set) break;
h0 = h2;
}
var result = {};
if (rise) result.rise = hoursLater(t, rise);
if (set) result.set = hoursLater(t, set);
if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;
return result;
};
// export as Node module / AMD module / browser variable
if (typeof exports === 'object' && typeof module !== 'undefined') module.exports = SunCalc;
else if (typeof define === 'function' && define.amd) define(SunCalc);
else window.SunCalc = SunCalc;
}());
</script>
<script>
//
// this python implementation didn't work for some reason: https://pypi.org/project/suncalc/
// ... so I'm using the "original" js library: https://github.com/mourner/suncalc
//
// It works. :) Double-checked it against this (older) NOAA calculator:
// https://gml.noaa.gov/grad/solcalc/azel.html
// Also, this comes in handy to convert from decimal-degrees to degree-minute-second:
// https://www.calculatorsoup.com/calculators/conversions/convert-decimal-degrees-to-degrees-minutes-seconds.php
//
// Since my local time is UTC-05:00, and my browser uses my local time,
// if I wanted to calculate the sun position in Greenwich, I would
// subtract 5 hours from the Greenwich time.
// ... But right now, I don't have the brain-space to consider that.
//
let LAT = 39.24631; // the latitude of my house (+ = N; - = S)
let LON = -84.62817;// the longitude of my house(+ = E; - = W)
let date = [ // every week-ish between "fall-back" 2022 and "spring-forward" 2023 non-inclusive
[2022, 10, 6],
[2022, 10, 13],
[2022, 10, 20],
[2022, 10, 27],
[2022, 11, 4],
[2022, 11, 11],
[2022, 11, 25],
[2023, 0, 1],
[2023, 0, 8],
[2023, 0, 15],
[2023, 0, 22],
[2023, 0, 29],
[2023, 1, 5],
[2023, 1, 12],
[2023, 1, 19],
[2023, 1, 26],
[2023, 2, 11]
];
let results = [];
for (thisDate of date)
{
for (thisHour of [14, 15, 16, 17, 18, 19, 20]) // yes, I know this is computationally inefficient
{
let d = new Date(thisDate[0], thisDate[1], thisDate[2], thisHour);
let resultRAW = SunCalc.getPosition(d, LAT, LON);
results.push( [(resultRAW["azimuth"]*(180/Math.PI))+180, resultRAW["altitude"]*(180/Math.PI)] );
}
}
//let testDateTime = new Date(2022, 10, 15, 9, 30);
//let g = SunCalc.getPosition(testDateTime, LAT, LON);
//let azimuth = (g["azimuth"] * (180/Math.PI)) + 180;
//let elevation = g["altitude"] * (180/Math.PI);
//console.log("azimuth = " + azimuth);
//console.log("elevation = " + elevation);
//document.getElementById("demo").innerHTML = testDateTime;
let azimuth = results.map((function(X){return X[0];}));
let elevation = results.map((function(X){return X[1];}));
document.getElementById("demo1").innerHTML = azimuth.join("\n");
document.getElementById("demo2").innerHTML = elevation.join("\n");
/*
azimuth <- unlist(strsplit("207.2978536103321 221.68881426662958 233.9432027557569 244.50780340185295 253.99048855572886 263.0290972598177 272.3404157530799 206.305994507455 220.40709115741038 232.49971576921195 242.96306981589942 252.35263584349994 261.27025552104146 270.40452549881087 205.28154266643108 219.15130710962157 231.1166633345194 241.49886679772445 250.80777782145702 259.6120690937002 268.57067661288664 204.2413536301239 217.9412556621482 229.8188959445667 240.14596797663188 249.39354250893246 258.1010417350757 266.89843968871793 203.20808265393185 216.80224747495328 228.63591158771737 238.93875830063126 248.1501334201535 256.78526330161606 265.4481578229355 202.20921294269726 215.76386445591837 227.60027420493856 237.91318652063657 247.11763650015718 255.71075986754596 264.27567392370725 200.44078130561974 214.1189957293515 226.10508378782407 236.54479138919544 245.82859567551134 254.43899584838465 262.9381900991624 199.73769969642836 213.5780486691329 225.70848707787468 236.26138102670689 245.628088524213 254.29559667494487 262.8273129308754 199.19881243946364 213.26539607811077 225.58159490217574 236.274943344429 245.74739099416703 254.49860693824343 263.1008412077167 198.85391545334838 213.2070672603619 225.7444216695314 236.59872168506342 246.19308000851035 255.04803463273385 263.75163055178757 198.7292667325636 213.42426216672135 226.21048484466968 237.2380408129502 246.96273455996604 255.9339416780765 264.7614325143956 198.8468765145852 213.93267483030047 226.98633351039803 238.19027784673662 248.0454989213293 257.1377012353716 266.10301082098186 199.2240948908073 214.74207144295303 228.0713534938327 239.44513764537612 249.42291816657374 258.63341100525656 267.74219250276167 199.8734102895885 215.85602206415913 229.45775517577187 240.9851260428855 251.0699088072148 260.38929883885135 269.63968888254095 200.8023431859281 217.2716844752189 231.13067103989354 242.7861448886273 252.95577267739077 262.3690293935423 271.7526325312905 202.01331260528843 218.9795629279929 233.0683257611243 244.81816704372662 255.04520242434577 264.53287518639496 274.0358450999596 204.99616701147102 222.86515248970747 237.2675558902062 249.08143529587926 259.3308084759768 268.8952753203903 278.56911599622833", split=" "))
elevation <- unlist(strsplit("30.22778199612346 23.62462234308256 15.001299880460405 5.013967899051903 -5.849022235973583 -17.228590916051424 -28.829408880717157 28.443797369353515 22.03418848527735 13.600168815609434 3.7721220153881947 -6.971508465842569 -18.276410602163008 -29.854537166358895 26.948166266276992 20.735208408085718 12.489203086099366 2.819346419642719 -7.8036340157315935 -19.02890014082308 -30.574454266919577 25.7693413514915 19.75288918421702 11.689784731859632 2.172766235887422 -8.332611415676615 -19.477340104426048 -30.9837772905101 24.92981342981433 19.105319532426098 11.215184670702238 1.840568195511449 -8.555333721806116 -19.6234691655321 -31.08858115430489 24.445098330003518 18.802622844154737 11.069920591173394 1.8216223102459292 -8.478419632760586 -19.47912293310586 -30.90555833879746 24.5629962080322 19.22983184387497 11.740959714830625 2.673965317536706 -7.496453194931129 -18.409257694852823 -29.784387911459657 25.156576700078098 19.937613180627558 12.523119513159426 3.5006749691439567 -6.644764561283617 -17.54450266415421 -28.913462160711475 26.08726838103662 20.94712375360189 13.568262599253977 4.554286774322492 -5.596483533737274 -16.50624669798269 -27.883675720254818 27.331582436885064 22.229326087071346 14.84334145023068 5.799488859063836 -4.387705675324702 -15.330281710393848 -26.729790001629354 28.860032069972505 23.75014077494099 16.31164724643061 7.198988405592129 -3.054723931686432 -14.050833756717436 -25.48329814192367 30.63836785533822 25.471896176641103 17.93446061467492 8.71526130598693 -1.6323238800231132 -12.699044912352676 -24.171225913134933 32.62888453418618 27.354790078375096 19.672633917724646 10.31211782094256 -0.15239701299764621 -11.301905713419378 -22.8155048588686 34.79170645486373 29.35828593517386 21.48803443342204 11.956021920519373 1.3570888486815151 -9.881635235520655 -21.432853710445624 37.085986398636535 31.442395557369533 23.344804240635483 13.617128684352023 2.8727517610144853 -8.455478294086953 -20.03508551571014 39.47098248017765 33.568824612459366 25.210410194319316 15.270021041381101 4.376098363393628 -7.035874237798327 -18.629752851430766 44.006900017898836 37.511319199981855 28.60516022349838 18.251380436034378 7.09212142621537 -4.441918640290573 -16.012359947341533", split=" "))
dateIndex <- integer(0)
for (i in 1:17)
{
dateIndex <- c(dateIndex, rep(i, 7))
}
length(azimuth)
length(elevation)
length(date)
scatterplot3d(
azimuth,
dateIndex,
elevation,
#highlight.3d = TRUE,
col.axis = "blue",
col.grid = "lightblue",
main = "",
pch = 20)
*/
</script>
</body>
</html>