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