aboutsummaryrefslogtreecommitdiff
path: root/cmd/metar.go
blob: ea72f705b4701a7c2ae6d98328b3e9621004736c (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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cmd

import (
	"context"
	"fmt"
	"os"
	"strings"
	"time"

	"bnbl.io/wx/metar"
	"github.com/spf13/cobra"
)

// wx metar --past 1h kbos
// wx metar --past 3h kbos kbed
// wx metar --past 1h @MA
// wx metar --since yesterday --until now kbos
// wx metar --since yesterday --until now --rect 0,0,1,1
// wx metar --past 1h --rect 0,0,1,1
// wx metar --past 1h --radial 90,0,0
// wx metar --past 6h --most-recent-by-station=constraint --path-dist 50 --waypoint ksea --waypoint kbos --min-dist 3

type relTime struct {
	t time.Time
}

func (t *relTime) String() string {
	return t.t.String()
}

func (t *relTime) Set(s string) (err error) {
	// first, see if s is a duration that we can use relative to now, e.g.
	// "--since 3d" means since 3 days ago.
	if d, err := time.ParseDuration(s); err == nil {
		t.t = time.Now().Add(-1 * d)
		return nil
	}

	// next, try to parse it in a series of formats
	t.t, err = time.Parse("2006-01-02T15:04:05", s)
	return
}

func (t *relTime) Type() string {
	return "relative time"
}

type rectangle struct {
	minLat, minLon, maxLat, maxLon float64
}

func (r *rectangle) String() string {
	return fmt.Sprintf("%f,%f,%f,%f", r.minLat, r.minLon, r.maxLat, r.maxLon)
}

func (r *rectangle) Set(s string) error {
	n, err := fmt.Sscanf(s, "%f,%f,%f,%f", &r.minLat, &r.minLon, &r.maxLat, &r.maxLon)
	if err != nil {
		return err
	}
	if n != 4 {
		return fmt.Errorf("excpected 4 values but got %d", n)
	}
	return nil
}

func (r *rectangle) Type() string {
	return "rect"
}

func (r *rectangle) IsZero() bool {
	return r.minLat == 0 && r.minLon == 0 && r.maxLat == 0 && r.maxLon == 0
}

type ring struct {
	radius, lat, lon float64
}

func (r *ring) String() string {
	return fmt.Sprintf("%f,%f,%f", r.radius, r.lat, r.lon)
}

func (r *ring) Set(s string) error {
	n, err := fmt.Sscanf(s, "%f,%f,%f", &r.radius, &r.lat, &r.lon)
	if err != nil {
		return err
	}
	if n != 3 {
		return fmt.Errorf("expected a radius, lat, and lon, but got %d values", n)
	}
	return nil
}

func (r *ring) Type() string {
	return "radial"
}

func (r *ring) IsZero() bool {
	return r.radius == 0 && r.lat == 0 && r.lon == 0
}

var (
	since, until        relTime
	past                time.Duration
	mostRecentByStation string
	mostRecent          bool
	rect                rectangle
	radial              ring
	pathDist            float64
	waypoints           []string
	minDist             float64
)

func init() {
	rootCmd.AddCommand(metarCmd)

	// time constraints
	metarCmd.Flags().Var(
		&since, "since",
		`Starting time for METARs (e.g. "2020-01-01T01:31:00" or "3h"), used in
conjunction with --until`)
	metarCmd.Flags().Var(
		&until, "until",
		`Ending time for METARs (e.g. "2020-01-01T01:31:00" or "3h"), used in
conjunction with --since`)
	metarCmd.Flags().DurationVar(
		&past, "past", time.Hour,
		`Duration to retrieve METARs since (e.g. "3h")`)

	// location constraints
	metarCmd.Flags().Var(
		&rect, "rect",
		`A lat-lon rect to search within (minLat,minLon,maxLat,maxLon) e.g.
"40,-71,41,-70"`)
	metarCmd.Flags().Var(
		&radial, "radial",
		`A distance around a radius to search within (dist,centerLat,centerLon)
e.g. "3.5,40,-71"`)
	metarCmd.Flags().Float64Var(
		&pathDist, "path-dist", 0,
		"The distance along the flight path to search METARs for")
	metarCmd.Flags().StringSliceVar(
		&waypoints, "waypoint", []string{},
		`Multiple waypoint arguments specify the waypoints that make up a flight
path, ordered from origin to destination. They may be specified as ICAO
identifiers e.g. "KBOS", or as lon-lat pairs, e.g. "-71,40".`)
	metarCmd.Flags().Float64Var(
		&minDist, "min-dist", 0,
		`The minimum distance in degrees latitude or longitude between stations
reported. A higher value results in less dense results.`)

	// result count limiting
	metarCmd.Flags().StringVar(
		&mostRecentByStation, "most-recent-by-station", "constraint",
		`Method for fetching most recent for each station ("constraint",
"postfilter", "false")`)
	metarCmd.Flags().BoolVar(
		&mostRecent, "most-recent", false,
		`Only fetch the single most recently-issued METAR of all the selected
stations`)
}

var metarCmd = &cobra.Command{
	Use:   "metar",
	Short: "Fetch METARs",
	Long:  "Fetch METARs matching the supplied criteria from the TDS",
	Run: func(cmd *cobra.Command, args []string) {
		reqArgs := []func(*metar.Request){}

		if len(args) > 0 {
			reqArgs = append(reqArgs, metar.StationString(strings.Join(args, " ")))
		}

		if !since.t.IsZero() || !until.t.IsZero() {
			reqArgs = append(reqArgs, metar.TimeRange(since.t, until.t))
		} else if dur, _ := cmd.Flags().GetDuration("past"); dur > 0 {
			reqArgs = append(reqArgs, metar.HoursBeforeNow(dur.Hours()))
		}

		if mostRecent {
			reqArgs = append(reqArgs, metar.MostRecent(true))
		} else if mostRecentByStation != "" {
			reqArgs = append(reqArgs, metar.MostRecentForEachStation(mostRecentByStation))
		}

		if !rect.IsZero() {
			reqArgs = append(reqArgs, metar.LonLatRect(rect.minLat, rect.minLon, rect.maxLat, rect.maxLon))
		}

		if !radial.IsZero() {
			reqArgs = append(reqArgs, metar.RadialDistance(radial.radius, radial.lat, radial.lon))
		}

		if pathDist > 0 && len(waypoints) > 0 {
			reqArgs = append(reqArgs, metar.FlightPath(pathDist, waypoints...))
		}

		if minDist > 0 {
			reqArgs = append(reqArgs, metar.MinDegreeDistance(minDist))
		}

		var mc metar.Client
		res, err := mc.GetMETARs(context.Background(), metar.NewRequest(reqArgs...))
		if err != nil {
			fmt.Fprintf(os.Stderr, "could not get metar: %v\n", err)
			os.Exit(1)
		}
		if len(res.Errors) > 0 {
			for _, err := range res.Errors {
				fmt.Fprintf(os.Stderr, "error: %s\n", err)
			}
			os.Exit(2)
		}
		for _, m := range res.Data.METARs {
			fmt.Printf("%s\n", strings.TrimSpace(m.RawText))
		}
	},
}