Skip to content

Commit

Permalink
feat(class): added classes v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ozym committed Aug 10, 2023
1 parent 36e5f29 commit 9ff2253
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 150 deletions.
1 change: 1 addition & 0 deletions delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
//go:embed install/*.csv
//go:embed network/*.csv
//go:embed environment/*.csv
//go:embed references/*.csv
var files embed.FS

// there can be but one
Expand Down
173 changes: 96 additions & 77 deletions meta/class.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
package meta

import (
"fmt"
"sort"
"strconv"
"strings"
)

const (
classStation = iota
classClass
classSiteClass
classVs30
classVs30Quality
classTsite
classZb
classQVs30
classQTsite
classDTsite
classQZb
classReferences
classTsiteMethod
classTsiteQuality
classBasementDepth
classDepthQuality
classLink
classCitations
classNotes
classLast
)

var classHeaders Header = map[string]int{
"Station": classStation,
"Site Class": classSiteClass,
"Vs30": classVs30,
"Vs30 Quality": classVs30Quality,
"Tsite": classTsite,
"Tsite Method": classTsiteMethod,
"Tsite Quality": classTsiteQuality,
"Basement Depth": classBasementDepth,
"Depth Quality": classDepthQuality,
"Link": classLink,
"Citations": classCitations,
"Notes": classNotes,
}

type Class struct {
Station string
Class string
Vs30 float64
Tsite Range
Zb float64
QVs30 string
QTsite string
DTsite string
QZb string
References string
Station string
SiteClass string
Vs30 float64
Vs30Quality string
Tsite Range
TsiteMethod string
TsiteQuality string
BasementDepth float64
DepthQuality string
Link string
Citations []string
Notes string
}

func (c Class) Less(class Class) bool {
Expand All @@ -50,76 +68,77 @@ func (c ClassList) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ClassList) Less(i, j int) bool { return c[i].Less(c[j]) }

func (c ClassList) encode() [][]string {
data := [][]string{{
"Station",
"Class",
"Vs30",
"Tsite",
"Zb",
"Q_Vs30",
"Q_Tsite",
"D_Tsite",
"Q_Zb",
"References",
}}
for _, v := range c {
var data [][]string

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

for _, row := range c {
data = append(data, []string{
strings.TrimSpace(v.Station),
strings.TrimSpace(v.Class),
strconv.FormatFloat(v.Vs30, 'g', -1, 64),
strings.TrimSpace(v.Tsite.String()),
strconv.FormatFloat(v.Zb, 'g', -1, 64),
strings.TrimSpace(v.QVs30),
strings.TrimSpace(v.QTsite),
strings.TrimSpace(v.DTsite),
strings.TrimSpace(v.QZb),
strings.TrimSpace(v.References),
strings.TrimSpace(row.Station),
strings.TrimSpace(row.SiteClass),
strconv.FormatFloat(row.Vs30, 'g', -1, 64),
strings.TrimSpace(row.Vs30Quality),
strings.TrimSpace(row.Tsite.String()),
strings.TrimSpace(row.TsiteMethod),
strings.TrimSpace(row.TsiteQuality),
strconv.FormatFloat(row.BasementDepth, 'g', -1, 64),
strings.TrimSpace(row.DepthQuality),
strings.TrimSpace(row.Link),
strings.Join(row.Citations, " "),
strings.TrimSpace(row.Notes),
})
}

return data
}

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

var classes []Class
if len(data) > 1 {
for _, d := range data[1:] {
if len(d) != classLast {
return fmt.Errorf("incorrect number of installed class fields")
}
if strings.HasPrefix(strings.TrimSpace(d[0]), "#") {
continue
}
var err error

var vs30, zb float64
if vs30, err = strconv.ParseFloat(d[classVs30], 64); err != nil {
return err
}
if zb, err = strconv.ParseFloat(d[classZb], 64); err != nil {
return err
}

var r Range
if r, err = NewRange(d[classTsite]); err != nil {
return err
}

classes = append(classes, Class{
Station: strings.TrimSpace(d[classStation]),
Class: strings.TrimSpace(d[classClass]),
Vs30: vs30,
Tsite: r,
Zb: zb,
QVs30: strings.TrimSpace(d[classQVs30]),
QTsite: strings.TrimSpace(d[classQTsite]),
DTsite: strings.TrimSpace(d[classDTsite]),
QZb: strings.TrimSpace(d[classQZb]),
References: strings.TrimSpace(d[classReferences]),
})

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

vs30, err := strconv.ParseFloat(d[classVs30], 64)
if err != nil {
return err
}
zb, err := strconv.ParseFloat(d[classBasementDepth], 64)
if err != nil {
return err
}

r, err := NewRange(d[classTsite])
if err != nil {
return err
}

*c = ClassList(classes)
citations := strings.Fields(strings.TrimSpace(d[classCitations]))

sort.Strings(citations)

classes = append(classes, Class{
Station: strings.TrimSpace(d[classStation]),
SiteClass: strings.TrimSpace(d[classSiteClass]),
Vs30: vs30,
Vs30Quality: strings.TrimSpace(d[classVs30Quality]),
Tsite: r,
TsiteMethod: strings.TrimSpace(d[classTsiteMethod]),
TsiteQuality: strings.TrimSpace(d[classTsiteQuality]),
BasementDepth: zb,
DepthQuality: strings.TrimSpace(d[classDepthQuality]),
Link: strings.TrimSpace(d[classLink]),
Citations: citations,
Notes: strings.TrimSpace(d[classNotes]),
})
}

*c = ClassList(classes)

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions meta/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func main() {
}{
"assets": {"Asset"},
"calibrations": {"Calibration"},
"classes": {"Class"},
"connections": {"Connection"},
"citations": {"Citation"},
"constituents": {"Constituent"},
Expand Down Expand Up @@ -52,6 +53,8 @@ func main() {
Fields []string
}{
"assets": {"Asset", []string{"make", "model", "serial"}},
"citations": {"Citation", []string{"key"}},
"classes": {"Class", []string{"station"}},
"marks": {"Mark", []string{"code"}},
"monuments": {"Monument", []string{"mark"}},
"mounts": {"Mount", []string{"code"}},
Expand Down
40 changes: 22 additions & 18 deletions meta/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,36 +1070,40 @@ func TestList(t *testing.T) {
View: "01",
},
},
},
{
"testdata/classes.csv",
&ClassList{
Class{
Station: "WHAS",
Class: "C",
Vs30: 270,
Station: "WHAS",
SiteClass: "C",
Vs30: 270,
Vs30Quality: "Q3",
Tsite: Range{
Value: 0.4,
},
Zb: 40,
QVs30: "Q3",
QTsite: "Q3",
DTsite: "I",
QZb: "Q3",
References: "Perrin et al. 2015",
TsiteMethod: "I",
TsiteQuality: "Q3",
BasementDepth: 40,
DepthQuality: "Q3",
Citations: []string{"Perrin2015a"},
Notes: "Perrin et al. 2015",
},
Class{
Station: "WKZ",
Class: "B",
Vs30: 1000,
Station: "WKZ",
SiteClass: "B",
Vs30: 1000,
Vs30Quality: "Q3",
Tsite: Range{
Compare: LessThan,
Value: 0.1,
},
Zb: 0,
QVs30: "Q3",
QTsite: "Q3",
DTsite: "I",
QZb: "Q3",
References: "Perrin et al. 2015",
TsiteMethod: "I",
TsiteQuality: "Q3",
BasementDepth: 0,
DepthQuality: "Q3",
Citations: []string{"Kaiser2017", "Perrin2015a"},
Notes: "Perrin et al. 2015",
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions meta/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
StreamsFile = "install/streams.csv"
TelemetriesFile = "install/telemetries.csv"

ClassesFile = "environment/classes.csv"
ConstituentsFile = "environment/constituents.csv"
FeaturesFile = "environment/features.csv"
GaugesFile = "environment/gauges.csv"
Expand Down Expand Up @@ -90,6 +91,7 @@ type Set struct {
streams StreamList
telemetries TelemetryList

classes ClassList
constituents ConstituentList
features FeatureList
gauges GaugeList
Expand Down Expand Up @@ -133,6 +135,7 @@ func (s *Set) files() map[string]List {
StreamsFile: &s.streams,
TelemetriesFile: &s.telemetries,

ClassesFile: &s.classes,
ConstituentsFile: &s.constituents,
FeaturesFile: &s.features,
GaugesFile: &s.gauges,
Expand Down
29 changes: 29 additions & 0 deletions meta/set_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func (s Set) Citations() []Citation {
return citations
}

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

// Components is a helper function to return a slice copy of Component values.
func (s Set) Components() []Component {
components := make([]Component, len(s.components))
Expand Down Expand Up @@ -272,6 +279,28 @@ func (s Set) Asset(make, model, serial string) (Asset, bool) {
return Asset{}, false
}

// Citation is a helper function to return a Citation value and true if one exists.
func (s Set) Citation(key string) (Citation, bool) {
for _, v := range s.citations {
if key != v.Key {
continue
}
return v, true
}
return Citation{}, false
}

// Class is a helper function to return a Class value and true if one exists.
func (s Set) Class(station string) (Class, bool) {
for _, v := range s.classes {
if station != v.Station {
continue
}
return v, true
}
return Class{}, false
}

// Mark is a helper function to return a Mark value and true if one exists.
func (s Set) Mark(code string) (Mark, bool) {
for _, v := range s.marks {
Expand Down
6 changes: 3 additions & 3 deletions meta/testdata/classes.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Station,Class,Vs30,Tsite,Zb,Q_Vs30,Q_Tsite,D_Tsite,Q_Zb,References
WHAS,C,270,0.4,40,Q3,Q3,I,Q3,Perrin et al. 2015
WKZ,B,1000,<0.1,0,Q3,Q3,I,Q3,Perrin et al. 2015
Station,Site Class,Vs30,Vs30 Quality,Tsite,Tsite Method,Tsite Quality,Basement Depth,Depth Quality,Link,Citations,Notes
WHAS,C,270,Q3,0.4,I,Q3,40,Q3,,Perrin2015a,Perrin et al. 2015
WKZ,B,1000,Q3,<0.1,I,Q3,0,Q3,,Kaiser2017 Perrin2015a,Perrin et al. 2015
Loading

0 comments on commit 9ff2253

Please sign in to comment.