forked from ebarkie/aprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wx.go
168 lines (143 loc) · 3.87 KB
/
wx.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
// Copyright (c) 2016 Eric Barkie. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package aprs
import (
"fmt"
"math"
"time"
"github.com/ebarkie/weatherlink/units"
)
// Wx represents a weather station observation.
type Wx struct {
Lat float64
Lon float64
Type string
Timestamp time.Time
Altimeter float64
Humidity int
RainLastHour float64
RainLast24Hours float64
RainToday float64
SolarRad int
Temp int
WindDir int
WindGust int
WindSpeed int
}
// Zero zeroes all measurements in the observation payload.
func (w *Wx) Zero() {
w.Timestamp = time.Time{}
w.Altimeter = 0
w.Humidity = -1
w.RainLastHour = -1.0
w.RainLast24Hours = -1.0
w.RainToday = -1.0
w.SolarRad = -1
w.Temp = -100
w.WindDir = -1
w.WindGust = -1
w.WindSpeed = -1
}
// decToDMS takes a float latitude or longitude and converts it to
// degrees, minutes (second as decimal), and a hemisphere string.
func decToDMS(l float64, hems [2]string) (float64, float64, string) {
deg, frac := math.Modf(math.Abs(l))
min := frac * 60.0
if l < 0 {
return deg, min, hems[1]
}
return deg, min, hems[0]
}
// String returns an APRS packet for the provided measurements.
func (w Wx) String() (s string) {
// Refer to APRS Protocol Reference 1.0
// Chapter 12: Weather Reports
//
// Parameters:
// _ = wind direction (in degrees) [3 chars]
// / = sustained one-minute wind speed (in mph) [3 chars]
// g = gust (peak wind speed in mph in the last 5 minutes) [3 chars]
// t = temperature (in degrees Fahrenheit). Temperatures below zero
// are expressed as -01 to -99 [3 chars]
// r = rainfall (in hundredths of an inch) in the last hour [3 chars]
// p = rainfall (in hundredths of an inch) in the last 24 hours [3 chars]
// P = rainfall (in hundredths of an inch) since midnight [3 chars]
// h = humidity (in %. 00 = 100%) [2 chars]
// b = barometric altimeter pressure (in tenths of millibars/tenths of
// hPascal) [4 chars]
//
// L = luminosity (in watts per square meter) 999 and below [3 chars]
// l = luminosity (in watts per square meter) 1000 and above [3 chars]
// (L is inserted in place of one of the rain values)
// s = snowfall (in inches) in the last 24 hours
// # = raw rain counter
// Timestamp as UTC two digit day, hour, and minute.
if w.Timestamp.IsZero() {
w.Timestamp = time.Now()
}
// Base prefix
latDeg, latMin, latHem := decToDMS(w.Lat, [2]string{"N", "S"})
lonDeg, lonMin, lonHem := decToDMS(w.Lon, [2]string{"E", "W"})
s = fmt.Sprintf("@%sz%02.0f%02.2f%s/%03.0f%02.2f%s",
w.Timestamp.In(time.UTC).Format("021504"),
latDeg, latMin, latHem,
lonDeg, lonMin, lonHem)
// Parameters
if w.WindDir < 0 {
s += "_..."
} else {
s += fmt.Sprintf("_%03d", w.WindDir)
}
if w.WindSpeed < 0 {
s += "/..."
} else {
s += fmt.Sprintf("/%03d", w.WindSpeed)
}
if w.WindGust < 0 {
s += "g..."
} else {
s += fmt.Sprintf("g%03d", w.WindGust)
}
if w.Temp < -99 {
s += "t..."
} else {
s += fmt.Sprintf("t%03d", w.Temp)
}
if w.RainLastHour < 0.0 {
s += "r..."
} else {
s += fmt.Sprintf("r%03.0f", w.RainLastHour*100.0)
}
if w.RainLast24Hours < 0.0 {
s += "p..."
} else {
s += fmt.Sprintf("p%03.0f", w.RainLast24Hours*100.0)
}
if w.RainToday < 0.0 {
s += "P..."
} else {
s += fmt.Sprintf("P%03.0f", w.RainToday*100.0)
}
if w.Humidity < 0 {
s += "h.."
} else {
s += fmt.Sprintf("h%02d", w.Humidity%100)
}
if w.Altimeter <= 0.0 {
s += "b....."
} else {
s += fmt.Sprintf("b%05.0f", units.Pressure(w.Altimeter*units.Inches).Millibars()*10.0)
}
if w.SolarRad >= 1000 {
s += fmt.Sprintf("l%03d", w.SolarRad-1000)
} else if w.SolarRad >= 0 {
s += fmt.Sprintf("L%03d", w.SolarRad)
}
// Software
s += SwName + SwVers
if w.Type != "" {
s += fmt.Sprintf("-%s", w.Type)
}
return
}