-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
171 lines (141 loc) · 4.51 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
package main
import (
"bufio"
"flag"
"io"
"log"
"os"
"runtime/pprof"
"sync"
"time"
"github.com/emilyselwood/gompcreader"
"github.com/emilyselwood/orbcalc/orbconvert"
"github.com/emilyselwood/orbcalc/orbcore"
"github.com/paulbellamy/ratecounter"
)
const monitoringInterval = 1 * time.Second
const processors = 3
const channelSize = 100000
var inputfile = flag.String("in", "", "the minor planet center file to read")
var outputfile = flag.String("out", "", "path to output file")
var count = flag.Int("count", 1000000, "number of records to run")
var skip = flag.Int("skip", 0, "number of records from the begining to skip")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
/*
An example program that uses the gompcreader and calculates the position in space for each object.
This example is a little more complex than strictly needed as it parallises the processing and keeps counters.
*/
func main() {
flag.Parse()
// If a cpu project has been requested then set that up.
// This is not strictly needed but it does help us know what is going on with the program
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *inputfile == "" {
log.Fatal("No input file provided. Use the -in /path/to/file")
}
if *outputfile == "" {
log.Fatal("No output file prvided. Use the -out /path/to/outputfile")
}
// rate counters for each processing stage
counter1 := ratecounter.NewRateCounter(monitoringInterval)
counter2 := ratecounter.NewRateCounter(monitoringInterval)
counter3 := ratecounter.NewRateCounter(monitoringInterval)
// start an thing to print rate information as we are processing.
timer := time.NewTicker(monitoringInterval)
defer timer.Stop()
go func() {
for range timer.C {
log.Printf("stage1: %v stage2: %v stage3: %v", counter1.Rate(), counter2.Rate(), counter3.Rate())
}
}()
// Prepares channels to transfer data between stages of the processing.
stage1 := make(chan *orbcore.Orbit, channelSize)
stage2 := make(chan *orbcore.Position, channelSize)
// Wait groups for each stage so we know when things are done.
var readGroup sync.WaitGroup
var fanGroup sync.WaitGroup
var complete sync.WaitGroup
// Setup stage one which reads in the input file, parses the records from it and passes them on to the next stage.
readGroup.Add(1)
go stageRead(*inputfile, *count, *skip, stage1, &readGroup, counter1)
// Stage two progates an object forward one day and then passes it on.
for i := 0; i < processors; i++ {
fanGroup.Add(1)
go stageMeanMotion(stage1, stage2, &fanGroup, counter2)
}
// The final stage converts the orbit information into a position in space and then writes it to a file.
complete.Add(1)
go stageOutput(*outputfile, stage2, &complete, counter3)
readGroup.Wait()
log.Println("done waiting for read")
fanGroup.Wait()
close(stage2)
log.Println("done waiting for fan")
complete.Wait()
log.Println("done")
}
// stageRead opens a file using the gompcreader project and reads out orbital information.
func stageRead(inputfile string, target int, skip int, output chan *orbcore.Orbit, wg *sync.WaitGroup, counter *ratecounter.RateCounter) {
mpcReader, err := gompcreader.NewMpcReader(inputfile)
if err != nil {
log.Fatal("error creating mpcReader ", err)
}
defer mpcReader.Close()
defer close(output)
defer wg.Done()
var count int
result, err := mpcReader.ReadEntry()
for err == nil {
orb := orbconvert.ConvertFromMinorPlanet(result)
if skip == 0 {
//fmt.Println(orb)
output <- orb
} else {
skip--
}
counter.Incr(1)
count++
if count >= target {
return
}
result, err = mpcReader.ReadEntry()
}
if err != io.EOF {
log.Fatal("error reading", err)
}
}
func stageMeanMotion(in chan *orbcore.Orbit, output chan *orbcore.Position, wg *sync.WaitGroup, counter *ratecounter.RateCounter) {
defer wg.Done()
oneDay := 24 * time.Hour
for orb := range in {
log.Println(orb.Epoch)
r := orbcore.MeanMotionStepped(orb, oneDay, 2000)
for _, o := range r {
output <- orbcore.OrbitToPosition(o)
}
counter.Incr(1)
}
}
func stageOutput(outputPath string, in chan *orbcore.Position, wg *sync.WaitGroup, counter *ratecounter.RateCounter) {
defer wg.Done()
f, err := os.Create(outputPath)
if err != nil {
log.Fatal("error creating outputfile ", err)
}
defer f.Close()
w := bufio.NewWriterSize(f, 64*1024)
defer w.Flush()
for orb := range in {
w.WriteString(orb.String())
w.WriteRune('\n')
counter.Incr(1)
}
}