init station parsing
This commit is contained in:
parent
1a4401ae1a
commit
723626cfa4
11
README.md
11
README.md
|
@ -1,2 +1,11 @@
|
|||
# dwd
|
||||
## DWD Open-Data Project
|
||||
|
||||
This project parses weather data which are provided by the Open-Data Webservice from the DWD.
|
||||
|
||||
Currently it only parses the station list.
|
||||
|
||||
File: parser/mosmix_stationskatalog.cfg
|
||||
|
||||
Source: Deutscher Wetterdienst
|
||||
|
||||
![Deutscher Wetterdient](dwd_logo_258x69.png)
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,143 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Place int
|
||||
|
||||
const (
|
||||
Country Place = iota
|
||||
Coast
|
||||
Sea
|
||||
)
|
||||
|
||||
func NewPlace(s string) Place {
|
||||
switch strings.ToLower(s) {
|
||||
case "land":
|
||||
return Place(0)
|
||||
case "kues":
|
||||
return Place(1)
|
||||
case "meer":
|
||||
return Place(2)
|
||||
}
|
||||
|
||||
return Place(0)
|
||||
}
|
||||
|
||||
func (p Place) String() string {
|
||||
str := [...]string{"Country", "Coast", "Sea"}
|
||||
|
||||
if p < Country || p > Sea {
|
||||
return "unknown place"
|
||||
}
|
||||
|
||||
return str[p]
|
||||
}
|
||||
|
||||
type Station struct {
|
||||
ID string
|
||||
Name string
|
||||
Longitude float32
|
||||
Latitude float32
|
||||
Elevation int
|
||||
Place Place
|
||||
}
|
||||
|
||||
type columnSize struct {
|
||||
start int
|
||||
end int
|
||||
}
|
||||
|
||||
func Parse(file *os.File) ([]Station, error) {
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
var stations []Station
|
||||
|
||||
dict := make(map[string]columnSize, 0)
|
||||
var headers []string
|
||||
var column []string
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
// Skip metadata and empty lines
|
||||
if strings.HasPrefix(line, "TABLE") {
|
||||
continue
|
||||
} else if len(line) == 0 {
|
||||
continue
|
||||
} else if strings.HasPrefix(line, "clu") {
|
||||
headers = strings.Fields(line)
|
||||
fmt.Println(headers)
|
||||
} else if strings.HasPrefix(line, "===") {
|
||||
column = strings.Fields(line)
|
||||
|
||||
start := 0
|
||||
for i, k := range column {
|
||||
c := columnSize{
|
||||
start: start,
|
||||
end: start + len(k),
|
||||
}
|
||||
|
||||
dict[headers[i]] = c
|
||||
|
||||
start += len(k) + 1
|
||||
|
||||
}
|
||||
} else {
|
||||
// TODO: evaluate what this stands for
|
||||
//cluColumn := dict["clu"]
|
||||
//cofxColumn := dict["CofX"]
|
||||
//icaoColumn := dict["ICAO"]
|
||||
//hmodhColumn := dict["Hmod-H"]
|
||||
|
||||
placeColumn := dict["type"]
|
||||
nameColumn := dict["name"]
|
||||
idColumn := dict["ID"]
|
||||
elevColumn := dict["elev"]
|
||||
longitudeColumn := dict["el."]
|
||||
latitudeColumn := dict["nb."]
|
||||
|
||||
id := strings.TrimSpace(line[idColumn.start:idColumn.end])
|
||||
name := strings.TrimSpace(line[nameColumn.start:nameColumn.end])
|
||||
|
||||
place := NewPlace(strings.TrimSpace(line[placeColumn.start:placeColumn.end]))
|
||||
|
||||
elevation, err := strconv.Atoi(strings.TrimSpace(line[elevColumn.start:elevColumn.end]))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
longitude, err := strconv.ParseFloat(strings.TrimSpace(line[longitudeColumn.start:longitudeColumn.end]), 32)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
latitude, err := strconv.ParseFloat(strings.TrimSpace(line[latitudeColumn.start:latitudeColumn.end]), 32)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := Station{
|
||||
ID: id,
|
||||
Name: name,
|
||||
Longitude: float32(longitude),
|
||||
Latitude: float32(latitude),
|
||||
Elevation: elevation,
|
||||
Place: place,
|
||||
}
|
||||
|
||||
stations = append(stations, s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return stations, nil
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package parser_test
|
||||
|
||||
import (
|
||||
"git.hoogi.eu/snafu/dwd/parser"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
file, err := os.Open("mosmix_stationskatalog.cfg")
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
st, err := parser.Parse(file)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if st[0].Name != "Beveringen" {
|
||||
t.Errorf()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue