Skip to content

Commit

Permalink
feat(notes): add a environment notes table
Browse files Browse the repository at this point in the history
  • Loading branch information
ozym committed Aug 10, 2023
1 parent 9ff2253 commit a8fc547
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions environment/notes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Code,Network,Entry
2 changes: 2 additions & 0 deletions meta/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
"monuments": {"Monument"},
"mounts": {"Mount"},
"networks": {"Network"},
"notes": {"Note"},
"placenames": {"Placename"},
"polarities": {"Polarity"},
"preamps": {"Preamp"},
Expand All @@ -59,6 +60,7 @@ func main() {
"monuments": {"Monument", []string{"mark"}},
"mounts": {"Mount", []string{"code"}},
"networks": {"Network", []string{"code"}},
"notes": {"Note", []string{"code", "network"}},
"placenames": {"Placename", []string{"name"}},
"samples": {"Sample", []string{"code"}},
"sites": {"Site", []string{"station", "location"}},
Expand Down
96 changes: 96 additions & 0 deletions meta/notes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package meta

import (
"sort"
"strings"
)

const (
noteCode = iota
noteNetwork
noteEntry
noteLast
)

var noteHeaders Header = map[string]int{
"Code": noteCode,
"Network": noteNetwork,
"Entry": noteEntry,
}

type Note struct {
Code string
Network string
Entry string
}

type NoteList []Note

func (n NoteList) Len() int { return len(n) }
func (n NoteList) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n NoteList) Less(i, j int) bool {
switch {
case n[i].Code < n[j].Code:
return true
case n[i].Code > n[j].Code:
return false
case n[i].Network < n[j].Network:
return true
case n[i].Network > n[j].Network:
return false
case n[i].Entry < n[j].Entry:
return true
default:
return false
}
}

func (n NoteList) encode() [][]string {
var data [][]string

data = append(data, noteHeaders.Columns())

for _, l := range n {
data = append(data, []string{
strings.TrimSpace(l.Code),
strings.TrimSpace(l.Network),
strings.TrimSpace(l.Entry),
})
}
return data
}

func (n *NoteList) decode(data [][]string) error {
if !(len(data) > 1) {
return nil
}

var notes []Note

fields := noteHeaders.Fields(data[0])
for _, v := range data[1:] {
d := fields.Remap(v)

notes = append(notes, Note{
Code: strings.TrimSpace(d[noteCode]),
Network: strings.TrimSpace(d[noteNetwork]),
Entry: strings.TrimSpace(d[noteEntry]),
})
}

*n = NoteList(notes)

return nil
}

func LoadNotes(path string) ([]Note, error) {
var v []Note

if err := LoadList(path, (*NoteList)(&v)); err != nil {
return nil, err
}

sort.Sort(NoteList(v))

return v, nil
}
25 changes: 25 additions & 0 deletions meta/notes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package meta

import (
"testing"
)

func TestNoteList(t *testing.T) {
t.Run("check notes", testListFunc("testdata/notes.csv", &NoteList{
Note{
Code: "ARTA",
Network: "CG",
Entry: "Concrete pillar with 3 stainless rods drilled to 0.5m into concrete butress.Note that between 2006-10-12 and 2006-11-08 the existing pillar adjacent to this one was occupied using the code ATIA as a test.",
},
Note{
Code: "ATIA",
Network: "XX",
Entry: "Test deployment for site ARTA",
},
Note{
Code: "AUCK",
Network: "LI",
Entry: "Site upgraded to Trimble NETRS/Trimble Zephyr combination on 3rd November 2005",
},
}))
}
3 changes: 3 additions & 0 deletions meta/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
ConstituentsFile = "environment/constituents.csv"
FeaturesFile = "environment/features.csv"
GaugesFile = "environment/gauges.csv"
NotesFile = "environment/notes.csv"
PlacenamesFile = "environment/placenames.csv"
VisibilityFile = "environment/visibility.csv"

Expand Down Expand Up @@ -95,6 +96,7 @@ type Set struct {
constituents ConstituentList
features FeatureList
gauges GaugeList
notes NoteList
placenames PlacenameList
visibilities VisibilityList

Expand Down Expand Up @@ -139,6 +141,7 @@ func (s *Set) files() map[string]List {
ConstituentsFile: &s.constituents,
FeaturesFile: &s.features,
GaugesFile: &s.gauges,
NotesFile: &s.notes,
PlacenamesFile: &s.placenames,
VisibilityFile: &s.visibilities,

Expand Down
21 changes: 21 additions & 0 deletions meta/set_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ func (s Set) Networks() []Network {
return networks
}

// Notes is a helper function to return a slice copy of Note values.
func (s Set) Notes() []Note {
notes := make([]Note, len(s.notes))
copy(notes, s.notes)
return notes
}

// Placenames is a helper function to return a slice copy of Placename values.
func (s Set) Placenames() []Placename {
placenames := make([]Placename, len(s.placenames))
Expand Down Expand Up @@ -345,6 +352,20 @@ func (s Set) Network(code string) (Network, bool) {
return Network{}, false
}

// Note is a helper function to return a Note value and true if one exists.
func (s Set) Note(code, network string) (Note, bool) {
for _, v := range s.notes {
if code != v.Code {
continue
}
if network != v.Network {
continue
}
return v, true
}
return Note{}, false
}

// Placename is a helper function to return a Placename value and true if one exists.
func (s Set) Placename(name string) (Placename, bool) {
for _, v := range s.placenames {
Expand Down
4 changes: 4 additions & 0 deletions meta/testdata/notes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code,Network,Entry
ARTA,CG,Concrete pillar with 3 stainless rods drilled to 0.5m into concrete butress.Note that between 2006-10-12 and 2006-11-08 the existing pillar adjacent to this one was occupied using the code ATIA as a test.
ATIA,XX,Test deployment for site ARTA
AUCK,LI,Site upgraded to Trimble NETRS/Trimble Zephyr combination on 3rd November 2005
1 change: 1 addition & 0 deletions tests/consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestConsistency(t *testing.T) {
"gauges": {f: "../environment/gauges.csv", l: &meta.GaugeList{}},
"constituents": {f: "../environment/constituents.csv", l: &meta.ConstituentList{}},
"features": {f: "../environment/features.csv", l: &meta.FeatureList{}},
"notes": {f: "../environment/notes.csv", l: &meta.NoteList{}},
"visibility": {f: "../environment/visibility.csv", l: &meta.VisibilityList{}},
"citations": {f: "../references/citations.csv", l: &meta.CitationList{}},
}
Expand Down
43 changes: 43 additions & 0 deletions tests/notes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package delta_test

import (
"testing"

"github.com/GeoNet/delta"
"github.com/GeoNet/delta/meta"
)

var noteChecks = map[string]func(*meta.Set) func(t *testing.T){

"check for duplicated notes": func(set *meta.Set) func(t *testing.T) {
return func(t *testing.T) {
notes := set.Notes()
for i := 0; i < len(notes); i++ {
for j := i + 1; j < len(notes); j++ {
if notes[i].Code != notes[j].Code {
continue
}
if notes[i].Network != notes[j].Network {
continue
}
if notes[i].Entry != notes[j].Entry {
continue
}
t.Errorf("note duplication: %s/%s", notes[i].Code, notes[i].Network)
}
}
}
},
}

func TestNotes(t *testing.T) {

set, err := delta.New()
if err != nil {
t.Fatal(err)
}

for k, v := range noteChecks {
t.Run(k, v(set))
}
}

0 comments on commit a8fc547

Please sign in to comment.