forked from mmachenry/how-much-snow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
howmuchsnow.py
33 lines (26 loc) · 1.2 KB
/
howmuchsnow.py
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
from subprocess import check_output
import re
from glob import glob
import pygeoip
WEATHER_DIRECTORY = "/home/mmachenry/HowMuchSnow/weather_data"
WGRIB_PROGRAM = "/home/mmachenry/wgrib2-v0.1.9.4/bin/wgrib2"
GEOIP_DATABASE = "/home/mmachenry/HowMuchSnow/GeoLiteCity.dat"
def how_much_snow_ipv4 (ip_address):
return how_much_snow_gps (ipv4_to_gps (ip_address))
def ipv4_to_gps (ip_address):
gi = pygeoip.GeoIP(GEOIP_DATABASE)
record = gi.record_by_addr(ip_address)
return record['latitude'], record['longitude']
def how_much_snow_gps ((lat, lon)):
'''Takes user's latitude and longitude. Returns amount of snow in inches.'''
amts_snow = [how_much_snow_one_file(lat, lon, f) for f in glob(WEATHER_DIRECTORY + '/*grb')]
return meters2inches(max(amts_snow))
def how_much_snow_one_file (lat, lon, filename):
'''Takes user's latitude and longitude and the forecast file for one three-hour window. Returns max amount of snow.'''
command = [WGRIB_PROGRAM, "-lon", str(lon), str(lat), filename]
output = check_output(command)
snow_line = re.match(r'(?m)^1.*?val=([0-9.]+)$', output)
amt_snow = snow_line.group(1)
return float(amt_snow)
def meters2inches (m):
return m * 39.37