summaryrefslogtreecommitdiff
path: root/metar.go
blob: c19fe6d3542aa23a5e3b7716eca409ecc37709d6 (plain)
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
package main

import (
	"errors"
	"io/ioutil"
	"net/http"
	"strings"
)

const (
	noaa         = `http://weather.noaa.gov/pub/data/observations/metar/`
	noaaDecoded  = noaa + `decoded/`
	noaaStations = noaa + `stations/`
)

var notFound = errors.New("not found")

func fetchMetar(base, station string) ([]string, error) {
	loc := base + strings.ToUpper(station[:4]) + ".TXT"
	resp, err := http.Get(loc)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode == http.StatusNotFound {
		return nil, notFound
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	return strings.Split(strings.TrimSpace(string(body)), "\n"), nil
}

func MetarDecoded(s string) ([]string, error) {
	return fetchMetar(noaaDecoded, s)
}

func MetarStation(s string) ([]string, error) {
	return fetchMetar(noaaStations, s)
}