-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
127 lines (108 loc) · 2.26 KB
/
csv.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
package gpelements
import (
"bufio"
"fmt"
"io"
"strings"
"time"
)
const (
CSVHeader = "OBJECT_NAME,OBJECT_ID,EPOCH,MEAN_MOTION,ECCENTRICITY,INCLINATION,RA_OF_ASC_NODE,ARG_OF_PERICENTER,MEAN_ANOMALY,EPHEMERIS_TYPE,CLASSIFICATION_TYPE,NORAD_CAT_ID,ELEMENT_SET_NO,REV_AT_EPOCH,BSTAR,MEAN_MOTION_DOT,MEAN_MOTION_DDOT"
)
var (
CSVTimeFormat = "2006-01-02T15:04:05.999999999"
)
func (e *Elements) MarshalCSV() (string, error) {
f := "%q,%q,%q,%g,%g,%g,%g,%g,%g,%d,%q,%q,%d,%g,%g,%g,%g"
return fmt.Sprintf(f,
e.Name,
e.Id,
e.Epoch.Format(CSVTimeFormat),
e.MeanMotion,
e.Eccentricity,
e.Inclination,
e.RightAscension,
e.ArgOfPericenter,
e.MeanAnomaly,
e.EphemerisType,
e.ClassificationType,
e.NoradCatId,
e.ElementSet,
e.RevAtEpoch,
e.BStar,
e.MeanMotionDot,
e.MeanMotionDDot,
), nil
}
// QuoteString will make sure the given line has double-quoted values
// where we (think) we want them.
func QuoteStrings(line string) string {
ss := strings.Split(line, ",")
which := []bool{
true, true, true,
false, false, false, false, false, false,
false,
true, true,
}
for i, fix := range which {
if fix {
s := strings.Trim(ss[i], `"`)
ss[i] = `"` + s + `"`
}
}
return strings.Join(ss, ",")
}
func ParseCSV(line string) (*Elements, int, error) {
line = QuoteStrings(line)
var (
e = &Elements{}
ts string
f = `%q,%q,%q,%f,%f,%f,%f,%f,%f,%d,%q,%q,%d,%f,%f,%f,%f`
)
// ToDo: Include Originator, CreationDate?
n, err := fmt.Sscanf(line, f,
&e.Name,
&e.Id,
&ts,
&e.MeanMotion,
&e.Eccentricity,
&e.Inclination,
&e.RightAscension,
&e.ArgOfPericenter,
&e.MeanAnomaly,
&e.EphemerisType,
&e.ClassificationType,
&e.NoradCatId,
&e.ElementSet,
&e.RevAtEpoch,
&e.BStar,
&e.MeanMotionDot,
&e.MeanMotionDDot,
)
if err != nil {
return nil, n, err
}
t, err := time.Parse(CSVTimeFormat, ts)
if err != nil {
return nil, n, err
}
t0 := Time(t)
e.Epoch = &t0
return e, n, nil
}
func DoLines(r *bufio.Reader, f func(s string) error) error {
i := 0
for {
line, err := r.ReadString('\n')
line = strings.TrimRight(line, "\n\r")
if 0 < len(line) {
if err := f(line); err != nil {
return fmt.Errorf("error parsing line %d: %s", i, err)
}
}
if err == io.EOF {
break
}
}
return nil
}