-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
755 lines (716 loc) · 29.4 KB
/
script.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
var type = 'metar', id = 'VEBS', c = 0, qry, p = $('p'), divs = $('div'), iframe = $('iframe'), value = $('input'), b = $('b'), rawText,
diffIvl;
//Fetches "METAR" & "TAF" from AWC.
function awc() {
//p[1].innerHTML = p[3].innerHTML = '<p id="msgBox"></p>';
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
const proxyUrl2 = 'https://corsproxy.org/?';
const proxyUrl3 = 'https://proxy.cors.sh/';
const apiUrl = `https://aviationweather.gov/api/data/${type}?ids=${id}&format=json`;
const url = proxyUrl + apiUrl;
// const url = proxyUrl2 + encodeURIComponent(apiUrl);
fetch(url)
.then(response => {
if(!response.ok)
alert('AWC error: '+response.status+' '+response.type);
return response.json();
})
.then(data => {
if(type == 'metar')
if(data.length)
awcMain(data[0]);
else
alert(`AWC: METAR expired for ${id.toUpperCase()} or airport doesn't exists! Showing cached data.`);
else
if(data.length)
awcTafMain(data[0], 'AWC');
else
alert(`AWC: TAF expired for ${id.toUpperCase()} or airport doesn't exists! Showing cached data.`);
})
.catch(err => {
console.log(`Error: `+ err.message);
})
}
//Returns "METAR" from AWC.
function awcMain(data) {
p.eq(1).text('AWC');
var T = data.temp, D = data.dewp;
ab = Math.exp((17.625*D)/(243.04+D));
cd = Math.exp((17.625*T)/(240.04+T));
ht = ((ab/cd)*100+1).toFixed(0);
b.eq(0).text(data.metarType);
if(!c) {
b.eq(1).html(data.name+` <img src="https://flagcdn.com/24x18/in.png">`);
c++;
} else
b.eq(1).html(data.name+` <img src="https://flagcdn.com/24x18/${flag}.png">`);
b.eq(2).text(data.icaoId);
b.eq(3).text(`${getIST(data.reportTime)} <span id="updateDiff">${time(data.reportTime)}</span>`);
b.eq(4).text(T+'°C');
b.eq(5).html(`${D}°C<br>Humidity: ${ht}%`);
b.eq(6).text(data.wspd+' Knot(s) ('+(data.wspd*1.85).toFixed(1)+' KM/H - '+data.wdir+'°) ');
if(data.rawOb.charAt(24) == 'V' || data.rawOb.charAt(25) == 'V') {
var ind = data.rawOb.indexOf('V', 4),
d1 = +data.rawOb.substr(ind-3, 3),
d2 = +data.rawOb.substr(ind+1, 3);
if(d1 && d2)
b.eq(6).append(`<i class="fa-solid fa-location-arrow arrow"></i>`);
arrow(d1, d2);
} else {
if(data.wspd)
b.eq(6).append(`<i class="fa-solid fa-location-arrow" style='rotate:${data.wdir+135}deg'></i>`);
} if(data.wgst)
b.eq(6).append(`<br> Gust: ${(data.wgst*1.85).toFixed(1)} KM/H`);
da = ''+data.visib;
if(da.charAt(da.length-1) == '+')
da = +da.split('+')[0];
b.eq(7).text(data.visib+' SM ('+(da*1.609).toFixed(1)+' KM)');
if(data.altim)
b.eq(8).text(data.altim+' hPa');
b.eq(9).text(data.wxString);
b.eq(11).text(data.rawOb);
b.eq(10).text('');
if(!data.clouds[0].base)
b.eq(10).text('Clear skies');
else {
var ul = $('<ul></ul>');
for(i = 0; i < data.clouds.length; i++) {
var li = $('<li></li>');
li.text(data.clouds[i].cover+' at '+data.clouds[i].base+' ft AGL');
ul.append(li);
}
b.eq(10).append(ul);
}
}
//Backbone of the website.Manipulates display property of important elements.
async function get() {
$('#inp').blur();
type = value[1].checked ? value[1].id : value[2].checked ? value[2].id : '';
inpVal = value[0].value.trim();
if(!inpVal) {
alert('Please enter Id!');
return;
}
p[1].innerHTML = p[2].innerHTML = p[3].innerHTML = divs[9].innerHTML = divs[8].innerHTML = '<p id="msgBox"></p>';
var a1 = +inpVal.substring(inpVal.length-3,inpVal.length),
a2 = +inpVal.substring(inpVal.length-2, inpVal.length),
a3 = +inpVal.substring(inpVal.length-1, inpVal.length);
hrs = a1 > a2 ? a1 : a2 > a3 ? a2 : a3 > a1 ? a1 : a3;
if(a1 > 0)
inpVal = inpVal.substring(0, inpVal.length-3);
else if(a2 > 0)
inpVal = inpVal.substring(0, inpVal.length-2);
else if(a3 > 0)
inpVal = inpVal.substring(0, inpVal.length-1);
const url = `https://avwx.rest/api/search/station?text=${inpVal}&token=2r_H32HZ2AzCZDotC-1GetnWkIZhkBMpdq2W3rLRabI`;
const res = await fetch(url);
if(!res.ok) {
if(res.status == 400)
alert('Error: 400 - Location not found!');
else
alert('Search error: '+res.status+' '+res.type);
return;
}
const data = await res.json();
id = data[0].icao;
flag = data[0].country.toLowerCase();
airp = `${data[0].name}, ${data[0].city}, ${data[0].state}, ${data[0].country}`;
if(data[0].name != qry)
iframe.attr('src', `https://maps.google.com/maps?width=600&height=400&hl=en&q=${data[0].name}&t=&z=13&ie=UTF8&iwloc=B&output=embed`);
qry = data[0].name;
/*var hrs = +inpVal.substring(4, inpVal.length);
id = inpVal.substring(0, 4).toUpperCase();
var gh = +inpVal.substring(3, inpVal.length);
if(gh > 0 || !inpVal.charAt(3)) {
var hrs = gh;
avwx(inpVal.substring(0, 3).toUpperCase());
} else {
var hrs = +inpVal.substring(4, inpVal.length);
id = inpVal.substring(0, 4).toUpperCase();
}*/
d3d = document.querySelector('.d3').style;
d4d = document.querySelector('.d4').style;
d5d = document.querySelector('.d5').style
d6d = document.querySelector('.d6').style;
if(value[5].checked) {
d3d.display = d4d.display = d5d.display = 'none';
d6d.display = 'block';
search(data, flag);
} else {
if(value[3].checked) {
awc();
d3d.display = 'block';
} else
d3d.display = 'none';
if(value[4].checked) {
cwx(id, type, flag);
d4d.display = 'block';
} else
d4d.display = 'none';
if(type == 'taf') {
d3d.display = d4d.display = 'none';
d5d.display = 'block';
if(value[6].checked)
d5d.display = 'none';
} else
d5d.display = 'none';
if(value[6].checked) {
divs[9].style.display = 'block';
avwxMain(id, type, airp, flag);
} else
divs[9].style.display = 'none';
if (hrs > 0) {
metarH(id, type, hrs, flag);
d6d.display = 'block';
} else
d6d.display = 'none';
}
}
//awc();
cwx('vebs', 'metar', 'in');
//Triggers "get()" function on "keypress" event in "input[type=search]".
$('input').on("keypress", function(event) {
if(event.key === "Enter") {
event.preventDefault();
get();
}
});
//Fetches "METAR" & "TAF" from CWX.
function cwx(id, type, flag) {
p[2].innerHTML = p[3].innerHTML = '<p id="msgBox"></p>';
const url = `https://api.checkwx.com/${type}/${id}/decoded?x-api-key=c7e806f2a82843d88129362226`;
fetch(url)
.then(response => {
if(!response.ok)
alert('CWX error: '+response.status+' '+response.type);
return response.json();
})
.then(data => {
//console.log(data);
if(!data.error)
if(type == 'metar')
if(data.data.length)
cxwMain(data.data[0], flag);
else
alert(`CWX: METAR expired for ${id.toUpperCase()} or airport doesn't exists! Showing cached data.`);
else
if(data.data.length)
cwxTafMain(data.data[0], flag, 'CWX');
else
alert(`CWX: TAF expired for ${id.toUpperCase()} or airport doesn't exists! Showing cached data.`);
else
alert('CWX error: '+data.error);
})
.catch(err => {
console.log(`Error:`+ err.message);
})
}
//Returns "METAR" from CWX.
function cxwMain(data, flag) {
p.eq(2).text('CWX');
b[12].innerHTML = type.toUpperCase();
b[13].innerHTML = data.station.name+', '+data.station.location+` <img src="https://flagcdn.com/24x18/${flag}.png">`;
b[14].innerHTML = data.icao;
b[15].innerHTML = `${getIST(data.observed)} <span id='updateDiff'>${time(data.observed)}</span>`;
b[16].innerHTML = data.temperature.celsius+'°C';
b[17].innerHTML = data.dewpoint.celsius+'°C';
b[18].innerHTML = data.humidity.percent+'%';
if(data.wind) {
b[19].innerHTML = data.wind.speed_kts+` Knot(s) (${data.wind.speed_kph} KM/H - ${data.wind.degrees}°) <i class="fa-solid fa-location-arrow" style='rotate:${data.wind.degrees+135}deg'></i>`;
if(data.wind.gust_kph)
b[19].innerHTML += `<br>Gust: ${data.wind.gust_kph} KM/H`;
} else
b[19].innerHTML = '0 Knot';
if(data.visibility)
b[20].innerHTML = data.visibility.miles+' Mi ('+data.visibility.meters_float/1000+' KM)';
if (data.barometer)
b[21].innerHTML = data.barometer.hpa+' hPa';
b[22].innerHTML = '';
if(data.conditions)
for(let i = 0; i < data.conditions.length; i++)
if(i != data.conditions.length - 1)
b[22].innerHTML += data.conditions[i].text+' | ';
else
b[22].innerHTML += data.conditions[i].text;
b[24].innerHTML = data.raw_text+' <i class="fa-regular fa-copy fa-xs"></i>';
b[23].innerHTML = '';
if(!data.clouds[0].feet)
b[23].innerHTML = data.clouds[0].text;
else {
ul = document.createElement('ul');
for(i = 0; i < data.clouds.length; i++) {
li = document.createElement('li');
li.innerHTML = data.clouds[i].text+' at '+data.clouds[i].feet+' ft AGL';
ul.append(li);
}
b[23].append(ul);
}
let flc = data.flight_category;
b[25].innerHTML = flc;
$('#fl').css('background',
flc == 'VFR' ? 'Green': flc == 'MVFR' ? 'Blue' : flc == 'LIFR' ? 'Magenta' : 'Red');
rawText = data.raw_text;
}
//Fetches & returns "METAR" & "TAF" from AVWX.
async function avwxMain(id, type, airp, flag) {
var url = `https://avwx.rest/api/${type}/${id}?token=2r_H32HZ2AzCZDotC-1GetnWkIZhkBMpdq2W3rLRabI`;
const res = await fetch(url);
if(!res.ok)
alert('AVWX error: '+res.status+' - '+res.type);
getHeaders(res);
const data = await res.json();
if(data.error) {
alert(data.error)
} if(data.meta.validation_error)
alert(data.meta.validation_error);
var d7 = divs.eq(9), flc = data.flight_rules;
d7.html(`<p>AVWX</p>
Type: <b>${type.toUpperCase()}</b> <br>`);
if(data.remarks)
d7.append(`Remark: <b>${data.remarks}</b> <br>`);
d7.append(`Airport: <b>${airp}</b> <img src="https://flagcdn.com/24x18/${flag}.png"> <br>
ICAO Code: <b>${data.station}</b> <br>`);
if(type == 'metar') {
d7.append(`Reported: <b>${getIST(data.time.dt)} <span id="updateDiff">${time(data.time.dt)}</span></b> <br>
Temperature: <b>${data.temperature.value}°C</b> <br>
Dewpoint: <b>${data.dewpoint.value}°C</b> <br>
Humidity: <b>${(data.relative_humidity*100).toFixed(0)}%</b> <br>
Wind: <b>${data.wind_speed.value} Knot(s) (${(data.wind_speed.value*1.85).toFixed(0)} KM/H - ${data.wind_direction.repr}°)</b> `);
if(data.wind_variable_direction.length) {
d7.append(`<i class="fa-solid fa-location-arrow arrow"></i> <br>`);
arrow(data.wind_variable_direction[0].value, data.wind_variable_direction[1].value);
} else
if(data.wind_direction.value)
d7.append(`<i class="fa-solid fa-location-arrow" style='rotate:${data.wind_direction.value+135}deg'></i> <br>`);
else
d7.append(`<i class="fa-solid fa-wind"></i> <br>`);
if(data.wind_gust)
d7.append(`Gust: <b>${data.wind_gust.value} Knot(s)</b> <br>`);
if(data.visibility)
if(data.units.visibility == 'm')
d7.append(`Visibility: <b>${(data.visibility.value/1000).toFixed(1)} Km</b> <br>`);
else
d7.append(`Visibility: <b>${(data.visibility.value*1.609).toFixed(1)} Km</b> <br>`);
if(data.units.altimeter == 'hPa')
d7.append(`Pressure: <b>${data.altimeter.value} hPa</b> <br>`);
else
d7.append(`Pressure: <b>${data.altimeter.value} mmHg</b> <br>`);
if(data.wx_codes.length) {
d7.append(`Condition: `);
for(i = 0; i < data.wx_codes.length; i++)
if(i == data.wx_codes.length-1)
d7.append('<b>'+data.wx_codes[i].value+'</b> <br>');
else
d7.append('<b>'+data.wx_codes[i].value+'</b>, ');
} d7.append(`Clouds: `);
if(!data.clouds.length)
d7.append(' <b>Clear skies</b> <br>');
else {
var ul = $('<ul></ul>');
for(i = 0; i < data.clouds.length; i++) {
var li = $('<li></li>');
li.html('<b>'+data.clouds[i].type+' at '+data.clouds[i].altitude*100+' ft AGL</b>');
if(data.clouds[i].modifier)
li.append(` <b>(${data.clouds[i].modifier})</b>`);
ul.append(li);
}
d7.append(ul);
}
d7.append(`Raw: <b>${data.raw}</b> <i class="fa-regular fa-copy fa-xs"></i> <br>
Category: <b>${flc}</b>`);
var fl = $('<div></div>');
fl.attr('id', 'fl');
fl.css('background',
flc == 'VFR' ? 'Green': flc == 'MVFR' ? 'Blue' : flc == 'LIFR' ? 'Magenta' : 'Red');
d7.append(fl);
} else {
var frst = data.forecast;
d7.append(`Issued: <b>${getIST(data.time.dt)} ${time(data.time.dt)} <br>
Span: ${getIST(data.start_time.dt)} until ${getIST(data.end_time.dt)}</b> <br>`);
for(i = 0; i < frst.length; i++) {
var span = $('<span></span>'),
span2 = $('<span></span>');
if(frst[i].type) {
span.html(`<b>${frst[i].type} from ${getIST(frst[i].start_time.dt)} to ${getIST(frst[i].end_time.dt)}</b>`);
if (frst[i].probability)
span.append(`<b> (${frst[i].probability.repr}% likely)</b> <br>`);
} else
span.html(`<b>Forecast from ${getIST(frst[i].start_time.dt)} to ${getIST(frst[i].end_time.dt)}</b> <br>`);
if(frst[i].wind_speed) {
var li = $('<li></li>');
li.html(`Wind: <b>${frst[i].wind_speed.value} Knot(s) (${(frst[i].wind_speed.value*1.85).toFixed(1)} KM/H - ${frst[i].wind_direction.value}°)</b> <br>`);
span2.append(li);
} if(frst[i].wind_gust) {
var li = $('<li></li>');
li.html(`Gust: <b>${frst[i].wind_gust.value} Knot</b> <br>`);
span2.append(li);
} if(frst[i].wx_codes.length) {
var li = $('<li></li>').text(`Weather: `);
for(j = 0; j < frst[i].wx_codes.length; j++) {
if(frst[i].wx_codes[j].value.includes('<BR>'))
frst[i].wx_codes[j].value = frst[i].wx_codes[j].value.replace('<BR>', ' ');
if(j == frst[i].wx_codes.length-1)
li.append('<b>'+frst[i].wx_codes[j].value+'</b><br>');
else
li.append('<b>'+frst[i].wx_codes[j].value+'</b>, ');
} span2.append(li);
} if (frst[i].visibility) {
var li = $('<li></li>')
if(frst[i].visibility.value)
if(data.units.visibility == 'sm')
li.html(`Visibility: <b>${frst[i].visibility.value} mile(s) (${(frst[i].visibility.value*1.609).toFixed(1)} Km)</b> <br>`);
else
li.html(`Visibility: <b>${(frst[i].visibility.value/1000).toFixed(1)} Km</b> <br>`);
else
li.html(`Visibility: <b>6+ miles (10+ Km)</b> <br>`);
span2.append(li);
} if(frst[i].clouds.length) {
var li = $('<li></li>');
li.text('Clouds: ');
span2.append(li);
} if (frst[i].clouds.length) {
var clouds = frst[i].clouds;
var ul = $('<ul></ul>');
for(j = 0; j < clouds.length; j++) {
var li = $('<li></li>');
li.html(`<b>${clouds[j].type} at ${clouds[j].altitude*100} ft AGL</b>`);
if (clouds[j].modifier && clouds[j].modifier != '<BR>')
li.html(`<b>${clouds[j].type} at ${clouds[j].altitude*100} ft (${clouds[j].modifier}) AGL</b>`);
ul.append(li);
}
span2.append(ul);
}
d7.append(span);
d7.append(span2);
} if(!data.raw.includes('<br>')) {
if(data.raw.includes('TEMPO'))
data.raw = data.raw.replaceAll('TEMPO','<br>TEMPO');
if(data.raw.includes('BECMG'))
data.raw = data.raw.replaceAll('BECMG','<br>BECMG');
if(data.raw.includes('FM'))
data.raw = data.raw.replaceAll('FM','<br>FM');
}
d7.append('Raw: <b>'+data.raw+'</b> <i class="fa-regular fa-copy fa-xs"></i>');
}
d7.append('<hr>');
rawText = data.raw;
}
//Returns "TAF" from AWC.
function awcTafMain(data, comp) {
var frst = data.fcsts;
p.eq(3).text(comp);
b.eq(26).text(type.toUpperCase());
if(data.remarks)
b.eq(27).text(data.remarks);
b.eq(28).html(data.name+` <img src="https://flagcdn.com/24x18/${flag}.png">`);
b.eq(29).text(data.icaoId);
b.eq(30).text(`${getIST(data.issueTime)} <span id="updateDiff">${time(data.issueTime)}</span>`);
b.eq(31).text(`${getIST(data.validTimeFrom)} until ${getIST(data.validTimeTo)}`);
b.eq(32).text('');
for(i = 0; i < frst.length; i++) {
var span = $('<span></span>'),
span2 = $('<span></span>');
if (frst[i].fcstChange) {
span.text(`${frst[i].fcstChange} from ${getIST(frst[i].timeFrom)} to ${getIST(frst[i].timeTo)}`);
if (frst[i].probability)
span.html(`${frst[i].fcstChange} from ${getIST(frst[i].timeFrom)} to ${getIST(frst[i].timeTo)} (${frst[i].probability}% likely)`);
} else
span.html(`Forecast from ${getIST(frst[i].timeFrom)} to ${getIST(frst[i].timeTo)} <br>`);
if (frst[i].wspd) {
var li = $('<li></li>');
if(frst[i].wdir == 'VRB')
li.html(`Wind: ${frst[i].wspd} Knot(s) (${(frst[i].wspd*1.85).toFixed(1)} KM/H - VRB) <i class="fa-solid fa-location-arrow" style='rotate:135deg'></i> <br>`);
else
li.html(`Wind: ${frst[i].wspd} Knot(s) (${(frst[i].wspd*1.85).toFixed(1)} KM/H - ${frst[i].wdir}°) <i class="fa-solid fa-location-arrow" style='rotate:${frst[i].wdir+135}deg'></i> <br>`);
span2.append(li);
} if (frst[i].wxString) {
var li = $('<li></li>');
li.html(`Weather: ${frst[i].wxString} <br>`);
span2.append(li);
} if (frst[i].visib) {
var li = $('<li></li>');
if(frst[i].visib == '6+')
li.text(`Visibility: ${frst[i].visib} mile(s) (10+ Km)`);
else
li.text(`Visibility: ${frst[i].visib} mile(s) (${(frst[i].visib*1.609).toFixed(1)} Km)`);
span2.append(li);
} if(frst[i].clouds.length) {
var li = $('<li></li>');
li.text('Clouds: ');
span2.append(li);
} if (frst[i].clouds.length) {
var clouds = frst[i].clouds,
ul = $('<ul></u>');
for(j = 0; j < clouds.length; j++) {
var li = $('<li></li>');
if(clouds[j].cover == 'NSC')
li.text('No significant clouds');
else
li.text(`${clouds[j].cover} at ${clouds[j].base} ft AGL`);
if(clouds[j].type)
li.text(`${clouds[j].cover} at ${clouds[j].base} ft (${clouds[j].type}) AGL`);
ul.append(li);
}
span2.append(ul);
}
b.eq(32).append(span);
b.eq(32).append(span2);
}
if(data.rawTAF.includes('TEMPO'))
data.rawTAF = data.rawTAF.replaceAll('TEMPO','<br>TEMPO');
if(data.rawTAF.includes('BECMG'))
data.rawTAF = data.rawTAF.replaceAll('BECMG','<br>BECMG');
if(data.rawTAF.includes('FM'))
data.rawTAF = data.rawTAF.replaceAll('FM','<br>FM');
b.eq(33).html(data.rawTAF);
}
//Returns "TAF" from CWX.
function cwxTafMain(data, flag, comp) {
var frst = data.forecast;
p.eq(3).text(comp);
b.eq(26).text(type.toUpperCase());
if (data.remarks)
b.eq(27).text(data.remarks);
b.eq(28).html(data.station.name+', '+data.station.location+` <img src="https://flagcdn.com/24x18/${flag}.png">`);
b.eq(29).text(data.icao);
b.eq(30).html(`${getIST(data.timestamp.issued)} <span id="updateDiff">${time(data.timestamp.issued)}</span>`);
b.eq(31).text(getIST(data.timestamp.from)+' until '+getIST(data.timestamp.to));
b.eq(32).text('');
for(i = 0; i < frst.length; i++) {
var span = $('<span></span>'),
span2 = $('<span></span>');
if(frst[i].change && frst[i].change.indicator.code != 'FM') {
span.text(`${frst[i].change.indicator.text} from ${getIST(frst[i].timestamp.from)} to ${getIST(frst[i].timestamp.to)}`);
if (frst[i].change.probability)
span.text(`${frst[i].change.indicator.text} from ${getIST(frst[i].timestamp.from)} to ${getIST(frst[i].timestamp.to)} (${frst[i].change.probability}% likely)`);
} else
span.text(`Forecast from ${getIST(frst[i].timestamp.from)} to ${getIST(frst[i].timestamp.to)}`);
if(frst[i].wind) {
var li = $('<li></li>');
li.html(`Wind: ${frst[i].wind.speed_kts} Knot(s) (${frst[i].wind.speed_kph} KM/H - ${frst[i].wind.degrees}°) <i class="fa-solid fa-location-arrow" style='rotate:${frst[i].wind.degrees+135}deg'></i> <br>`);
span2.append(li);
} if (frst[i].conditions) {
var li = $('<li></li>');
li.html(`Weather: `);
for(j = 0; j < frst[i].conditions.length; j++) {
if(j == frst[i].conditions.length-1)
li.append('<b>'+frst[i].conditions[j].text+'</b><br>');
else
li.append('<b>'+frst[i].conditions[j].text+'</b>, ');
} span2.append(li);
} if (frst[i].visibility) {
var li = $('<li></li>');
li.text(`Visibility: ${frst[i].visibility.miles} mile(s) (${(frst[i].visibility.meters_float/1000).toFixed(1)} Km)`);
span2.append(li);
} if(frst[i].clouds.length) {
var li = $('<li></li>');
li.text('Clouds: ');
span2.append(li);
} if (frst[i].clouds.length) {
var clouds = frst[i].clouds,
ul = $('<ul></ul>');
for(j = 0; j < clouds.length; j++) {
var li = $('<li></li>');
if(!clouds[j].feet)
li.text(clouds[j].text);
else
li.text(`${clouds[j].text} at ${clouds[j].feet} ft AGL`);
if(clouds[j].type)
li.text(`${clouds[j].cover} at ${clouds[j].base} ft (${clouds[j].type}) AGL`);
ul.append(li);
}
span2.append(ul);
}
b.eq(32).append(span, span2);
}
if(data.raw_text.includes('TEMPO'))
data.raw_text = data.raw_text.replaceAll('TEMPO','<br>TEMPO');
if(data.raw_text.includes('BECMG'))
data.raw_text = data.raw_text.replaceAll('BECMG','<br>BECMG');
if(data.raw_text.includes('FM'))
data.raw_text = data.raw_text.replaceAll('FM','<br>FM');
b[33].innerHTML = data.raw_text+' <i class="fa-regular fa-copy fa-xs"></i>';
rawText = data.raw_text;
}
//Converts UTC to IST.
function getIST(date) {
let vr;
if (typeof date == 'string')
if(date.includes('Z'))
vr = new Date(date);
else
vr = new Date(date+'Z')
else
vr = new Date(date*1000);
let day = vr.getDate(),
month = vr.getMonth()+1,
year = vr.getFullYear();
vr = vr.toLocaleString().split(',');
return `${day}/${month}/${year}, ${vr[1].replace(':00','')}`;
}
//Fetches "METAR" history from AWC.
async function metarH(id, type, hrs, flag) {
const proxyUrl = 'https://corsproxy.org/?';
const proxyUrl2 = 'https://proxy.cors.sh/';
const proxyUrl3 = 'https://cors-anywhere.herokuapp.com/';
const apiUrl = `https://aviationweather.gov/api/data/${type}?ids=${id}&hours=${hrs}&format=json`;
// const url = proxyUrl + encodeURIComponent(apiUrl);
const url = proxyUrl3 + apiUrl;
try {
const response = await fetch(url, {
method: 'GET',
mode: 'cors', // Allow CORS for the proxy request
headers: {
'Accept': '*/*',
'X-Requested-With': 'XMLHttpRequest', // Potential workaround for some APIs
},
redirect: 'follow', // Follow redirects if encountered
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const data = await response.json();
awcMetH(data, hrs, flag);
console.log(data);
} catch (error) {
console.error('Error fetching data:', error.message);
}
}
//Returns "METAR" history from AWC.
function awcMetH(data, hrs, flag) {
var d6 = $('.d6');
d6.html(`<p>${type.toUpperCase()} HISTORY</p>`);
if(data.length) {
const ol = $('<ol></ol>');
d6.append(`Name: <strong>${data[0].name}</strong> <img src="https://flagcdn.com/24x18/${flag}.png"> <br>
ICAO: <strong>${data[0].icaoId}</strong> <br>`);
if(type == 'metar') {
for(i = 0; i < data.length; i++) {
var li = $('<li></li>');
li.html(getIST(data[i].reportTime)+': <strong>'+data[i].rawOb+`<br>`);
ol.append(li);
}
} else {
for(i = 0; i < data.length; i++) {
var li = $('<li></li>');
li.html(getIST(data[i].issueTime)+': <strong>'+data[i].rawTAF+`<br>`);
ol.append(li);
}
}
d6.append(ol);
} else
d6.append(`No ${type.toUpperCase()}(s) in previous ${hrs} hour(s)!`);
d6.append('<hr>');
}
//Confirms if user wants to know about ICAO code, redirects upon confirmation.
function info() {
let reL = 'https://www.world-airport-codes.com';
if(confirm(`METAR - Meteorological Aerodrome Report.\nTAF - Terminal Aerodrome Forecast.\nTo get ICAO, click 'OK'.\n(redirects to ${reL})\nYour screen resolution: ${screen.width}×${screen.height}px`))
window.open(reL);
}
//This function returns details about an airport like Name, Coords, ICAO, IATA, Runway & website of the airport(if available).
async function search(data, flag) {
var d6 = $('.d6');
//d6.html('<p id="msgBox"></p>');
var stn = data[0];
d6.html('<p>Station Info</p>');
d6.append(`Name: <b>${stn.name}, ${stn.city}, ${stn.state}, ${stn.country}</b> <img src="https://flagcdn.com/24x18/${flag}.png"> <br>
Coordinate: <b>${stn.latitude.toFixed(2)}, ${stn.longitude.toFixed(2)} </b><br>
IATA: <b>${stn.iata} </b><br>
ICAO: <b>${stn.icao} </b><br>
Reporting: <b>${stn.reporting?'Yes':'No'} </b><br>`);
for(j = 0; j < stn.runways.length; j++) {
var rny = stn.runways[j],
span = $('<span></span>'), ul = $('<ul></ul>');
span.text('Runway '+(j+1)+':-');
ul.html(`<b><li>Surface: ${rny.surface}</li></b>
<b><li>Numbers: ${rny.ident1} & ${rny.ident2}</li></b>
<b><li>Length: ${rny.length_ft} ft / ${(rny.length_ft*0.3048).toFixed(0)} m</li></b>
<b><li>Width: ${rny.width_ft} ft</li></b>
<b><li>Lights: ${rny.lights} </li></b>`);
span.append(ul);
d6.append(span);
}
if(stn.website)
d6.append(`Website: <a href='${stn.website}'>Visit</a>`);
else if(stn.wiki)
d6.append(`Website: <a href='${stn.wiki}'>Visit</a>`);
d6.append('<hr>');
}
//Returns time difference between issused time & an instance.
function time(t, x = 0) {
x || hlp(t);
var tm;
if(t.charAt(t.length-1) == 'Z')
tm = ((new Date() - new Date(t))/60000).toFixed(0);
else
tm = ((new Date() - new Date(t+'Z'))/60000).toFixed(0);
var hb = Math.ceil(tm/60);
if(hb > 1)
return `[${hb-1} hour(s) ago]`;
return `[${tm} min(s) ago]`;
}
//Function for logging header metadata.
function getHeaders(response) {
const headers = {};
response.headers.forEach((value, name) => {
headers[name] = value;
});
//console.log(headers);
}
//Updates time difference every 10s interval.
function hlp(t) {
if (diffIvl)
clearInterval(diffIvl)
diffIvl = setInterval(() => {
$('#updateDiff').text(time(t, 1));
}, 5000);
}
//Arrow from fontawesome.com for variable wind animation.
//
function arrow(strt, end) {
var i = $('.arrow');
i.css({'--start': (strt+135)+'deg',
'--end': (end+135)+'deg'});
}
/* Copy raw METAR or TAF to clipboard */
$(document).on('click', '.fa-copy', () => {
let copy = $('.fa-copy');
navigator.clipboard.writeText(rawText);
copy.toggleClass('fa-solid fa-sm fa-bounce fa-shake');
setTimeout(() => {
copy.toggleClass('fa-solid fa-sm fa-bounce fa-shake')
}, 1000);
})
//Selects checkbox with id "#map" & toggles iframe's display between "block" & "none".
$('#map').change(function() {
/*if($(this).prop('checked')) {
$('iframe').show();
//Smooth scroll to map when checked
$('html, body').animate({
scrollTop: iframe.offset().top
}, 1000);
} else
$('iframe').hide();*/
iframe.toggle();
$(this).is(':checked') ?
$('html, body').animate({
scrollTop: iframe.offset().top
}, 1000) :
$('html, body').animate({
scrollTop: 0
}, 500);
})
//Randomly picks a color & set it as "accent color" of input tags.
const colors = ['olive','teal','indianred','coral','lightcoral','salmon','cromson','turquoise','moccasin','peachpuff','khaki','orchid','darkmagenta','chartreuse','seagreen','mediumaquamarine','lightseagreen','navajowhite','burlywood','rosybrown','peru','sienna','lightcoral','lightseagreen','mistyrose'];
const clrs = colors[Math.floor(Math.random() * colors.length)];
var ob = (e) => {
e.style.accentColor = clrs;
}
//ob(value[1]); ob(value[2]); ob(value[3]); ob(value[4]); ob(value[5]); ob(value[6]); ob(value[7]);
window.onload = function() {
// Use setTimeout to ensure the page has fully loaded before scrolling
setTimeout(function() {
window.scrollTo(0, 1); // Scroll down to hide address bar
}, 100);
};