# HG changeset patch # User Sascha L. Teichmann # Date 1536585031 -7200 # Node ID e6d95e6d232b99d0d3732dabda8be6afdf33c010 # Parent 255f9a4e747f33d1139bc3473e5f287d75d74fc6 Sounding results: Moved point handling stuff to separate file. diff -r 255f9a4e747f -r e6d95e6d232b cmd/soundingresults/main.go --- a/cmd/soundingresults/main.go Mon Sep 10 12:51:04 2018 +0200 +++ b/cmd/soundingresults/main.go Mon Sep 10 15:10:31 2018 +0200 @@ -2,17 +2,13 @@ import ( "bufio" - "compress/bzip2" - "compress/gzip" "database/sql" "flag" "fmt" - "io" "log" "os" "path/filepath" "runtime" - "strconv" "strings" "sync" "time" @@ -58,85 +54,6 @@ depthReference string } -func wrap(fname string, f io.Reader) (io.Reader, error) { - - switch strings.ToLower(filepath.Ext(fname)) { - case ".gz": - return gzip.NewReader(f) - case ".bz2": - return bzip2.NewReader(f), nil - } - - return bufio.NewReader(f), nil -} - -type point3d struct { - x float64 - y float64 - z float64 -} - -func parseXYZ(fname string) ([]*point3d, error) { - f, err := os.Open(fname) - if err != nil { - return nil, err - } - defer f.Close() - - r, err := wrap(fname, f) - if err != nil { - return nil, err - } - - // Alloc in larger chunks to reduce pressure on memory management. - var chunk []point3d - alloc := func() *point3d { - if len(chunk) == 0 { - chunk = make([]point3d, 8*1024) - } - p := &chunk[0] - chunk = chunk[1:] - return p - } - - var points []*point3d - - s := bufio.NewScanner(r) - if s.Scan() { // Skip header line. - for line := 2; s.Scan(); line++ { - p := alloc() - text := s.Text() - // fmt.Sscanf(text, "%f,%f,%f") is 4 times slower. - idx := strings.IndexByte(text, ',') - if idx == -1 { - log.Printf("format error in line %d\n", line) - continue - } - if p.x, err = strconv.ParseFloat(text[:idx], 64); err != nil { - log.Printf("format error in line %d: %v\n", line, err) - continue - } - text = text[idx+1:] - if idx = strings.IndexByte(text, ','); idx == -1 { - log.Printf("format error in line %d\n", line) - continue - } - if p.y, err = strconv.ParseFloat(text[:idx], 64); err != nil { - log.Printf("format error in line %d: %v\n", line, err) - continue - } - text = text[idx+1:] - if p.z, err = strconv.ParseFloat(text, 64); err != nil { - log.Printf("format error in line %d: %v\n", line, err) - continue - } - points = append(points, p) - } - } - - return points, s.Err() -} - func substituteName(fname, name string) string { dir := filepath.Dir(fname) info := filepath.Join(dir, "INFO.txt") diff -r 255f9a4e747f -r e6d95e6d232b cmd/soundingresults/points.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd/soundingresults/points.go Mon Sep 10 15:10:31 2018 +0200 @@ -0,0 +1,99 @@ +package main + +import ( + "bufio" + "compress/bzip2" + "compress/gzip" + "io" + "log" + "os" + "path/filepath" + "strconv" + "strings" +) + +type point3d struct { + x float64 + y float64 + z float64 +} + +type points3d []*point3d + +func parseXYZ(fname string) (points3d, error) { + f, err := os.Open(fname) + if err != nil { + return nil, err + } + defer f.Close() + + r, err := wrap(fname, f) + if err != nil { + return nil, err + } + return readXYZ(r) +} + +func wrap(fname string, f io.Reader) (io.Reader, error) { + + switch strings.ToLower(filepath.Ext(fname)) { + case ".gz": + return gzip.NewReader(f) + case ".bz2": + return bzip2.NewReader(f), nil + } + + return bufio.NewReader(f), nil +} + +func readXYZ(r io.Reader) (points3d, error) { + + // Alloc in larger chunks to reduce pressure on memory management. + var chunk []point3d + alloc := func() *point3d { + if len(chunk) == 0 { + chunk = make([]point3d, 8*1024) + } + p := &chunk[0] + chunk = chunk[1:] + return p + } + + var points points3d + + s := bufio.NewScanner(r) + if s.Scan() { // Skip header line. + for line := 2; s.Scan(); line++ { + p := alloc() + text := s.Text() + // fmt.Sscanf(text, "%f,%f,%f") is 4 times slower. + idx := strings.IndexByte(text, ',') + if idx == -1 { + log.Printf("format error in line %d\n", line) + continue + } + var err error + if p.x, err = strconv.ParseFloat(text[:idx], 64); err != nil { + log.Printf("format error in line %d: %v\n", line, err) + continue + } + text = text[idx+1:] + if idx = strings.IndexByte(text, ','); idx == -1 { + log.Printf("format error in line %d\n", line) + continue + } + if p.y, err = strconv.ParseFloat(text[:idx], 64); err != nil { + log.Printf("format error in line %d: %v\n", line, err) + continue + } + text = text[idx+1:] + if p.z, err = strconv.ParseFloat(text, 64); err != nil { + log.Printf("format error in line %d: %v\n", line, err) + continue + } + points = append(points, p) + } + } + + return points, s.Err() +}