-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidetable.js
161 lines (146 loc) · 6.66 KB
/
tidetable.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
var SunCalc = require('suncalc');
var moment = require('moment');
var lowestoft_gps = [52.481220, 1.762786];
/* Get list of astronomical events */
var eventsList = []
function moon_phase_ascii(a) {
return " <i class='wi "+[
"wi-moon-new",
"wi-moon-waxing-crescent-1", "wi-moon-waxing-crescent-2",
"wi-moon-waxing-crescent-3", "wi-moon-waxing-crescent-4",
"wi-moon-waxing-crescent-5", "wi-moon-waxing-crescent-6",
"wi-moon-first-quarter",
"wi-moon-waxing-gibbous-1", "wi-moon-waxing-gibbous-2",
"wi-moon-waxing-gibbous-3", "wi-moon-waxing-gibbous-4",
"wi-moon-waxing-gibbous-5", "wi-moon-waxing-gibbous-6",
"wi-moon-full",
"wi-moon-waning-gibbous-1", "wi-moon-waning-gibbous-2",
"wi-moon-waning-gibbous-3", "wi-moon-waning-gibbous-4",
"wi-moon-waning-gibbous-5", "wi-moon-waning-gibbous-6",
"wi-moon-third-quarter",
"wi-moon-waning-crescent-1", "wi-moon-waning-crescent-2",
"wi-moon-waning-crescent-3", "wi-moon-waning-crescent-4",
"wi-moon-waning-crescent-5", "wi-moon-waning-crescent-6"
][Math.floor(a.phase * 28)]+"' moon></i>";
}
function bearing_ascii(n) {
n = n * 180 / Math.PI;
n = (n + 180) % 360;
bearing_string = ("000"+Math.round(n).toString()).slice(-3)+"°";
return bearing_string;
}
function bearing_symbol(n) {
n = n * 180 / Math.PI;
n = (Math.round(n) + 180) % 360;
return "<i class='wi wi-wind towards-"+n+"-deg'></i>";
}
function euroscope_ascii(n) {
n = n * 180 / Math.PI;
n = (n + 180) % 360;
euroscope_string = [
"NORTH", "N½E", "NʙʏE", "NʙʏE½E",
"NNE", "NNE½E", "NEʙʏN", "NEʙʏN½E",
"NE", "NE½E", "NEʙʏE", "NEʙʏE½E",
"ENE", "ENE½E", "EʙʏN", "EʙʏN½E",
"EAST", "E½S", "EʙʏS", "EʙʏS½S",
"ESE", "ESE½S", "SEʙʏE", "SEʙʏE½S",
"SE", "SE½S", "SEʙʏS", "SEʙʏS½S",
"SSE", "SSE½S", "SʙʏE", "SʙʏE½S",
"SOUTH", "S½W", "SʙʏW", "SʙʏW½W",
"SSW", "SSW½W", "SWʙʏS", "SWʙʏS½W",
"SW", "SW½W", "SWʙʏW", "SWʙʏW½W",
"WSW", "WSW½W", "WʙʏS", "WʙʏS½W",
"WEST", "W½N", "WʙʏN", "WʙʏN½N",
"WNW", "WNW½N", "NWʙʏW", "NWʙʏW½N",
"NW", "NW½N", "NWʙʏN", "NWʙʏN½N",
"NNW", "NNW½N", "NʙʏW", "NʙʏW½N"
][
Math.round((n/5.625) % 64)
];
return euroscope_string;
}
var startDate = moment();
for (var day = 0; day < 28; day++) {
var sunTimes = SunCalc.getTimes(startDate, lowestoft_gps[0], lowestoft_gps[1]);
eventsList.push({
date: sunTimes.sunrise,
start: moment(sunTimes.sunrise).format("HH:mm"),
type: "<i class='wi wi-sunrise'></i>",
bearing: SunCalc.getPosition(sunTimes.sunrise, lowestoft_gps[0], lowestoft_gps[1]).azimuth
});
eventsList.push({
date: sunTimes.sunsetStart,
start: moment(sunTimes.sunsetStart).format("HH:mm"),
type: "<i class='wi wi-sunset'></i>",
bearing: SunCalc.getPosition(sunTimes.sunsetStart, lowestoft_gps[0], lowestoft_gps[1]).azimuth
});
var moonTimes = SunCalc.getMoonTimes(startDate, lowestoft_gps[0], lowestoft_gps[1]);
if (moonTimes.rise) {
moonPosition = SunCalc.getMoonPosition(moonTimes.rise, lowestoft_gps[0], lowestoft_gps[1]);
moonIllumination = SunCalc.getMoonIllumination(moonTimes.rise);
eventsList.push({
date: moonTimes.rise,
start: moment(moonTimes.rise).format("HH:mm"),
type: "<i class='wi wi-moonrise'></i>"+moon_phase_ascii(SunCalc.getMoonIllumination(moonTimes.rise)),
bearing: SunCalc.getMoonPosition(moonTimes.rise, lowestoft_gps[0], lowestoft_gps[1]).azimuth
})
}
if (moonTimes.set) {
moonPosition = SunCalc.getMoonPosition(moonTimes.set, lowestoft_gps[0], lowestoft_gps[1]);
moonIllumination = SunCalc.getMoonIllumination(moonTimes.set);
eventsList.push({
date: moonTimes.set,
start: moment(moonTimes.set).format("HH:mm"),
type: "<i class='wi wi-moonset'></i>"+moon_phase_ascii(SunCalc.getMoonIllumination(moonTimes.set)),
bearing: SunCalc.getMoonPosition(moonTimes.set, lowestoft_gps[0], lowestoft_gps[1]).azimuth
})
}
startDate.add(1, 'd');
}
/* Sort list of events */
eventsList.sort(function (a, b){
return a.date > b.date ? 1 : -1;
});
function ambient_theme(t) {
var sun_angle = SunCalc.getPosition(t, lowestoft_gps[0], lowestoft_gps[1]).altitude;
sun_angle *= 180 / Math.PI;
if (sun_angle < -18) return 'night';
if (sun_angle < -12) return 'astro_twilight';
if (sun_angle < -6) return 'nautical_twilight';
if (sun_angle < -0.833) return 'civil_twilight';
if (sun_angle < -0.3) return 'sunrise';
if (sun_angle < 6) return 'golden_hour';
return 'day';
}
/* Inject table into page */
var almanac = document.getElementById('almanac');
for (var i = 0; i < eventsList.length; i++) {
var j = eventsList[i];
var row = document.createElement('tr');
row.setAttribute('tod', ambient_theme(j.date));
var cell_text = document.createTextNode(moment(j.date).format("Do MMM"));
var cell = document.createElement('td');
cell.appendChild(cell_text);
row.appendChild(cell);
var cell_text = document.createTextNode(j.start);
var cell = document.createElement('td');
cell.appendChild(cell_text);
row.appendChild(cell);
var cell_text = document.createRange().createContextualFragment(j.type);
var cell = document.createElement('td');
cell.appendChild(cell_text);
row.appendChild(cell);
var cell_text = document.createRange().createContextualFragment(bearing_symbol(j.bearing));
var cell = document.createElement('td');
cell.appendChild(cell_text);
row.appendChild(cell);
var cell_text = document.createRange().createContextualFragment(bearing_ascii(j.bearing));
var cell = document.createElement('td');
cell.appendChild(cell_text);
row.appendChild(cell);
var cell_text = document.createRange().createContextualFragment(euroscope_ascii(j.bearing));
var cell = document.createElement('td');
cell.appendChild(cell_text);
row.appendChild(cell);
almanac.appendChild(row);
}