-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoaa.go
112 lines (86 loc) · 2.9 KB
/
noaa.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
package noaa
import (
"encoding/csv"
"io"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
func request(url string) []byte {
response, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
return body
}
func realtimeMeteorological(url string) []MeteorologicalObservation {
body := request(url)
r := csv.NewReader(strings.NewReader(string(body)))
r.FieldsPerRecord = 0
r.TrimLeadingSpace = true
var mos []MeteorologicalObservation
for {
var mo MeteorologicalObservation
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
// TODO: refactor parsing approach
row := record[0]
trimmed := strings.TrimSpace(row)
singleSpacePattern := regexp.MustCompile(`\s+`)
rowValues := strings.Split(singleSpacePattern.ReplaceAllString(trimmed, " "), " ")
year, _ := strconv.ParseInt(rowValues[0], 10, 16)
month, _ := strconv.ParseInt(rowValues[1], 10, 16)
day, _ := strconv.ParseInt(rowValues[2], 10, 16)
hour, _ := strconv.ParseInt(rowValues[3], 10, 16)
minute, _ := strconv.ParseInt(rowValues[4], 10, 16)
mo.Datetime = time.Date(int(year), time.Month(month), int(day), int(hour), int(minute), 0, 0, time.UTC)
windDirValue, _ := strconv.ParseFloat(rowValues[5], 32)
mo.WindDirection = int16(windDirValue)
windSpeedValue, _ := strconv.ParseFloat(rowValues[6], 32)
mo.WindSpeed = float32(windSpeedValue)
windGustValue, _ := strconv.ParseFloat(rowValues[7], 32)
mo.WindGust = float32(windGustValue)
waveHeightValue, _ := strconv.ParseFloat(rowValues[8], 32)
mo.WaveHeight = float32(waveHeightValue)
dominantWavePeriodValue, _ := strconv.ParseFloat(rowValues[9], 32)
mo.DominantWavePeriod = float32(dominantWavePeriodValue)
averageWavePeriodValue, _ := strconv.ParseFloat(rowValues[10], 32)
mo.AverageWavePeriod = float32(averageWavePeriodValue)
waveDirectionValue, _ := strconv.ParseFloat(rowValues[11], 32)
mo.WaveDirection = int16(waveDirectionValue)
seaLevelPresValue, _ := strconv.ParseFloat(rowValues[12], 32)
mo.SeaLevelPressure = float32(seaLevelPresValue)
airTempValue, _ := strconv.ParseFloat(rowValues[13], 32)
mo.AirTemperature = float32(airTempValue)
waterTempValue, _ := strconv.ParseFloat(rowValues[14], 32)
mo.WaterTemperature = float32(waterTempValue)
dewpointTempValue, _ := strconv.ParseFloat(rowValues[15], 32)
mo.DewpointTemperature = float32(dewpointTempValue)
visibilityValue, _ := strconv.ParseFloat(rowValues[16], 32)
mo.Visibility = float32(visibilityValue)
pressureTendencyVal, _ := strconv.ParseFloat(rowValues[17], 32)
mo.PressureTendency = float32(pressureTendencyVal)
tideVal, _ := strconv.ParseFloat(rowValues[18], 32)
mo.Tide = float32(tideVal)
mos = append(mos, mo)
}
return mos
}