]> Pileus Git - aweather/blob - src/plugins/radar.c
Support for multiple radar sites
[aweather] / src / plugins / radar.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _XOPEN_SOURCE
19 #include <time.h>
20 #include <config.h>
21 #include <glib/gstdio.h>
22 #include <gtk/gtk.h>
23 #include <gtk/gtkgl.h>
24 #include <gio/gio.h>
25 #include <GL/gl.h>
26 #include <math.h>
27 #include <rsl.h>
28
29 #include <gis.h>
30
31 #include "radar.h"
32 #include "level2.h"
33 #include "../aweather-location.h"
34
35 void _gtk_bin_set_child(GtkBin *bin, GtkWidget *new)
36 {
37         GtkWidget *old = gtk_bin_get_child(bin);
38         if (old)
39                 gtk_widget_destroy(old);
40         gtk_container_add(GTK_CONTAINER(bin), new);
41         gtk_widget_show_all(new);
42 }
43
44 static gchar *_find_nearest(time_t time, GList *files,
45                 gsize offset, gchar *format)
46 {
47         g_debug("GisPluginRadar: _find_nearest");
48         time_t  nearest_time = 0;
49         char   *nearest_file = NULL;
50
51         struct tm tm = {};
52         for (GList *cur = files; cur; cur = cur->next) {
53                 gchar *file = cur->data;
54                 g_message("file=%s", file);
55                 strptime(file+offset, format, &tm);
56                 if (ABS(time - mktime(&tm)) <
57                     ABS(time - nearest_time)) {
58                         nearest_file = file;
59                         nearest_time = mktime(&tm);
60                 }
61         }
62
63         g_debug("GisPluginRadar: _find_nearest = %s", nearest_file);
64         if (nearest_file)
65                 return g_strdup(nearest_file);
66         else
67                 return NULL;
68 }
69
70
71 /**************
72  * RadarSites *
73  **************/
74 typedef enum {
75         STATUS_UNLOADED,
76         STATUS_LOADING,
77         STATUS_LOADED,
78 } RadarSiteStatus;
79 struct _RadarSite {
80         /* Information */
81         gchar     *code;   // Site name. e.g. KLSX
82         gchar     *name;   // Site name. e.g. St. Louis
83         GisPoint   pos;    // LLE positions of antena 
84         GisMarker *marker; // Map marker for libgis
85
86         /* Stuff from the parents */
87         GisViewer     *viewer;
88         GisHttp       *http;
89         GisPrefs      *prefs;
90         GtkWidget     *pconfig;
91
92         /* When loaded */
93         RadarSiteStatus status;     // Loading status for the site
94         GtkWidget      *config;
95         AWeatherLevel2 *level2;     // The Level2 structure for the current volume
96         gpointer        level2_ref; // GisViewer reference to the added radar
97
98         /* Internal data */
99         time_t   time;        // Current timestamp of the level2
100         gchar   *message;     // Error message set while updating
101         guint    time_id;     // "time-changed"     callback ID
102         guint    refresh_id;  // "refresh"          callback ID
103         guint    location_id; // "locaiton-changed" callback ID
104 };
105
106 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
107 void _site_update_loading(gchar *file, goffset cur,
108                 goffset total, gpointer _site)
109 {
110         RadarSite *site = _site;
111         GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(site->config));
112         double percent = (double)cur/total;
113         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
114         gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
115                         percent*100, (double)cur/1000000, (double)total/1000000);
116         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
117         g_free(msg);
118 }
119 gboolean _site_update_end(gpointer _site)
120 {
121         RadarSite *site = _site;
122         if (site->message) {
123                 g_warning("GisPluginRadar: _update_end - %s", site->message);
124                 _gtk_bin_set_child(GTK_BIN(site->config), gtk_label_new(site->message));
125         } else {
126                 _gtk_bin_set_child(GTK_BIN(site->config),
127                                 aweather_level2_get_config(site->level2));
128         }
129         site->status = STATUS_LOADED;
130         return FALSE;
131 }
132 gpointer _site_update_thread(gpointer _site)
133 {
134         RadarSite *site = _site;
135         g_debug("GisPluginRadar: _update - %s", site->code);
136         site->status = STATUS_LOADING;
137         site->message = NULL;
138
139         gboolean offline = gis_viewer_get_offline(site->viewer);
140         gchar *nexrad_url = gis_prefs_get_string(site->prefs,
141                         "aweather/nexrad_url", NULL);
142
143         /* Remove old volume */
144         g_debug("GisPluginRadar: _update - remove - %s", site->code);
145         if (site->level2_ref) {
146                 gis_viewer_remove(site->viewer, site->level2_ref);
147                 site->level2_ref = NULL;
148         }
149
150         /* Find nearest volume (temporally) */
151         g_debug("GisPluginRadar: _update - find nearest - %s", site->code);
152         gchar *dir_list = g_strconcat(nexrad_url, "/", site->code,
153                         "/", "dir.list", NULL);
154         GList *files = gis_http_available(site->http,
155                         "^K\\w{3}_\\d{8}_\\d{4}$", site->code,
156                         "\\d+ (.*)", (offline ? NULL : dir_list));
157         g_free(dir_list);
158         gchar *nearest = _find_nearest(site->time, files, 5, "%Y%m%d_%H%M");
159         g_list_foreach(files, (GFunc)g_free, NULL);
160         g_list_free(files);
161         if (!nearest) {
162                 site->message = "No suitable files found";
163                 goto out;
164         }
165
166         /* Fetch new volume */
167         g_debug("GisPluginRadar: _update - fetch");
168         gchar *local = g_strconcat(site->code, "/", nearest, NULL);
169         gchar *uri   = g_strconcat(nexrad_url, "/", local,   NULL);
170         gchar *file = gis_http_fetch(site->http, uri, local,
171                         offline ? GIS_LOCAL : GIS_UPDATE,
172                         _site_update_loading, site);
173         g_free(local);
174         g_free(uri);
175         if (!file) {
176                 site->message = "Fetch failed";
177                 goto out;
178         }
179
180         /* Load and add new volume */
181         g_debug("GisPluginRadar: _update - load - %s", site->code);
182         site->level2 = aweather_level2_new_from_file(
183                         site->viewer, colormaps, file, site->code);
184         if (!site->level2) {
185                 site->message = "Load failed";
186                 goto out;
187         }
188         site->level2_ref = gis_viewer_add(site->viewer,
189                         GIS_OBJECT(site->level2), GIS_LEVEL_WORLD, TRUE);
190
191 out:
192         g_idle_add(_site_update_end, site);
193         return NULL;
194 }
195 void _site_update(RadarSite *site)
196 {
197         site->time = gis_viewer_get_time(site->viewer);
198         g_debug("GisPluginRadar: _on_time_changed %s - %d",
199                         site->code, (gint)site->time);
200
201         /* Add a progress bar */
202         GtkWidget *progress = gtk_progress_bar_new();
203         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
204         _gtk_bin_set_child(GTK_BIN(site->config), progress);
205
206         /* Fork loading right away so updating the
207          * list of times doesn't take too long */
208         g_thread_create(_site_update_thread, site, FALSE, NULL);
209 }
210
211 /* RadarSite methods */
212 void radar_site_unload(RadarSite *site)
213 {
214         g_debug("GisPluginRadar: radar_site_unload %s", site->code);
215
216         if (site->status == STATUS_LOADING)
217                 return; // Abort if it's still loading
218
219         g_signal_handler_disconnect(site->viewer, site->time_id);
220         g_signal_handler_disconnect(site->viewer, site->refresh_id);
221
222         /* Remove tab */
223         gtk_widget_destroy(site->config);
224
225         /* Remove radar */
226         if (site->level2_ref) {
227                 gis_viewer_remove(site->viewer, site->level2_ref);
228                 site->level2_ref = NULL;
229         }
230
231         site->status = STATUS_UNLOADED;
232 }
233
234 void radar_site_load(RadarSite *site)
235 {
236         g_debug("GisPluginRadar: radar_site_load %s", site->code);
237         site->status = STATUS_LOADING;
238
239         /* Add tab page */
240         site->config = gtk_alignment_new(0, 0, 1, 1);
241         GtkWidget *tab   = gtk_hbox_new(FALSE, 0);
242         GtkWidget *close = gtk_button_new();
243         GtkWidget *label = gtk_label_new(site->name);
244         gtk_container_add(GTK_CONTAINER(close),
245                         gtk_image_new_from_stock(GTK_STOCK_CLOSE,
246                                 GTK_ICON_SIZE_MENU));
247         gtk_button_set_relief(GTK_BUTTON(close), GTK_RELIEF_NONE);
248         g_signal_connect_swapped(close, "clicked",
249                         G_CALLBACK(radar_site_unload), site);
250         gtk_box_pack_start(GTK_BOX(tab), label, TRUE, TRUE, 0);
251         gtk_box_pack_end(GTK_BOX(tab), close, FALSE, FALSE, 0);
252         gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig),
253                         site->config, tab);
254         gtk_widget_show_all(site->config);
255         gtk_widget_show_all(tab);
256
257         /* Set up radar loading */
258         site->time_id = g_signal_connect_swapped(site->viewer, "time-changed",
259                         G_CALLBACK(_site_update), site);
260         site->refresh_id = g_signal_connect_swapped(site->viewer, "refresh",
261                         G_CALLBACK(_site_update), site);
262         _site_update(site);
263 }
264
265 void _site_on_location_changed(GisViewer *viewer,
266                 gdouble lat, gdouble lon, gdouble elev,
267                 gpointer _site)
268 {
269         static gdouble min_dist = EARTH_R / 20;
270         RadarSite *site = _site;
271
272         /* Calculate distance, could cache xyz values */
273         gdouble eye_xyz[3], site_xyz[3];
274         lle2xyz(lat, lon, elev, &eye_xyz[0], &eye_xyz[1], &eye_xyz[2]);
275         lle2xyz(site->pos.lat, site->pos.lon, site->pos.elev,
276                         &site_xyz[0], &site_xyz[1], &site_xyz[2]);
277         gdouble dist = distd(site_xyz, eye_xyz);
278
279         /* Load or unload the site if necessasairy */
280         if (dist <= min_dist && dist < elev*1.25 && site->status == STATUS_UNLOADED)
281                 radar_site_load(site);
282         else if (dist > 2*min_dist &&  site->status != STATUS_UNLOADED)
283                 radar_site_unload(site);
284 }
285
286 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
287                 GisViewer *viewer, GisPrefs *prefs, GisHttp *http)
288 {
289         RadarSite *site = g_new0(RadarSite, 1);
290         site->viewer  = g_object_ref(viewer);
291         site->prefs   = g_object_ref(prefs);
292         site->http    = http;
293         site->code    = g_strdup(city->code);
294         site->name    = g_strdup(city->name);
295         site->pos     = city->pos;
296         site->pconfig = pconfig;
297
298         /* Add marker */
299         site->marker = gis_marker_new(city->name);
300         GIS_OBJECT(site->marker)->center = site->pos;
301         GIS_OBJECT(site->marker)->lod    = EARTH_R*city->lod;
302         gis_viewer_add(site->viewer, GIS_OBJECT(site->marker),
303                         GIS_LEVEL_OVERLAY, FALSE);
304
305         /* Connect signals */
306         site->location_id  =
307                 g_signal_connect(viewer, "location-changed",
308                         G_CALLBACK(_site_on_location_changed), site);
309         return site;
310 }
311
312 void radar_site_free(RadarSite *site)
313 {
314         radar_site_unload(site);
315         /* Stuff? */
316         g_object_unref(site->viewer);
317         g_free(site->code);
318         g_free(site);
319 }
320
321
322 /******************
323  * GisPluginRadar *
324  ******************/
325 static void _draw_hud(GisCallback *callback, gpointer _self)
326 {
327         /* TODO */
328         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
329         if (!self->colormap)
330                 return;
331
332         g_debug("GisPluginRadar: _draw_hud");
333         /* Print the color table */
334         glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
335         glMatrixMode(GL_PROJECTION); glLoadIdentity();
336         glDisable(GL_TEXTURE_2D);
337         glDisable(GL_ALPHA_TEST);
338         glDisable(GL_CULL_FACE);
339         glDisable(GL_LIGHTING);
340         glEnable(GL_COLOR_MATERIAL);
341         glBegin(GL_QUADS);
342         int i;
343         for (i = 0; i < 256; i++) {
344                 glColor4ubv(self->colormap->data[i]);
345                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
346                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
347                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
348                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
349         }
350         glEnd();
351 }
352
353 /* Methods */
354 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
355 {
356         /* TODO: move to constructor if possible */
357         g_debug("GisPluginRadar: new");
358         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
359         self->viewer = viewer;
360         self->prefs  = prefs;
361
362         /* Load HUD */
363         GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
364         gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
365
366         /* Load radar sites */
367         for (city_t *city = cities; city->type; city++) {
368                 if (city->type != LOCATION_CITY)
369                         continue;
370                 RadarSite *site = radar_site_new(city, self->config,
371                                 self->viewer, self->prefs, self->sites_http);
372                 g_hash_table_insert(self->sites, city->code, site);
373         }
374
375         return self;
376 }
377
378 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
379 {
380         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
381         return self->config;
382 }
383
384 /* GObject code */
385 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
386 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
387                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
388                         gis_plugin_radar_plugin_init));
389 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
390 {
391         g_debug("GisPluginRadar: plugin_init");
392         /* Add methods to the interface */
393         iface->get_config = gis_plugin_radar_get_config;
394 }
395 static void gis_plugin_radar_init(GisPluginRadar *self)
396 {
397         g_debug("GisPluginRadar: class_init");
398         /* Set defaults */
399         self->sites_http = gis_http_new(G_DIR_SEPARATOR_S "nexrad" G_DIR_SEPARATOR_S "level2" G_DIR_SEPARATOR_S);
400         self->sites      = g_hash_table_new(g_str_hash, g_str_equal);
401         self->config     = gtk_notebook_new();
402         gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_LEFT);
403 }
404 static void gis_plugin_radar_dispose(GObject *gobject)
405 {
406         g_debug("GisPluginRadar: dispose");
407         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
408         /* Drop references */
409         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
410 }
411 static void gis_plugin_radar_finalize(GObject *gobject)
412 {
413         g_debug("GisPluginRadar: finalize");
414         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
415         /* Free data */
416         gis_http_free(self->sites_http);
417         g_hash_table_destroy(self->sites);
418         gtk_widget_destroy(self->config);
419         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
420
421 }
422 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
423 {
424         g_debug("GisPluginRadar: class_init");
425         GObjectClass *gobject_class = (GObjectClass*)klass;
426         gobject_class->dispose  = gis_plugin_radar_dispose;
427         gobject_class->finalize = gis_plugin_radar_finalize;
428 }
429