-
Notifications
You must be signed in to change notification settings - Fork 36
/
build.go
334 lines (285 loc) · 8.39 KB
/
build.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"encoding/xml"
"log"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/GeoNet/delta/internal/metadb"
"github.com/GeoNet/delta/resp"
)
const header = "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
const warning = "<!-- warning this file is automatically generated -->\n"
type Chart struct {
XMLName xml.Name `xml:"chart"`
Spectras []Spectra
}
type Spectra struct {
XMLName xml.Name `xml:"spectra"`
Id string `xml:"id,attr"`
Archive int32 `xml:"archive,attr"`
Daemon string `xml:"daemon,attr,omitempty"`
Days int32 `xml:"days,attr"`
Delta int32 `xml:"delta,attr"`
Dir string `xml:"dir,attr"`
File string `xml:"file,attr"`
Forever int32 `xml:"forever,attr"`
Step int32 `xml:"step,attr"`
Stations []Station
}
type Station struct {
XMLName xml.Name `xml:"station"`
StationId string `xml:"station_id,attr"`
Name string `xml:"name,attr"`
NetworkId string `xml:"network_id,attr"`
Streams []Stream
}
type Stream struct {
XMLName xml.Name `xml:"stream"`
StreamId string `xml:"stream_id,attr"`
Bins int32 `xml:"bins,attr"`
ChannelId string `xml:"channel_id,attr"`
Gain string `xml:"gain,attr"`
LocationId string `xml:"location_id,attr"`
Log string `xml:"log,attr,omitempty"`
Max string `xml:"max,attr,omitempty"`
Min string `xml:"min,attr,omitempty"`
Mod int32 `xml:"mod,attr"`
Nfft int32 `xml:"nfft,attr"`
Period string `xml:"period,attr,omitempty"`
Rate float64 `xml:"rate,attr"`
}
type Spectras []Spectra
func (v Spectras) Len() int { return len(v) }
func (v Spectras) Swap(a, b int) { v[a], v[b] = v[b], v[a] }
func (v Spectras) Less(a, b int) bool { return v[a].Id < v[b].Id }
type Streams []Stream
func (v Streams) Len() int { return len(v) }
func (v Streams) Swap(a, b int) { v[a], v[b] = v[b], v[a] }
func (v Streams) Less(a, b int) bool { return v[a].StreamId < v[b].StreamId }
func buildSpectras(cfgs map[string]Config, base, dir string) ([]Spectra, error) {
// load delta meta helper
db := metadb.NewMetaDB(base)
// load station details
stations, err := db.Stations()
if err != nil {
return nil, err
}
now := time.Now()
var spectras []Spectra
var keys []string
for key := range cfgs {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
cfg := cfgs[key]
networks := make(map[string]interface{})
for _, n := range cfg.Networks {
networks[n] = true
}
xstations := make(map[string]interface{})
for _, s := range cfg.Stations {
xstations[s] = true
}
loggers := make(map[string]interface{})
for _, d := range cfg.Loggers {
loggers[d] = true
}
sensors := make(map[string]interface{})
for _, s := range cfg.Sensors {
sensors[s] = true
}
components := make(map[string]interface{})
for _, c := range cfg.Components {
components[c] = true
}
// run through each station ....
for _, station := range stations {
network, err := db.Network(station.Network)
if err != nil {
log.Fatalf("problem finding network %s: %v\n", base, err)
}
if network == nil {
continue
}
if _, ok := networks[network.Code]; !ok {
if _, ok := xstations[station.Code]; !ok {
continue
}
}
installations, err := db.Installations(station.Code)
if err != nil {
log.Fatalf("problem loading stations %s: %v\n", base, err)
}
var streams []Stream
for _, installation := range installations {
if installation.End.Before(now) {
continue
}
if _, ok := loggers[installation.Datalogger.Model]; !ok {
continue
}
if _, ok := sensors[installation.Sensor.Model]; !ok {
continue
}
for _, response := range resp.Streams(installation.Datalogger.Model, installation.Sensor.Model) {
stream, err := db.StationLocationSamplingRateStartStream(
station.Code,
installation.Location,
response.Datalogger.SampleRate,
installation.Start)
if err != nil {
log.Fatalf("problem loading streams %s: %v\n", base, err)
}
if stream == nil {
continue
}
r, ok := cfg.Rates[strconv.FormatFloat(response.Datalogger.SampleRate, 'g', -1, 64)]
if !ok {
continue
}
lookup := response.Channels(stream.Axial)
for pin := range response.Components {
if !(pin < len(lookup)) {
continue
}
channel := lookup[pin]
if _, ok := components[string(channel[len(channel)-1])]; !ok {
continue
}
streams = append(streams, Stream{
StreamId: station.Code + "/" + installation.Location + "-" + channel + "/" + network.External,
LocationId: installation.Location,
ChannelId: channel,
Rate: response.Datalogger.SampleRate,
Gain: func() string {
var gain float64 = 1.0
if !strings.HasSuffix(channel, "DF") {
if x, ok := r.Scales[string(channel[len(channel)-1])]; ok {
gain *= x.(float64)
} else if r.Scale != 0.0 {
gain *= r.Scale
}
}
for _, s := range append(response.Sensor.Stages, response.Datalogger.Stages...) {
if s.StageSet == nil {
continue
}
if s.Gain == 0.0 {
switch t := s.StageSet.(type) {
case resp.A2D:
log.Fatalf("A2D stage should have gain set")
case resp.PAZ:
gain *= s.StageSet.(resp.PAZ).Gain(s.Frequency)
case resp.FIR:
gain *= s.StageSet.(resp.FIR).Gain
case resp.Polynomial:
gain *= s.StageSet.(resp.Polynomial).Gain
default:
log.Fatalf("unexpected stage type %T", t)
}
} else {
gain *= s.Gain
}
}
return strconv.FormatFloat(gain, 'f', 1, 64)
}(),
Bins: cfg.Bins,
Max: cfg.Max,
Min: cfg.Min,
Log: cfg.Log,
Period: cfg.Period,
Nfft: r.Nfft,
Mod: r.Mod,
})
}
}
}
sort.Sort(Streams(streams))
if len(streams) > 0 {
spectras = append(spectras, Spectra{
Id: key + "-" + strings.ToLower(station.Code),
Archive: cfg.Archive,
Days: cfg.Days,
Delta: cfg.Delta,
Dir: strings.Replace(filepath.Join(dir, cfg.Dir), "%n", strings.ToLower(network.Code), -1),
File: strings.Replace(cfg.File, "%n", strings.ToLower(network.Code), -1),
Forever: cfg.Forever,
Step: cfg.Step,
Daemon: cfg.Daemon,
Stations: []Station{
{
StationId: station.Code,
NetworkId: network.External,
Name: station.Name,
Streams: streams,
},
},
})
}
}
for _, extra := range cfg.Extras {
spectras = append(spectras, Spectra{
Id: key + "-" + strings.ToLower(extra.StationId),
Archive: cfg.Archive,
Days: cfg.Days,
Delta: cfg.Delta,
Dir: strings.Replace(filepath.Join(dir, cfg.Dir), "%n", strings.ToLower(extra.NetworkId), -1),
File: strings.Replace(cfg.File, "%n", strings.ToLower(extra.NetworkId), -1),
Forever: cfg.Forever,
Step: cfg.Step,
Daemon: cfg.Daemon,
Stations: []Station{
{
StationId: extra.StationId,
NetworkId: extra.NetworkId,
Name: extra.Name,
Streams: func() []Stream {
var s []Stream
for l, loc := range extra.Locations {
for c, cha := range loc {
r, ok := cfg.Rates[strconv.FormatFloat(cha.Rate, 'g', -1, 64)]
if !ok {
continue
}
s = append(s, Stream{
StreamId: extra.StationId + "/" + l + "-" + c + "/" + extra.NetworkId,
LocationId: l,
ChannelId: c,
Rate: cha.Rate,
Gain: strconv.FormatFloat(cha.Gain, 'f', 1, 64),
Bins: cfg.Bins,
Max: cfg.Max,
Min: cfg.Min,
Log: cfg.Log,
Period: cfg.Period,
Nfft: r.Nfft,
Mod: r.Mod,
})
}
}
sort.Sort(Streams(s))
return s
}(),
},
},
})
}
}
sort.Sort(Spectras(spectras))
return spectras, nil
}
func encodeSpectras(spectras []Spectra) ([]byte, error) {
res, err := xml.MarshalIndent(&Chart{
Spectras: spectras,
}, "", " ")
if err != nil {
return nil, err
}
str := strings.Replace(string(res), "></stream>", " />", -1)
res = append([]byte(header+warning), append([]byte(str), '\n')...)
return res, nil
}