-
Notifications
You must be signed in to change notification settings - Fork 1
/
NOAAcalculator.js
285 lines (227 loc) · 7.56 KB
/
NOAAcalculator.js
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
/*
Copyright (c) 2018 Y Paritcher
*/
/************
Implementation of sunrise and sunset methods to calculate astronomical times based on the NOAA algorithm.
This calculator is based on equations from http://www.srrb.noaa.gov/highlights/sunrise/sunrise.html and
https://github.com/KosherJava/zmanim/blob/master/src/net/sourceforge/zmanim/util/NOAACalculator.java
************/
(function () {
'use strict'
zmanJS.refraction = 34 / 60.0;
zmanJS.solarradius = 16 / 60.0;
zmanJS.earthradius = 6356.9;
zmanJS.radToDeg = function (angleRad)
{
return (180.0 * angleRad / Math.PI);
}
zmanJS.degToRad = function (angleDeg)
{
return (Math.PI * angleDeg / 180.0);
}
zmanJS.calcTimeJulianCent = function (jd)
{
let jcent = (jd - 2451545.0)/36525.0;
return jcent;
}
zmanJS.calcJDFromJulianCent = function (jcent)
{
let JD = jcent * 36525.0 + 2451545.0;
return JD;
}
zmanJS.calcGeomMeanLongSun = function (jcent)
{
let gmls = 280.46646 + jcent * (36000.76983 + 0.0003032 * jcent);
while(gmls > 360.0)
{
gmls -= 360.0;
}
while(gmls < 0.0)
{
gmls += 360.0;
}
return gmls;
}
zmanJS.calcGeomMeanAnomalySun = function (jcent)
{
let gmas = 357.52911 + jcent * (35999.05029 - 0.0001537 * jcent);
return gmas;
}
zmanJS.calcEccentricityEarthOrbit = function (jcent)
{
let eeo = 0.016708634 - jcent * (0.000042037 + 0.0000001267 * jcent);
return eeo;
}
zmanJS.calcSunEqOfCenter = function (jcent)
{
let m = zmanJS.calcGeomMeanAnomalySun(jcent);
let mrad = zmanJS.degToRad(m);
let sinm = Math.sin(mrad);
let sin2m = Math.sin(mrad+mrad);
let sin3m = Math.sin(mrad+mrad+mrad);
let seoc = sinm * (1.914602 - jcent * (0.004817 + 0.000014 * jcent)) + sin2m * (0.019993 - 0.000101 * jcent) + sin3m * 0.000289;
return seoc;
}
zmanJS.calcSunTrueLong = function (jcent)
{
let gmls = zmanJS.calcGeomMeanLongSun(jcent);
let seoc = zmanJS.calcSunEqOfCenter(jcent);
let stl = gmls + seoc;
return stl;
}
zmanJS.calcSunApparentLong = function (jcent)
{
let stl = zmanJS.calcSunTrueLong(jcent);
let omega = 125.04 - 1934.136 * jcent;
let sal = stl - 0.00569 - 0.00478 * Math.sin(zmanJS.degToRad(omega));
return sal;
}
zmanJS.calcMeanObliquityOfEcliptic = function (jcent)
{
let seconds = 21.448 - jcent*(46.8150 + jcent*(0.00059 - jcent*(0.001813)));
let mooe = 23.0 + (26.0 + (seconds/60.0))/60.0;
return mooe;
}
zmanJS.calcObliquityCorrection = function (jcent)
{
let mooe = zmanJS.calcMeanObliquityOfEcliptic(jcent);
let omega = 125.04 - 1934.136 * jcent;
let oc = mooe + 0.00256 * Math.cos(zmanJS.degToRad(omega));
return oc;
}
zmanJS.calcSunDeclination = function (jcent)
{
let oc = zmanJS.calcObliquityCorrection(jcent);
let sal = zmanJS.calcSunApparentLong(jcent);
let sint = Math.sin(zmanJS.degToRad(oc)) * Math.sin(zmanJS.degToRad(sal));
let sd = zmanJS.radToDeg(Math.asin(sint));
return sd;
}
zmanJS.calcEquationOfTime = function (jcent)
{
let oc = zmanJS.calcObliquityCorrection(jcent);
let gmls = zmanJS.calcGeomMeanLongSun(jcent);
let eeo = zmanJS.calcEccentricityEarthOrbit(jcent);
let gmas = zmanJS.calcGeomMeanAnomalySun(jcent);
let y = Math.tan(zmanJS.degToRad(oc)/2.0);
y *= y;
let sin2gmls = Math.sin(2.0 * zmanJS.degToRad(gmls));
let singmas = Math.sin(zmanJS.degToRad(gmas));
let cos2gmls = Math.cos(2.0 * zmanJS.degToRad(gmls));
let sin4gmls = Math.sin(4.0 * zmanJS.degToRad(gmls));
let sin2gmas = Math.sin(2.0 * zmanJS.degToRad(gmas));
let Etime = y * sin2gmls - 2.0 * eeo * singmas + 4.0 * eeo * y * singmas * cos2gmls
- 0.5 * y * y * sin4gmls - 1.25 * eeo * eeo * sin2gmas;
return zmanJS.radToDeg(Etime)*4.0;
}
zmanJS.calcHourAngleSunrise = function (lat, solarDec, zenith)
{
let latRad = zmanJS.degToRad(lat);
let sdRad = zmanJS.degToRad(solarDec);
let HA = (Math.acos(Math.cos(zmanJS.degToRad(zenith))/(Math.cos(latRad)*Math.cos(sdRad))-Math.tan(latRad) * Math.tan(sdRad)));
return HA;
}
zmanJS.calcHourAngleSunset = function (lat, solarDec, zenith)
{
let latRad = zmanJS.degToRad(lat);
let sdRad = zmanJS.degToRad(solarDec);
let HA = (Math.acos(Math.cos(zmanJS.degToRad(zenith))/(Math.cos(latRad)*Math.cos(sdRad))-Math.tan(latRad) * Math.tan(sdRad)));
return -HA;
}
zmanJS.calcSolNoonUTC = function (jcent, longitude)
{
let tnoon = zmanJS.calcTimeJulianCent(zmanJS.calcJDFromJulianCent(jcent) + longitude/360.0);
let eqTime = zmanJS.calcEquationOfTime(tnoon);
let solNoonUTC = 720 + (longitude * 4) - eqTime;
let newt = zmanJS.calcTimeJulianCent(zmanJS.calcJDFromJulianCent(jcent) -0.5 + solNoonUTC/1440.0);
eqTime = zmanJS.calcEquationOfTime(newt);
solNoonUTC = 720 + (longitude * 4) - eqTime;
return solNoonUTC;
}
zmanJS.calcSunriseUTC = function (JD, latitude, longitude, zenith)
{
let jcent = zmanJS.calcTimeJulianCent(JD);
let noonmin = zmanJS.calcSolNoonUTC(jcent, longitude);
let tnoon = zmanJS.calcTimeJulianCent (JD+noonmin/1440.0);
let eqTime = zmanJS.calcEquationOfTime(tnoon);
let solarDec = zmanJS.calcSunDeclination(tnoon);
let hourAngle = zmanJS.calcHourAngleSunrise(latitude, solarDec, zenith);
let delta = longitude - zmanJS.radToDeg(hourAngle);
let timeDiff = 4 * delta;
let timeUTC = 720 + timeDiff - eqTime;
let newt = zmanJS.calcTimeJulianCent(zmanJS.calcJDFromJulianCent(jcent) + timeUTC/1440.0);
eqTime = zmanJS.calcEquationOfTime(newt);
solarDec = zmanJS.calcSunDeclination(newt);
hourAngle = zmanJS.calcHourAngleSunrise(latitude, solarDec, zenith);
delta = longitude - zmanJS.radToDeg(hourAngle);
timeDiff = 4 * delta;
timeUTC = 720 + timeDiff - eqTime;
return timeUTC;
}
zmanJS.calcSunsetUTC = function (JD, latitude, longitude, zenith)
{
let jcent = zmanJS.calcTimeJulianCent(JD);
let noonmin = zmanJS.calcSolNoonUTC(jcent, longitude);
let tnoon = zmanJS.calcTimeJulianCent (JD+noonmin/1440.0);
let eqTime = zmanJS.calcEquationOfTime(tnoon);
let solarDec = zmanJS.calcSunDeclination(tnoon);
let hourAngle = zmanJS.calcHourAngleSunset(latitude, solarDec, zenith);
let delta = longitude - zmanJS.radToDeg(hourAngle);
let timeDiff = 4 * delta;
let timeUTC = 720 + timeDiff - eqTime;
let newt = zmanJS.calcTimeJulianCent(zmanJS.calcJDFromJulianCent(jcent) + timeUTC/1440.0);
eqTime = zmanJS.calcEquationOfTime(newt);
solarDec = zmanJS.calcSunDeclination(newt);
hourAngle = zmanJS.calcHourAngleSunset(latitude, solarDec, zenith);
delta = longitude - zmanJS.radToDeg(hourAngle);
timeDiff = 4 * delta;
timeUTC = 720 + timeDiff - eqTime;
return timeUTC;
}
zmanJS.getElevationAdjustment = function (elevation)
{
let elevationAdjustment = zmanJS.radToDeg(Math.acos(zmanJS.earthradius / (zmanJS.earthradius + (elevation / 1000))));
return elevationAdjustment;
}
zmanJS.adjustZenith = function (zenith, elevation)
{
let adjustedZenith = zenith;
if (zenith == 90.0)
{
adjustedZenith = zenith + (zmanJS.solarradius + zmanJS.refraction + zmanJS.getElevationAdjustment(elevation));
}
return adjustedZenith;
}
zmanJS.getUTCSunrise = function (JD, here, zenith, adjustForElevation)
{
let elevation = adjustForElevation ? here.elevation : 0;
let adjustedZenith = zmanJS.adjustZenith(zenith, elevation);
let sunrise = zmanJS.calcSunriseUTC(JD, here.latitude, -here.longitude, adjustedZenith);
sunrise = sunrise / 60;
while (sunrise < 0.0)
{
sunrise += 24.0;
}
while (sunrise >= 24.0)
{
sunrise -= 24.0;
}
return sunrise;
}
zmanJS.getUTCSunset = function (JD, here, zenith, adjustForElevation)
{
let elevation = adjustForElevation ? here.elevation : 0;
let adjustedZenith = zmanJS.adjustZenith(zenith, elevation);
let sunset = zmanJS.calcSunsetUTC(JD, here.latitude, -here.longitude, adjustedZenith);
sunset = sunset / 60;
while (sunset < 0.0)
{
sunset += 24.0;
}
while (sunset >= 24.0)
{
sunset -= 24.0;
}
return sunset;
}
}());