/
weather.go
133 lines (116 loc) · 3.29 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
package weather
import (
"code.google.com/p/go-charset/charset"
"github.com/jteeuwen/go-pkg-xmlx"
"fmt"
"go/build"
"math"
"time"
)
const (
url = "http://forecast.weather.gov/MapClick.php?lat=30.27&lon=-97.74&FcstType=digitalDWML"
arrows = "↑↗→↘↓↙←↖"
firstOctile = 0x2581
maxHours = 48
)
func Forecast() string {
charset.CharsetDir = build.Default.GOPATH + "/src/code.google.com/p/go-charset/datafiles"
doc := xmlx.New()
err := doc.LoadUri(url, charset.NewReader)
if err != nil {
return ""
}
startTimeNodes := doc.SelectNodes("", "start-valid-time")
endTimeNodes := doc.SelectNodes("", "end-valid-time")
if len(startTimeNodes) == 0 || len(endTimeNodes) == 0 {
return ""
}
if len(endTimeNodes) > maxHours {
endTimeNodes = endTimeNodes[:maxHours]
}
startTime, _ := time.Parse(time.RFC3339, startTimeNodes[0].Value)
endTime, _ := time.Parse(time.RFC3339, endTimeNodes[len(endTimeNodes)-1].Value)
temps := findVals("temperature", "hourly", doc)
humids := findVals("humidity", "", doc)
precips := findVals("probability-of-precipitation", "", doc)
speeds := findVals("wind-speed", "sustained", doc)
dirs := findVals("direction", "", doc)
minTemp, maxTemp, tempGraph := makeGraph(temps)
minHumid, maxHumid, humidGraph := makeGraph(humids)
minPrecip, maxPrecip, precipGraph := makeGraph(precips)
minSpeed, maxSpeed, speedGraph := makeGraph(speeds)
dirGraph := ""
for _, dir := range dirs {
idx := dirIndex(dir)
dirGraph += string([]rune(arrows)[idx])
}
timeFmt := "2006-01-02 15:04"
start, end := startTime.Format(timeFmt), endTime.Format(timeFmt)
tempRange := fmt.Sprintf("%3d %3d", minTemp, maxTemp)
humidRange := fmt.Sprintf("%3d %3d", minHumid, maxHumid)
precipRange := fmt.Sprintf("%3d %3d", minPrecip, maxPrecip)
speedRange := fmt.Sprintf("%3d %3d", minSpeed, maxSpeed)
out := fmt.Sprintf(" min max %-24s%24s\n", start, end)
out += fmt.Sprintf("Temp °F %7s %s\n", tempRange, tempGraph)
out += fmt.Sprintf("Humid %% %7s %s\n", humidRange, humidGraph)
out += fmt.Sprintf("Precip %% %7s %s\n", precipRange, precipGraph)
out += fmt.Sprintf("Wind mph %7s %s\n", speedRange, speedGraph)
out += fmt.Sprintf("Wind dir %s\n", dirGraph)
return out
}
func minmax(vals []int) (int, int) {
min, max := math.MaxInt32, math.MinInt32
for _, v := range vals {
if v < min {
min = v
}
if v > max {
max = v
}
}
return min, max
}
func rescale(val, min, max, bins int) int {
if min >= max {
return 0
}
span := max - min
v := (val - min) * bins / span
if v < 0 {
v = 0
} else if v > bins-1 {
v = bins - 1
}
return v
}
func dirIndex(dir int) int {
return ((dir + 360/16) * 8 / 360) % 8
}
func findVals(name, typ string, doc *xmlx.Document) []int {
vals := []int{}
nodes := doc.SelectNodes("", name)
for _, node := range nodes {
if typ == "" || node.As("", "type") == typ {
for _, kid := range node.Children {
vals = append(vals, kid.I("", "value"))
if len(vals) >= maxHours {
break
}
}
break // just use the first set
}
}
return vals
}
func makeGraph(vals []int) (int, int, string) {
if len(vals) == 0 {
return 0, 0, ""
}
graph := ""
min, max := minmax(vals)
for _, val := range vals {
octile := rescale(val, min, max, 8)
graph += string(firstOctile + octile)
}
return min, max, graph
}