Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.35 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.         "bufio"
  5.         "encoding/csv"
  6.         //"encoding/json"
  7.         "fmt"
  8.         "gonum.org/v1/plot"
  9.         "gonum.org/v1/plot/plotter"
  10.         "gonum.org/v1/plot/plotutil"
  11.         "gonum.org/v1/plot/vg"
  12.         "io"
  13.         "log"
  14.         "os"
  15.         "strconv"
  16.         "strings"
  17.         "time"
  18. )
  19.  
  20. type Reading struct {
  21.         ID       int
  22.         DateTime time.Time
  23.         Value    float64
  24. }
  25.  
  26. func read_csv(filepath string) ([]Reading, error) {
  27.         csvFile, err := os.Open(filepath)
  28.         defer csvFile.Close()
  29.         reader := csv.NewReader(bufio.NewReader(csvFile))
  30.         reader.Comma = ' '
  31.         var readings []Reading
  32.         for {
  33.                 line, error := reader.Read()
  34.                 if error == io.EOF {
  35.                         break
  36.                 } else if error != nil {
  37.                         log.Fatal(error)
  38.                 }
  39.                 DateTime := fmt.Sprintf("%s %s", line[2], line[3])
  40.                 datetime, _ := time.Parse("01/02/2006 15:04:05", DateTime)
  41.                 id, _ := strconv.Atoi(line[0])
  42.                 val, _ := strconv.ParseFloat(strings.Replace(line[4], ",", ".", 1), 64)
  43.                 readings = append(readings, Reading{
  44.                         ID:       id,
  45.                         DateTime: datetime,
  46.                         Value:    val,
  47.                 })
  48.         }
  49.         return readings, err
  50. }
  51.  
  52. func read_and_plot(sensor string) {
  53.         p, err := plot.New()
  54.         p.Title.Text = "values"
  55.         readings, err := read_csv("nmos1/1/" + sensor + ".log")
  56.         if err != nil {
  57.                 log.Fatal(err)
  58.         }
  59.         var values []float64
  60.         for _, reading := range readings {
  61.                 values = append(values, reading.Value)
  62.         }
  63.  
  64.         pts := make(plotter.XYs, len(readings))
  65.         for i := range pts {
  66.                 pts[i].X = float64(readings[i].ID)
  67.                 pts[i].Y = readings[i].Value
  68.         }
  69.         err = plotutil.AddLinePoints(p,
  70.                 "val", pts)
  71.         if err := p.Save(4*vg.Inch, 4*vg.Inch, sensor+".png"); err != nil {
  72.                 panic(err)
  73.         }
  74. }
  75. func main() {
  76.         sensors := [2]string{"radfet_0", "radfet_1"}
  77.         for _, sensor := range sensors {
  78.                 fmt.Print(sensor)
  79.                 go read_and_plot(sensor)
  80.         }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement