-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWx.go
233 lines (202 loc) · 4.03 KB
/
Wx.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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"io"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
)
type METAR struct {
gorm.Model `json:"-"`
Airport Airport `gorm:"foreignkey:AirportId" json:"-"`
AirportId uint `json:"-"`
Timestamp int64 `json:"-"`
Ceiling int `json:"C"`
Visibility int `json:"V"`
Wind int `json:"W"`
Temperature int `json:"T"`
Sky int `json:"S"`
}
type tinyMETAR struct {
C []int
V []int
W []int
T []int
S []int
}
type miniMETAR struct {
C [][]int
V [][]int
W [][]int
T [][]int
S [][]int
}
func GetAirportWx(c *gin.Context) {
var wx []METAR
var codes []string
final := miniMETAR{}
iata := c.Param("iata")
codes = strings.Split(iata, ",")
for _, code := range codes {
airport := GetAirport(code)
db.Limit(10).Where("airport_id = ?", airport.ID).Find(&wx)
res := tinyMETAR{}
for _, metar := range wx {
res.C = append(res.C, metar.Ceiling)
res.V = append(res.V, metar.Visibility)
res.W = append(res.W, metar.Wind)
res.T = append(res.T, metar.Temperature)
res.S = append(res.S, metar.Sky)
}
final.C = append(final.C, res.C)
final.V = append(final.V, res.V)
final.W = append(final.W, res.W)
final.T = append(final.T, res.T)
final.S = append(final.S, res.S)
}
c.JSON(http.StatusOK, final)
}
func scraper(id int, jobs <-chan []string, results chan *METAR) {
for row := range jobs {
// IATA code
airport := GetAirport(row[1])
if airport.ID != 0 {
// Observation Time
fmt.Println(row[2])
timestamp, err := time.Parse(time.RFC3339Nano, row[2])
if err != nil {
log.Fatal(err)
}
// Ceiling 24
ceiling, _ := strconv.ParseInt(row[24], 0, 64)
// Visibility 10
visibility, _ := strconv.ParseInt(row[10], 0, 64)
// Wind 8
wind, _ := strconv.ParseInt(row[8], 0, 64)
// Temperature 5
temperature, _ := strconv.ParseInt(row[5], 0, 64)
db.Create(&METAR{
Airport: airport,
AirportId: airport.ID,
Timestamp: timestamp.Unix(),
Ceiling: CeilingParser(ceiling),
Visibility: VisibilityParser(visibility),
Wind: WindParser(wind),
Temperature: TemperatureParser(temperature),
Sky: SkyParser(row[30]),
})
}
}
}
const (
Purple int = iota + 1
Red
Blue
Green
)
func CeilingParser(ceil int64) int {
if ceil < 5 {
return Purple
} else if ceil < 10 {
return Red
} else if ceil < 30 {
return Blue
} else if ceil >= 30 {
return Green
} else {
return Purple
}
}
func VisibilityParser(vis int64) int {
if vis < 1 {
return Purple
} else if vis < 3 {
return Red
} else if vis < 5 {
return Blue
} else if vis >= 5 {
return Green
} else {
return Purple
}
}
func WindParser(wind int64) int {
if wind > 30 {
return Purple
} else if wind > 20 {
return Red
} else if wind > 10 {
return Blue
} else if wind <= 10 {
return Green
} else {
return Purple
}
}
func TemperatureParser(temp int64) int {
if temp <= 0 {
return Blue
} else if temp < 30 {
return Green
} else if temp >= 30 {
return Red
} else {
return Red
}
}
func SkyParser(sky string) int {
switch sky {
case "VFR":
return Green
case "MVFR":
return Blue
case "IFR":
return Red
case "LIFR":
return Purple
default:
return Purple
}
}
func UpdateWxData(c *gin.Context) {
var airports []Airport
db.Find(&airports)
// get wx data from adds
resp, err := http.Get("https://aviationweather.gov/adds/dataserver_current/current/metars.cache.csv")
if err != nil {
log.Fatal(err)
}
// parse into a string to trim first 60 characters
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
jobs := make(chan []string, 5000)
results := make(chan *METAR, 5000)
for i := 0; i < 10; i++ {
go scraper(i, jobs, results)
}
// parse the csv
r := csv.NewReader(bytes.NewReader(body[659:]))
for {
row, err := r.Read()
// eof case
if err == io.EOF {
break
}
// error case
if err != nil {
log.Fatal(err)
}
jobs <- row
}
close(jobs)
c.JSON(http.StatusOK, "ok")
}