-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
121 lines (105 loc) · 2.8 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
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/GeoNet/delta"
)
type Settings struct {
base string // base directory of delta files on disk
network string // dart network
banks string // what payloads are configured
platform string // dart platform
output string // optional output file
}
func main() {
var settings Settings
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Build a DART update meta-data message for NOAA/NDBC\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", "", "base directory of delta files on disk")
flag.StringVar(&settings.network, "network", "TD", "DART network code")
flag.StringVar(&settings.banks, "banks", "P,S", "DART buoy payload banks")
flag.StringVar(&settings.platform, "platform", "DART 4G", "platform to encode")
flag.StringVar(&settings.output, "output", "", "optional output file")
flag.Parse()
set, err := delta.NewBase(settings.base)
if err != nil {
log.Fatal(err)
}
var banks []string
for _, s := range strings.Split(settings.banks, ",") {
if s = strings.TrimSpace(s); s != "" {
banks = append(banks, s)
}
}
var deployments []Deployment
for _, dart := range set.Darts() {
station, ok := set.Station(dart.Station)
if !ok {
continue
}
if station.Network != settings.network {
continue
}
for _, site := range set.Sites() {
if site.Station != station.Code {
continue
}
for _, collection := range set.Collections(site) {
deployments = append(deployments, Deployment{
Buoy: dart.Station,
Deployment: site.Location,
Name: station.Name,
Pid: dart.Pid,
Region: dart.WmoIdentifier,
Banks: banks,
Platform: settings.platform,
Latitude: site.Latitude,
Longitude: site.Longitude,
Serial: collection.InstalledSensor.Serial,
Depth: site.Depth,
Start: collection.Start,
End: collection.End,
})
}
}
}
sort.Slice(deployments, func(i, j int) bool {
switch {
case deployments[i].Buoy < deployments[j].Buoy:
return true
case deployments[i].Buoy > deployments[j].Buoy:
return false
case deployments[i].Start.Before(deployments[j].Start):
return true
default:
return false
}
})
res, err := Deployments(deployments).Process()
if err != nil {
log.Fatal(err)
}
switch {
case settings.output != "":
if err := os.WriteFile(settings.output, res, 0600); err != nil {
log.Fatal(err)
}
default:
fmt.Println(string(res))
}
}