-
Notifications
You must be signed in to change notification settings - Fork 0
/
current_conditions.py
executable file
·134 lines (110 loc) · 4.58 KB
/
current_conditions.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
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
#!/usr/bin/env python
"""
This set of libraries and scripts originally started as a means to put a
weather radar image on my Mac Finder background. It has since grown into
a larger personal project, including a responsive weather webpage and modest
python Flask API endpoint.
The National Weather Service (NWS, part of NOAA)
has recently changed their radar image delivery and so that portion of the
functionality does not work at present.
Jesse Hamner, 2019--2020
"""
from __future__ import print_function
import sys
import os
import logging
import weather_functions as wf
from imagery import Imagery
from alerts import Alerts
from radar import Radar
from obs import Observation
from forecast import Forecast, ZoneForecast
import weathersvg as wsvg
# Pull settings in from two YAML files:
SETTINGS_DIR = os.path.dirname(os.path.realpath(__file__))
# OUTPUT_DIR = os.path.join(os.environ['HOME'], 'Library/Caches/weatherwidget/')
def main():
"""
- Parse user-specified data from YaML
- Check to see that the needed graphics are available. If not, get them.
- Get the radar imagery, complete with warnings graphics
- Get today's hazardous weather outlook statement and parse it
- Check for FTM outage notifications
- Get, parse, and write out current weather conditions to specified locations.
- TODO: should run the getweather.sh shell script, that overlays/composites
the weather graphics. At present, that shell script calls this script
and runs the overlays with -bash-.
- Check for and acquire current multi-band GOES-x imagery of a given resolution.
"""
if os.path.exists('weatherwidget.log'):
os.remove('weatherwidget.log')
logging.basicConfig(filename='weatherwidget.log', level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(threadName)-10s %(message)s',)
data = wf.load_settings_and_defaults(SETTINGS_DIR, 'settings.yml', 'defaults.yml')
if not data:
logging.error('Unable to load settings files. These are required.')
sys.exit('settings files are required and could not be loaded successfully.')
logging.info('Checking for radar outage.')
wf.outage_check(data)
logging.info('Retrieving current weather observations.')
right_now = Observation(data)
right_now.get_current_conditions()
right_now.get_backup_obs(use_json=False)
right_now.merge_good_observations()
logging.debug('Merged current conditions: %s', right_now.con1.obs)
sum_con = right_now.conditions_summary()
if right_now.con1.obs and sum_con:
text_conditions, nice_con = right_now.format_current_conditions()
logging.debug('Current conditions from primary source: %s', nice_con)
wf.write_json(some_dict=nice_con,
outputdir=data['output_dir'],
filename='current_conditions.json'
)
else:
logging.error('Something went wrong getting the current conditions. Halting.')
return 1
wf.write_text(os.path.join(data['output_dir'], 'current_conditions.txt'), text_conditions)
# Get radar image:
current_radar = Radar(data)
current_radar.check_assets()
current_radar.get_radar()
current_radar.get_warnings_box()
if current_radar.problem:
logging.error('Unable to retrieve weather radar image. Halting now.')
# Hazardous Weather Outlook and alerts:
today_alerts = Alerts(data)
today_alerts.get_alerts()
# Get hydrograph image.
if wf.get_hydrograph(abbr=data['river_gauge_abbr'],
hydro_url=data['defaults']['water_url'],
outputdir=data['output_dir']).ok:
logging.info('Requesting hydrograph for station %s, gauge "%s".',
data['radar_station'], data['river_gauge_abbr'])
else:
logging.error('Failed to get hydrograph information.')
return 1
forecast_obj = Forecast(data=data)
logging.debug('Getting the forecasts.')
forecast_obj.get_forecast()
forecastdict = forecast_obj.parse_forecast()
if forecastdict is None:
logging.error('Unable to parse forecast!')
return 1
forecast_obj.write_forecast(outputdir=data['output_dir'])
logging.debug('Getting area forecast discussion.')
forecast_obj.get_afd()
logging.debug('Getting zone forecast.')
zoneforecast = ZoneForecast(data)
zoneforecast.get()
wf.write_json(some_dict=forecastdict,
outputdir=data['output_dir'],
filename='forecast.json'
)
wsvg.make_forecast_icons(forecastdict, outputdir=data['output_dir'])
# Satellite imagery:
current_image = Imagery(band='GEOCOLOR', data=data)
current_image.get_all()
logging.info('Finished program run.')
return 0
if __name__ == '__main__':
sys.exit(main())