aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/weather.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/weather.lua b/lib/weather.lua
index 0b4b1b7..6e2741a 100644
--- a/lib/weather.lua
+++ b/lib/weather.lua
@@ -63,4 +63,45 @@ function M.icon(id, now, sunrise, sunset)
return M.ICON.unknown
end
+-- OWM's metric units report wind in m/s. The card shows km/h.
+function M.kmh(ms)
+ if type(ms) ~= 'number' then return nil end
+ return ms * 3.6
+end
+
+-- Beaufort force from km/h. Upper bounds carried over from the polybar
+-- script; a value equal to a bound stays in the lower force.
+local BEAUFORT = { 1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117 }
+
+function M.beaufort(kmh)
+ if type(kmh) ~= 'number' then return nil end
+ for force, bound in ipairs(BEAUFORT) do
+ if kmh <= bound then return force - 1 end
+ end
+ return 12
+end
+
+-- Eight compass points. The half-step offset is what makes north straddle
+-- zero: without it, 350 degrees would land in NW and 10 in NE, leaving north
+-- with only half its arc.
+local POINTS = { 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW' }
+
+function M.compass(deg)
+ if type(deg) ~= 'number' then return nil end
+ local i = math.floor(((deg % 360) + 22.5) / 45) % 8
+ return POINTS[i + 1]
+end
+
+-- Arrow glyphs, in the same order as POINTS but rotated half a turn: OWM
+-- reports the direction the wind comes FROM, and an arrow reads as the
+-- direction it goes TO.
+local ARROWS = { '\u{2193}', '\u{2199}', '\u{2190}', '\u{2196}',
+ '\u{2191}', '\u{2197}', '\u{2192}', '\u{2198}' }
+
+function M.arrow(deg)
+ if type(deg) ~= 'number' then return '' end
+ local i = math.floor(((deg % 360) + 22.5) / 45) % 8
+ return ARROWS[i + 1]
+end
+
return M