This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindforecast.go
103 lines (81 loc) · 2.72 KB
/
windforecast.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
package surfnerd
import (
"encoding/json"
"io/ioutil"
"time"
)
type WindForecast struct {
Location
Model NOAAModel
ForecastData []WindForecastItem
}
// Converts all of the objects to a given unit system
func (w *WindForecast) ChangeUnits(newUnits UnitSystem) {
if w.Model.Units == newUnits {
return
}
for index, _ := range w.ForecastData {
(&w.ForecastData[index]).ChangeUnits(newUnits)
}
w.Model.Units = newUnits
}
// Convert Forecast object to a json formatted string
func (w *WindForecast) ToJSON() ([]byte, error) {
return json.MarshalIndent(w, "", " ")
}
// Export a Forecast object to json file with a given filename
func (w *WindForecast) ExportAsJSON(filename string) error {
jsonData, jsonErr := w.ToJSON()
if jsonErr != nil {
return jsonErr
}
fileErr := ioutil.WriteFile(filename, jsonData, 0644)
return fileErr
}
// Convert the WindForecast object into a ModelData container. Useful for converting to
// a more plottable format
func (w *WindForecast) ToModelData() *ModelData {
dataCount := len(w.ForecastData)
// Create and initialize the map with the correct variables
dataMap := ModelDataMap{}
dataMap["windSpeed"] = make([]float64, dataCount)
dataMap["windDirection"] = make([]float64, dataCount)
dataMap["windGustSpeed"] = make([]float64, dataCount)
for forcIndex, forecast := range w.ForecastData {
dataMap["windSpeed"][forcIndex] = forecast.WindSpeed
dataMap["windDirection"][forcIndex] = forecast.WindDirection
dataMap["windGustSpeed"][forcIndex] = forecast.WindGustSpeed
}
modelData := &ModelData{
Location: w.Location,
Model: w.Model,
Data: dataMap,
}
return modelData
}
// Create a new WindForecastObject from an existing ModelData object
func WindForecastFromModelData(modelData *ModelData) *WindForecast {
if modelData == nil {
return nil
}
itemCount := len(modelData.Data["ugrd10m"])
forecastItems := make([]WindForecastItem, itemCount)
modelTime, _ := LatestModelDateTime()
for i := 0; i < itemCount; i++ {
thisForecastItem := WindForecastItem{}
forecastTime := modelTime.Add(time.Duration(3 * int64(i) * int64(time.Hour)))
thisForecastItem.Date = forecastTime.In(modelData.Model.TimezoneLocation()).Format("Monday January 02, 2006")
thisForecastItem.Time = forecastTime.In(modelData.Model.TimezoneLocation()).Format("03 PM")
speed, direction := ScalarFromUV(modelData.Data["ugrd10m"][i], modelData.Data["vgrd10m"][i])
thisForecastItem.WindSpeed = speed
thisForecastItem.WindDirection = direction
thisForecastItem.WindGustSpeed = modelData.Data["gustsfc"][i]
forecastItems[i] = thisForecastItem
}
forecast := &WindForecast{
Location: modelData.Location,
Model: modelData.Model,
ForecastData: forecastItems,
}
return forecast
}