forked from ajstarks/conditions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
306 lines (271 loc) · 10.3 KB
/
history.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
/*
* history.go
*
* This file is part of wu. It contains functions related to
* the --history and --yesterday switches (historical data).
*
* Written and maintained by Stephen Ramsay <sramsay.unl@gmail.com>
* and Anthony Starks.
*
* Last Modified: Wed Dec 18 16:11:39 CST 2013
*
* Copyright © 2010-2014 by Stephen Ramsay and Anthony Starks.
*
* wu is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* wu is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wu; see the file COPYING. If not see
* <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"math"
"os"
"strconv"
)
type HistoryConditions struct {
History History
}
type History struct {
Date Date // Defined in wu.go
Observations []Observations
Dailysummary []Dailysummary
}
type Observations struct {
}
type Dailysummary struct {
Fog string
Rain string
Snow string
Snowfallm string
Snowfalli string
Monthtodatesnowfallm string
Monthtodatesnowfalli string
Since1julsnowfallm string
Since1julsnowfalli string
Snowdepthm string
Snowdepthi string
Hail string
Thunder string
Tornado string
Meantempm string
Meantempi string
Meandewptm string
Meandewpti string
Meanpressurem string
Meanpressurei string
Meanwindspdm string
Meanwindspdi string
Meanwdire string
Meanwdird string
Meanvism string
Meanvisi string
Humidity string
Maxtempm string
Maxtempi string
Mintempm string
Mintempi string
Maxhumidity string
Minhumidity string
Maxdewptm string
Maxdewpti string
Mindewptm string
Mindewpti string
Maxpressurem string
Maxpressurei string
Minpressurem string
Minpressurei string
Maxwspdm string
Maxwspdi string
Minwspdm string
Minwspdi string
Maxvism string
Maxvisi string
Minvism string
Minvisi string
Gdegreedays string
Heatingdegreedays string
Coolingdegreedays string
Precipm string
Precipi string
Heatingdegreedaysnormal string
Monthtodateheatingdegreedays string
Monthtodateheatingdegreedaysnormal string
Since1sepheatingdegreedays string
Since1sepheatingdegreedaysnormal string
Since1julheatingdegreedays string
Since1julheatingdegreedaysnormal string
Coolingdegreedaysnormal string
Monthtodatecoolingdegreedays string
Monthtodatecoolingdegreedaysnormal string
Since1sepcoolingdegreedays string
Since1sepcoolingdegreedaysnormal string
Since1jancoolingdegreedays string
Since1jancoolingdegreedaysnormal string
}
func PrintHistory(obs *HistoryConditions, stationId string) {
if len(obs.History.Observations) == 0 {
fmt.Println("No data available for specified date")
os.Exit(0)
}
history := obs.History.Dailysummary[0]
fmt.Printf("Weather summary for %s: ", obs.History.Date.Pretty)
if history.Fog == "1" {
fmt.Print("fog ")
}
if history.Rain == "1" {
fmt.Print("rain ")
}
if history.Snow == "1" {
fmt.Print("snow ")
}
if history.Hail == "1" {
fmt.Print("hail ")
}
if history.Tornado == "1" {
fmt.Print("tornado ")
}
fmt.Print("\n")
// if "month to date" is nil, it likely means that the station
// doesn't report full almanac information (which is frequently
// the case for non-U.S (NWS) station sources. This may be the
// case for several measurements in this section.
// Snow
if history.Snow == "1" && history.Monthtodatesnowfalli != "" {
fmt.Println(" Snow:")
if history.Snowfalli == "T" {
fmt.Println(" trace")
} else if history.Snowfalli >= "0.00" {
fmt.Printf(" %s in (%s mm)\n", history.Snowfalli, history.Snowfallm)
fmt.Printf(" Snow depth: %s in (%s mm)\n", history.Snowdepthi, history.Snowdepthm)
fmt.Printf(" Month to date: %s in (%s mm)\n", history.Monthtodatesnowfalli, history.Monthtodatesnowfallm)
fmt.Printf(" Since July 1st: %s in (%s mm)\n", history.Since1julsnowfalli, history.Since1julsnowfallm)
}
}
// Precipitation
if history.Rain == "1" {
if history.Precipi == "T" {
fmt.Printf(" Precipitation: trace\n")
} else {
fmt.Printf(" Precipitation: %s in (%s mm)\n", history.Precipi, history.Precipm)
}
}
// Temperature
fmt.Println(" Temperature:")
fmt.Printf(" Mean Temperature: %s F (%s C)\n", history.Meantempi, history.Meantempm)
fmt.Printf(" Max Temperature: %s F (%s C)\n", history.Maxtempi, history.Maxtempm)
fmt.Printf(" Min Temperature: %s F (%s C)\n", history.Mintempi, history.Mintempm)
// Degree Days
fmt.Println(" Degree Days:")
if history.Heatingdegreedays != "" {
fmt.Print(" Heating Degree Days: " + history.Heatingdegreedays)
if history.Heatingdegreedaysnormal != "" {
fmt.Printf(" (%s days normal)\n", history.Heatingdegreedaysnormal)
}
if history.Heatingdegreedaysnormal != "" && history.Heatingdegreedaysnormal != "0" {
fmt.Printf(" HDG month to date: %s (%s days normal)\n", history.Monthtodateheatingdegreedays, history.Monthtodateheatingdegreedaysnormal)
if history.Since1julheatingdegreedaysnormal == "" {
fmt.Printf(" HDG since Sept 1st: %s (%s days normal)\n", history.Since1sepheatingdegreedays, history.Since1sepheatingdegreedaysnormal)
} else {
fmt.Printf(" HDG since July 1st: %s (%s days normal)\n", history.Since1julheatingdegreedays, history.Since1julheatingdegreedaysnormal)
}
} else {
fmt.Print("\n")
}
}
if history.Coolingdegreedaysnormal != "" && history.Coolingdegreedaysnormal != "0" {
fmt.Print(" Cooling Degree Days: " + history.Coolingdegreedays)
if history.Coolingdegreedaysnormal != "" {
fmt.Printf(" (%s days normal)\n", history.Coolingdegreedaysnormal)
} else {
fmt.Print("\n")
}
if history.Coolingdegreedaysnormal != "" {
fmt.Printf(" CDG month to date: %s (%s days normal)\n", history.Monthtodatecoolingdegreedays, history.Monthtodatecoolingdegreedaysnormal)
if history.Since1jancoolingdegreedaysnormal == "" {
fmt.Printf(" CDG since Sept 1st: %s (%s days normal)\n", history.Since1sepcoolingdegreedays, history.Since1sepcoolingdegreedaysnormal)
} else {
fmt.Printf(" CDG since Jan 1st: %s (%s days normal)\n", history.Since1jancoolingdegreedays, history.Since1jancoolingdegreedaysnormal)
}
} else {
fmt.Print("\n")
}
}
// Moisture
fmt.Println(" Moisture:")
fmt.Printf(" Mean Dew Point: %s (%s C)\n", history.Meandewpti, history.Meandewptm)
fmt.Printf(" Max Dew Point: %s (%s C)\n", history.Maxdewpti, history.Maxdewptm)
fmt.Printf(" Min Dew Point: %s (%s C)\n", history.Mindewpti, history.Mindewptm)
if history.Humidity != "" {
fmt.Printf(" Humidity: %s%%\n", history.Humidity)
}
fmt.Printf(" Max Humidity: %s%%\n", history.Maxhumidity)
fmt.Printf(" Min Humidity: %s%%\n", history.Minhumidity)
// Pressure
fmt.Println(" Pressure:")
fmt.Printf(" Mean Pressure: %s in (%s mb)\n", history.Meanpressurei, history.Meanpressurem)
fmt.Printf(" Max Pressure: %s in (%s mb)\n", history.Maxpressurei, history.Maxpressurem)
fmt.Printf(" Min Pressure: %s in (%s mb)\n", history.Minpressurei, history.Minpressurem)
// Wind
fmt.Println(" Wind:")
fmt.Printf(" Mean Wind Speed: %s mph (%s kph)\n", history.Meanwindspdi, history.Meanwindspdm)
fmt.Printf(" Max Wind Speed: %s mph (%s kph)\n", history.Maxwspdi, history.Maxwspdm)
fmt.Printf(" Min Wind Speed: %s mph (%s kph)\n", history.Minwspdi, history.Minwspdm)
boxedPoint := boxCompass(history.Meanwdird)
fmt.Printf(" Mean Wind Direction: %s° (%s)\n", history.Meanwdird, boxedPoint)
// Visibility
fmt.Println(" Visibility:")
fmt.Printf(" Mean Visibility %s mi (%s km)\n", history.Meanvisi, history.Meanvism)
fmt.Printf(" Max Visibility %s mi (%s km)\n", history.Maxvisi, history.Maxvism)
fmt.Printf(" Min Visibility %s mi (%s km)\n", history.Minvisi, history.Minvism)
}
// Convert wind degrees to boxed compass points.
func boxCompass(degreeString string) string {
var direction string
degrees, _ := strconv.ParseFloat(degreeString, 64)
bearing := int(math.Floor((degrees / 22.5) + 0.5))
switch bearing {
case 1:
direction = "NNE"
case 2:
direction = "NE"
case 3:
direction = "ENE"
case 4:
direction = "E"
case 5:
direction = "ESE"
case 6:
direction = "SE"
case 7:
direction = "SSE"
case 8:
direction = "S"
case 9:
direction = "SSW"
case 10:
direction = "SW"
case 11:
direction = "WSW"
case 12:
direction = "W"
case 13:
direction = "WNW"
case 14:
direction = "NW"
case 15:
direction = "NNW"
default:
direction = "N"
}
return direction
}