summaryrefslogtreecommitdiff
path: root/weather.go
blob: 7e615cc55d0486f18224adbda0d28e0dd9fd5004 (plain)
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
// Copyright 2018-2021 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package main

// TODO: allow free-text lookups of place names, rather than ids.
//       see README for details of how to do that.

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

const metdefid = "310004"
const bbcdefid = "2654675"

const meturl = "https://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/%s.json?concise=true"
const bbcurl = "https://weather-broker-cdn.api.bbci.co.uk/en/forecast/aggregated/%s"

const usage = `Usage: weather [-s source] [-v] [locationid]

weather shows the weather forecast for a location. Read the README
for instructions on finding your location ID.
`

const mpsToMphMultiplier = 2.23693629

// BBC structures
type BBCResponse struct {
	Forecasts []struct {
		Detailed struct {
			Reports []Report
		}
	}
}

type Report struct {
	EnhancedWeatherDescription        string
	ExtendedWeatherType               int
	FeelsLikeTemperatureC             int
	FeelsLikeTemperatureF             int
	GustSpeedKph                      int
	GustSpeedMph                      int
	Humidity                          int
	LocalDate                         string
	PrecipitationProbabilityInPercent int
	PrecipitationProbabilityText      string
	Pressure                          int
	TemperatureC                      int
	TemperatureF                      int
	Timeslot                          string
	TimeslotLength                    int
	Visibility                        string
	WeatherType                       int
	WeatherTypeText                   string
	WindDescription                   string
	WindDirection                     string
	WindDirectionAbbreviation         string
	WindDirectionFull                 string
	WindSpeedKph                      int
	WindSpeedMph                      int
}

// Met Office structures
type MetResponse struct {
	BestFcst struct {
		Forecast struct {
			Location struct {
				Days []Day `json:"Day"`
			}
		}
	}
}

type Day struct {
	Date      string `json:"@date"`
	DayValues struct {
		WeatherParameters WeatherParams
	}
	NightValues struct {
		WeatherParameters WeatherParams
	}
	TimeSteps struct {
		TimeStep []struct {
			Time              string `json:"@time"`
			WeatherParameters WeatherParams
		}
	}
}

type WeatherParams struct {
	AQIndex int     // Air Quality
	F       float64 // Feels Like Temperature
	H       int     // Humidity
	P       int     // Pressure
	PP      int     // Precipitation Probability
	T       float64 // Temperature
	UV      int     // Max UV Index
	V       int     // Visibility
	WD      string  // Wind Direction
	WG      float64 // Wind Gust
	WS      float64 // Wind Speed
	WT      int     // Weather Type
}

var TypeDescription = map[int]string{
	0:  "Clear Sky",
	1:  "Sunny",
	2:  "Partly Cloudy",
	3:  "Sunny Intervals",
	4:  "Unknown",
	5:  "Mist",
	6:  "Fog",
	7:  "Light Cloud",
	8:  "Thick Cloud",
	9:  "Light Rain Showers",
	10: "Light Rain Showers",
	11: "Drizzle",
	12: "Light Rain",
	13: "Heavy Rain Showers",
	14: "Heavy Rain Showers",
	15: "Heavy Rain",
	16: "Sleet Showers",
	17: "Sleet Showers",
	18: "Sleet",
	19: "Hail Showers",
	20: "Hail Showers",
	21: "Hail",
	22: "Light Snow Showers",
	23: "Light Snow Showers",
	24: "Light Snow",
	25: "Heavy Snow Showers",
	26: "Heavy Snow Showers",
	27: "Heavy Snow",
	28: "Thundery Showers",
	29: "Thundery Showers",
	30: "Thunder",
}

// Our prefered struct
type Weather struct {
	airquality        int
	date              string
	feelsliketempDegC float64
	humidity          int
	maxuv             int
	precipitationPerc int
	pressure          int
	temperatureDegC   float64
	time              string
	visibilityMetres  int
	weathertype       int
	winddir           string
	windgustMph       float64
	windspeedMph      float64
}

var (
	src     = flag.String("s", "bbc", "data source provider (valid options: 'bbc', 'metoffice')")
	verbose = flag.Bool("v", false, "verbose: show all weather details")
)

func processBBC(b []byte) []Weather {
	var r BBCResponse
	var weather []Weather
	var w Weather
	err := json.Unmarshal(b, &r)
	if err != nil {
		log.Fatal(err)
	}

	for _, f := range r.Forecasts {
		for _, report := range f.Detailed.Reports {
			w.date = report.LocalDate
			w.time = report.Timeslot
			w.feelsliketempDegC = float64(report.FeelsLikeTemperatureC)
			w.humidity = report.Humidity
			w.temperatureDegC = float64(report.TemperatureC)
			w.precipitationPerc = report.PrecipitationProbabilityInPercent
			w.pressure = report.Pressure
			w.visibilityMetres = estimateVisibility(report.Visibility)
			w.weathertype = report.WeatherType
			w.winddir = report.WindDirectionFull
			w.windgustMph = float64(report.GustSpeedMph)
			w.windspeedMph = float64(report.WindSpeedMph)
			weather = append(weather, w)
		}
	}
	return weather
}

// estimateVisibility returns a rough number of meters
// of vilibility based on descriptive text.
func estimateVisibility(s string) int {
	switch s {
	case "Good":
		return 10000
	case "Moderate":
		return 5000
	case "Poor":
		return 500
	default:
		return 0
	}
}

func parseMetWeather(wp WeatherParams) Weather {
	var w Weather

	w.airquality = wp.AQIndex
	w.feelsliketempDegC = wp.F
	w.humidity = wp.H
	w.pressure = wp.P
	w.precipitationPerc = wp.PP
	w.temperatureDegC = wp.T
	w.maxuv = wp.UV
	w.visibilityMetres = wp.V
	w.weathertype = wp.WT
	w.winddir = wp.WD
	w.windgustMph = wp.WG
	w.windspeedMph = wp.WS * mpsToMphMultiplier

	return w
}

func processMet(b []byte) []Weather {
	var r MetResponse
	var weather []Weather
	var w Weather
	err := json.Unmarshal(b, &r)
	if err != nil {
		log.Fatal(err)
	}

	for _, d := range r.BestFcst.Forecast.Location.Days {
		w = parseMetWeather(d.DayValues.WeatherParameters)
		w.date = d.Date
		w.time = "Day     "
		weather = append(weather, w)

		w = parseMetWeather(d.NightValues.WeatherParameters)
		w.date = d.Date
		w.time = "Night   "
		weather = append(weather, w)

		for _, t := range d.TimeSteps.TimeStep {
			w = parseMetWeather(t.WeatherParameters)
			w.date = d.Date
			w.time = t.Time
			weather = append(weather, w)
		}
	}
	return weather
}

func main() {
	var err error
	var id string
	var url string
	var parsefunc func([]byte) []Weather
	var resp *http.Response
	var weather []Weather

	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	flag.Parse()

	switch *src {
	case "bbc":
		id = bbcdefid
		url = bbcurl
		parsefunc = processBBC
	case "metoffice":
		id = metdefid
		url = meturl
		parsefunc = processMet
	default:
		log.Fatalf("data source %s not supported; use either 'bbc' or 'metoffice'\n", *src)
	}

	if flag.NArg() > 0 {
		id = flag.Arg(0)
	}

	resp, err = http.Get(fmt.Sprintf(url, id))
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		log.Fatalf("HTTP status code: %d\n", resp.StatusCode)
	}
	b, err := ioutil.ReadAll(resp.Body)

	weather = parsefunc(b)

	for _, w := range weather {
		fmt.Printf("%s %s  ", w.date, w.time)
		desc, ok := TypeDescription[w.weathertype]
		if !ok {
			desc = fmt.Sprintf("%d", w.weathertype)
		}
		fmt.Printf("%18s, Temp: %4.1f°C, ", desc, w.temperatureDegC)
		fmt.Printf("Rain: %2d%%, Wind: %4.1fmph\n", w.precipitationPerc, w.windspeedMph)
		if *verbose {
			fmt.Printf("%+v\n\n", w)
		}
	}
}