forked from badvectors/ATISPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMETAR.cs
More file actions
234 lines (181 loc) · 6.62 KB
/
METAR.cs
File metadata and controls
234 lines (181 loc) · 6.62 KB
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
using System.Text.RegularExpressions;
using System;
namespace ATISPlugin
{
public class METAR
{
public string ICAO { get; set; }
public DateTime ProductTime { get; set; } = DateTime.MaxValue;
public string Wind { get; set; }
public string Visibility { get; set; }
public string Cloud { get; set; }
public string Weather { get; set; }
public string Temperature { get; set; }
public string DewPoint { get; set; }
public string QNH { get; set; }
public METAR Process(string metar)
{
if (metar.Length < 4) return null;
ICAO = metar.Substring(0, 4);
string[] components = Regex.Replace(metar.Substring(5), "\\t|\\n|\\r", "").Split(' ');
foreach (var item in components)
{
if (item == "RMK") break;
if (item == "AUTO") continue;
if (item == "CAVOK")
{
Weather = "CAVOK";
continue;
}
if (item == "//") // weather
{
// Weather = "WEATHER NAVBL";
continue;
}
if (item == "////") // visibility,
{
Visibility = "NAVBL";
continue;
}
if (item == "//////") // cloud
{
Cloud = "NAVBL";
continue;
}
// TIME
if (Regex.IsMatch(item, "^\\d{6}Z$"))
{
ProductTime = new DateTime(1, 1, int.Parse(item.Substring(0, 2)), int.Parse(item.Substring(2, 2)), int.Parse(item.Substring(4, 2)), 0);
continue;
}
// WIND
if (Regex.IsMatch(item, "^(\\d{3}|VRB)\\d{2}(KT|MPS|KPH)$") || Regex.IsMatch(item, "^(\\d{3}|VRB)\\d{2}G\\d{2,3}(KT|MPS|KPH)$"))
{
var wind = item.Replace("KT", "").Replace("MPS", "").Replace("KPH", "");
if (wind == "00000")
{
Wind = "CALM";
continue;
}
if (wind.EndsWith("01") || wind.EndsWith("02") || wind.EndsWith("03"))
{
Wind = "VRB";
continue;
}
var direction = wind.Substring(0, 3);
var speed = wind.Substring(3, 2);
Wind = $"{direction}/{speed}";
var gust = wind.Split('G');
if (gust.Length > 1) Wind += $"-{gust[1]}";
continue;
}
// VARIABLE WIND
if (Wind != "CALM" && Regex.IsMatch(item, "^\\d{3}V\\d{3}$"))
{
if (Wind == null) continue;
string[] wind = Wind.Split('/');
string[] variable = item.Split('V');
if (wind.Length == 2 && variable.Length == 2)
{
Wind = variable[0] + "-" + variable[1] + "/" + wind[1];
}
else
{
Wind = Wind + " " + item;
}
continue;
}
// WEATHER
if (Regex.IsMatch(item, "^(\\+|\\-){0,1}([A-Z]{2}){1,3}$"))
{
if (Weather != null) Weather += " ";
Weather += item;
continue;
}
// VISIBILITY
if (Regex.IsMatch(item, "^(\\d\\/\\d+|\\d{1,2})(KM|SM)$"))
{
Visibility = item;
continue;
}
if (Regex.IsMatch(item, "^\\d{4}$"))
{
if (item == "9999")
{
Visibility = "GT 10KM";
continue;
}
var visOk = int.TryParse(item, out int visibility);
if (!visOk) continue;
Visibility = visibility <= 5000 ? $"{visibility}M" : $"{visibility / 1000}KM";
continue;
}
// CLOUD
var cloud = Regex.Match(item, "^(FEW|SCT|BKN|OVC)\\d{3}(CB|TCU){0,1}$");
if (cloud.Success)
{
var levelOk = int.TryParse(item.Substring(3, 3), out int level);
if (!levelOk) continue;
if (level > 50) continue; // TODO: Should be or below MSA + add CB and TCU to ATIS voice.
if (Cloud != null) Cloud += ", ";
Cloud += item;
continue;
}
// TEMP/ DEP
if (Regex.IsMatch(item, "^M{0,1}\\d{2}\\/M{0,1}\\d{2}$"))
{
Temperature = item[0] != '0' ? item.Substring(0, 2) : item.Substring(1, 1);
DewPoint = item[3] != '0' ? item.Substring(3) : item.Substring(4);
continue;
}
// QNH
if (Regex.IsMatch(item, "^(Q|A)\\d{4}$"))
{
QNH = item.Substring(1).TrimStart('0');
continue;
}
}
if (Cloud == null && (Visibility == "GT 10KM" || Visibility == null) && Weather == null)
{
Visibility = null;
Weather = "CAVOK";
}
return this;
}
public string GetField(METARField field)
{
switch (field)
{
case METARField.None:
return null;
case METARField.Wind:
return Wind;
case METARField.Visibility:
return Visibility;
case METARField.Cloud:
return Cloud;
case METARField.Weather:
return Weather;
case METARField.Temperature:
return Temperature;
case METARField.DewPoint:
return DewPoint;
case METARField.QNH:
return QNH;
default:
return null;
}
}
}
public enum METARField
{
None,
Wind,
Visibility,
Cloud,
Weather,
Temperature,
DewPoint,
QNH,
}
}