Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,101 +1,91 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var fn = "readings.txt"
|
||||
const (
|
||||
filename = "readings.txt"
|
||||
readings = 24 // per line
|
||||
fields = readings*2 + 1 // per line
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := os.Open(fn)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
var (
|
||||
badRun, maxRun int
|
||||
badDate, maxDate string
|
||||
fileSum float64
|
||||
fileAccept int
|
||||
)
|
||||
for lr := bufio.NewReader(f); ; {
|
||||
line, pref, err := lr.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if pref {
|
||||
fmt.Println("Unexpected long line.")
|
||||
return
|
||||
}
|
||||
f := strings.Fields(string(line))
|
||||
if len(f) != 49 {
|
||||
fmt.Println("unexpected format,", len(f), "fields.")
|
||||
return
|
||||
}
|
||||
var accept int
|
||||
var sum float64
|
||||
for i := 1; i < 49; i += 2 {
|
||||
flag, err := strconv.Atoi(f[i+1])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if flag > 0 { // value is good
|
||||
if badRun > 0 { // terminate bad run
|
||||
if badRun > maxRun {
|
||||
maxRun = badRun
|
||||
maxDate = badDate
|
||||
}
|
||||
badRun = 0
|
||||
}
|
||||
value, err := strconv.ParseFloat(f[i], 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
sum += value
|
||||
accept++
|
||||
} else { // value is bad
|
||||
if badRun == 0 {
|
||||
badDate = f[0]
|
||||
}
|
||||
badRun++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Line: %s Reject %2d Accept: %2d Line_tot:%9.3f",
|
||||
f[0], 24-accept, accept, sum)
|
||||
if accept > 0 {
|
||||
fmt.Printf(" Line_avg:%8.3f\n", sum/float64(accept))
|
||||
} else {
|
||||
fmt.Println("")
|
||||
}
|
||||
fileSum += sum
|
||||
fileAccept += accept
|
||||
}
|
||||
fmt.Println("\nFile =", fn)
|
||||
fmt.Printf("Total = %.3f\n", fileSum)
|
||||
fmt.Println("Readings = ", fileAccept)
|
||||
if fileAccept > 0 {
|
||||
fmt.Printf("Average = %.3f\n", fileSum/float64(fileAccept))
|
||||
}
|
||||
if badRun > 0 && badRun > maxRun {
|
||||
maxRun = badRun
|
||||
maxDate = badDate
|
||||
}
|
||||
if maxRun == 0 {
|
||||
fmt.Println("\nAll data valid.")
|
||||
} else {
|
||||
fmt.Printf("\nMax data gap = %d, beginning on line %s.\n",
|
||||
maxRun, maxDate)
|
||||
}
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
var (
|
||||
badRun, maxRun int
|
||||
badDate, maxDate string
|
||||
fileSum float64
|
||||
fileAccept int
|
||||
)
|
||||
endBadRun := func() {
|
||||
if badRun > maxRun {
|
||||
maxRun = badRun
|
||||
maxDate = badDate
|
||||
}
|
||||
badRun = 0
|
||||
}
|
||||
s := bufio.NewScanner(file)
|
||||
for s.Scan() {
|
||||
f := strings.Fields(s.Text())
|
||||
if len(f) != fields {
|
||||
log.Fatal("unexpected format,", len(f), "fields.")
|
||||
}
|
||||
var accept int
|
||||
var sum float64
|
||||
for i := 1; i < fields; i += 2 {
|
||||
flag, err := strconv.Atoi(f[i+1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if flag <= 0 { // value is bad
|
||||
if badRun++; badRun == 1 {
|
||||
badDate = f[0]
|
||||
}
|
||||
} else { // value is good
|
||||
endBadRun()
|
||||
value, err := strconv.ParseFloat(f[i], 64)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
sum += value
|
||||
accept++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Line: %s Reject %2d Accept: %2d Line_tot:%9.3f",
|
||||
f[0], readings-accept, accept, sum)
|
||||
if accept > 0 {
|
||||
fmt.Printf(" Line_avg:%8.3f\n", sum/float64(accept))
|
||||
} else {
|
||||
fmt.Println()
|
||||
}
|
||||
fileSum += sum
|
||||
fileAccept += accept
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
endBadRun()
|
||||
|
||||
fmt.Println("\nFile =", filename)
|
||||
fmt.Printf("Total = %.3f\n", fileSum)
|
||||
fmt.Println("Readings = ", fileAccept)
|
||||
if fileAccept > 0 {
|
||||
fmt.Printf("Average = %.3f\n", fileSum/float64(fileAccept))
|
||||
}
|
||||
if maxRun == 0 {
|
||||
fmt.Println("\nAll data valid.")
|
||||
} else {
|
||||
fmt.Printf("\nMax data gap = %d, beginning on line %s.\n",
|
||||
maxRun, maxDate)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue