-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstation.go
More file actions
77 lines (70 loc) · 1.81 KB
/
station.go
File metadata and controls
77 lines (70 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package synopcsv
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/pkg/errors"
)
// Station as defined in Synop
type Station struct {
ID string
Name string
Latitude float64
Longitude float64
Altitude float64
}
func fetchURL(url string) (r io.Reader, err error) {
var client = &http.Client{
Timeout: time.Second * 10,
}
response, err := client.Get(url)
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
errClose := response.Body.Close()
if err == nil {
err = errors.WithStack(errClose)
}
}()
b, err := ioutil.ReadAll(response.Body)
// stations, err = parseStationCSV(response.Body)
if err != nil {
return nil, errors.WithStack(err)
}
return bytes.NewReader(b), nil
}
// FetchStationCSV retrieve station lists as csv from
// https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/postesSynop.csv
func FetchStationCSV() (r io.Reader, err error) {
url := "https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/postesSynop.csv"
return fetchURL(url)
}
// ParseStationsCSV parses stations from a CSV file formated as "https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/postesSynop.csv"
func ParseStationsCSV(in io.Reader) ([]Station, error) {
csvVals, err := parseCSV(in)
if err != nil {
return nil, err
}
stations := make([]Station, 0)
for _, row := range csvVals.rows {
station := Station{ID: row["ID"], Name: row["Nom"]}
station.Latitude, err = strconv.ParseFloat(row["Latitude"], 64)
if err != nil {
return nil, err
}
station.Longitude, err = strconv.ParseFloat(row["Longitude"], 64)
if err != nil {
return nil, err
}
station.Altitude, err = strconv.ParseFloat(row["Altitude"], 64)
if err != nil {
return nil, err
}
stations = append(stations, station)
}
return stations, nil
}