Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "os"
  5. "encoding/csv"
  6. "fmt"
  7. "strconv"
  8. "sort"
  9. "time"
  10. )
  11.  
  12. func main(){
  13. f, err := os.Open("./Environmental_Data_Deep_Moor_2015.txt")
  14. if err!=nil{
  15. panic(err)
  16. }
  17. defer f.Close()
  18.  
  19. rdr := csv.NewReader(f)
  20. rdr.Comma = '\t'
  21. rdr.TrimLeadingSpace = true
  22. rows, err := rdr.ReadAll()
  23. if err!=nil{
  24. panic(err)
  25. }
  26.  
  27. start:=time.Now()
  28. fmt.Println("Total record:", len(rows)-1)
  29. if len(rows)-1>0{
  30. fmt.Println("Mean Air Temparature:", mean(rows, 1))
  31. fmt.Println("Mean Barometric pressure:", mean(rows, 2))
  32. fmt.Println("Mean Wind Speed", mean(rows, 7))
  33.  
  34. fmt.Println("Median Air Temparature:", median(rows, 1))
  35. fmt.Println("Median Barometric pressure:", median(rows, 2))
  36. fmt.Println("Median Wind Speed", median(rows, 7))
  37. }
  38. end:=time.Now()
  39. fmt.Println(end.Sub(start))
  40. }
  41.  
  42. func mean(rows [][]string, idx int) float64{
  43. var total float64
  44.  
  45. for i,row := range rows{
  46. if i!=0{
  47. val, _ := strconv.ParseFloat(row[idx],64)
  48. total+=val
  49. }
  50. }
  51.  
  52. return total/ float64(len(rows)-1)
  53. }
  54.  
  55. func median(rows [][]string, idx int) float64{
  56. var sorted[] float64
  57.  
  58. for i,row := range rows{
  59. if i!=0{
  60. val, _ := strconv.ParseFloat(row[idx],64)
  61. sorted = append(sorted, val)
  62. }
  63. }
  64.  
  65. sort.Float64s(sorted)
  66.  
  67. middle := len(rows)/2
  68.  
  69. if len(rows)%2==0{
  70. high := sorted[middle]
  71. low := sorted[middle-1]
  72.  
  73. return (high+low)/2
  74. }
  75.  
  76. return sorted[middle]/2
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement