-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
350 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Station,Location,Fault,Start Date,End Date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package meta | ||
|
||
import ( | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
qualityStation = iota | ||
qualityLocation | ||
qualityFault | ||
qualityStart | ||
qualityEnd | ||
qualityLast | ||
) | ||
|
||
var qualityHeaders Header = map[string]int{ | ||
"Station": qualityStation, | ||
"Location": qualityLocation, | ||
"Fault": qualityFault, | ||
"Start Date": qualityStart, | ||
"End Date": qualityEnd, | ||
} | ||
|
||
// Quality describes when a datalogger is connected to a sensor via analogue quality (e.g. FM radio). | ||
type Quality struct { | ||
Span | ||
|
||
Station string | ||
Location string | ||
Fault bool | ||
|
||
fault string | ||
} | ||
|
||
// String implements the Stringer interface. | ||
func (q Quality) String() string { | ||
return strings.Join([]string{q.Station, q.Location, Format(q.Start)}, " ") | ||
} | ||
|
||
// Id returns a unique string which can be used for sorting or checking. | ||
func (q Quality) Id() string { | ||
return strings.Join([]string{q.Station, q.Location}, ":") | ||
} | ||
|
||
// Less returns whether one Quality sorts before another. | ||
func (q Quality) Less(quality Quality) bool { | ||
switch { | ||
case q.Station < quality.Station: | ||
return true | ||
case q.Station > quality.Station: | ||
return false | ||
case q.Location < quality.Location: | ||
return true | ||
case q.Location > quality.Location: | ||
return false | ||
case q.Span.Start.Before(quality.Span.Start): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
type QualityList []Quality | ||
|
||
func (t QualityList) Len() int { return len(t) } | ||
func (t QualityList) Swap(i, j int) { t[i], t[j] = t[j], t[i] } | ||
func (t QualityList) Less(i, j int) bool { return t[i].Less(t[j]) } | ||
|
||
func (t QualityList) encode() [][]string { | ||
var data [][]string | ||
|
||
data = append(data, qualityHeaders.Columns()) | ||
for _, v := range t { | ||
data = append(data, []string{ | ||
strings.TrimSpace(v.Station), | ||
strings.TrimSpace(v.Location), | ||
strings.TrimSpace(v.fault), | ||
v.Start.Format(DateTimeFormat), | ||
v.End.Format(DateTimeFormat), | ||
}) | ||
} | ||
|
||
return data | ||
} | ||
|
||
// toFloat64 is used in decoding to allow mathematical expressions as well as actual floating point values, | ||
// if the string parameter is empty the default value will be returned. | ||
func (q *QualityList) toBool(str string, def bool) (bool, error) { | ||
switch s := strings.TrimSpace(str); { | ||
case s != "": | ||
return strconv.ParseBool(s) | ||
default: | ||
return def, nil | ||
} | ||
} | ||
|
||
func (q *QualityList) decode(data [][]string) error { | ||
var telemetries []Quality | ||
|
||
// needs more than a comment line | ||
if !(len(data) > 1) { | ||
return nil | ||
} | ||
|
||
fields := qualityHeaders.Fields(data[0]) | ||
for _, v := range data[1:] { | ||
d := fields.Remap(v) | ||
|
||
fault, err := q.toBool(d[qualityFault], false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
start, err := time.Parse(DateTimeFormat, d[qualityStart]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
end, err := time.Parse(DateTimeFormat, d[qualityEnd]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
telemetries = append(telemetries, Quality{ | ||
Span: Span{ | ||
Start: start, | ||
End: end, | ||
}, | ||
Fault: fault, | ||
Station: strings.TrimSpace(d[qualityStation]), | ||
Location: strings.TrimSpace(d[qualityLocation]), | ||
|
||
fault: strings.TrimSpace(d[qualityFault]), | ||
}) | ||
} | ||
|
||
*q = QualityList(telemetries) | ||
|
||
return nil | ||
} | ||
|
||
func LoadQualities(path string) ([]Quality, error) { | ||
var g []Quality | ||
|
||
if err := LoadList(path, (*QualityList)(&g)); err != nil { | ||
return nil, err | ||
} | ||
|
||
sort.Sort(QualityList(g)) | ||
|
||
return g, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package meta | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestQuality(t *testing.T) { | ||
|
||
t.Run("check quality", testListFunc("testdata/qualities.csv", &QualityList{ | ||
Quality{ | ||
Station: "GISS", | ||
Location: "40", | ||
Span: Span{ | ||
Start: time.Date(2016, 12, 3, 22, 0, 0, 0, time.UTC), | ||
End: time.Date(2016, 12, 14, 19, 0, 0, 0, time.UTC), | ||
}, | ||
Fault: true, | ||
fault: "true", | ||
}, | ||
Quality{ | ||
Station: "GISS", | ||
Location: "41", | ||
Span: Span{ | ||
Start: time.Date(2016, 12, 3, 22, 0, 0, 0, time.UTC), | ||
End: time.Date(2016, 12, 14, 19, 0, 0, 0, time.UTC), | ||
}, | ||
Fault: false, | ||
}, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Station,Location,Fault,Start Date,End Date | ||
GISS,40,true,2016-12-03T22:00:00Z,2016-12-14T19:00:00Z | ||
GISS,41,,2016-12-03T22:00:00Z,2016-12-14T19:00:00Z |
Oops, something went wrong.