-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetar.ts
162 lines (154 loc) · 4.66 KB
/
metar.ts
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
import { Message, MessageEmbed } from 'discord.js';
import Redis from 'ioredis';
import axios from 'axios';
import mp from 'metar-parser';
import dotenv from 'dotenv-flow';
dotenv.config();
const redis = new Redis(process.env.REDIS_URI);
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
// Calculate density altitude for ZAB airfields
function calcDA(icaoId:string, temp:number, altSet:number) {
const elevation: {[key:string]:number} = {
KAMA: 3649,
KCVS: 4295,
KROW: 3671,
KELP: 3962,
KBIF: 3947,
KHMN: 4093,
KABQ: 5355,
KAEG: 5837,
KSAF: 6349,
KSKX: 7095,
KFHU: 4719,
KTUS: 2643,
KDMA: 2704,
KRYN: 2419,
KPHX: 1135,
KIWA: 1384,
KFFZ: 1394,
KGYR: 969,
KLUF: 1085,
KGEU: 1071,
KDVT: 1478,
KSDL: 1510,
KGXF: 883,
KPRC: 5045,
KSEZ: 4831,
KFLG: 7015,
KINW: 4941,
KGUP: 6472,
KAXX: 8380,
KLVS: 6877,
KLAM: 7171,
KRTN: 6352,
KCAO: 4970,
KDHT: 3991,
KDUX: 3706,
KE42: 3090,
KBGD: 3055,
KPPA: 3245,
KHHF: 2396,
KHRX: 3788,
KCVN: 4216,
KTCC: 4065,
KSXU: 4791,
K0E0: 6204,
KSRR: 6814,
KALM: 4200,
KATS: 3545,
KCNM: 3295,
KPEQ: 2613,
KFST: 3011,
KVHN: 3957,
KE38: 4514,
KMRF: 4849,
KPRS: 2938,
KDNA: 4113,
KLRU: 4457,
KDMN: 4314,
KSVC: 5446,
K0A0: 4595,
KTCS: 4862,
KONM: 4875,
KBRG: 5200,
KGNT: 6537,
KRQE: 6742,
KSJN: 5737,
KJTC: 7055,
KOLS: 3955,
KAVQ: 2032,
KMZJ: 1893,
KSAD: 3178,
KP08: 1576,
KCGZ: 1464,
KA39: 1307,
KCHD: 1243,
KSOW: 6416,
KTYL: 5823,
KPAN: 5157,
KCMR: 6691,
KBXK: 1033,
};
if (elevation[icaoId.toUpperCase()]) {
const pressAlt = ((29.92 - altSet) * 1000) + elevation[icaoId.toUpperCase()];
const stdTemp = 15 + ((elevation[icaoId.toUpperCase()] / 1000) * -2);
const densityAlt = pressAlt + (120 * (temp - stdTemp));
return Math.round(densityAlt);
}
return null;
}
const sendMetarMessage = async (metar: string, msg: Message) => {
const embed = new MessageEmbed()
.setColor('#208951')
.setAuthor('Albuquerque ARTCC', 'https://zabartcc.sfo3.digitaloceanspaces.com/images/zab_logo.png', '')
.setFooter('For flight simulation use only. The METAR fetcher is a feature of Charles Barkdozer, the Albuquerque ARTCC Discord Bot.', 'https://zabartcc.sfo3.digitaloceanspaces.com/images/zab_logo.png');
if(!metar) {
embed.setTitle('METAR not found.')
.setDescription('The METAR for that airport was not found. This could be an issue with the VATSIM METAR service, or that no METAR exists for this airport.');
} else {
const m = mp(metar);
embed.setTitle(`METAR for ${m.station}`);
embed.setDescription(metar);
embed.addField('Observation Time :timer:', m.time.date);
embed.addField('Wind :wind_blowing_face:', `${m.wind.direction}@${m.wind.speedKt}${m.wind.gust ? ` Gust ${m.wind.gust}` : ''}`);
embed.addField('Visibility :eyes:', `${m.visibility?.miles || 10}SM`);
if(m.weather.length) {
const sigweather = m.weather.map((w: any) => `${w.intensity !== 'moderate' ? w.intensity.capitalize() : ''} ${(w.precipitation || w.obscuration).capitalize()}`);
embed.addField('Weather :white_sun_small_cloud:', sigweather.join(', '));
}
if(m.clouds.length) {
const clouds = m.clouds.map((c: any) => `${c.meaning.capitalize()} clouds at ${c.altitude}`);
embed.addField('Clouds :cloud:', `${clouds.join(', ')} - AGL`);
}
embed.addField('Temperature :thermometer:', `${m.temperature.celsius}°C (${m.temperature.farenheit || Math.round(m.temperature.celsius * 9 / 5)}°F)`);
embed.addField('Dewpoint :droplet:', `${m.dewpoint.celsius}°C (${m.dewpoint.farenheit || Math.round(m.dewpoint.celsius * 9 / 5)}°F)`);
embed.addField('Altimeter :chart_with_upwards_trend:', `${m.altimeter.inches} inHg (QNH ${m.altimeter.millibars})`);
const da = calcDA(m.station, m.temperature.celsius, m.altimeter.inches);
if (da != null) {
embed.addField('Density Altitude :high_brightness:', `${da} ft`);
}
}
await msg.channel.send(embed);
};
export default async (msg: Message) => {
const msgParts = msg.content.split(' ');
const airport = msgParts[1].toUpperCase();
if (!airport) {
msg.reply('Please specify an airport code.');
return;
}
let metar = await redis.get(`METAR:${airport}`) || '';
if (!metar) {
const { data } = await axios.get(`https://metar.vatsim.net/${airport}`, {
headers: {
'User-Agent': 'Charles Barkdozer/Albuquerque ARTCC',
},
});
await redis.set(`METAR:${airport}`, data);
await redis.expire(`METAR:${airport}`, 1200);
metar = `${data}`;
}
await sendMetarMessage(metar, msg);
};