-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.go
383 lines (367 loc) · 14.7 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
package cormorant
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/innotechdevops/openmeteo"
"github.com/tymasconfederation/cormorant/pb"
"google.golang.org/protobuf/proto"
)
// weatherCodeName is a map for turning weather codes returned by open-meteo into emojis and brief descriptions.
var weatherCodeName map[int]string = map[int]string{
0: ":sun: Clear sky",
1: ":white_sun_small_cloud: Mainly clear",
2: ":white_sun_cloud: Partly cloudy",
3: ":cloud: Overcast",
45: ":fog: Fog",
48: ":fog: Depositing rime fog",
51: ":cloud_rain: Light drizzle",
53: ":cloud_rain: Moderate drizzle",
55: ":cloud_rain: Dense drizzle",
56: ":cloud_rain: Light freezing drizzle",
57: ":cloud_rain: Dense freezing drizzle",
61: ":cloud_rain: Slight rain",
63: ":cloud_rain: Moderate rain",
65: ":cloud_rain: Heavy rain",
66: ":cloud_rain: Light freezing rain",
67: ":cloud_rain: Heavy freezing rain",
71: ":cloud_snow: Slight snowfall",
73: ":cloud_snow: Moderate snowfall",
75: ":cloud_snow: Heavy snowfall",
77: ":cloud_snow: Snow grains",
80: ":cloud_rain: Slight rain showers",
81: ":cloud_rain: Moderate rain showers",
82: ":cloud_rain: Violent rain showers",
85: ":cloud_snow: Slight snow showers",
86: ":cloud_snow: Heavy snow showers",
95: ":thunder_cloud_rain: Thunderstorm",
96: ":thunder_cloud_rain: Thunderstorm with slight hail",
99: ":thunder_cloud_rain: Thunderstorm with heavy hail",
}
// ForecastType indicates what type of forecast the user wants.
type ForecastType int
// Constants representing which forecast the user is requesting
const (
CurrentForecast ForecastType = iota // The current conditions
TodayForecast // Today's forecast
WeekForecast // The forecast for the next week
)
// Geocode calls the open-meteo geocoding API to get information a postal code or place name,
// and returns a struct containing that information unless an error occurred or we failed to find anything.
// If extractState is true, it splits the place string at a "," if one exists, searches for the left hand side,
// and looks through the results to try to find one whose geo.Admin1 matches the right hand side.
func Geocode(place string, extractState bool) (ret *pb.GeocodingApi_Geoname, err error) {
state := ""
stateL := ""
count := 1
if extractState {
splitStr := strings.Split(place, ",")
if len(splitStr) > 1 {
place = strings.Trim(splitStr[0], " ")
state = strings.Trim(splitStr[1], " ")
stateL = strings.ToLower(state)
count = 50
}
}
uri := fmt.Sprintf("https://geocoding-api.open-meteo.com/v1/search?name=%v&count=%v&language=en&format=protobuf", url.QueryEscape(place), count)
var resp []byte
if resp, err = call(uri); err == nil {
var msg *pb.GeocodingApi_SearchResults = &pb.GeocodingApi_SearchResults{}
if err = proto.Unmarshal(resp, msg); err == nil {
results := msg.GetResults()
if len(results) == 0 {
err = fmt.Errorf("Unable to find `%v`", place)
} else if len(results) >= 1 {
if extractState {
for _, r := range results {
if strings.ToLower(r.Admin1) == stateL {
ret = r
break
}
}
if ret == nil {
err = fmt.Errorf("Unable to find `%v, %v`", place, state)
}
} else {
ret = results[0]
}
}
}
}
return
}
// Forecast returns a forecast for a location, or an error if something goes wrong. The forecast parameter specifies the type of forecast we want.
func Forecast(place string, forecast ForecastType) (ret string, err error) {
var geo *pb.GeocodingApi_Geoname
if geo, err = Geocode(place, false); err != nil {
geo, err = Geocode(place, true)
}
if err == nil {
if geo == nil {
err = fmt.Errorf("Geocode(%v) returned nil for both geo and err.", place)
} else {
switch forecast {
case CurrentForecast:
param := openmeteo.Parameter{
Latitude: openmeteo.Float32(geo.Latitude),
Longitude: openmeteo.Float32(geo.Longitude),
Elevation: openmeteo.Float32(geo.Elevation),
Timezone: openmeteo.String(geo.Timezone),
Daily: &[]string{
openmeteo.DailySunrise,
openmeteo.DailySunset,
},
CurrentWeather: openmeteo.Bool(true),
}
m := openmeteo.New()
var resp string
if resp, err = m.Execute(param); err == nil {
var respMap map[string]interface{} = nil
if err = json.Unmarshal([]byte(resp), &respMap); err == nil {
curWeather := respMap["current_weather"].(map[string]interface{})
curTemp := curWeather["temperature"].(float64)
curWindspeed := curWeather["windspeed"].(float64)
// curWindDir := curWeather["winddirection"].(float64)
weatherCode := curWeather["weathercode"].(float64)
dailyWeather := respMap["daily"].(map[string]interface{})
sunrise := dailyWeather["sunrise"].([]interface{})[0].(string)
sunset := dailyWeather["sunset"].([]interface{})[0].(string)
idx := strings.IndexRune(sunrise, 'T')
if idx > -1 {
sunrise = sunrise[idx+1:]
}
idx = strings.IndexRune(sunset, 'T')
if idx > -1 {
sunset = sunset[idx+1:]
}
curTempF := Fahrenheit(curTemp)
weatherCodeStr := weatherCodeName[int(weatherCode)]
curWindspeedMph := Mph(curWindspeed)
fmt.Printf("Weather code %v, weatherCodeStr %v\n", int(weatherCode), weatherCodeStr)
ret = fmt.Sprintf("Current conditions for %v, %v, %v:\n%v\n"+
":thermometer: Temperature: Currently %0.1f°F (%0.1f°C).\n"+
":dash: Wind: %0.2f MPH (%0.2f km/h)\n"+
":sunrise: Sunrise at %v\n"+
":city_dusk: Sunset at %v",
geo.Name, geo.Admin1, geo.Country, weatherCodeStr, curTempF, curTemp,
curWindspeedMph, curWindspeed, sunrise, sunset)
}
}
case TodayForecast:
param := openmeteo.Parameter{
Latitude: openmeteo.Float32(geo.Latitude),
Longitude: openmeteo.Float32(geo.Longitude),
Elevation: openmeteo.Float32(geo.Elevation),
Timezone: openmeteo.String(geo.Timezone),
Daily: &[]string{
openmeteo.DailyTemperature2mMin,
openmeteo.DailyTemperature2mMax,
openmeteo.DailyApparentTemperatureMin,
openmeteo.DailyApparentTemperatureMax,
openmeteo.DailyRainSum,
openmeteo.DailyShowersSum,
openmeteo.DailySnowfallSum,
openmeteo.DailyPrecipitationProbabilityMean,
openmeteo.DailySunrise,
openmeteo.DailySunset,
},
CurrentWeather: openmeteo.Bool(true),
}
m := openmeteo.New()
var resp string
if resp, err = m.Execute(param); err == nil {
var respMap map[string]interface{} = nil
if err = json.Unmarshal([]byte(resp), &respMap); err == nil {
curWeather := respMap["current_weather"].(map[string]interface{})
curTemp := curWeather["temperature"].(float64)
curWindspeed := curWeather["windspeed"].(float64)
// curWindDir := curWeather["winddirection"].(float64)
weatherCode := curWeather["weathercode"].(float64)
dailyWeather := respMap["daily"].(map[string]interface{})
minTemp := dailyWeather["temperature_2m_min"].([]interface{})[0].(float64)
maxTemp := dailyWeather["temperature_2m_max"].([]interface{})[0].(float64)
minTempApparent := dailyWeather["apparent_temperature_min"].([]interface{})[0].(float64)
maxTempApparent := dailyWeather["apparent_temperature_max"].([]interface{})[0].(float64)
rainSum := dailyWeather["rain_sum"].([]interface{})[0].(float64)
showersSum := dailyWeather["showers_sum"].([]interface{})[0].(float64)
snowfallSum := dailyWeather["snowfall_sum"].([]interface{})[0].(float64)
precipChance := dailyWeather["precipitation_probability_mean"].([]interface{})[0].(float64)
sunrise := dailyWeather["sunrise"].([]interface{})[0].(string)
sunset := dailyWeather["sunset"].([]interface{})[0].(string)
idx := strings.IndexRune(sunrise, 'T')
if idx > -1 {
sunrise = sunrise[idx+1:]
}
idx = strings.IndexRune(sunset, 'T')
if idx > -1 {
sunset = sunset[idx+1:]
}
curTempF := Fahrenheit(curTemp)
minTempF := Fahrenheit(minTemp)
maxTempF := Fahrenheit(maxTemp)
minTempApparentF := Fahrenheit(minTempApparent)
maxTempApparentF := Fahrenheit(maxTempApparent)
weatherCodeStr := weatherCodeName[int(weatherCode)]
curWindspeedMph := Mph(curWindspeed)
precipStr := ""
if precipChance > 0.1 {
precipStr = fmt.Sprintf(":8ball: %0.0f%% chance of precipitation", precipChance)
rainSumI := Inches(rainSum)
showersSumI := Inches(showersSum)
snowfallSumI := Inches(snowfallSum)
if rainSum > 0 {
precipStr = fmt.Sprintf("%v | :umbrella: %0.2f inches / %0.2f mm rain", precipStr, rainSumI, rainSum)
}
if showersSum > 0 {
precipStr = fmt.Sprintf("%v | :droplet: %0.2f inches / %0.2f mm showers", precipStr, showersSumI, showersSum)
}
if snowfallSum > 0 {
precipStr = fmt.Sprintf("%v | :snowflake: %0.2f inches / %0.2f mm snowfall", precipStr, snowfallSumI, snowfallSum)
}
precipStr = fmt.Sprintf("%v\n", precipStr)
}
ret = fmt.Sprintf("Weather for %v, %v, %v:\n%v\n"+
":thermometer: Temperature: Currently %0.1f°F (%0.1f°C).\n"+
":arrow_down: Low of %0.1f°F (%0.1f°C), apparent %0.1f°F (%0.1f°C).\n"+
":arrow_up: High of %0.1f°F (%0.1f°C), apparent %0.1f°F (%0.1f°C)\n"+
"%v"+
":dash: Wind: %0.2f MPH (%0.2f km/h)\n"+
":sunrise: Sunrise at %v\n"+
":city_dusk: Sunset at %v",
geo.Name, geo.Admin1, geo.Country, weatherCodeStr, curTempF, curTemp, minTempF, minTemp,
minTempApparentF, minTempApparent, maxTempF, maxTemp, maxTempApparentF, maxTempApparent, precipStr,
curWindspeedMph, curWindspeed, sunrise, sunset)
}
}
case WeekForecast:
param := openmeteo.Parameter{
Latitude: openmeteo.Float32(geo.Latitude),
Longitude: openmeteo.Float32(geo.Longitude),
Elevation: openmeteo.Float32(geo.Elevation),
Timezone: openmeteo.String(geo.Timezone),
Daily: &[]string{
openmeteo.DailyWeatherCode,
openmeteo.DailyTemperature2mMin,
openmeteo.DailyTemperature2mMax,
// openmeteo.DailyApparentTemperatureMin,
// openmeteo.DailyApparentTemperatureMax,
openmeteo.DailyRainSum,
openmeteo.DailyShowersSum,
openmeteo.DailySnowfallSum,
openmeteo.DailyPrecipitationProbabilityMean,
openmeteo.DailySunrise,
openmeteo.DailySunset,
},
}
m := openmeteo.New()
var resp string
if resp, err = m.Execute(param); err == nil {
var respMap map[string]interface{} = nil
if err = json.Unmarshal([]byte(resp), &respMap); err == nil {
dailyWeather := respMap["daily"].(map[string]interface{})
dates := dailyWeather["time"].([]interface{})
weatherCode := dailyWeather["weathercode"].([]interface{})
minTemp := dailyWeather["temperature_2m_min"].([]interface{})
maxTemp := dailyWeather["temperature_2m_max"].([]interface{})
// minTempApparent := dailyWeather["apparent_temperature_min"].([]interface{})
// maxTempApparent := dailyWeather["apparent_temperature_max"].([]interface{})
rainSum := dailyWeather["rain_sum"].([]interface{})
showersSum := dailyWeather["showers_sum"].([]interface{})
snowfallSum := dailyWeather["snowfall_sum"].([]interface{})
precipChance := dailyWeather["precipitation_probability_mean"].([]interface{})
sunrise := dailyWeather["sunrise"].([]interface{})
sunset := dailyWeather["sunset"].([]interface{})
ret = "" // "Day | Weather | Low | High | % Precip | Total rain | Total showers | Total snow"
for day := 0; day < len(weatherCode); day++ {
dateStr := dates[day].(string)
var dayStr = "Today"
if day == 1 {
dayStr = "Tomorrow"
} else if day > 1 {
if t, err := time.Parse(time.DateOnly, dateStr); err == nil {
dayStr = t.Weekday().String()
} else {
dayStr = extractErrorMessage(err)
fmt.Printf("Error calling time.Parse(DateOnly, \"%v\": %v\n", dateStr, extractErrorMessage(err))
}
}
weatherCodeD := weatherCode[day].(float64)
minTempD := minTemp[day].(float64)
maxTempD := maxTemp[day].(float64)
// minTempApparentD := minTempApparent[day].(float64)
// maxTempApparentD := maxTempApparent[day].(float64)
rainSumD := rainSum[day].(float64)
showersSumD := showersSum[day].(float64)
snowfallSumD := snowfallSum[day].(float64)
precipChanceD := precipChance[day].(float64)
sunriseD := sunrise[day].(string)
sunsetD := sunset[day].(string)
minTempF := Fahrenheit(minTempD)
maxTempF := Fahrenheit(maxTempD)
// minTempApparentF := Fahrenheit(minTempApparentD)
// maxTempApparentF := Fahrenheit(maxTempApparentD)
weatherCodeStr := weatherCodeName[int(weatherCodeD)]
rainSumI, showersSumI, snowfallSumI := 0.0, 0.0, 0.0
if precipChanceD > 0.1 {
rainSumI = Inches(rainSumD)
showersSumI = Inches(showersSumD)
snowfallSumI = Inches(snowfallSumD)
}
idx := strings.IndexRune(sunriseD, 'T')
if idx > -1 {
sunriseD = sunriseD[idx+1:]
}
idx = strings.IndexRune(sunsetD, 'T')
if idx > -1 {
sunsetD = sunsetD[idx+1:]
}
// Day | Weather | Low | High | % Precip | Total rain | Total showers | Total snow
rainStr := ""
showersStr := ""
snowStr := ""
if rainSumD > 0 {
rainStr = fmt.Sprintf(" | :umbrella: %0.2f in (%0.2f mm) rain", rainSumI, rainSumD)
}
if showersSumD > 0 {
showersStr = fmt.Sprintf(" | :droplet: %0.2f in (%0.2f mm) showers", showersSumI, showersSumD)
}
if snowfallSumD > 0 {
snowStr = fmt.Sprintf(" | :snowflake: %0.2f in (%0.2f mm) snowfall", snowfallSumI, snowfallSumD)
}
ret = fmt.Sprintf("%v\n"+
"%v: %v | :arrow_down: Low %0.1f°F (%0.1f°C) | :arrow_up: High %0.1f°F (%0.1f°C) | :8ball: %0.0f%% chance of precipitation%v%v%v", ret,
dayStr, weatherCodeStr, minTempF, minTempD, maxTempF, maxTempD,
precipChanceD, rainStr, showersStr, snowStr) // rainSumI, rainSumD, showersSumI, showersSumD, snowfallSumI, snowfallSumD)
}
}
}
}
}
}
return
}
// call uses HTTP get to call into an API endpoint and returns the body of the response unless an error occurred.
func call(uri string) (ret []byte, err error) {
var resp *http.Response
if resp, err = http.Get(uri); err == nil {
defer resp.Body.Close()
ret, err = io.ReadAll(resp.Body)
}
return
}
// Fahrenheit converts celsius to fahrenheit
func Fahrenheit(celsius float64) float64 {
return celsius*1.8 + 32.0
}
// Inches converts millimeters to inches
func Inches(mm float64) float64 {
return mm / 25.4
}
// Mph converts km/h to MPH
func Mph(kmPerH float64) float64 {
return kmPerH / 1.609344
}