2 * Copyright (C) 2009-2012 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
21 #include <glib/gstdio.h>
31 #include "../aweather-location.h"
33 static void _gtk_bin_set_child(GtkBin *bin, GtkWidget *new)
35 GtkWidget *old = gtk_bin_get_child(bin);
37 gtk_widget_destroy(old);
38 gtk_container_add(GTK_CONTAINER(bin), new);
39 gtk_widget_show_all(new);
42 static gchar *_find_nearest(time_t time, GList *files,
45 g_debug("RadarSite: find_nearest ...");
46 time_t nearest_time = 0;
47 char *nearest_file = NULL;
50 for (GList *cur = files; cur; cur = cur->next) {
51 gchar *file = cur->data;
52 sscanf(file+offset, "%4d%2d%2d_%2d%2d",
53 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
54 &tm.tm_hour, &tm.tm_min);
57 if (ABS(time - mktime(&tm)) <
58 ABS(time - nearest_time)) {
60 nearest_time = mktime(&tm);
64 g_debug("RadarSite: find_nearest = %s", nearest_file);
66 return g_strdup(nearest_file);
83 GritsMarker *marker; // Map marker for grits
85 /* Stuff from the parents */
93 RadarSiteStatus status; // Loading status for the site
95 AWeatherLevel2 *level2; // The Level2 structure for the current volume
98 time_t time; // Current timestamp of the level2
99 gchar *message; // Error message set while updating
100 guint time_id; // "time-changed" callback ID
101 guint refresh_id; // "refresh" callback ID
102 guint location_id; // "locaiton-changed" callback ID
105 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
106 void _site_update_loading(gchar *file, goffset cur,
107 goffset total, gpointer _site)
109 RadarSite *site = _site;
110 GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(site->config));
111 double percent = (double)cur/total;
112 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
113 gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
114 percent*100, (double)cur/1000000, (double)total/1000000);
115 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
118 gboolean _site_update_end(gpointer _site)
120 RadarSite *site = _site;
122 g_warning("RadarSite: update_end - %s", site->message);
123 const char *fmt = "http://forecast.weather.gov/product.php?site=NWS&product=FTM&format=TXT&issuedby=%s";
124 char *uri = g_strdup_printf(fmt, site->city->code+1);
125 GtkWidget *box = gtk_vbox_new(TRUE, 0);
126 GtkWidget *msg = gtk_label_new(site->message);
127 GtkWidget *btn = gtk_link_button_new_with_label(uri, "View Radar Status");
128 gtk_box_pack_start(GTK_BOX(box), msg, TRUE, TRUE, 0);
129 gtk_box_pack_start(GTK_BOX(box), btn, TRUE, TRUE, 0);
130 _gtk_bin_set_child(GTK_BIN(site->config), box);
133 _gtk_bin_set_child(GTK_BIN(site->config),
134 aweather_level2_get_config(site->level2));
136 site->status = STATUS_LOADED;
139 gpointer _site_update_thread(gpointer _site)
141 RadarSite *site = _site;
142 g_debug("RadarSite: update_thread - %s", site->city->code);
143 site->message = NULL;
145 gboolean offline = grits_viewer_get_offline(site->viewer);
146 gchar *nexrad_url = grits_prefs_get_string(site->prefs,
147 "aweather/nexrad_url", NULL);
149 /* Find nearest volume (temporally) */
150 g_debug("RadarSite: update_thread - find nearest - %s", site->city->code);
151 gchar *dir_list = g_strconcat(nexrad_url, "/", site->city->code,
152 "/", "dir.list", NULL);
153 GList *files = grits_http_available(site->http,
154 "^\\w{4}_\\d{8}_\\d{4}$", site->city->code,
155 "\\d+ (.*)", (offline ? NULL : dir_list));
157 gchar *nearest = _find_nearest(site->time, files, 5);
158 g_list_foreach(files, (GFunc)g_free, NULL);
161 site->message = "No suitable files found";
165 /* Fetch new volume */
166 g_debug("RadarSite: update_thread - fetch");
167 gchar *local = g_strconcat(site->city->code, "/", nearest, NULL);
168 gchar *uri = g_strconcat(nexrad_url, "/", local, NULL);
169 gchar *file = grits_http_fetch(site->http, uri, local,
170 offline ? GRITS_LOCAL : GRITS_UPDATE,
171 _site_update_loading, site);
177 site->message = "Fetch failed";
181 /* Load and add new volume */
182 g_debug("RadarSite: update_thread - load - %s", site->city->code);
183 site->level2 = aweather_level2_new_from_file(
184 file, site->city->code, colormaps);
187 site->message = "Load failed";
190 grits_object_hide(GRITS_OBJECT(site->level2), site->hidden);
191 grits_viewer_add(site->viewer, GRITS_OBJECT(site->level2),
192 GRITS_LEVEL_WORLD+3, TRUE);
195 g_idle_add(_site_update_end, site);
198 void _site_update(RadarSite *site)
200 if (site->status == STATUS_LOADING)
202 site->status = STATUS_LOADING;
204 site->time = grits_viewer_get_time(site->viewer);
205 g_debug("RadarSite: update %s - %d",
206 site->city->code, (gint)site->time);
208 /* Add a progress bar */
209 GtkWidget *progress = gtk_progress_bar_new();
210 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
211 _gtk_bin_set_child(GTK_BIN(site->config), progress);
213 /* Remove old volume */
214 g_debug("RadarSite: update - remove - %s", site->city->code);
216 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
220 /* Fork loading right away so updating the
221 * list of times doesn't take too long */
222 g_thread_new("site-update-thread", _site_update_thread, site);
225 /* RadarSite methods */
226 void radar_site_unload(RadarSite *site)
228 if (site->status != STATUS_LOADED)
229 return; // Abort if it's still loading
231 g_debug("RadarSite: unload %s", site->city->code);
234 g_signal_handler_disconnect(site->viewer, site->time_id);
235 if (site->refresh_id)
236 g_signal_handler_disconnect(site->viewer, site->refresh_id);
240 gtk_widget_destroy(site->config);
244 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
248 site->status = STATUS_UNLOADED;
251 void radar_site_load(RadarSite *site)
253 g_debug("RadarSite: load %s", site->city->code);
256 site->config = gtk_alignment_new(0, 0, 1, 1);
257 g_object_set_data(G_OBJECT(site->config), "site", site);
258 gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig), site->config,
259 gtk_label_new(site->city->name));
260 gtk_widget_show_all(site->config);
261 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(site->pconfig)) == 0)
262 gtk_notebook_set_current_page(GTK_NOTEBOOK(site->pconfig), -1);
264 /* Set up radar loading */
265 site->time_id = g_signal_connect_swapped(site->viewer, "time-changed",
266 G_CALLBACK(_site_update), site);
267 site->refresh_id = g_signal_connect_swapped(site->viewer, "refresh",
268 G_CALLBACK(_site_update), site);
272 void _site_on_location_changed(GritsViewer *viewer,
273 gdouble lat, gdouble lon, gdouble elev,
276 static gdouble min_dist = EARTH_R / 30;
277 RadarSite *site = _site;
279 /* Calculate distance, could cache xyz values */
280 gdouble eye_xyz[3], site_xyz[3];
281 lle2xyz(lat, lon, elev, &eye_xyz[0], &eye_xyz[1], &eye_xyz[2]);
282 lle2xyz(site->city->pos.lat, site->city->pos.lon, site->city->pos.elev,
283 &site_xyz[0], &site_xyz[1], &site_xyz[2]);
284 gdouble dist = distd(site_xyz, eye_xyz);
286 /* Load or unload the site if necessasairy */
287 if (dist <= min_dist && dist < elev*1.25 && site->status == STATUS_UNLOADED)
288 radar_site_load(site);
289 else if (dist > 2*min_dist && site->status != STATUS_UNLOADED)
290 radar_site_unload(site);
293 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
294 GritsViewer *viewer, GritsPrefs *prefs, GritsHttp *http)
296 RadarSite *site = g_new0(RadarSite, 1);
297 site->viewer = g_object_ref(viewer);
298 site->prefs = g_object_ref(prefs);
300 site->http = grits_http_new(G_DIR_SEPARATOR_S
301 "nexrad" G_DIR_SEPARATOR_S
302 "level2" G_DIR_SEPARATOR_S);
304 site->pconfig = pconfig;
307 /* Set initial location */
308 gdouble lat, lon, elev;
309 grits_viewer_get_location(viewer, &lat, &lon, &elev);
310 _site_on_location_changed(viewer, lat, lon, elev, site);
313 site->marker = grits_marker_new(site->city->name);
314 GRITS_OBJECT(site->marker)->center = site->city->pos;
315 GRITS_OBJECT(site->marker)->lod = EARTH_R*0.75*site->city->lod;
316 grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker),
317 GRITS_LEVEL_HUD, FALSE);
319 /* Connect signals */
320 site->location_id = g_signal_connect(viewer, "location-changed",
321 G_CALLBACK(_site_on_location_changed), site);
325 void radar_site_free(RadarSite *site)
327 radar_site_unload(site);
328 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->marker));
329 if (site->location_id)
330 g_signal_handler_disconnect(site->viewer, site->location_id);
331 grits_http_free(site->http);
332 g_object_unref(site->viewer);
333 g_object_unref(site->prefs);
341 #define CONUS_NORTH 50.406626367301044
342 #define CONUS_WEST -127.620375523875420
343 #define CONUS_WIDTH 3400.0
344 #define CONUS_HEIGHT 1600.0
345 #define CONUS_DEG_PER_PX 0.017971305190311
352 const gchar *message;
358 guint time_id; // "time-changed" callback ID
359 guint refresh_id; // "refresh" callback ID
362 void _conus_update_loading(gchar *file, goffset cur,
363 goffset total, gpointer _conus)
365 RadarConus *conus = _conus;
366 GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(conus->config));
367 double percent = (double)cur/total;
368 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
369 gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
370 percent*100, (double)cur/1000000, (double)total/1000000);
371 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
375 /* Copy images to graphics memory */
376 static void _conus_update_end_copy(GritsTile *tile, guchar *pixels)
379 tile->data = g_new0(guint, 1);
380 glGenTextures(1, tile->data);
383 gchar *clear = g_malloc0(2048*2048*4);
384 guint *tex = tile->data;
385 glBindTexture(GL_TEXTURE_2D, *tex);
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, clear);
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_LINEAR);
398 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
405 /* Split the pixbuf into east and west halves (with 2K sides)
406 * Also map the pixbuf's alpha values */
407 static void _conus_update_end_split(guchar *pixels, guchar *west, guchar *east,
408 gint width, gint height, gint pxsize)
410 g_debug("Conus: update_end_split");
411 guchar *out[] = {west,east};
412 const guchar alphamap[][4] = {
413 {0x04, 0xe9, 0xe7, 0x30},
414 {0x01, 0x9f, 0xf4, 0x60},
415 {0x03, 0x00, 0xf4, 0x90},
417 for (int y = 0; y < height; y++)
418 for (int x = 0; x < width; x++) {
419 gint subx = x % (width/2);
420 gint idx = x / (width/2);
421 guchar *src = &pixels[(y*width+x)*pxsize];
422 guchar *dst = &out[idx][(y*(width/2)+subx)*4];
431 dst[3] = 0xff * 0.75;
432 for (int j = 0; j < G_N_ELEMENTS(alphamap); j++)
433 if (src[0] == alphamap[j][0] &&
434 src[1] == alphamap[j][1] &&
435 src[2] == alphamap[j][2])
436 dst[3] = alphamap[j][3];
441 gboolean _conus_update_end(gpointer _conus)
443 RadarConus *conus = _conus;
444 g_debug("Conus: update_end");
446 /* Check error status */
447 if (conus->message) {
448 g_warning("Conus: update_end - %s", conus->message);
449 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message));
453 /* Load and pixbuf */
454 GError *error = NULL;
455 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error);
456 if (!pixbuf || error) {
457 g_warning("Conus: update_end - error loading pixbuf: %s", conus->path);
458 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf"));
459 g_remove(conus->path);
463 /* Split pixels into east/west parts */
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 guchar *pixels_west = g_malloc(4*(width/2)*height);
469 guchar *pixels_east = g_malloc(4*(width/2)*height);
470 _conus_update_end_split(pixels, pixels_west, pixels_east,
471 width, height, pxsize);
472 g_object_unref(pixbuf);
474 /* Copy pixels to graphics memory */
475 _conus_update_end_copy(conus->tile[0], pixels_west);
476 _conus_update_end_copy(conus->tile[1], pixels_east);
481 gchar *label = g_path_get_basename(conus->path);
482 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label));
483 gtk_widget_queue_draw(GTK_WIDGET(conus->viewer));
488 g_mutex_unlock(&conus->loading);
492 gpointer _conus_update_thread(gpointer _conus)
494 RadarConus *conus = _conus;
495 conus->message = NULL;
498 g_debug("Conus: update_thread - nearest");
499 gboolean offline = grits_viewer_get_offline(conus->viewer);
500 gchar *conus_url = "http://radar.weather.gov/Conus/RadarImg/";
502 if (time(NULL) - conus->time < 60*60*5 && !offline) {
503 /* radar.weather.gov is full of lies.
504 * the index pages get cached and out of date */
505 /* gmtime is not thread safe, but it's not used very often so
506 * hopefully it'll be alright for now... :-( */
507 struct tm *tm = gmtime(&conus->time);
508 time_t onthe8 = conus->time - 60*((tm->tm_min+1)%10+1);
509 tm = gmtime(&onthe8);
510 nearest = g_strdup_printf("Conus_%04d%02d%02d_%02d%02d_N0Ronly.gif",
511 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
512 tm->tm_hour, tm->tm_min);
514 GList *files = grits_http_available(conus->http,
515 "^Conus_[^\"]*_N0Ronly.gif$", "", NULL, NULL);
516 nearest = _find_nearest(conus->time, files, 6);
517 g_list_foreach(files, (GFunc)g_free, NULL);
520 conus->message = "No suitable files";
525 /* Fetch the image */
526 g_debug("Conus: update_thread - fetch");
527 gchar *uri = g_strconcat(conus_url, nearest, NULL);
528 conus->path = grits_http_fetch(conus->http, uri, nearest,
529 offline ? GRITS_LOCAL : GRITS_ONCE,
530 _conus_update_loading, conus);
534 conus->message = "Fetch failed";
539 g_debug("Conus: update_thread - done");
540 g_idle_add(_conus_update_end, conus);
544 void _conus_update(RadarConus *conus)
546 if (!g_mutex_trylock(&conus->loading))
548 conus->time = grits_viewer_get_time(conus->viewer);
549 g_debug("Conus: update - %d",
552 /* Add a progress bar */
553 GtkWidget *progress = gtk_progress_bar_new();
554 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
555 _gtk_bin_set_child(GTK_BIN(conus->config), progress);
557 g_thread_new("conus-update-thread", _conus_update_thread, conus);
560 RadarConus *radar_conus_new(GtkWidget *pconfig,
561 GritsViewer *viewer, GritsHttp *http)
563 RadarConus *conus = g_new0(RadarConus, 1);
564 conus->viewer = g_object_ref(viewer);
566 conus->config = gtk_alignment_new(0, 0, 1, 1);
567 g_mutex_init(&conus->loading);
569 gdouble south = CONUS_NORTH - CONUS_DEG_PER_PX*CONUS_HEIGHT;
570 gdouble east = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH;
571 gdouble mid = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH/2;
572 conus->tile[0] = grits_tile_new(NULL, CONUS_NORTH, south, mid, CONUS_WEST);
573 conus->tile[1] = grits_tile_new(NULL, CONUS_NORTH, south, east, mid);
574 conus->tile[0]->zindex = 2;
575 conus->tile[1]->zindex = 1;
576 grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD+2, FALSE);
577 grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD+2, FALSE);
579 conus->time_id = g_signal_connect_swapped(viewer, "time-changed",
580 G_CALLBACK(_conus_update), conus);
581 conus->refresh_id = g_signal_connect_swapped(viewer, "refresh",
582 G_CALLBACK(_conus_update), conus);
584 g_object_set_data(G_OBJECT(conus->config), "conus", conus);
585 gtk_notebook_append_page(GTK_NOTEBOOK(pconfig), conus->config,
586 gtk_label_new("Conus"));
588 _conus_update(conus);
592 void radar_conus_free(RadarConus *conus)
594 g_signal_handler_disconnect(conus->viewer, conus->time_id);
595 g_signal_handler_disconnect(conus->viewer, conus->refresh_id);
597 for (int i = 0; i < 2; i++) {
598 GritsTile *tile = conus->tile[i];
600 glDeleteTextures(1, tile->data);
603 grits_viewer_remove(conus->viewer, GRITS_OBJECT(tile));
606 g_object_unref(conus->viewer);
611 /********************
613 ********************/
614 static void _draw_hud(GritsCallback *callback, GritsOpenGL *opengl, gpointer _self)
616 g_debug("GritsPluginRadar: _draw_hud");
618 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
619 glMatrixMode(GL_PROJECTION); glLoadIdentity();
620 glDisable(GL_TEXTURE_2D);
621 glDisable(GL_ALPHA_TEST);
622 glDisable(GL_CULL_FACE);
623 glDisable(GL_LIGHTING);
624 glEnable(GL_COLOR_MATERIAL);
627 gpointer name, _site;
628 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(_self);
629 g_hash_table_iter_init(&iter, self->sites);
630 while (g_hash_table_iter_next(&iter, &name, &_site)) {
631 /* Pick correct colormaps */
632 RadarSite *site = _site;
633 if (site->hidden || !site->level2)
635 AWeatherColormap *colormap = site->level2->sweep_colors;
637 /* Print the color table */
639 int len = colormap->len;
640 for (int i = 0; i < len; i++) {
641 glColor4ubv(colormap->data[i]);
642 glVertex3f(-1.0, (float)((i ) - len/2)/(len/2), 0.0); // bot left
643 glVertex3f(-1.0, (float)((i+1) - len/2)/(len/2), 0.0); // top left
644 glVertex3f(-0.9, (float)((i+1) - len/2)/(len/2), 0.0); // top right
645 glVertex3f(-0.9, (float)((i ) - len/2)/(len/2), 0.0); // bot right
651 static void _load_colormap(gchar *filename, AWeatherColormap *cm)
653 g_debug("GritsPluginRadar: _load_colormap - %s", filename);
654 FILE *file = fopen(filename, "r");
656 g_error("GritsPluginRadar: open failed");
658 GArray *array = g_array_sized_new(FALSE, TRUE, sizeof(color), 256);
659 if (!fgets(cm->name, sizeof(cm->name), file)) goto out;
660 if (!fscanf(file, "%f\n", &cm->scale)) goto out;
661 if (!fscanf(file, "%f\n", &cm->shift)) goto out;
663 while (fscanf(file, "%d %d %d %d\n", &r, &g, &b, &a) == 4) {
668 g_array_append_val(array, color);
670 cm->len = (gint )array->len;
671 cm->data = (void*)array->data;
673 g_array_free(array, FALSE);
677 static void _update_hidden(GtkNotebook *notebook,
678 GtkNotebookPage *page, guint page_num, gpointer viewer)
680 g_debug("GritsPluginRadar: _update_hidden - 0..%d = %d",
681 gtk_notebook_get_n_pages(notebook), page_num);
683 for (gint i = 0; i < gtk_notebook_get_n_pages(notebook); i++) {
684 gboolean is_hidden = (i != page_num);
685 GtkWidget *config = gtk_notebook_get_nth_page(notebook, i);
686 RadarConus *conus = g_object_get_data(G_OBJECT(config), "conus");
687 RadarSite *site = g_object_get_data(G_OBJECT(config), "site");
691 grits_object_hide(GRITS_OBJECT(conus->tile[0]), is_hidden);
692 grits_object_hide(GRITS_OBJECT(conus->tile[1]), is_hidden);
694 site->hidden = is_hidden;
696 grits_object_hide(GRITS_OBJECT(site->level2), is_hidden);
698 g_warning("GritsPluginRadar: _update_hidden - no site or counus found");
701 gtk_widget_queue_draw(GTK_WIDGET(viewer));
705 GritsPluginRadar *grits_plugin_radar_new(GritsViewer *viewer, GritsPrefs *prefs)
707 /* TODO: move to constructor if possible */
708 g_debug("GritsPluginRadar: new");
709 GritsPluginRadar *self = g_object_new(GRITS_TYPE_PLUGIN_RADAR, NULL);
710 self->viewer = viewer;
713 /* Setup page switching */
714 self->tab_id = g_signal_connect(self->config, "switch-page",
715 G_CALLBACK(_update_hidden), viewer);
718 self->hud = grits_callback_new(_draw_hud, self);
719 grits_viewer_add(viewer, GRITS_OBJECT(self->hud), GRITS_LEVEL_HUD, FALSE);
722 self->conus = radar_conus_new(self->config, self->viewer, self->conus_http);
724 /* Load radar sites */
725 for (city_t *city = cities; city->type; city++) {
726 if (city->type != LOCATION_CITY)
728 RadarSite *site = radar_site_new(city, self->config,
729 self->viewer, self->prefs, self->sites_http);
730 g_hash_table_insert(self->sites, city->code, site);
736 static GtkWidget *grits_plugin_radar_get_config(GritsPlugin *_self)
738 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(_self);
743 static void grits_plugin_radar_plugin_init(GritsPluginInterface *iface);
744 G_DEFINE_TYPE_WITH_CODE(GritsPluginRadar, grits_plugin_radar, G_TYPE_OBJECT,
745 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
746 grits_plugin_radar_plugin_init));
747 static void grits_plugin_radar_plugin_init(GritsPluginInterface *iface)
749 g_debug("GritsPluginRadar: plugin_init");
750 /* Add methods to the interface */
751 iface->get_config = grits_plugin_radar_get_config;
753 static void grits_plugin_radar_init(GritsPluginRadar *self)
755 g_debug("GritsPluginRadar: class_init");
757 self->sites_http = grits_http_new(G_DIR_SEPARATOR_S
758 "nexrad" G_DIR_SEPARATOR_S
759 "level2" G_DIR_SEPARATOR_S);
760 self->conus_http = grits_http_new(G_DIR_SEPARATOR_S
761 "nexrad" G_DIR_SEPARATOR_S
762 "conus" G_DIR_SEPARATOR_S);
763 self->sites = g_hash_table_new_full(g_str_hash, g_str_equal,
764 NULL, (GDestroyNotify)radar_site_free);
765 self->config = gtk_notebook_new();
768 for (int i = 0; colormaps[i].file; i++) {
769 gchar *file = g_build_filename(PKGDATADIR,
770 "colors", colormaps[i].file, NULL);
771 _load_colormap(file, &colormaps[i]);
775 /* Need to position on the top because of Win32 bug */
776 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_LEFT);
778 static void grits_plugin_radar_dispose(GObject *gobject)
780 g_debug("GritsPluginRadar: dispose");
781 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
783 GritsViewer *viewer = self->viewer;
785 g_signal_handler_disconnect(self->config, self->tab_id);
786 grits_viewer_remove(viewer, GRITS_OBJECT(self->hud));
787 radar_conus_free(self->conus);
789 /* Drop references */
790 G_OBJECT_CLASS(grits_plugin_radar_parent_class)->dispose(gobject);
792 static void grits_plugin_radar_finalize(GObject *gobject)
794 g_debug("GritsPluginRadar: finalize");
795 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
797 grits_http_free(self->conus_http);
798 grits_http_free(self->sites_http);
799 g_hash_table_destroy(self->sites);
800 gtk_widget_destroy(self->config);
801 G_OBJECT_CLASS(grits_plugin_radar_parent_class)->finalize(gobject);
804 static void grits_plugin_radar_class_init(GritsPluginRadarClass *klass)
806 g_debug("GritsPluginRadar: class_init");
807 GObjectClass *gobject_class = (GObjectClass*)klass;
808 gobject_class->dispose = grits_plugin_radar_dispose;
809 gobject_class->finalize = grits_plugin_radar_finalize;