Skip to content

Commit

Permalink
updated to lastest
Browse files Browse the repository at this point in the history
  • Loading branch information
ozym committed Aug 10, 2023
1 parent 10a4005 commit 6946bca
Show file tree
Hide file tree
Showing 6 changed files with 797 additions and 0 deletions.
133 changes: 133 additions & 0 deletions meta/class.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package meta

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

const (
classStation = iota
classClass
classVs30
classTsite
classZb
classQVs30
classQTsite
classDTsite
classQZb
classReferences
classLast
)

type Class struct {
Station string
Class string
Vs30 float64
Tsite Range
Zb float64
QVs30 string
QTsite string
DTsite string
QZb string
References string
}

func (c Class) Less(class Class) bool {
switch {
case c.Station < class.Station:
return true
default:
return false
}
}

type ClassList []Class

func (c ClassList) Len() int { return len(c) }
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 {
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),
})
}
return data
}

func (c *ClassList) decode(data [][]string) error {
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")
}
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]),
})
}

*c = ClassList(classes)
}
return nil
}

func LoadClasses(path string) ([]Class, error) {
var c []Class

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

sort.Sort(ClassList(c))

return c, nil
}
32 changes: 32 additions & 0 deletions meta/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,38 @@ func TestList(t *testing.T) {
View: "01",
},
},
"testdata/classes.csv",
&ClassList{
Class{
Station: "WHAS",
Class: "C",
Vs30: 270,
Tsite: Range{
Value: 0.4,
},
Zb: 40,
QVs30: "Q3",
QTsite: "Q3",
DTsite: "I",
QZb: "Q3",
References: "Perrin et al. 2015",
},
Class{
Station: "WKZ",
Class: "B",
Vs30: 1000,
Tsite: Range{
Compare: LessThan,
Value: 0.1,
},
Zb: 0,
QVs30: "Q3",
QTsite: "Q3",
DTsite: "I",
QZb: "Q3",
References: "Perrin et al. 2015",
},
},
},
}

Expand Down
56 changes: 56 additions & 0 deletions meta/meta.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meta

import (
"strconv"
"strings"
"time"
)
Expand All @@ -15,6 +16,14 @@ func Format(t time.Time) string {
return t.Format(DateTimeFormat)
}

type Compare int

const (
EqualTo Compare = iota
LessThan
GreaterThan
)

// Reference describes a location where measurements can be taken.
type Reference struct {
// Code is used to identify the measurement location.
Expand Down Expand Up @@ -222,6 +231,53 @@ func (s Span) Extent(spans ...Span) (Span, bool) {
return clip, true
}

type Range struct {
Value float64
Compare Compare
}

func NewRange(s string) (Range, error) {
switch {
case strings.HasPrefix(s, "<"):
v, err := strconv.ParseFloat(s[1:], 64)
if err != nil {
return Range{}, err
}
return Range{
Value: v,
Compare: LessThan,
}, nil
case strings.HasPrefix(s, ">"):
v, err := strconv.ParseFloat(s[1:], 64)
if err != nil {
return Range{}, err
}
return Range{
Value: v,
Compare: GreaterThan,
}, nil
default:
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return Range{}, err
}
return Range{
Value: v,
}, nil
}
}

func (r Range) String() string {
switch r.Compare {
case LessThan:
return "<" + strconv.FormatFloat(r.Value, 'g', -1, 64)
case GreaterThan:
return ">" + strconv.FormatFloat(r.Value, 'g', -1, 64)
default:
return strconv.FormatFloat(r.Value, 'g', -1, 64)
}
}

// Equipment represents an indiviual piece of hardware.
type Equipment struct {
// Make describes the manufacturer or equipment maker.
Expand Down
3 changes: 3 additions & 0 deletions meta/testdata/classes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +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
Loading

0 comments on commit 6946bca

Please sign in to comment.