-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
248 lines (204 loc) · 6.03 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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/GeoNet/delta"
"github.com/GeoNet/delta/meta"
)
type Settings struct {
base string // optional delta base directory
output string // output caps key directory
configured string // configured networks
exclude string // list of stations to exclude
include string // list of stations to include
purge bool // remove unknown station files
}
func main() {
var settings Settings
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.StringVar(&settings.base, "base", "", "optional delta base directory")
flag.StringVar(&settings.output, "output", "key", "output caps configuration directory")
flag.StringVar(&settings.configured, "networks", "AK,CB,CH,EC,FI,HB,KI,NM,NZ,OT,RA,RT,SC,SI,SM,SP,TP,TR,WL,TG", "comma separated list of networks to use")
flag.StringVar(&settings.exclude, "exclude", "", "comma separated list of stations to skip (either NN_SSSS, or simply SSSS)")
flag.StringVar(&settings.include, "include", "", "comma separated list of external stations to add (requires NN_SSSS)")
flag.BoolVar(&settings.purge, "purge", false, "remove unknown single xml files")
flag.Parse()
set, err := delta.NewBase(settings.base)
if err != nil {
log.Fatalf("unable to load delta set: %v", err)
}
if err := os.MkdirAll(settings.output, 0700); err != nil {
log.Fatalf("unable to create output directory %s: %v", settings.output, err)
}
files := make(map[string]string)
if err := filepath.Walk(settings.output, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
switch d := filepath.Base(filepath.Dir(path)); d {
case "global", "scautopick":
files[filepath.Join(d, filepath.Base(path))] = path
default:
files[filepath.Base(path)] = path
}
return nil
}); err != nil {
log.Fatalf("unable to walk dir %s: %v", settings.output, err)
}
check := make(map[string]interface{})
for _, n := range strings.Split(settings.configured, ",") {
check[strings.TrimSpace(strings.ToUpper(n))] = true
}
extra := make(map[string]interface{})
for _, s := range strings.Split(settings.include, ",") {
extra[strings.TrimSpace(strings.ToUpper(s))] = true
}
var unmatch []string
for _, p := range strings.Split(settings.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)
stationMap := make(map[string]Station)
lookup := make(map[string]meta.Network)
for _, n := range set.Networks() {
lookup[n.Code] = n
}
for _, station := range set.Stations() {
if _, ok := check[station.Network]; !ok {
if _, ok := extra[station.Code]; !ok {
continue
}
}
network, ok := lookup[station.Network]
if !ok {
continue
}
for _, site := range set.Sites() {
if site.Station != station.Code {
continue
}
for _, collection := range set.Collections(site) {
if collection.Start.After(now) || collection.End.Before(now) {
continue
}
key := ToKey(network.External, station.Code)
if s, ok := stationMap[key]; ok {
if !(collection.InstalledSensor.Location < s.Global.Location) {
continue
}
}
var code [2]byte
copy(code[:], collection.Code())
global := Global{
Stream: string(code[:]),
Location: collection.Stream.Location,
}
if global.Style() == "" {
continue
}
if _, ok := globals[global.Key()]; !ok {
globals[global.Key()] = global
}
autoPick := AutoPick{
Style: global.Style(),
Filter: DefaultFilter,
Correction: DefaultCorrection,
Location: collection.Stream.Location,
SamplingRate: collection.Stream.SamplingRate,
}
if _, ok := autoPicks[autoPick.Key()]; !ok {
autoPicks[autoPick.Key()] = autoPick
}
if a, ok := autoPicks[autoPick.Key()]; ok {
switch {
case collection.Stream.Location < a.Location:
autoPicks[autoPick.Key()] = autoPick
case collection.Stream.SamplingRate > a.SamplingRate:
autoPicks[autoPick.Key()] = autoPick
}
}
station := Station{
Global: global,
AutoPick: autoPick,
Code: station.Code,
Network: network.External,
}
stationMap[station.Key()] = station
}
}
}
var count int
for _, g := range globals {
if err := Store(g, settings.output); err != nil {
log.Fatalf("unable to store global profile %s: %v", g.Key(), err)
}
delete(files, g.Path())
count++
}
for _, a := range autoPicks {
if err := Store(a, settings.output); err != nil {
log.Fatalf("unable to store scautopick profile %s: %v", a.Key(), err)
}
delete(files, a.Path())
count++
}
for k, s := range stationMap {
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, settings.output); err != nil {
log.Fatalf("unable to store key output %s: %v", s.Key(), err)
}
delete(files, s.Path())
count++
}
}
for k, v := range files {
log.Printf("removing extra file: %s", k)
if err := os.Remove(v); err != nil {
log.Fatalf("unable to remove file %s: %v", k, err)
}
}
log.Printf("updated %d files, removed %d", count, len(files))
}