2 * Copyright (C) 2009-2011 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 _gtk_bin_set_child(GTK_BIN(site->config),
124 gtk_label_new(site->message));
126 _gtk_bin_set_child(GTK_BIN(site->config),
127 aweather_level2_get_config(site->level2));
129 site->status = STATUS_LOADED;
132 gpointer _site_update_thread(gpointer _site)
134 RadarSite *site = _site;
135 g_debug("RadarSite: update_thread - %s", site->city->code);
136 site->message = NULL;
138 gboolean offline = grits_viewer_get_offline(site->viewer);
139 gchar *nexrad_url = grits_prefs_get_string(site->prefs,
140 "aweather/nexrad_url", NULL);
142 /* Find nearest volume (temporally) */
143 g_debug("RadarSite: update_thread - find nearest - %s", site->city->code);
144 gchar *dir_list = g_strconcat(nexrad_url, "/", site->city->code,
145 "/", "dir.list", NULL);
146 GList *files = grits_http_available(site->http,
147 "^\\w{4}_\\d{8}_\\d{4}$", site->city->code,
148 "\\d+ (.*)", (offline ? NULL : dir_list));
150 gchar *nearest = _find_nearest(site->time, files, 5);
151 g_list_foreach(files, (GFunc)g_free, NULL);
154 site->message = "No suitable files found";
158 /* Fetch new volume */
159 g_debug("RadarSite: update_thread - fetch");
160 gchar *local = g_strconcat(site->city->code, "/", nearest, NULL);
161 gchar *uri = g_strconcat(nexrad_url, "/", local, NULL);
162 gchar *file = grits_http_fetch(site->http, uri, local,
163 offline ? GRITS_LOCAL : GRITS_UPDATE,
164 _site_update_loading, site);
170 site->message = "Fetch failed";
174 /* Load and add new volume */
175 g_debug("RadarSite: update_thread - load - %s", site->city->code);
176 site->level2 = aweather_level2_new_from_file(
177 file, site->city->code, colormaps);
180 site->message = "Load failed";
183 grits_object_hide(GRITS_OBJECT(site->level2), site->hidden);
184 grits_viewer_add(site->viewer, GRITS_OBJECT(site->level2),
185 GRITS_LEVEL_WORLD+3, TRUE);
188 g_idle_add(_site_update_end, site);
191 void _site_update(RadarSite *site)
193 if (site->status == STATUS_LOADING)
195 site->status = STATUS_LOADING;
197 site->time = grits_viewer_get_time(site->viewer);
198 g_debug("RadarSite: update %s - %d",
199 site->city->code, (gint)site->time);
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);
206 /* Remove old volume */
207 g_debug("RadarSite: update - remove - %s", site->city->code);
209 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
213 /* Fork loading right away so updating the
214 * list of times doesn't take too long */
215 g_thread_create(_site_update_thread, site, FALSE, NULL);
218 /* RadarSite methods */
219 void radar_site_unload(RadarSite *site)
221 if (site->status != STATUS_LOADED)
222 return; // Abort if it's still loading
224 g_debug("RadarSite: unload %s", site->city->code);
227 g_signal_handler_disconnect(site->viewer, site->time_id);
228 if (site->refresh_id)
229 g_signal_handler_disconnect(site->viewer, site->refresh_id);
233 gtk_widget_destroy(site->config);
237 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
241 site->status = STATUS_UNLOADED;
244 void radar_site_load(RadarSite *site)
246 g_debug("RadarSite: load %s", site->city->code);
249 site->config = gtk_alignment_new(0, 0, 1, 1);
250 g_object_set_data(G_OBJECT(site->config), "site", site);
251 gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig), site->config,
252 gtk_label_new(site->city->name));
253 gtk_widget_show_all(site->config);
254 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(site->pconfig)) == 0)
255 gtk_notebook_set_current_page(GTK_NOTEBOOK(site->pconfig), -1);
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);
265 void _site_on_location_changed(GritsViewer *viewer,
266 gdouble lat, gdouble lon, gdouble elev,
269 static gdouble min_dist = EARTH_R / 30;
270 RadarSite *site = _site;
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->city->pos.lat, site->city->pos.lon, site->city->pos.elev,
276 &site_xyz[0], &site_xyz[1], &site_xyz[2]);
277 gdouble dist = distd(site_xyz, eye_xyz);
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);
286 gboolean _site_add_marker(gpointer _site)
288 RadarSite *site = _site;
289 site->marker = grits_marker_new(site->city->name);
290 GRITS_OBJECT(site->marker)->center = site->city->pos;
291 GRITS_OBJECT(site->marker)->lod = EARTH_R*2*site->city->lod;
292 grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker),
293 GRITS_LEVEL_OVERLAY, FALSE);
296 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
297 GritsViewer *viewer, GritsPrefs *prefs, GritsHttp *http)
299 RadarSite *site = g_new0(RadarSite, 1);
300 site->viewer = g_object_ref(viewer);
301 site->prefs = g_object_ref(prefs);
303 site->http = grits_http_new(G_DIR_SEPARATOR_S
304 "nexrad" G_DIR_SEPARATOR_S
305 "level2" G_DIR_SEPARATOR_S);
307 site->pconfig = pconfig;
310 /* Set initial location */
311 gdouble lat, lon, elev;
312 grits_viewer_get_location(viewer, &lat, &lon, &elev);
313 _site_on_location_changed(viewer, lat, lon, elev, site);
316 g_idle_add_full(G_PRIORITY_LOW, _site_add_marker, site, NULL);
318 /* Connect signals */
319 site->location_id = g_signal_connect(viewer, "location-changed",
320 G_CALLBACK(_site_on_location_changed), site);
324 void radar_site_free(RadarSite *site)
326 radar_site_unload(site);
327 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->marker));
328 if (site->location_id)
329 g_signal_handler_disconnect(site->viewer, site->location_id);
330 grits_http_free(site->http);
331 g_object_unref(site->viewer);
332 g_object_unref(site->prefs);
340 #define CONUS_NORTH 50.406626367301044
341 #define CONUS_WEST -127.620375523875420
342 #define CONUS_WIDTH 3400.0
343 #define CONUS_HEIGHT 1600.0
344 #define CONUS_DEG_PER_PX 0.017971305190311
351 const gchar *message;
352 GStaticMutex loading;
357 guint time_id; // "time-changed" callback ID
358 guint refresh_id; // "refresh" callback ID
361 void _conus_update_loading(gchar *file, goffset cur,
362 goffset total, gpointer _conus)
364 RadarConus *conus = _conus;
365 GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(conus->config));
366 double percent = (double)cur/total;
367 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
368 gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
369 percent*100, (double)cur/1000000, (double)total/1000000);
370 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
374 /* Copy images to graphics memory */
375 static void _conus_update_end_copy(GritsTile *tile, guchar *pixels)
378 tile->data = g_new0(guint, 1);
379 glGenTextures(1, tile->data);
382 gchar *clear = g_malloc0(2048*2048*4);
383 guint *tex = tile->data;
384 glBindTexture(GL_TEXTURE_2D, *tex);
386 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
387 glPixelStorei(GL_PACK_ALIGNMENT, 1);
388 glTexImage2D(GL_TEXTURE_2D, 0, 4, 2048, 2048, 0,
389 GL_RGBA, GL_UNSIGNED_BYTE, clear);
390 glTexSubImage2D(GL_TEXTURE_2D, 0, 1,1, CONUS_WIDTH/2,CONUS_HEIGHT,
391 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
392 tile->coords.n = 1.0/(CONUS_WIDTH/2);
393 tile->coords.w = 1.0/ CONUS_HEIGHT;
394 tile->coords.s = tile->coords.n + CONUS_HEIGHT / 2048.0;
395 tile->coords.e = tile->coords.w + (CONUS_WIDTH/2) / 2048.0;
396 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
397 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
398 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
404 /* Split the pixbuf into east and west halves (with 2K sides)
405 * Also map the pixbuf's alpha values */
406 static void _conus_update_end_split(guchar *pixels, guchar *west, guchar *east,
407 gint width, gint height, gint pxsize)
409 g_debug("Conus: update_end_split");
410 guchar *out[] = {west,east};
411 const guchar alphamap[][4] = {
412 {0x04, 0xe9, 0xe7, 0x30},
413 {0x01, 0x9f, 0xf4, 0x60},
414 {0x03, 0x00, 0xf4, 0x90},
416 for (int y = 0; y < height; y++)
417 for (int x = 0; x < width; x++) {
418 gint subx = x % (width/2);
419 gint idx = x / (width/2);
420 guchar *src = &pixels[(y*width+x)*pxsize];
421 guchar *dst = &out[idx][(y*(width/2)+subx)*4];
430 dst[3] = 0xff * 0.75;
431 for (int j = 0; j < G_N_ELEMENTS(alphamap); j++)
432 if (src[0] == alphamap[j][0] &&
433 src[1] == alphamap[j][1] &&
434 src[2] == alphamap[j][2])
435 dst[3] = alphamap[j][3];
440 gboolean _conus_update_end(gpointer _conus)
442 RadarConus *conus = _conus;
443 g_debug("Conus: update_end");
445 /* Check error status */
446 if (conus->message) {
447 g_warning("Conus: update_end - %s", conus->message);
448 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message));
452 /* Load and pixbuf */
453 GError *error = NULL;
454 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error);
455 if (!pixbuf || error) {
456 g_warning("Conus: update_end - error loading pixbuf: %s", conus->path);
457 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf"));
458 g_remove(conus->path);
462 /* Split pixels into east/west parts */
463 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
464 gint width = gdk_pixbuf_get_width(pixbuf);
465 gint height = gdk_pixbuf_get_height(pixbuf);
466 gint pxsize = gdk_pixbuf_get_has_alpha(pixbuf) ? 4 : 3;
467 guchar *pixels_west = g_malloc(4*(width/2)*height);
468 guchar *pixels_east = g_malloc(4*(width/2)*height);
469 _conus_update_end_split(pixels, pixels_west, pixels_east,
470 width, height, pxsize);
471 g_object_unref(pixbuf);
473 /* Copy pixels to graphics memory */
474 _conus_update_end_copy(conus->tile[0], pixels_west);
475 _conus_update_end_copy(conus->tile[1], pixels_east);
480 gchar *label = g_path_get_basename(conus->path);
481 _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label));
482 gtk_widget_queue_draw(GTK_WIDGET(conus->viewer));
487 g_static_mutex_unlock(&conus->loading);
491 gpointer _conus_update_thread(gpointer _conus)
493 RadarConus *conus = _conus;
494 conus->message = NULL;
497 g_debug("Conus: update_thread - nearest");
498 gboolean offline = grits_viewer_get_offline(conus->viewer);
499 gchar *conus_url = "http://radar.weather.gov/Conus/RadarImg/";
501 if (time(NULL) - conus->time < 60*60*5 && !offline) {
502 /* radar.weather.gov is full of lies.
503 * the index pages get cached and out of date */
504 /* gmtime is not thread safe, but it's not used very often so
505 * hopefully it'll be alright for now... :-( */
506 struct tm *tm = gmtime(&conus->time);
507 time_t onthe8 = conus->time - 60*((tm->tm_min+1)%10+1);
508 tm = gmtime(&onthe8);
509 nearest = g_strdup_printf("Conus_%04d%02d%02d_%02d%02d_N0Ronly.gif",
510 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
511 tm->tm_hour, tm->tm_min);
513 GList *files = grits_http_available(conus->http,
514 "^Conus_[^\"]*_N0Ronly.gif$", "", NULL, NULL);
515 nearest = _find_nearest(conus->time, files, 6);
516 g_list_foreach(files, (GFunc)g_free, NULL);
519 conus->message = "No suitable files";
524 /* Fetch the image */
525 g_debug("Conus: update_thread - fetch");
526 gchar *uri = g_strconcat(conus_url, nearest, NULL);
527 conus->path = grits_http_fetch(conus->http, uri, nearest,
528 offline ? GRITS_LOCAL : GRITS_ONCE,
529 _conus_update_loading, conus);
533 conus->message = "Fetch failed";
538 g_debug("Conus: update_thread - done");
539 g_idle_add(_conus_update_end, conus);
543 void _conus_update(RadarConus *conus)
545 if (!g_static_mutex_trylock(&conus->loading))
547 conus->time = grits_viewer_get_time(conus->viewer);
548 g_debug("Conus: update - %d",
551 /* Add a progress bar */
552 GtkWidget *progress = gtk_progress_bar_new();
553 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
554 _gtk_bin_set_child(GTK_BIN(conus->config), progress);
556 g_thread_create(_conus_update_thread, conus, FALSE, NULL);
559 RadarConus *radar_conus_new(GtkWidget *pconfig,
560 GritsViewer *viewer, GritsHttp *http)
562 RadarConus *conus = g_new0(RadarConus, 1);
563 conus->viewer = g_object_ref(viewer);
565 conus->config = gtk_alignment_new(0, 0, 1, 1);
566 g_static_mutex_init(&conus->loading);
568 gdouble south = CONUS_NORTH - CONUS_DEG_PER_PX*CONUS_HEIGHT;
569 gdouble east = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH;
570 gdouble mid = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH/2;
571 conus->tile[0] = grits_tile_new(NULL, CONUS_NORTH, south, mid, CONUS_WEST);
572 conus->tile[1] = grits_tile_new(NULL, CONUS_NORTH, south, east, mid);
573 conus->tile[0]->zindex = 2;
574 conus->tile[1]->zindex = 1;
575 grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD+2, FALSE);
576 grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD+2, FALSE);
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);
583 g_object_set_data(G_OBJECT(conus->config), "conus", conus);
584 gtk_notebook_append_page(GTK_NOTEBOOK(pconfig), conus->config,
585 gtk_label_new("Conus"));
587 _conus_update(conus);
591 void radar_conus_free(RadarConus *conus)
593 g_signal_handler_disconnect(conus->viewer, conus->time_id);
594 g_signal_handler_disconnect(conus->viewer, conus->refresh_id);
596 for (int i = 0; i < 2; i++) {
597 GritsTile *tile = conus->tile[i];
599 glDeleteTextures(1, tile->data);
602 grits_viewer_remove(conus->viewer, GRITS_OBJECT(tile));
605 g_object_unref(conus->viewer);
610 /********************
612 ********************/
613 static void _draw_hud(GritsCallback *callback, GritsOpenGL *opengl, gpointer _self)
615 g_debug("GritsPluginRadar: _draw_hud");
617 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
618 glMatrixMode(GL_PROJECTION); glLoadIdentity();
619 glDisable(GL_TEXTURE_2D);
620 glDisable(GL_ALPHA_TEST);
621 glDisable(GL_CULL_FACE);
622 glDisable(GL_LIGHTING);
623 glEnable(GL_COLOR_MATERIAL);
626 gpointer name, _site;
627 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(_self);
628 g_hash_table_iter_init(&iter, self->sites);
629 while (g_hash_table_iter_next(&iter, &name, &_site)) {
630 /* Pick correct colormaps */
631 RadarSite *site = _site;
632 if (site->hidden || !site->level2)
634 AWeatherColormap *colormap = site->level2->sweep_colors;
636 /* Print the color table */
638 int len = colormap->len;
639 for (int i = 0; i < len; i++) {
640 glColor4ubv(colormap->data[i]);
641 glVertex3f(-1.0, (float)((i ) - len/2)/(len/2), 0.0); // bot left
642 glVertex3f(-1.0, (float)((i+1) - len/2)/(len/2), 0.0); // top left
643 glVertex3f(-0.9, (float)((i+1) - len/2)/(len/2), 0.0); // top right
644 glVertex3f(-0.9, (float)((i ) - len/2)/(len/2), 0.0); // bot right
650 static void _load_colormap(gchar *filename, AWeatherColormap *cm)
652 g_debug("GritsPluginRadar: _load_colormap - %s", filename);
653 FILE *file = fopen(filename, "r");
655 g_error("GritsPluginRadar: open failed");
657 GArray *array = g_array_sized_new(FALSE, TRUE, sizeof(color), 256);
658 if (!fgets(cm->name, sizeof(cm->name), file)) goto out;
659 if (!fscanf(file, "%f\n", &cm->scale)) goto out;
660 if (!fscanf(file, "%f\n", &cm->shift)) goto out;
662 while (fscanf(file, "%d %d %d %d\n", &r, &g, &b, &a) == 4) {
667 g_array_append_val(array, color);
669 cm->len = (gint )array->len;
670 cm->data = (void*)array->data;
672 g_array_free(array, FALSE);
676 static void _update_hidden(GtkNotebook *notebook,
677 GtkNotebookPage *page, guint page_num, gpointer viewer)
679 g_debug("GritsPluginRadar: _update_hidden - 0..%d = %d",
680 gtk_notebook_get_n_pages(notebook), page_num);
682 for (gint i = 0; i < gtk_notebook_get_n_pages(notebook); i++) {
683 gboolean is_hidden = (i != page_num);
684 GtkWidget *config = gtk_notebook_get_nth_page(notebook, i);
685 RadarConus *conus = g_object_get_data(G_OBJECT(config), "conus");
686 RadarSite *site = g_object_get_data(G_OBJECT(config), "site");
690 grits_object_hide(GRITS_OBJECT(conus->tile[0]), is_hidden);
691 grits_object_hide(GRITS_OBJECT(conus->tile[1]), is_hidden);
693 site->hidden = is_hidden;
695 grits_object_hide(GRITS_OBJECT(site->level2), is_hidden);
697 g_warning("GritsPluginRadar: _update_hidden - no site or counus found");
700 gtk_widget_queue_draw(GTK_WIDGET(viewer));
704 GritsPluginRadar *grits_plugin_radar_new(GritsViewer *viewer, GritsPrefs *prefs)
706 /* TODO: move to constructor if possible */
707 g_debug("GritsPluginRadar: new");
708 GritsPluginRadar *self = g_object_new(GRITS_TYPE_PLUGIN_RADAR, NULL);
709 self->viewer = viewer;
712 /* Setup page switching */
713 self->tab_id = g_signal_connect(self->config, "switch-page",
714 G_CALLBACK(_update_hidden), viewer);
717 self->hud = grits_callback_new(_draw_hud, self);
718 grits_viewer_add(viewer, GRITS_OBJECT(self->hud), GRITS_LEVEL_HUD, FALSE);
721 self->conus = radar_conus_new(self->config, self->viewer, self->conus_http);
723 /* Load radar sites */
724 for (city_t *city = cities; city->type; city++) {
725 if (city->type != LOCATION_CITY)
727 RadarSite *site = radar_site_new(city, self->config,
728 self->viewer, self->prefs, self->sites_http);
729 g_hash_table_insert(self->sites, city->code, site);
735 static GtkWidget *grits_plugin_radar_get_config(GritsPlugin *_self)
737 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(_self);
742 static void grits_plugin_radar_plugin_init(GritsPluginInterface *iface);
743 G_DEFINE_TYPE_WITH_CODE(GritsPluginRadar, grits_plugin_radar, G_TYPE_OBJECT,
744 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
745 grits_plugin_radar_plugin_init));
746 static void grits_plugin_radar_plugin_init(GritsPluginInterface *iface)
748 g_debug("GritsPluginRadar: plugin_init");
749 /* Add methods to the interface */
750 iface->get_config = grits_plugin_radar_get_config;
752 static void grits_plugin_radar_init(GritsPluginRadar *self)
754 g_debug("GritsPluginRadar: class_init");
756 self->sites_http = grits_http_new(G_DIR_SEPARATOR_S
757 "nexrad" G_DIR_SEPARATOR_S
758 "level2" G_DIR_SEPARATOR_S);
759 self->conus_http = grits_http_new(G_DIR_SEPARATOR_S
760 "nexrad" G_DIR_SEPARATOR_S
761 "conus" G_DIR_SEPARATOR_S);
762 self->sites = g_hash_table_new_full(g_str_hash, g_str_equal,
763 NULL, (GDestroyNotify)radar_site_free);
764 self->config = gtk_notebook_new();
767 for (int i = 0; colormaps[i].file; i++) {
768 gchar *file = g_build_filename(PKGDATADIR,
769 "colors", colormaps[i].file, NULL);
770 _load_colormap(file, &colormaps[i]);
774 /* Need to position on the top because of Win32 bug */
775 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_LEFT);
777 static void grits_plugin_radar_dispose(GObject *gobject)
779 g_debug("GritsPluginRadar: dispose");
780 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
781 g_signal_handler_disconnect(self->config, self->tab_id);
782 grits_viewer_remove(self->viewer, GRITS_OBJECT(self->hud));
783 radar_conus_free(self->conus);
784 /* Drop references */
785 G_OBJECT_CLASS(grits_plugin_radar_parent_class)->dispose(gobject);
787 static void grits_plugin_radar_finalize(GObject *gobject)
789 g_debug("GritsPluginRadar: finalize");
790 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
792 grits_http_free(self->conus_http);
793 grits_http_free(self->sites_http);
794 g_hash_table_destroy(self->sites);
795 gtk_widget_destroy(self->config);
796 G_OBJECT_CLASS(grits_plugin_radar_parent_class)->finalize(gobject);
799 static void grits_plugin_radar_class_init(GritsPluginRadarClass *klass)
801 g_debug("GritsPluginRadar: class_init");
802 GObjectClass *gobject_class = (GObjectClass*)klass;
803 gobject_class->dispose = grits_plugin_radar_dispose;
804 gobject_class->finalize = grits_plugin_radar_finalize;