summaryrefslogtreecommitdiff
path: root/waterspout-radar.py
blob: ec58e64f7aeaa07976371d7b11485e99dc9a5e1d (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
import argparse
import os

import szilagyi
from geopy.geocoders import Nominatim
from metpy import calc
from metpy.units import units
from windy import point_forecast
from windy import Windy


_L = point_forecast.Level


def main():
	parser = argparse.ArgumentParser()
	parser.add_argument("location")
	args = parser.parse_args()
	key = os.getenv("WINDY_KEY")
	if not key:
		raise RuntimeError("expected windy's api key")
	locator = Nominatim(user_agent="waterspout-radar")
	location = locator.geocode(args.location)
	coords = (location.latitude, location.longitude)
	registry = units
	model = point_forecast.Model.ICONEU
	levels = tuple(_L)
	windy = Windy(registry)
	forecast = windy.point_forecast(key, *coords, model, ("temp", "dewpoint", "wind", "pressure"), levels)
	for pred in forecast:
		dt = abs(pred.at('temp', _L.H850) - pred.at('temp', _L.SURFACE))
		pressure, _ = calc.lcl(pred["pressure"][0], pred["temp"][0], pred["dewpoint"][0])
		lcl = calc.pressure_to_height_std(pressure)
		pressure, _ = calc.el(pred['pressure'], pred['temp'], pred['dewpoint'])
		el = calc.pressure_to_height_std(pressure)
		ccd = (el - lcl).to(units.ft)
		try:
			swi = szilagyi.calculate_swi(dt, ccd, pred.at('wind_u', _L.H850))  # merge with wind_v
		except ValueError:
			swi = -10
		print(pred.timestamp, dt, ccd, swi)


if __name__ == '__main__':
	main()