-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
214 lines (175 loc) · 4.91 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/GeoNet/delta/internal/metadb"
"github.com/GeoNet/delta/resp"
)
func main() {
var base string
flag.StringVar(&base, "base", "../..", "delta base files")
var output string
flag.StringVar(&output, "output", "key", "output sc3 configuration directory")
var networks string
//TODO: this could be managed via configuration elsewhere.
flag.StringVar(&networks, "networks", "AK,CB,CH,EC,FI,HB,KI,MN,NX,NZ,OT,RT,SC,SI,SM,SP,SX,TP,TR,WL", "comma separated list of networks to use")
var exclude string
flag.StringVar(&exclude, "exclude", "", "comma separated list of stations to skip (either NN_SSSS, or simply SSSS)")
var include string
flag.StringVar(&include, "include", "", "comma separated list of external stations to add (requires NN_SSSS)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Build a set of sc3 key files\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, " %s [options]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, "\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
}
flag.Parse()
check := make(map[string]interface{})
for _, n := range strings.Split(networks, ",") {
check[strings.TrimSpace(strings.ToUpper(n))] = true
}
var unmatch []string
for _, p := range strings.Split(exclude, ",") {
if s := func() string {
switch r := strings.Split(strings.ToUpper(p), "_"); len(r) {
case 2:
return strings.TrimSpace(r[0]) + "_" + strings.TrimSpace(r[1])
case 1:
if strings.ContainsAny(r[0], "*?\\[") {
return strings.TrimSpace(r[0])
}
return "*_" + strings.TrimSpace(r[0])
default:
return ""
}
}(); s != "" {
unmatch = append(unmatch, s)
}
}
now := time.Now().UTC()
globals := make(map[string]Global)
autoPicks := make(map[string]AutoPick)
stations := make(map[string]Station)
db := metadb.NewMetaDB(base)
dbStations, err := db.Stations()
if err != nil {
log.Fatalf("unable to load metadata stations: %v", err)
}
for _, station := range dbStations {
if _, ok := check[station.Network]; !ok {
continue
}
installations, err := db.Installations(station.Code)
if err != nil {
log.Fatalf("unable to load metadata installations %s: %v", station.Code, err)
}
network, err := db.Network(station.Network)
if err != nil {
log.Fatalf("unable to load metadata network %s: %v", station.Network, err)
}
if network == nil {
continue
}
for _, installation := range installations {
if installation.Start.After(now) || installation.End.Before(now) {
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 stream == nil || err != nil {
if err != nil {
log.Fatalf("unable to load metadata streams %s: %v", station.Code, err)
}
continue
}
lookup := response.Channels(stream.Axial)
for pin := range response.Components {
if !(pin < len(lookup)) {
continue
}
channel := lookup[pin]
if len(channel) != 3 {
continue
}
key := Station{
Code: station.Code,
Network: network.External,
}.Key()
if s, ok := stations[key]; ok {
if !(installation.Location < s.Global.Location) {
continue
}
}
global := Global{
Stream: channel[0:2],
Location: installation.Location,
}
if global.Style() == "" {
continue
}
if _, ok := globals[global.Key()]; !ok {
globals[global.Key()] = global
}
autoPick := AutoPick{
Style: global.Style(),
Filter: DefaultFilter,
Correction: DefaultCorrection,
}
autoPicks[autoPick.Key()] = autoPick
station := Station{
Global: global,
AutoPick: autoPick,
Code: station.Code,
Network: network.External,
}
stations[station.Key()] = station
}
}
}
}
var count int
for _, g := range globals {
if err := Store(g, output); err != nil {
log.Fatalf("unable to store global profile %s: %v", g.Key(), err)
}
count++
}
for _, a := range autoPicks {
if err := Store(a, output); err != nil {
log.Fatalf("unable to store scautopick profile %s: %v", a.Key(), err)
}
count++
}
for k, s := range stations {
for _, m := range unmatch {
ok, err := filepath.Match(m, k)
if err != nil {
log.Fatalf("unable to match station %s (%s): %v", s.Key(), m, err)
}
if ok {
continue
}
if err := Store(s, output); err != nil {
log.Fatalf("unable to store key output %s: %v", s.Key(), err)
}
count++
}
}
log.Printf("updated %d files", count)
}