-
Notifications
You must be signed in to change notification settings - Fork 0
/
observation.go
230 lines (207 loc) · 4.96 KB
/
observation.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
package twweather
import (
"encoding/xml"
"fmt"
"strconv"
"time"
"github.com/MinecraftXwinP/twweather/cwbdata"
)
const ObservationsDataID = "O-A0001-001"
type Observations map[string]*Observation
func (os Observations) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
origin := new(struct {
Locations []Observation `xml:"location"`
})
err := d.DecodeElement(origin, &start)
if err != nil {
return err
}
for _, location := range origin.Locations {
os[location.StationName] = &location
}
return nil
}
type Observation struct {
StationName string
CityName string
CitySN int
TownName string
TownSN int
latitude float64
longitude float64
WeatherElements map[string]interface{}
}
func (o *Observation) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
raw := new(struct {
StationName string `xml:"locationName"`
Latitude float64 `xml:"lat"`
Longitude float64 `xml:"lon"`
WeatherElements []rawWeatherElement `xml:"weatherElement"`
Parameters []struct {
Name string `xml:"parameterName"`
Value string `xml:"parameterValue"`
} `xml:"parameter"`
})
err := d.DecodeElement(raw, &start)
if err != nil {
return err
}
o.StationName = raw.StationName
o.latitude = raw.Latitude
o.longitude = raw.Longitude
// init map
o.WeatherElements = make(map[string]interface{}, 11)
for _, element := range raw.WeatherElements {
o.WeatherElements[element.Name] = element.Value
}
for _, parameter := range raw.Parameters {
switch parameter.Name {
case "CITY":
o.CityName = parameter.Value
break
case "CITY_SN":
i, err := strconv.Atoi(parameter.Value)
if err == nil {
o.CitySN = i
}
break
case "TOWN":
o.TownName = parameter.Value
break
case "TOWN_SN":
i, err := strconv.Atoi(parameter.Value)
if err == nil {
o.TownSN = i
}
break
}
}
return nil
}
func GetObservations(apiKey string) (*Observations, error) {
openData, err := cwbdata.GetOpenData(apiKey, ObservationsDataID)
if err != nil {
return nil, err
}
obs := make(Observations, 10)
err = xml.Unmarshal(openData.DataSet, &obs)
if err != nil {
return nil, err
}
return &obs, nil
}
type rawWeatherElement struct {
Name string
Value interface{}
}
func (rawElement *rawWeatherElement) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
const timeShowFormat = "2006-01-02T15:04:05-07:00"
raw := new(struct {
Name string `xml:"elementName"`
Value string `xml:"elementValue>value"`
})
err := d.DecodeElement(raw, &start)
if err != nil {
return err
}
rawElement.Name = raw.Name
valStr := raw.Value
timeStamp, err := time.Parse(timeShowFormat, valStr)
if err != nil {
f, err := strconv.ParseFloat(valStr, 64)
if err != nil {
return err
}
rawElement.Value = f
} else {
rawElement.Value = timeStamp
}
return nil
}
// testWeatherElementValid tests if there's data for a weather element.
func (s *Observation) testWeatherElementValid(key string) (result bool) {
we, ok := s.WeatherElements[key]
if !ok {
return false
}
switch v := we.(type) {
case float64:
return v != -99
}
return true
}
func (s *Observation) getWeatherElement(key string, humanReadable string) (interface{}, error) {
if !s.testWeatherElementValid(key) {
return nil, fmt.Errorf("no %s data", humanReadable)
}
element, _ := s.WeatherElements[key]
return element, nil
}
func (s *Observation) GetTemperature(celsius bool) (tempture float64, err error) {
const key = "TEMP"
we, err := s.getWeatherElement(key, "temperature")
if err != nil {
tempture = -99
return
}
tempture = we.(float64)
if !celsius {
tempture = tempture*1.8 + 32
}
return
}
func (s *Observation) GetPressure() (hPa float64, err error) {
const key = "PRES"
we, err := s.getWeatherElement(key, "pressure")
if err != nil {
hPa = -99
return
}
hPa = we.(float64)
return
}
// GetHumidity returns percentage of relative humidity.
func (s *Observation) GetHumidity() (rh int, err error) {
const key = "HUMD"
we, err := s.getWeatherElement(key, "humidity")
if err != nil {
rh = -99
return
}
// convert to percentage
rh = int(we.(float64) * 100)
return
}
// GetSunHours returns Sun hours
func (s *Observation) GetSunHours() (hours int, err error) {
const key = "SUN"
we, err := s.getWeatherElement(key, "sun hours")
if err != nil {
hours = -99
return
}
hours = int(we.(float64))
return
}
// GetDailyRainfall returns daily rainfall of the station in millimeters.
func (s *Observation) GetDailyRainfall() (mm float64, err error) {
const key = "H_24R"
we, err := s.getWeatherElement(key, "daily rainfall")
if err != nil {
mm = -99
return
}
mm = we.(float64)
return
}
// GetMaximumWindSpeed returns maximum wind speed in meter per second (m/s).
func (s *Observation) GetMaximumWindSpeed() (speed float64, err error) {
const key = "H_FX"
we, err := s.getWeatherElement(key, "maximum wind speed")
if err != nil {
speed = -99
return
}
speed = we.(float64)
return
}