-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrealtime.go
119 lines (109 loc) · 4.34 KB
/
realtime.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
package noaa
import (
"encoding/json"
"fmt"
"time"
)
// Realtime Datasets
//
// There are nine different data sources:
// - data_spec Raw Spectral Wave Data
// - ocean Oceanographic Data
// - spec Spectral Wave Summary Data
// - supl Supplemental Measurements Data
// - swdir Spectral Wave Data (alpha1)
// - swdir2 Spectral Wave Data (alpha2)
// - swr1 Spectral Wave Data (r1)
// - swr2 Spectral Wave Data (r2)
// - txt Standard Meteorological Data
const (
DATASPEC = "data_spec"
OCEAN = "ocean"
SPEC = "spec"
SUPL = "supl"
SWDIR = "swdir"
SWDIR2 = "swdir2"
SWR1 = "swr1"
SWR2 = "swr2"
TXT = "txt"
)
type MeteorologicalObservation struct {
Datetime time.Time `json:"datetime"`
WindDirection int16 `json:"wind_direction"`
WindSpeed float32 `json:"wind_speed"`
WindGust float32 `json:"wind_gust"`
WaveHeight float32 `json:"wave_height"`
DominantWavePeriod float32 `json:"dominant_wave_period"`
AverageWavePeriod float32 `json:"average_wave_period"`
WaveDirection int16 `json:"wave_direction"`
SeaLevelPressure float32 `json:"sea_level_pressure"`
PressureTendency float32 `json:"pressure_tendency"`
AirTemperature float32 `json:"air_temperature"`
WaterTemperature float32 `json:"water_temperature"`
DewpointTemperature float32 `json:"dewpoint_temperature"`
Visibility float32 `json:"visibility"`
Tide float32 `json:"tide"`
}
type WaveSummaryObservation struct {
Datetime time.Time `json:"datetime"`
SignificantWaveHeight float32 `json:"significant_wave_height"`
SwellHeight float32 `json:"swell_height"`
SwellPeriod float32 `json:"swell_period"`
WindWaveHeight float32 `json:"wind_wave_height"`
WindWavePeriod float32 `json:"wind_wave_period"`
SwellDirection string `json:"swell_direction"`
WindWaveDirection float32 `json:"wind_wave_direction"`
Steepness string `json:"steepness"`
AverageWavePeriod float32 `json:"average_wave_period"`
DominantWaveDirection int16 `json:"dominant_wave_direction"`
}
// Encodes time.Time object to ISODateString
func (o MeteorologicalObservation) MarshalJSON() ([]byte, error) {
// TODO: refactor
metObs := struct {
Datetime string `json:"datetime"`
WindDirection int16 `json:"wind_direction"`
WindSpeed float32 `json:"wind_speed"`
WindGust float32 `json:"wind_gust"`
WaveHeight float32 `json:"wave_height"`
DominantWavePeriod float32 `json:"dominant_wave_period"`
AverageWavePeriod float32 `json:"average_wave_period"`
WaveDirection int16 `json:"wave_direction"`
SeaLevelPressure float32 `json:"sea_level_pressure"`
PressureTendency float32 `json:"pressure_tendency"`
AirTemperature float32 `json:"air_temperature"`
WaterTemperature float32 `json:"water_temperature"`
DewpointTemperature float32 `json:"dewpoint_temperature"`
Visibility float32 `json:"visibility"`
Tide float32 `json:"tide"`
}{
Datetime: o.Datetime.Format((time.RFC3339)),
WindDirection: o.WindDirection,
WindSpeed: o.WindSpeed,
WindGust: o.WindGust,
WaveHeight: o.WaveHeight,
DominantWavePeriod: o.DominantWavePeriod,
AverageWavePeriod: o.AverageWavePeriod,
WaveDirection: o.WaveDirection,
SeaLevelPressure: o.SeaLevelPressure,
PressureTendency: o.PressureTendency,
AirTemperature: o.AirTemperature,
WaterTemperature: o.WaterTemperature,
DewpointTemperature: o.DewpointTemperature,
Visibility: o.Visibility,
Tide: o.Tide,
}
return json.Marshal(metObs)
}
// func (mo MeteorologicalObservation) String() string {
// return fmt.Sprintf("(MeteorologicalObservation datetime=%s, wind_direction=%v, wind_speed=%v, wind_gust=%v)",
// mo.Datetime, mo.WindDirection, mo.WindSpeed, mo.WindGust,
// )
// }
// TODO: add support for other realtime datasets and different return signatures (json, struct)
func GetRealtime(stationId string) string {
url := fmt.Sprintf("%s/%s.%s", Realtime, stationId, TXT)
data := realtimeMeteorological(url)
jsonData, _ := json.Marshal(data)
return string(jsonData)
}