-
Notifications
You must be signed in to change notification settings - Fork 6
/
functionExamples_test.go
94 lines (77 loc) · 2.84 KB
/
functionExamples_test.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
// Tests and examples for package ais
package ais_test
import (
"fmt"
"strings"
"time"
"github.com/FATHOM5/ais"
)
// Example demonstrates a simple use of the Distance function.
func ExampleRecord_Distance() {
h := strings.Split("MMSI,BaseDateTime,LAT,LON,SOG,COG,Heading,VesselName,IMO,CallSign,VesselType,Status,Length,Width,Draft,Cargo", ",")
headers := ais.Headers{Fields: h}
latIndex, _ := headers.Contains("LAT")
lonIndex, _ := headers.Contains("LON")
data1 := strings.Split("477307900,2017-12-01T00:00:03,36.90512,-76.32652,0.0,131.0,352.0,FIRST,IMO9739666,VRPJ6,1004,moored,337,,,", ",")
data2 := strings.Split("477307902,2017-12-01T00:00:03,36.91512,-76.22652,2.3,311.0,182.0,SECOND,IMO9739800,XHYSF,,underway using engines,337,,,", ",")
rec1 := ais.Record(data1)
rec2 := ais.Record(data2)
nm, err := rec1.Distance(rec2, latIndex, lonIndex)
if err != nil {
panic(err)
}
fmt.Printf("The ships are %.1fnm away from one another.\n", nm)
// Output:
// The ships are 4.8nm away from one another.
}
func ExampleRecord_ParseTime() {
h := strings.Split("MMSI,BaseDateTime,LAT,LON,SOG,COG,Heading,VesselName,IMO,CallSign,VesselType,Status,Length,Width,Draft,Cargo", ",")
data := strings.Split("477307900,2017-12-01T00:00:03,36.90512,-76.32652,0.0,131.0,352.0,FIRST,IMO9739666,VRPJ6,1004,moored,337,,,", ",")
headers := ais.Headers{Fields: h}
rec := ais.Record(data)
timeIndex, _ := headers.Contains("BaseDateTime")
t, err := rec.ParseTime(timeIndex)
if err != nil {
panic(err)
}
fmt.Printf("The record timestamp is at %s\n", t.Format(ais.TimeLayout))
// Output:
// The record timestamp is at 2017-12-01T00:00:03
}
type subsetOneDay struct {
rs *ais.RecordSet
d1 time.Time // date we want to match
timeIndex int //index value of BaseDateTime in the record
}
func (sod *subsetOneDay) Match(rec *ais.Record) (bool, error) {
d2, err := time.Parse(ais.TimeLayout, (*rec)[sod.timeIndex])
if err != nil {
return false, fmt.Errorf("subsetOneDay: %v", err)
}
d2 = d2.Truncate(24 * time.Hour)
return sod.d1.Equal(d2), nil
}
func ExampleRecordSet_Subset() {
rs, _ := ais.OpenRecordSet("testdata/ten.csv")
defer rs.Close()
// Implement a concreate type of subsetOneDay to return records
// from 25 Dec 2017.
timeIndex, ok := rs.Headers().Contains("BaseDateTime")
if !ok {
panic("recordset does not contain the header BaseDateTime")
}
targetDate, _ := time.Parse("2006-01-02", "2017-12-25")
sod := &subsetOneDay{
rs: rs,
d1: targetDate,
timeIndex: timeIndex,
}
matches, _ := rs.Subset(sod)
//matches.Save("newSet.csv")
subsetRec, _ := matches.Read()
subsetDate := (*subsetRec)[timeIndex]
date, _ := time.Parse(ais.TimeLayout, subsetDate)
fmt.Printf("The first record in the subset has BaseDateTime %v\n", date.Format("2006-01-02"))
// Output:
// The first record in the subset has BaseDateTime 2017-12-25
}