1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>'
19
20 from elisa.core.common import application
21 from elisa.base_components.activity import Activity
22
23 from sun import suntime
24
25
26 from elisa.core.media_uri import MediaUri
27 from elisa.core import common
28
29 from weather_model import WeatherModel
30
31 import os
32 import time, calendar, datetime
33 import csv
34
35 try:
36 from elisa.extern.metar import Metar
37 except ImportError:
38 Metar = None
39
40 import string
41
42 import math
43
44
45
46 BASE_URL = "http://weather.noaa.gov/pub/data/observations/metar/stations"
47
49 """ The weather activity is retrieving the weather informations with metar
50 """
51 config_doc = {'ICAO': 'Four letters designing your location; ' \
52 'a complete list can be found at ' \
53 'http://en.wikipedia.org/wiki/ICAO_airport_code'
54 }
55
56 default_config = {"ICAO": "LEBL"
57 }
58
59
60
61
62 weather_to_pixmap = {"day": {
63 "sun": "clear",
64 "suncloud": "clear_cloud",
65 "cloud": "cloud",
66 "clear": "clear",
67 "fog": "fog",
68 "storm": "storm",
69 "rain": "rain",
70 "snow": "snow",
71 "unknown": "clear"
72 },
73 "night": {
74 "sun": "night_clear",
75 "suncloud": "night_clear_cloud",
76 "cloud": "night_cloud",
77 "clear": "night_clear",
78 "fog": "night_fog",
79 "storm": "night_storm",
80 "rain": "night_rain",
81 "snow": "night_snow",
82 "unknown": "night_clear"
83 }
84 }
86 Activity.initialize(self)
87 self._station_code = self.config["ICAO"]
88 stations_path = self.plugin.get_resource_file('/data/stations.csv')
89
90 self._location = "couldn't look it up"
91
92 self._latitude = 0
93 self._longitude = 0
94
95 try:
96 stations = csv.reader(open(stations_path, 'rb'))
97 for row in stations:
98 if len(row) > 7:
99 if (row[3] == self._station_code):
100 self._location = row[2] + ',' + row[0]
101 self._latitude = float(row[6])
102 self._longitude = float(row[7])
103 break
104 except:
105 self.warning("The location couldn't be looked up."\
106 "May be the station file %s is corrupt..." %
107 stations_path)
108
118
120 """ Fetches the weather information on the Internet thanks to metar
121 and fill in the weather report self._report
122 """
123 self._report = None
124 url = "%s/%s.TXT" % (BASE_URL, self._station_code)
125 handler = application.media_manager.open(MediaUri(url))
126
127 if not handler:
128 self.warning("No handler for http:// found")
129 return
130
131 report = ''
132 for line in handler.read(-1).splitlines():
133 if line.startswith(self._station_code):
134 report = line.strip()
135 self._report = Metar.Metar(line)
136 break
137
138
140 """ Returns a string for a icon corresponding to a given weather and time
141
142 weather_report: report to consider containing weather and time
143 """
144
145 t = datetime.datetime.now()
146
147 rise_time = suntime('rise', float(self._latitude),
148 float(self._longitude), 1, time.localtime()[7])
149 set_time = suntime('set', float(self._latitude),
150 float(self._longitude), 1, time.localtime()[7])
151 current_time = time.localtime()[3:5]
152
153 night_time = (current_time > set_time) or (current_time < rise_time)
154
155 weather = None
156
157 for i in self._report.sky:
158 for j in i:
159 if (j == "SKC") or (j == "CLR"):
160 sky_weather = "sun"
161 elif (j == "FEW") or (j == "SCT"):
162 sky_weather = "suncloud"
163 elif (j == "FEW") or (j == "SCT"):
164 weather = "cloud"
165 break
166
167 for i in self._report.weather:
168 for j in i:
169 if (j == "TS"):
170 weather = "storm"
171 elif (j == "DZ") or (j == "RA"):
172 weather = "rain"
173 break
174 elif (j == "SN") or (j == "SG") or (j == "IC") \
175 or (j == "PL") or (j == "GS"):
176 weather = "snow"
177 break
178 elif (j == "BR") or (j == "FG") or (j == "FU"):
179 weather = "fog"
180
181 if weather == None:
182 weather = "unknown"
183
184 if not night_time:
185 pixmap = self.weather_to_pixmap["day"][weather]
186 else:
187 pixmap = self.weather_to_pixmap["night"][weather]
188
189 return pixmap
190
228