-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
weather.go
404 lines (351 loc) · 10.9 KB
/
weather.go
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
package weather
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/evogelsa/DCS-real-weather/util"
)
var SelectedPreset string
var SelectedBase int
const (
MPSToKT = 1.944
MetersToFeet = 3.281
)
func CelsiusToFahrenheit(c float64) float64 {
return (c * 1.8) + 32
}
func GetWeather() (WeatherData, error) {
log.Println("Getting weather from CheckWX...")
// create http client to fetch weather data, timeout after 5 sec
timeout := time.Duration(5 * time.Second)
client := http.Client{Timeout: timeout}
request, err := http.NewRequest(
"GET",
"https://api.checkwx.com/metar/"+util.Config.METAR.ICAO+"/decoded",
nil,
)
if err != nil {
return WeatherData{}, err
}
request.Header.Set("X-API-Key", util.Config.APIKey)
// make api request
resp, err := client.Do(request)
if err != nil {
return WeatherData{}, fmt.Errorf(
"Error making request to CheckWX: %v",
err,
)
}
// verify response OK
if resp.StatusCode != http.StatusOK {
return WeatherData{}, fmt.Errorf("CheckWX bad status: %v", resp.Status)
}
defer resp.Body.Close()
// parse response byte array
body, err := io.ReadAll(resp.Body)
if err != nil {
return WeatherData{}, fmt.Errorf(
"Error parsing CheckWX response: %v",
err,
)
}
log.Println("Got weather data:", string(body))
log.Println("Parsing weather...")
// format json resposne into weatherdata struct
var res WeatherData
err = json.Unmarshal(body, &res)
if err != nil {
return WeatherData{}, err
}
if err := checkWeather(&res); err != nil {
return res, err
}
log.Println("Parsed weather")
return res, nil
}
// checkWeather takes in weather data from the API and checks the first results
// for reasonable results that can be applied to DCS weather. In the case of
// bad or missing data, it modifies the value in data to a reasonable default
func checkWeather(data *WeatherData) error {
if data.NumResults < 1 {
return fmt.Errorf("no data to check")
}
if data.Data[0].Barometer.Hg == 0 {
log.Println("No barometer data from CheckWX, defaulting to 760 mmHg")
data.Data[0].Barometer.Hg = 29.92
data.Data[0].Barometer.HPa = 1013.2
data.Data[0].Barometer.KPa = 101.32
data.Data[0].Barometer.MB = 1013.2
}
return nil
}
// GenerateMETAR generates a metar based on the weather settings added to the
// DCS miz
func GenerateMETAR(wx WeatherData) (string, error) {
data := wx.Data[0]
var metar string
// add ICAO
metar += "METAR: " + data.ICAO + " "
// get observed time, no need to translate time zone since it's in Zulu
t, err := time.Parse("2006-01-02T15:04:05Z", data.Observed)
if err != nil {
t, err = time.Parse("2006-01-02T15:04:05", data.Observed)
if err != nil {
return "", fmt.Errorf("Error parsing METAR time: %v", err)
}
}
// want format DDHHMMZ
metar += fmt.Sprintf("%02d%02d%02dZ ", t.Day(), t.Hour(), t.Minute())
// winds DIRSPDKT
metar += fmt.Sprintf(
"%03d%02dKT ",
int(data.Wind.Degrees),
int(data.Wind.SpeedKTS),
)
// visibility
metar += fmt.Sprintf("%dSM ", int(data.Visibility.MilesFloat))
// conditions
for _, cond := range data.Conditions {
metar += fmt.Sprintf("%s ", cond.Code)
}
// clouds
if SelectedPreset == "" {
metar += "CLR "
} else if clouds, ok := DecodePreset[SelectedPreset]; ok {
for i, cld := range clouds {
if i == 0 {
// convert base to hundreds of feet
base := int(float64(SelectedBase)*3.28+50) / 100
metar += fmt.Sprintf("%s%03d ", cld.Name, base)
} else {
metar += fmt.Sprintf("%s%s ", cld.Name, cld.Base)
}
}
} else {
// using legacy/custom wx
cloudKind := SelectedPreset[7:10]
// convert base to hundreds of feet
base := int(float64(SelectedBase)*3.28+50) / 100
metar += fmt.Sprintf("%s%03d ", cloudKind, base)
}
// temperature
if data.Temperature.Celsius < 0 {
metar += fmt.Sprintf("M%02d/", int(-1*data.Temperature.Celsius))
} else {
metar += fmt.Sprintf("%02d/", int(data.Temperature.Celsius))
}
// dewpoint
if data.Dewpoint.Celsius < 0 {
metar += fmt.Sprintf("M%02d ", int(-1*data.Dewpoint.Celsius))
} else {
metar += fmt.Sprintf("%02d ", int(data.Dewpoint.Celsius))
}
// altimeter
metar += fmt.Sprintf("A%4d ", int(data.Barometer.Hg*100))
// nosig because usually not updated until 4 hours (whenever rw gets run)
metar += "NOSIG"
// rmks
metar += " " + util.Config.METAR.Remarks
return metar, nil
}
type WeatherData struct {
Data []Data `json:"data"`
NumResults int `json:"results"`
}
type Data struct {
Barometer Barometer `json:"barometer"`
Ceiling Ceiling `json:"ceiling"`
Clouds []Clouds `json:"clouds"`
Conditions []Conditions `json:"conditions"`
Dewpoint Dewpoint `json:"dewpoint"`
Elevation Elevation `json:"elevation"`
FlightCategory FlightCategory `json:"flight_category"`
Humidity Humidity `json:"humidity"`
ICAO string `json:"icao"`
ID string `json:"id"`
Location Location `json:"location"`
Observed string `json:"observed"`
RawText string `json:"raw_text"`
Station Station `json:"station"`
Temperature Temperature `json:"temperature"`
Visibility Visibility `json:"visibility"`
Wind Wind `json:"wind"`
}
type Barometer struct {
Hg float64 `json:"hg"`
HPa float64 `json:"hpa"`
KPa float64 `json:"kpa"`
MB float64 `json:"mb"`
}
type Ceiling struct {
BaseFeetAGL float64 `json:"base_feet_agl"`
BaseMetersAGL float64 `json:"base_meters_agl"`
Code string `json:"code"`
Feet float64 `json:"feet"`
Meters float64 `json:"meters"`
Text string `json:"text"`
}
type Clouds struct {
BaseFeetAGL float64 `json:"base_feet_agl"`
BaseMetersAGL float64 `json:"base_meters_agl"`
Code string `json:"code"`
Feet float64 `json:"feet"`
Meters float64 `json:"meters"`
Text string `json:"text"`
}
type Conditions struct {
Code string `json:"code"`
Text string `json:"text"`
}
type Dewpoint struct {
Celsius float64 `json:"celsius"`
Fahrenheit float64 `json:"fahrenheit"`
}
type Elevation struct {
Feet float64 `json:"feet"`
Meters float64 `json:"meters"`
}
type FlightCategory string
type Humidity struct {
Percent float64 `json:"percent"`
}
type Location struct {
Coordinates []float64 `json:"coordinates"`
Type string `json:"type"`
}
type Station struct {
Name string `json:"name"`
}
type Temperature struct {
Celsius float64 `json:"celsius"`
Fahrenheit float64 `json:"fahrenheit"`
}
type Visibility struct {
Meters string `json:"meters"`
MetersFloat float64 `json:"meters_float"`
Miles string `json:"miles"`
MilesFloat float64 `json:"miles_float"`
}
type Wind struct {
Degrees float64 `json:"degrees"`
SpeedKPH float64 `json:"speed_kph"`
SpeedKTS float64 `json:"speed_kts"`
SpeedMPH float64 `json:"speed_mph"`
SpeedMPS float64 `json:"speed_mps"`
GustKPH float64 `json:"gust_kph"`
GustKTS float64 `json:"gust_kts"`
GustMPH float64 `json:"gust_mph"`
GustMPS float64 `json:"gust_mps"`
}
type CloudPreset struct {
Name string
MinBase int
MaxBase int
}
var CloudPresets map[string][]CloudPreset = map[string][]CloudPreset{
"FEW": {
{`"Preset1"`, 840, 4200}, // Light Scattered 1
{`"Preset2"`, 1260, 2520}, // Light Scattered 2
},
"SCT": {
{`"Preset3"`, 840, 2520}, // High Scattered 1
{`"Preset4"`, 1260, 2520}, // High Scattered 2
{`"Preset5"`, 1260, 4620}, // Scattered 1
{`"Preset6"`, 1260, 4200}, // Scattered 2
{`"Preset7"`, 1680, 5040}, // Scattered 3
{`"Preset8"`, 3780, 5460}, // High Scattered 3
{`"Preset9"`, 1680, 3780}, // Scattered 4
{`"Preset10"`, 1260, 4200}, // Scattered 5
{`"Preset11"`, 2520, 5460}, // Scattered 6
{`"Preset12"`, 1680, 3360}, // Scattered 7
},
"BKN": {
{`"Preset13"`, 1680, 3360}, // Broken 1
{`"Preset14"`, 1680, 3360}, // Broken 2
{`"Preset15"`, 840, 5040}, // Broken 3
{`"Preset16"`, 1260, 4200}, // Broken 4
{`"Preset17"`, 0, 2520}, // Broken 5
{`"Preset18"`, 0, 3780}, // Broken 6
{`"Preset19"`, 0, 2940}, // Broken 7
{`"Preset20"`, 0, 3780}, // Broken 8
},
"OVC": {
{`"Preset21"`, 1260, 4200}, // Overcast 1
{`"Preset22"`, 420, 4200}, // Overcast 2
{`"Preset23"`, 840, 3360}, // Overcast 3
{`"Preset24"`, 420, 2520}, // Overcast 4
{`"Preset25"`, 420, 3360}, // Overcast 5
{`"Preset26"`, 420, 2940}, // Overcast 6
{`"Preset27"`, 420, 2520}, // Overcast 7
},
"OVC+RA": {
{`"RainyPreset1"`, 420, 2940}, // Overcast And Rain 1
{`"RainyPreset2"`, 840, 2520}, // Overcast And Rain 2
{`"RainyPreset3"`, 840, 2520}, // Overcast And Rain 3
},
}
type Cloud struct {
Name string
Base string
}
var (
DecodePreset = map[string][]Cloud{
`"Preset1"`: {{"FEW", "070"}},
`"Preset2"`: {{"FEW", "080"}, {"SCT", "230"}},
`"Preset3"`: {{"SCT", "080"}, {"FEW", "210"}},
`"Preset4"`: {{"SCT", "080"}, {"SCT", "240"}},
`"Preset5"`: {{"SCT", "140"}, {"FEW", "270"}, {"BKN", "400"}},
`"Preset6"`: {{"SCT", "080"}, {"FEW", "400"}},
`"Preset7"`: {{"BKN", "075"}, {"SCT", "210"}, {"SCT", "400"}},
`"Preset8"`: {{"SCT", "180"}, {"FEW", "360"}, {"FEW", "400"}},
`"Preset9"`: {{"BKN", "075"}, {"SCT", "200"}, {"FEW", "410"}},
`"Preset10"`: {{"SCT", "180"}, {"FEW", "360"}, {"FEW", "400"}},
`"Preset11"`: {{"BKN", "180"}, {"BKN", "320"}, {"FEW", "410"}},
`"Preset12"`: {{"BKN", "120"}, {"SCT", "220"}, {"FEW", "410"}},
`"Preset13"`: {{"BKN", "120"}, {"BKN", "260"}, {"FEW", "410"}},
`"Preset14"`: {{"BKN", "070"}, {"FEW", "410"}},
`"Preset15"`: {{"SCT", "140"}, {"BKN", "240"}, {"FEW", "400"}},
`"Preset16"`: {{"BKN", "140"}, {"BKN", "280"}, {"FEW", "400"}},
`"Preset17"`: {{"BKN", "070"}, {"BKN", "200"}, {"BKN", "320"}},
`"Preset18"`: {{"BKN", "130"}, {"BKN", "250"}, {"BKN", "380"}},
`"Preset19"`: {{"OVC", "090"}, {"BKN", "230"}, {"BKN", "310"}},
`"Preset20"`: {{"BKN", "130"}, {"BKN", "280"}, {"FEW", "380"}},
`"Preset21"`: {{"BKN", "070"}, {"OVC", "170"}},
`"Preset22"`: {{"OVC", "070"}, {"BKN", "170"}},
`"Preset23"`: {{"OVC", "110"}, {"BKN", "180"}, {"SCT", "320"}},
`"Preset24"`: {{"OVC", "030"}, {"OVC", "170"}, {"BKN", "340"}},
`"Preset25"`: {{"OVC", "120"}, {"OVC", "220"}, {"OVC", "400"}},
`"Preset26"`: {{"OVC", "090"}, {"BKN", "230"}, {"SCT", "320"}},
`"Preset27"`: {{"OVC", "080"}, {"BKN", "250"}, {"BKN", "340"}},
`"RainyPreset1"`: {{"OVC", "030"}, {"OVC", "280"}, {"FEW", "400"}},
`"RainyPreset2"`: {{"OVC", "030"}, {"SCT", "180"}, {"FEW", "400"}},
`"RainyPreset3"`: {{"OVC", "060"}, {"OVC", "190"}, {"SCT", "340"}},
}
)
var DefaultWeather WeatherData = WeatherData{
Data: []Data{
{
Wind: Wind{
SpeedMPS: 1.25,
Degrees: 270,
GustMPS: 3,
},
Temperature: Temperature{
Celsius: 15,
},
Barometer: Barometer{
Hg: 29.92,
},
Clouds: []Clouds{
{
Code: "CLR",
},
},
Observed: time.Now().Format("2006-01-02T15:04:05"),
},
},
NumResults: 1,
}