]> Pileus Git - aweather/blob - src/plugins/radar.c
Fix Conus double-loading crash
[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 <sys/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 GtkWidget *_gtk_check_label_new(const gchar *text, gboolean state,
36                 GCallback on_clicked, gpointer user_data)
37 {
38         GtkWidget *hbox  = gtk_hbox_new(FALSE, 0);
39         GtkWidget *check = gtk_check_button_new();
40         GtkWidget *label = gtk_label_new(text);
41         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), state);
42         g_signal_connect_swapped(check , "clicked",
43                         G_CALLBACK(on_clicked), user_data);
44         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
45         gtk_box_pack_end(GTK_BOX(hbox), check , FALSE, FALSE, 0);
46         gtk_widget_show_all(hbox);
47         return hbox;
48 }
49
50 void _gtk_bin_set_child(GtkBin *bin, GtkWidget *new)
51 {
52         GtkWidget *old = gtk_bin_get_child(bin);
53         if (old)
54                 gtk_widget_destroy(old);
55         gtk_container_add(GTK_CONTAINER(bin), new);
56         gtk_widget_show_all(new);
57 }
58
59 static gchar *_find_nearest(time_t time, GList *files,
60                 gsize offset, gchar *format)
61 {
62         g_debug("GisPluginRadar: _find_nearest ...");
63         time_t  nearest_time = 0;
64         char   *nearest_file = NULL;
65
66         struct tm tm = {};
67         for (GList *cur = files; cur; cur = cur->next) {
68                 gchar *file = cur->data;
69                 strptime(file+offset, format, &tm);
70                 if (ABS(time - mktime(&tm)) <
71                     ABS(time - nearest_time)) {
72                         nearest_file = file;
73                         nearest_time = mktime(&tm);
74                 }
75         }
76
77         g_debug("GisPluginRadar: _find_nearest = %s", nearest_file);
78         if (nearest_file)
79                 return g_strdup(nearest_file);
80         else
81                 return NULL;
82 }
83
84
85 /**************
86  * RadarSites *
87  **************/
88 typedef enum {
89         STATUS_UNLOADED,
90         STATUS_LOADING,
91         STATUS_LOADED,
92 } RadarSiteStatus;
93 struct _RadarSite {
94         /* Information */
95         city_t    *city;
96         GisMarker *marker;     // Map marker for libgis
97         gpointer  *marker_ref; // Reference to maker
98
99         /* Stuff from the parents */
100         GisViewer     *viewer;
101         GisHttp       *http;
102         GisPrefs      *prefs;
103         GtkWidget     *pconfig;
104
105         /* When loaded */
106         gboolean        hidden;
107         RadarSiteStatus status;     // Loading status for the site
108         GtkWidget      *config;
109         AWeatherLevel2 *level2;     // The Level2 structure for the current volume
110         gpointer        level2_ref; // GisViewer reference to the added radar
111
112         /* Internal data */
113         time_t   time;        // Current timestamp of the level2
114         gchar   *message;     // Error message set while updating
115         guint    time_id;     // "time-changed"     callback ID
116         guint    refresh_id;  // "refresh"          callback ID
117         guint    location_id; // "locaiton-changed" callback ID
118 };
119
120 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
121 void _site_update_loading(gchar *file, goffset cur,
122                 goffset total, gpointer _site)
123 {
124         RadarSite *site = _site;
125         GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(site->config));
126         double percent = (double)cur/total;
127         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
128         gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
129                         percent*100, (double)cur/1000000, (double)total/1000000);
130         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
131         g_free(msg);
132 }
133 gboolean _site_update_end(gpointer _site)
134 {
135         RadarSite *site = _site;
136         if (site->message) {
137                 g_warning("GisPluginRadar: _update_end - %s", site->message);
138                 _gtk_bin_set_child(GTK_BIN(site->config), gtk_label_new(site->message));
139         } else {
140                 _gtk_bin_set_child(GTK_BIN(site->config),
141                                 aweather_level2_get_config(site->level2));
142         }
143         site->status = STATUS_LOADED;
144         return FALSE;
145 }
146 gpointer _site_update_thread(gpointer _site)
147 {
148         RadarSite *site = _site;
149         g_debug("GisPluginRadar: _site_update_thread - %s", site->city->code);
150         site->status = STATUS_LOADING;
151         site->message = NULL;
152
153         gboolean offline = gis_viewer_get_offline(site->viewer);
154         gchar *nexrad_url = gis_prefs_get_string(site->prefs,
155                         "aweather/nexrad_url", NULL);
156
157         /* Find nearest volume (temporally) */
158         g_debug("GisPluginRadar: _site_update_thread - find nearest - %s", site->city->code);
159         gchar *dir_list = g_strconcat(nexrad_url, "/", site->city->code,
160                         "/", "dir.list", NULL);
161         GList *files = gis_http_available(site->http,
162                         "^K\\w{3}_\\d{8}_\\d{4}$", site->city->code,
163                         "\\d+ (.*)", (offline ? NULL : dir_list));
164         g_free(dir_list);
165         gchar *nearest = _find_nearest(site->time, files, 5, "%Y%m%d_%H%M");
166         g_list_foreach(files, (GFunc)g_free, NULL);
167         g_list_free(files);
168         if (!nearest) {
169                 site->message = "No suitable files found";
170                 goto out;
171         }
172
173         /* Fetch new volume */
174         g_debug("GisPluginRadar: _site_update_thread - fetch");
175         gchar *local = g_strconcat(site->city->code, "/", nearest, NULL);
176         gchar *uri   = g_strconcat(nexrad_url, "/", local,   NULL);
177         gchar *file = gis_http_fetch(site->http, uri, local,
178                         offline ? GIS_LOCAL : GIS_UPDATE,
179                         _site_update_loading, site);
180         g_free(nexrad_url);
181         g_free(nearest);
182         g_free(local);
183         g_free(uri);
184         if (!file) {
185                 site->message = "Fetch failed";
186                 goto out;
187         }
188
189         /* Load and add new volume */
190         g_debug("GisPluginRadar: _site_update_thread - load - %s", site->city->code);
191         site->level2 = aweather_level2_new_from_file(
192                         site->viewer, colormaps, file, site->city->code);
193         GIS_OBJECT(site->level2)->hidden = site->hidden;
194         g_free(file);
195         if (!site->level2) {
196                 site->message = "Load failed";
197                 goto out;
198         }
199         site->level2_ref = gis_viewer_add(site->viewer,
200                         GIS_OBJECT(site->level2), GIS_LEVEL_WORLD, TRUE);
201
202 out:
203         g_idle_add(_site_update_end, site);
204         return NULL;
205 }
206 void _site_update(RadarSite *site)
207 {
208         site->time = gis_viewer_get_time(site->viewer);
209         g_debug("GisPluginRadar: _site_update %s - %d",
210                         site->city->code, (gint)site->time);
211
212         /* Add a progress bar */
213         GtkWidget *progress = gtk_progress_bar_new();
214         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
215         _gtk_bin_set_child(GTK_BIN(site->config), progress);
216
217         /* Remove old volume */
218         g_debug("GisPluginRadar: _site_update - remove - %s", site->city->code);
219         if (site->level2_ref) {
220                 gis_viewer_remove(site->viewer, site->level2_ref);
221                 site->level2_ref = NULL;
222         }
223
224         /* Fork loading right away so updating the
225          * list of times doesn't take too long */
226         g_thread_create(_site_update_thread, site, FALSE, NULL);
227 }
228
229 /* RadarSite methods */
230 void radar_site_unload(RadarSite *site)
231 {
232         if (site->status != STATUS_LOADED)
233                 return; // Abort if it's still loading
234
235         g_debug("GisPluginRadar: radar_site_unload %s", site->city->code);
236
237         if (site->time_id)
238                 g_signal_handler_disconnect(site->viewer, site->time_id);
239         if (site->refresh_id)
240                 g_signal_handler_disconnect(site->viewer, site->refresh_id);
241
242         /* Remove tab */
243         if (site->config)
244                 gtk_widget_destroy(site->config);
245
246         /* Remove radar */
247         if (site->level2_ref) {
248                 gis_viewer_remove(site->viewer, site->level2_ref);
249                 site->level2_ref = NULL;
250         }
251
252         site->status = STATUS_UNLOADED;
253 }
254
255 void radar_site_toggle(RadarSite *site)
256 {
257         site->hidden = !site->hidden;
258         if (site->level2) {
259                 GIS_OBJECT(site->level2)->hidden = site->hidden;
260                 gtk_widget_queue_draw(GTK_WIDGET(site->viewer));
261         }
262 }
263
264 void radar_site_load(RadarSite *site)
265 {
266         g_debug("GisPluginRadar: radar_site_load %s", site->city->code);
267         site->status = STATUS_LOADING;
268
269         /* Add tab page */
270         site->config = gtk_alignment_new(0, 0, 1, 1);
271         gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig), site->config,
272                         _gtk_check_label_new(site->city->name, !site->hidden,
273                                 G_CALLBACK(radar_site_toggle), site));
274         gtk_widget_show_all(site->config);
275
276         /* Set up radar loading */
277         site->time_id = g_signal_connect_swapped(site->viewer, "time-changed",
278                         G_CALLBACK(_site_update), site);
279         site->refresh_id = g_signal_connect_swapped(site->viewer, "refresh",
280                         G_CALLBACK(_site_update), site);
281         _site_update(site);
282 }
283
284 void _site_on_location_changed(GisViewer *viewer,
285                 gdouble lat, gdouble lon, gdouble elev,
286                 gpointer _site)
287 {
288         static gdouble min_dist = EARTH_R / 20;
289         RadarSite *site = _site;
290
291         /* Calculate distance, could cache xyz values */
292         gdouble eye_xyz[3], site_xyz[3];
293         lle2xyz(lat, lon, elev, &eye_xyz[0], &eye_xyz[1], &eye_xyz[2]);
294         lle2xyz(site->city->pos.lat, site->city->pos.lon, site->city->pos.elev,
295                         &site_xyz[0], &site_xyz[1], &site_xyz[2]);
296         gdouble dist = distd(site_xyz, eye_xyz);
297
298         /* Load or unload the site if necessasairy */
299         if (dist <= min_dist && dist < elev*1.25 && site->status == STATUS_UNLOADED)
300                 radar_site_load(site);
301         else if (dist > 2*min_dist &&  site->status != STATUS_UNLOADED)
302                 radar_site_unload(site);
303 }
304
305 gboolean _site_add_marker(gpointer _site)
306 {
307         RadarSite *site = _site;
308         site->marker = gis_marker_new(site->city->name);
309         GIS_OBJECT(site->marker)->center = site->city->pos;
310         GIS_OBJECT(site->marker)->lod    = EARTH_R*site->city->lod;
311         site->marker_ref = gis_viewer_add(site->viewer,
312                         GIS_OBJECT(site->marker), GIS_LEVEL_OVERLAY, FALSE);
313         return FALSE;
314 }
315 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
316                 GisViewer *viewer, GisPrefs *prefs, GisHttp *http)
317 {
318         RadarSite *site = g_new0(RadarSite, 1);
319         site->viewer  = g_object_ref(viewer);
320         site->prefs   = g_object_ref(prefs);
321         site->http    = http;
322         site->city    = city;
323         site->pconfig = pconfig;
324
325         /* Add marker */
326         g_idle_add_full(G_PRIORITY_LOW, _site_add_marker, site, NULL);
327
328
329         /* Connect signals */
330         site->location_id  = g_signal_connect(viewer, "location-changed",
331                         G_CALLBACK(_site_on_location_changed), site);
332         return site;
333 }
334
335 void radar_site_free(RadarSite *site)
336 {
337         radar_site_unload(site);
338         gis_viewer_remove(site->viewer, site->marker_ref);
339         if (site->location_id)
340                 g_signal_handler_disconnect(site->viewer, site->location_id);
341         g_object_unref(site->viewer);
342         g_object_unref(site->prefs);
343         g_free(site);
344 }
345
346
347 /**************
348  * RadarConus *
349  **************/
350 #define CONUS_NORTH       50.406626367301044
351 #define CONUS_WEST       -127.620375523875420
352 #define CONUS_WIDTH       3400.0
353 #define CONUS_HEIGHT      1600.0
354 #define CONUS_DEG_PER_PX  0.017971305190311
355
356 struct _RadarConus {
357         GisViewer   *viewer;
358         GisHttp     *http;
359         GtkWidget   *config;
360         time_t       time;
361         const gchar *message;
362         GStaticMutex loading;
363
364         gchar       *path;
365         GisTile     *tile[2];
366         gpointer    *tile_ref[2];
367
368         guint        time_id;     // "time-changed"     callback ID
369         guint        refresh_id;  // "refresh"          callback ID
370 };
371
372 void _conus_update_loading(gchar *file, goffset cur,
373                 goffset total, gpointer _conus)
374 {
375         RadarConus *conus = _conus;
376         GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(conus->config));
377         double percent = (double)cur/total;
378         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
379         gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
380                         percent*100, (double)cur/1000000, (double)total/1000000);
381         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
382         g_free(msg);
383 }
384
385 /* Copy images to graphics memory */
386 static void _conus_update_end_copy(GisTile *tile, guchar *pixels)
387 {
388         if (!tile->data) {
389                 tile->data = g_new0(guint, 1);
390                 glGenTextures(1, tile->data);
391         }
392
393         guint *tex = tile->data;
394         glBindTexture(GL_TEXTURE_2D, *tex);
395
396         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
397         glPixelStorei(GL_PACK_ALIGNMENT, 1);
398         glTexImage2D(GL_TEXTURE_2D, 0, 4, 2048, 2048, 0,
399                         GL_RGBA, GL_UNSIGNED_BYTE, NULL);
400         glTexSubImage2D(GL_TEXTURE_2D, 0, 1,1, CONUS_WIDTH/2,CONUS_HEIGHT,
401                         GL_RGBA, GL_UNSIGNED_BYTE, pixels);
402         tile->coords.n = 1.0/(CONUS_WIDTH/2);
403         tile->coords.w = 1.0/ CONUS_HEIGHT;
404         tile->coords.s = tile->coords.n +  CONUS_HEIGHT   / 2048.0;
405         tile->coords.e = tile->coords.w + (CONUS_WIDTH/2) / 2048.0;
406         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
407         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
408         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
409         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
410         glFlush();
411 }
412
413 /* Split the pixbuf into east and west halves (with 2K sides)
414  * Also map the pixbuf's alpha values */
415 static void _conus_update_end_split(guchar *pixels, guchar *west, guchar *east,
416                 gint width, gint height, gint pxsize)
417 {
418         g_debug("GisPluginRadar: _conus_update_end_split");
419         guchar *out[] = {west,east};
420         const guchar alphamap[][4] = {
421                 {0x04, 0xe9, 0xe7, 0x30},
422                 {0x01, 0x9f, 0xf4, 0x60},
423                 {0x03, 0x00, 0xf4, 0x90},
424         };
425         for (int y = 0; y < height; y++)
426         for (int x = 0; x < width;  x++) {
427                 gint subx = x % (width/2);
428                 gint idx  = x / (width/2);
429                 guchar *src = &pixels[(y*width+x)*pxsize];
430                 guchar *dst = &out[idx][(y*(width/2)+subx)*4];
431                 if (src[0] > 0xe0 &&
432                     src[1] > 0xe0 &&
433                     src[2] > 0xe0) {
434                         dst[3] = 0x00;
435                 } else {
436                         dst[0] = src[0];
437                         dst[1] = src[1];
438                         dst[2] = src[2];
439                         dst[3] = 0xff;
440                         for (int j = 0; j < G_N_ELEMENTS(alphamap); j++)
441                                 if (src[0] == alphamap[j][0] &&
442                                     src[1] == alphamap[j][1] &&
443                                     src[2] == alphamap[j][2])
444                                         dst[3] = alphamap[j][3];
445                 }
446         }
447 }
448
449 gboolean _conus_update_end(gpointer _conus)
450 {
451         RadarConus *conus = _conus;
452         g_debug("GisPluginRadar: _conus_update_end");
453
454         /* Check error status */
455         if (conus->message) {
456                 g_warning("GisPluginRadar: _conus_update_end - %s", conus->message);
457                 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message));
458                 goto out;
459         }
460
461         /* Load and pixbuf */
462         GError *error = NULL;
463         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error);
464         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
465         gint       width  = gdk_pixbuf_get_width(pixbuf);
466         gint       height = gdk_pixbuf_get_height(pixbuf);
467         gint       pxsize = gdk_pixbuf_get_has_alpha(pixbuf) ? 4 : 3;
468
469         /* Split pixels into east/west parts */
470         guchar *pixels_west = g_malloc(4*(width/2)*height);
471         guchar *pixels_east = g_malloc(4*(width/2)*height);
472         _conus_update_end_split(pixels, pixels_west, pixels_east,
473                         width, height, pxsize);
474         g_object_unref(pixbuf);
475
476         /* Copy pixels to graphics memory */
477         _conus_update_end_copy(conus->tile[0], pixels_west);
478         _conus_update_end_copy(conus->tile[1], pixels_east);
479         g_free(pixels_west);
480         g_free(pixels_east);
481
482         /* Update GUI */
483         gchar *label = g_path_get_basename(conus->path);
484         _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label));
485         gtk_widget_queue_draw(GTK_WIDGET(conus->viewer));
486         g_free(label);
487
488 out:
489         g_free(conus->path);
490         g_static_mutex_unlock(&conus->loading);
491         return FALSE;
492 }
493
494 gpointer _conus_update_thread(gpointer _conus)
495 {
496         RadarConus *conus = _conus;
497         conus->message = NULL;
498
499         /* Find nearest */
500         g_debug("GisPluginRadar: _conus_update_thread - nearest");
501         gboolean offline = gis_viewer_get_offline(conus->viewer);
502         gchar *conus_url = "http://radar.weather.gov/Conus/RadarImg/";
503         GList *files = gis_http_available(conus->http,
504                         "^Conus_[^\"]*_N0Ronly.gif$", "",
505                         NULL, (offline ? NULL : conus_url));
506         gchar *nearest = _find_nearest(conus->time, files, 6, "%Y%m%d_%H%M");
507         g_list_foreach(files, (GFunc)g_free, NULL);
508         g_list_free(files);
509         if (!nearest) {
510                 conus->message = "No suitable files";
511                 goto out;
512         }
513
514         /* Fetch the image */
515         g_debug("GisPluginRadar: _conus_update_thread - fetch");
516         gchar *uri  = g_strconcat(conus_url, nearest, NULL);
517         conus->path = gis_http_fetch(conus->http, uri, nearest, GIS_ONCE,
518                         _conus_update_loading, conus);
519         g_free(nearest);
520         g_free(uri);
521         if (!conus->path) {
522                 conus->message = "Fetch failed";
523                 goto out;
524         }
525
526 out:
527         g_debug("GisPluginRadar: _conus_update_thread - done");
528         g_idle_add(_conus_update_end, conus);
529         return NULL;
530 }
531
532 void _conus_update(RadarConus *conus)
533 {
534         if (!g_static_mutex_trylock(&conus->loading))
535                 return;
536         conus->time = gis_viewer_get_time(conus->viewer);
537         g_debug("GisPluginRadar: _conus_update - %d",
538                         (gint)conus->time);
539
540         /* Add a progress bar */
541         GtkWidget *progress = gtk_progress_bar_new();
542         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
543         _gtk_bin_set_child(GTK_BIN(conus->config), progress);
544
545         g_thread_create(_conus_update_thread, conus, FALSE, NULL);
546 }
547
548 void radar_conus_toggle(RadarConus *conus)
549 {
550         GIS_OBJECT(conus->tile[0])->hidden =
551                 !GIS_OBJECT(conus->tile[0])->hidden;
552         GIS_OBJECT(conus->tile[1])->hidden =
553                 !GIS_OBJECT(conus->tile[1])->hidden;
554         gtk_widget_queue_draw(GTK_WIDGET(conus->viewer));
555 }
556
557 RadarConus *radar_conus_new(GtkWidget *pconfig,
558                 GisViewer *viewer, GisHttp *http)
559 {
560         RadarConus *conus = g_new0(RadarConus, 1);
561         conus->viewer  = g_object_ref(viewer);
562         conus->http    = http;
563         conus->config  = gtk_alignment_new(0, 0, 1, 1);
564         g_static_mutex_init(&conus->loading);
565
566         gdouble south =  CONUS_NORTH - CONUS_DEG_PER_PX*CONUS_HEIGHT;
567         gdouble east  =  CONUS_WEST  + CONUS_DEG_PER_PX*CONUS_WIDTH;
568         gdouble mid   =  CONUS_WEST  + CONUS_DEG_PER_PX*CONUS_WIDTH/2;
569         conus->tile[0] = gis_tile_new(NULL, CONUS_NORTH, south, mid, CONUS_WEST);
570         conus->tile[1] = gis_tile_new(NULL, CONUS_NORTH, south, east, mid);
571         conus->tile[0]->zindex = 2;
572         conus->tile[1]->zindex = 1;
573         conus->tile_ref[0] = gis_viewer_add(viewer,
574                         GIS_OBJECT(conus->tile[0]), GIS_LEVEL_WORLD, TRUE);
575         conus->tile_ref[1] = gis_viewer_add(viewer,
576                         GIS_OBJECT(conus->tile[1]), GIS_LEVEL_WORLD, TRUE);
577
578         conus->time_id = g_signal_connect_swapped(viewer, "time-changed",
579                         G_CALLBACK(_conus_update), conus);
580         conus->refresh_id = g_signal_connect_swapped(viewer, "refresh",
581                         G_CALLBACK(_conus_update), conus);
582
583         gtk_notebook_append_page(GTK_NOTEBOOK(pconfig), conus->config,
584                         _gtk_check_label_new("Conus", TRUE,
585                                 G_CALLBACK(radar_conus_toggle), conus));
586         _conus_update(conus);
587         return conus;
588 }
589
590 void radar_conus_free(RadarConus *conus)
591 {
592         g_signal_handler_disconnect(conus->viewer, conus->time_id);
593         g_signal_handler_disconnect(conus->viewer, conus->refresh_id);
594
595         for (int i = 0; i < 2; i++) {
596                 GisTile *tile = conus->tile[i];
597                 gpointer ref  = conus->tile_ref[i];
598                 if (tile->data) {
599                         glDeleteTextures(1, tile->data);
600                         g_free(tile->data);
601                 }
602                 gis_viewer_remove(conus->viewer, ref);
603         }
604
605         g_object_unref(conus->viewer);
606         g_free(conus);
607 }
608
609
610 /******************
611  * GisPluginRadar *
612  ******************/
613 static void _draw_hud(GisCallback *callback, gpointer _self)
614 {
615         /* TODO */
616         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
617         if (!self->colormap)
618                 return;
619
620         g_debug("GisPluginRadar: _draw_hud");
621         /* Print the color table */
622         glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
623         glMatrixMode(GL_PROJECTION); glLoadIdentity();
624         glDisable(GL_TEXTURE_2D);
625         glDisable(GL_ALPHA_TEST);
626         glDisable(GL_CULL_FACE);
627         glDisable(GL_LIGHTING);
628         glEnable(GL_COLOR_MATERIAL);
629         glBegin(GL_QUADS);
630         int i;
631         for (i = 0; i < 256; i++) {
632                 glColor4ubv(self->colormap->data[i]);
633                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
634                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
635                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
636                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
637         }
638         glEnd();
639 }
640
641 /* Methods */
642 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
643 {
644         /* TODO: move to constructor if possible */
645         g_debug("GisPluginRadar: new");
646         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
647         self->viewer = viewer;
648         self->prefs  = prefs;
649
650         /* Load HUD */
651         GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
652         self->hud_ref = gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
653
654         /* Load Conus */
655         self->conus = radar_conus_new(self->config, self->viewer, self->conus_http);
656
657         /* Load radar sites */
658         for (city_t *city = cities; city->type; city++) {
659                 if (city->type != LOCATION_CITY)
660                         continue;
661                 RadarSite *site = radar_site_new(city, self->config,
662                                 self->viewer, self->prefs, self->sites_http);
663                 g_hash_table_insert(self->sites, city->code, site);
664         }
665
666         return self;
667 }
668
669 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
670 {
671         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
672         return self->config;
673 }
674
675 /* GObject code */
676 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
677 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
678                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
679                         gis_plugin_radar_plugin_init));
680 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
681 {
682         g_debug("GisPluginRadar: plugin_init");
683         /* Add methods to the interface */
684         iface->get_config = gis_plugin_radar_get_config;
685 }
686 static void gis_plugin_radar_init(GisPluginRadar *self)
687 {
688         g_debug("GisPluginRadar: class_init");
689         /* Set defaults */
690         self->sites_http = gis_http_new(G_DIR_SEPARATOR_S "nexrad" G_DIR_SEPARATOR_S "level2" G_DIR_SEPARATOR_S);
691         self->conus_http = gis_http_new(G_DIR_SEPARATOR_S "nexrad" G_DIR_SEPARATOR_S "conus" G_DIR_SEPARATOR_S);
692         self->sites      = g_hash_table_new_full(g_str_hash, g_str_equal,
693                                 NULL, (GDestroyNotify)radar_site_free);
694         self->config     = gtk_notebook_new();
695         gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_LEFT);
696 }
697 static void gis_plugin_radar_dispose(GObject *gobject)
698 {
699         g_debug("GisPluginRadar: dispose");
700         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
701         gis_viewer_remove(self->viewer, self->hud_ref);
702         radar_conus_free(self->conus);
703         /* Drop references */
704         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
705 }
706 static void gis_plugin_radar_finalize(GObject *gobject)
707 {
708         g_debug("GisPluginRadar: finalize");
709         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
710         /* Free data */
711         gis_http_free(self->conus_http);
712         gis_http_free(self->sites_http);
713         g_hash_table_destroy(self->sites);
714         gtk_widget_destroy(self->config);
715         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
716
717 }
718 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
719 {
720         g_debug("GisPluginRadar: class_init");
721         GObjectClass *gobject_class = (GObjectClass*)klass;
722         gobject_class->dispose  = gis_plugin_radar_dispose;
723         gobject_class->finalize = gis_plugin_radar_finalize;
724 }