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 pathmodeldata.go
72 lines (61 loc) · 1.86 KB
/
modeldata.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
package surfnerd
import (
"encoding/json"
"io/ioutil"
"strconv"
"strings"
)
// A generic map useful for encapsulating model data from NOAA GRADS servers. This holds the data in a map so
// the data can be conveinently used for plotting and physics calculations to name a few.
type ModelDataMap map[string][]float64
// Encapsulated model data with the raw ModelDataMap format but also holds the location of the model data
// as well as the run time and model description.
type ModelData struct {
Location
Model NOAAModel
Data ModelDataMap
}
// Export a ModelData object to a json formatted string
func (m *ModelData) ToJSON() ([]byte, error) {
return json.MarshalIndent(m, "", " ")
}
// Export a ModelData object to a json file with a given filename
func (m *ModelData) ExportAsJSON(filename string) error {
jsonData, jsonErr := m.ToJSON()
if jsonErr != nil {
return jsonErr
}
fileErr := ioutil.WriteFile(filename, jsonData, 0644)
return fileErr
}
func parseRawModelData(data []byte) ModelDataMap {
if data == nil {
return nil
}
// Get the data into a better status
allData := string(data)
splitData := strings.Split(allData, "\n")
// Create the model data object to parse into
modelData := ModelDataMap{}
currentVar := ""
for _, value := range splitData {
switch {
case len(value) < 1:
continue
case value[0] == '[':
datas := strings.Split(value, ",")
f, _ := strconv.ParseFloat(strings.TrimSpace(datas[1]), 64)
modelData[currentVar] = append(modelData[currentVar], f)
case value[0] >= '0' && value[0] <= '9':
timestamps := strings.Split(value, ",")
for _, timestamp := range timestamps {
timeValue, _ := strconv.ParseFloat(strings.TrimSpace(timestamp), 64)
modelData["time"] = append(modelData["time"], timeValue)
}
default:
variables := strings.Split(value, ",")
currentVar = variables[0]
}
}
return modelData
}