-
Notifications
You must be signed in to change notification settings - Fork 1
/
metars.js
452 lines (331 loc) · 11.5 KB
/
metars.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// ** (c) Steve Kollmansberger, 2014
var baseUrl = '//metars.mobi/';
var searched = false;
var KM_TO_NM = 0.539957;
function getMETARsAndTAFs(murl, turl) {
return $.ajax(turl).pipe(function (turlData) {
return $.ajax(murl).pipe(function (murlData) {
return {
'METARs': murlData,
'TAFs': turlData
};
});
});
}
function getMETARsAndTAFsForIdents(idents) {
var murl = baseUrl + 'metars.php?ds=metars&idents=' + idents.join();
var turl = baseUrl + 'metars.php?ds=tafs&idents=' + idents.join();
return getMETARsAndTAFs(murl, turl);
}
function getMETARsAndTAFsForLoc(lat, lon) {
var murl = baseUrl + 'metars.php?ds=metars&loc=' + lon + ',' + lat;
var turl = baseUrl + 'metars.php?ds=tafs&loc=' + lon + ',' + lat;
return getMETARsAndTAFs(murl, turl);
}
// applies a regex to a string. Each match, function f
// is given the matching portion, and expected to return
// an object containing before and after, strings which will be appended
// before and after, respectively.
function adorn(str, regex, f) {
return str.replace(regex, function repl(match) {
var fr = f(match);
return fr.before + match + fr.after;
});
}
function spancls(cls) {
if (cls)
return {
before: '<span class=' + cls + '>',
after: '</span>'
};
else
return {
before: '', after: ''
};
}
var coverages = ["FEW", "SCT", "BKN", "OVC", "VV"];
function enhance(str, worst, station) {
// taf: line-breaks for TEMPO and FM and BECMG
var tafnl = /(TEMPO)|(FM\d+)|(BECMG)|(PROB\d\d)/g;
str = adorn(str, tafnl, function (m) {
return { before: '<br /><strong>', after: '</strong>' };
});
var vis = /[ ](M|P)?(\d[ ])?(\d\/)?\d+SM/g;
str = adorn(str, vis, function (m) {
var cls = "";
m = m.slice(1); // remove space from front
var worst_vis = m;
m = m.slice(0, m.length - 2); // remove SM from end
var worst_viscode = 0;
if (m.indexOf('P') > -1) {
worst_viscode = 100;
cls = ""; // vfr
} else if (m.indexOf('M') > -1) {
worst_viscode = 0.1; // can't use 0, 0 is a falsey value!
cls = "IFR";
} else if (m.indexOf('/') > -1) {
if (m.length == 3)
worst_viscode = parseInt(m[0], 10) / parseInt(m[2], 10);
else
worst_viscode = parseInt(m[0], 10) + parseInt(m[2], 10) / parseInt(m[4], 10);
cls = "IFR";
} else {
var visv = parseInt(m, 10);
worst_viscode = visv;
if (visv < 3)
cls = "IFR";
else if (visv <= 5)
cls = "MVFR";
else if (visv <= 6)
cls = "QVFR";
}
if (worst && (!worst.viscode || worst.viscode > worst_viscode)) {
worst.vis = worst_vis;
worst.viscode = worst_viscode;
worst.visstation = station;
}
return spancls(cls);
});
var cld = /(FEW|SCT|BKN|OVC|VV)\d{3}/g;
str = adorn(str, cld, function (m) {
var cls = "";
var lvl = parseInt(m.slice(m.length - 3, m.length), 10);
var coverage = m.slice(0, m.length - 3);
if (lvl < 20 && coverage === "SCT") {
cls = "QVFR";
}
if (coverage === "OVC" || coverage === "BKN") { // formal ceiling
if (lvl < 10)
cls = "IFR";
else if (lvl <= 30)
cls = "MVFR";
}
if (coverage === "VV")
cls = "IFR";
if (worst && (!worst[lvl] || coverages.indexOf(worst[lvl]) < coverages.indexOf(coverage))) {
worst[lvl] = coverage;
worst[lvl + "station"] = station;
}
return spancls(cls);
});
var wnd = /\d{3}\d{2}(G\d{2})?KT/g;
str = adorn(str, wnd, function (m) {
var cls = "";
var ws = 0;
if (m.indexOf('G') > -1)
ws = parseInt(m.slice(6, 8), 10);
else
ws = parseInt(m.slice(3, 5), 10);
if (ws >= 20)
cls = "QVFR";
if (worst && (!worst.windcode || worst.windcode < ws)) {
worst.windcode = ws;
worst.wind = m;
worst.windstation = station;
}
return spancls(cls);
});
// pad temp with spaces to avoid catching chunks of the taf hour blocks
var tempdew = /[ ](M)?\d{2}[/](M)?\d{2}[ ]/g;
str = adorn(str, tempdew, function (m) {
var cls = "";
m = m.replace(/M/g, '-');
var slidx = m.indexOf('/');
var temp = parseInt(m.slice(0, slidx), 10);
var dew = parseInt(m.slice(slidx + 1), 10);
if (temp - dew <= 3)
cls = "QVFR";
return spancls(cls);
});
return str;
}
function worstCls(str) {
if (str.indexOf("IFR") > -1)
return "IFR";
if (str.indexOf("MVFR") > -1)
return "MVFR";
if (str.indexOf("QVFR") > -1)
return "QVFR";
return "VFR";
}
function distDir(latlon, origin) {
var item = {
distance: origin.distanceTo(latlon) * KM_TO_NM,
direction: origin.bearingTo(latlon),
};
var dir = "";
var split = 45 / 2;
if (item.direction < 45 - split)
dir = "N";
else if (item.direction < 90 - split)
dir = "NE";
else if (item.direction < 135 - split)
dir = "E";
else if (item.direction < 180 - split)
dir = "SE";
else if (item.direction < 225 - split)
dir = "S";
else if (item.direction < 270 - split)
dir = "SW";
else if (item.direction < 315 - split)
dir = "W";
else if (item.direction < 360 - split)
dir = "NW";
else
dir = "N";
return Math.round(item.distance) + " nm " + dir;
}
function buildMETAR(worst) {
var str = "WORST ";
if (worst.windstation)
str += "<span class=worstStation>" + worst.windstation + "</span> ";
str += worst.wind;
str += " ";
if (worst.visstation)
str += "<span class=worstStation>" + worst.visstation + "</span> ";
str += worst.vis;
str += " ";
var cidx = 0;
for (var lvl = 1; lvl < 250; lvl++) {
if (worst[lvl] && cidx <= coverages.indexOf(worst[lvl])) {
str += worst[lvl] + ("000" + lvl).slice(-3) + " ";
if (worst[lvl + "station"])
str += "<span class='worstStation lvl'>" + worst[lvl + "station"] + "</span> ";
cidx = coverages.indexOf(worst[lvl]);
if (worst[lvl] === "OVC" || worst[lvl] === "VV")
break; // hard ceiling
}
}
return str.trim();
}
function formatDate(dt) {
var hr = dt.getUTCHours();
var min = dt.getUTCMinutes();
return ("00" + hr).slice(-2) + ":" + ("00" + min).slice(-2) + " Z";
}
function calculateAge(dt1, dt2) {
var diff = (dt1.getTime() - dt2.getTime());
if (diff <= 0)
return "just now"; // future check
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
var mins = Math.floor(diff / (1000 * 60));
if (hours > 0 && mins >= 30) {
hours++;
mins = 0;
}
if (hours > 23)
return "a day or more ago";
if (hours > 1)
return "about " + hours + " hours ago";
if (hours === 1)
return "about an hour ago";
if (mins > 1)
return "about " + mins + " minutes ago";
if (mins === 1)
return "about a minute ago";
return "just now";
}
function locate() {
viewModel.message("Awaiting GPS Location...");
viewModel.stations.removeAll();
viewModel.applyDistanceFilter(true);
if (navigator.geolocation) {
searched = false;
navigator.geolocation.getCurrentPosition(gotPosition);
} else {
viewModel.message("Geolocation is not supported by this browser.");
}
}
function gotPosition(loc) {
viewModel.myOrigin(new LatLon(loc.coords.latitude, loc.coords.longitude));
viewModel.message(null);
if (!searched) {
loadData(getMETARsAndTAFsForLoc(loc.coords.latitude, loc.coords.longitude));
}
}
function initData(data) {
// ARE YOU INSANE. Service returns array if 2+ objects, but just the object if only 1
var metararr;
if (data.METARs.data.METAR instanceof Array)
metararr = data.METARs.data.METAR;
else if (!data.METARs.data.METAR) {
viewModel.message("No results");
metararr = [];
} else
metararr = new Array(data.METARs.data.METAR);
var tafarr;
if (data.TAFs.data.TAF instanceof Array)
tafarr = data.TAFs.data.TAF;
else if (!data.TAFs.data.TAF)
tafarr = [];
else
tafarr = new Array(data.TAFs.data.TAF);
viewModel.metararr(metararr);
viewModel.tafarr(tafarr);
displayData();
}
function displayData() {
var worst = {};
viewModel.stations.removeAll();
viewModel.metararr().forEach(function (metar) {
var result = {
id: metar.station_id,
metar: metar.raw_text,
latlon: new LatLon(metar.latitude, metar.longitude),
sectioncls: ''
};
if (viewModel.myOrigin()) {
result.distance = viewModel.myOrigin().distanceTo(result.latlon) * KM_TO_NM;
} else {
result.distance = 0; // not for display, just for sorting
}
if (!viewModel.applyDistanceFilter() ||
result.distance <= viewModel.range()) {
result.metarHtml = enhance(metar.raw_text, worst, result.id);
result.metarTime = new Date(metar.observation_time);
result.flightcls = worstCls(result.metarHtml);
var tafs = viewModel.tafarr().filter(function (taf) {
return taf.station_id === metar.station_id;
});
if (tafs.length > 0) {
result.taf = tafs[0].raw_text;
result.tafTime = new Date(tafs[0].issue_time);
result.tafHtml = enhance(result.taf);
}
viewModel.stations.push(result);
}
});
if (viewModel.stations().length > 1) {
viewModel.stations().sort(function compare(a, b) {
if (a.distance < b.distance) return -1;
else if (a.distance > b.distance) return 1;
else return 0;
});
var worstresult = {
id: 'WORST',
metar: buildMETAR(worst),
metarHtml: enhance(buildMETAR(worst)),
sectioncls: 'worst'
};
worstresult.flightcls = worstCls(worstresult.metarHtml)
viewModel.stations.unshift(worstresult);
}
$('[data-role=collapsible]').collapsible().trigger('create');
}
function loadData(f) {
searched = true;
viewModel.stations.removeAll();
viewModel.loadTime(new Date());
$.mobile.loading("show", {
text: "Loading..."
});
f.done(initData)
.fail(function (err) {
viewModel.loadTime(null);
viewModel.message("Error loading data - " + JSON.stringify(err));
})
.always(function () {
$.mobile.loading("hide");
});
}