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"
35 static void aweather_bin_set_child(GtkBin *bin, GtkWidget *new)
37 GtkWidget *old = gtk_bin_get_child(bin);
39 gtk_widget_destroy(old);
40 gtk_container_add(GTK_CONTAINER(bin), new);
41 gtk_widget_show_all(new);
44 static gchar *_find_nearest(time_t time, GList *files,
47 g_debug("RadarSite: find_nearest ...");
48 time_t nearest_time = 0;
49 char *nearest_file = NULL;
52 for (GList *cur = files; cur; cur = cur->next) {
53 gchar *file = cur->data;
54 sscanf(file+offset, "%4d%2d%2d_%2d%2d",
55 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
56 &tm.tm_hour, &tm.tm_min);
59 if (ABS(time - mktime(&tm)) <
60 ABS(time - nearest_time)) {
62 nearest_time = mktime(&tm);
66 g_debug("RadarSite: find_nearest = %s", nearest_file);
68 return g_strdup(nearest_file);
85 GritsMarker *marker; // Map marker for grits
87 /* Stuff from the parents */
95 RadarSiteStatus status; // Loading status for the site
97 AWeatherLevel2 *level2; // The Level2 structure for the current volume
100 time_t time; // Current timestamp of the level2
101 gchar *message; // Error message set while updating
102 guint time_id; // "time-changed" callback ID
103 guint refresh_id; // "refresh" callback ID
104 guint location_id; // "locaiton-changed" callback ID
105 guint idle_source; // _site_update_end idle source
108 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
109 void _site_update_loading(gchar *file, goffset cur,
110 goffset total, gpointer _site)
112 RadarSite *site = _site;
113 GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(site->config));
114 double percent = (double)cur/total;
115 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
116 gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
117 percent*100, (double)cur/1000000, (double)total/1000000);
118 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
121 gboolean _site_update_end(gpointer _site)
123 RadarSite *site = _site;
125 g_warning("RadarSite: update_end - %s", site->message);
126 const char *fmt = "http://forecast.weather.gov/product.php?site=NWS&product=FTM&format=TXT&issuedby=%s";
127 char *uri = g_strdup_printf(fmt, site->city->code+1);
128 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
129 GtkWidget *msg = gtk_label_new(site->message);
130 GtkWidget *btn = gtk_link_button_new_with_label(uri, "View Radar Status");
131 gtk_box_set_homogeneous(GTK_BOX(box), TRUE);
132 gtk_box_pack_start(GTK_BOX(box), msg, TRUE, TRUE, 0);
133 gtk_box_pack_start(GTK_BOX(box), btn, TRUE, TRUE, 0);
134 aweather_bin_set_child(GTK_BIN(site->config), box);
137 aweather_bin_set_child(GTK_BIN(site->config),
138 aweather_level2_get_config(site->level2));
140 site->status = STATUS_LOADED;
141 site->idle_source = 0;
144 gpointer _site_update_thread(gpointer _site)
146 RadarSite *site = _site;
147 g_debug("RadarSite: update_thread - %s", site->city->code);
148 site->message = NULL;
150 gboolean offline = grits_viewer_get_offline(site->viewer);
151 gchar *nexrad_url = grits_prefs_get_string(site->prefs,
152 "aweather/nexrad_url", NULL);
154 /* Find nearest volume (temporally) */
155 g_debug("RadarSite: update_thread - find nearest - %s", site->city->code);
156 gchar *dir_list = g_strconcat(nexrad_url, "/", site->city->code,
157 "/", "dir.list", NULL);
158 GList *files = grits_http_available(site->http,
159 "^\\w{4}_\\d{8}_\\d{4}$", site->city->code,
160 "\\d+ (.*)", (offline ? NULL : dir_list));
162 gchar *nearest = _find_nearest(site->time, files, 5);
163 g_list_foreach(files, (GFunc)g_free, NULL);
166 site->message = "No suitable files found";
170 /* Fetch new volume */
171 g_debug("RadarSite: update_thread - fetch");
172 gchar *local = g_strconcat(site->city->code, "/", nearest, NULL);
173 gchar *uri = g_strconcat(nexrad_url, "/", local, NULL);
174 gchar *file = grits_http_fetch(site->http, uri, local,
175 offline ? GRITS_LOCAL : GRITS_UPDATE,
176 _site_update_loading, site);
182 site->message = "Fetch failed";
186 /* Load and add new volume */
187 g_debug("RadarSite: update_thread - load - %s", site->city->code);
188 site->level2 = aweather_level2_new_from_file(
189 file, site->city->code, colormaps);
192 site->message = "Load failed";
195 grits_object_hide(GRITS_OBJECT(site->level2), site->hidden);
196 grits_viewer_add(site->viewer, GRITS_OBJECT(site->level2),
197 GRITS_LEVEL_WORLD+3, TRUE);
200 if (!site->idle_source)
201 site->idle_source = g_idle_add(_site_update_end, site);
204 void _site_update(RadarSite *site)
206 if (site->status == STATUS_LOADING)
208 site->status = STATUS_LOADING;
210 site->time = grits_viewer_get_time(site->viewer);
211 g_debug("RadarSite: update %s - %d",
212 site->city->code, (gint)site->time);
214 /* Add a progress bar */
215 GtkWidget *progress = gtk_progress_bar_new();
216 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
217 aweather_bin_set_child(GTK_BIN(site->config), progress);
219 /* Remove old volume */
220 g_debug("RadarSite: update - remove - %s", site->city->code);
222 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
226 /* Fork loading right away so updating the
227 * list of times doesn't take too long */
228 g_thread_new("site-update-thread", _site_update_thread, site);
231 /* RadarSite methods */
232 void radar_site_unload(RadarSite *site)
234 if (site->status != STATUS_LOADED)
235 return; // Abort if it's still loading
237 g_debug("RadarSite: unload %s", site->city->code);
240 g_signal_handler_disconnect(site->viewer, site->time_id);
241 if (site->refresh_id)
242 g_signal_handler_disconnect(site->viewer, site->refresh_id);
243 if (site->idle_source)
244 g_source_remove(site->idle_source);
245 site->idle_source = 0;
249 gtk_widget_destroy(site->config);
253 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
257 site->status = STATUS_UNLOADED;
260 void radar_site_load(RadarSite *site)
262 g_debug("RadarSite: load %s", site->city->code);
265 site->config = gtk_alignment_new(0, 0, 1, 1);
266 g_object_set_data(G_OBJECT(site->config), "site", site);
267 gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig), site->config,
268 gtk_label_new(site->city->name));
269 gtk_widget_show_all(site->config);
270 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(site->pconfig)) == 0)
271 gtk_notebook_set_current_page(GTK_NOTEBOOK(site->pconfig), -1);
273 /* Set up radar loading */
274 site->time_id = g_signal_connect_swapped(site->viewer, "time-changed",
275 G_CALLBACK(_site_update), site);
276 site->refresh_id = g_signal_connect_swapped(site->viewer, "refresh",
277 G_CALLBACK(_site_update), site);
281 void _site_on_location_changed(GritsViewer *viewer,
282 gdouble lat, gdouble lon, gdouble elev,
285 static gdouble min_dist = EARTH_R / 30;
286 RadarSite *site = _site;
288 /* Calculate distance, could cache xyz values */
289 gdouble eye_xyz[3], site_xyz[3];
290 lle2xyz(lat, lon, elev, &eye_xyz[0], &eye_xyz[1], &eye_xyz[2]);
291 lle2xyz(site->city->pos.lat, site->city->pos.lon, site->city->pos.elev,
292 &site_xyz[0], &site_xyz[1], &site_xyz[2]);
293 gdouble dist = distd(site_xyz, eye_xyz);
295 /* Load or unload the site if necessasairy */
296 if (dist <= min_dist && dist < elev*1.25 && site->status == STATUS_UNLOADED)
297 radar_site_load(site);
298 else if (dist > 2*min_dist && site->status != STATUS_UNLOADED)
299 radar_site_unload(site);
302 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
303 GritsViewer *viewer, GritsPrefs *prefs, GritsHttp *http)
305 RadarSite *site = g_new0(RadarSite, 1);
306 site->viewer = g_object_ref(viewer);
307 site->prefs = g_object_ref(prefs);
309 site->http = grits_http_new(G_DIR_SEPARATOR_S
310 "nexrad" G_DIR_SEPARATOR_S
311 "level2" G_DIR_SEPARATOR_S);
313 site->pconfig = pconfig;
316 /* Set initial location */
317 gdouble lat, lon, elev;
318 grits_viewer_get_location(viewer, &lat, &lon, &elev);
319 _site_on_location_changed(viewer, lat, lon, elev, site);
322 site->marker = grits_marker_new(site->city->name);
323 GRITS_OBJECT(site->marker)->center = site->city->pos;
324 GRITS_OBJECT(site->marker)->lod = EARTH_R*0.75*site->city->lod;
325 grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker),
326 GRITS_LEVEL_HUD, FALSE);
328 /* Connect signals */
329 site->location_id = g_signal_connect(viewer, "location-changed",
330 G_CALLBACK(_site_on_location_changed), site);
334 void radar_site_free(RadarSite *site)
336 radar_site_unload(site);
337 grits_viewer_remove(site->viewer, GRITS_OBJECT(site->marker));
338 if (site->location_id)
339 g_signal_handler_disconnect(site->viewer, site->location_id);
340 grits_http_free(site->http);
341 g_object_unref(site->viewer);
342 g_object_unref(site->prefs);
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
361 const gchar *message;
367 guint time_id; // "time-changed" callback ID
368 guint refresh_id; // "refresh" callback ID
369 guint idle_source; // _conus_update_end idle source
372 void _conus_update_loading(gchar *file, goffset cur,
373 goffset total, gpointer _conus)
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);
385 /* Copy images to graphics memory */
386 static void _conus_update_end_copy(GritsTile *tile, guchar *pixels)
389 glGenTextures(1, &tile->tex);
391 gchar *clear = g_malloc0(2048*2048*4);
392 glBindTexture(GL_TEXTURE_2D, tile->tex);
394 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
395 glPixelStorei(GL_PACK_ALIGNMENT, 1);
396 glTexImage2D(GL_TEXTURE_2D, 0, 4, 2048, 2048, 0,
397 GL_RGBA, GL_UNSIGNED_BYTE, clear);
398 glTexSubImage2D(GL_TEXTURE_2D, 0, 1,1, CONUS_WIDTH/2,CONUS_HEIGHT,
399 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
400 tile->coords.n = 1.0/(CONUS_WIDTH/2);
401 tile->coords.w = 1.0/ CONUS_HEIGHT;
402 tile->coords.s = tile->coords.n + CONUS_HEIGHT / 2048.0;
403 tile->coords.e = tile->coords.w + (CONUS_WIDTH/2) / 2048.0;
404 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
405 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
412 /* Split the pixbuf into east and west halves (with 2K sides)
413 * Also map the pixbuf's alpha values */
414 static void _conus_update_end_split(guchar *pixels, guchar *west, guchar *east,
415 gint width, gint height, gint pxsize)
417 g_debug("Conus: update_end_split");
418 guchar *out[] = {west,east};
419 const guchar alphamap[][4] = {
420 {0x04, 0xe9, 0xe7, 0x30},
421 {0x01, 0x9f, 0xf4, 0x60},
422 {0x03, 0x00, 0xf4, 0x90},
424 for (int y = 0; y < height; y++)
425 for (int x = 0; x < width; x++) {
426 gint subx = x % (width/2);
427 gint idx = x / (width/2);
428 guchar *src = &pixels[(y*width+x)*pxsize];
429 guchar *dst = &out[idx][(y*(width/2)+subx)*4];
438 dst[3] = 0xff * 0.75;
439 for (int j = 0; j < G_N_ELEMENTS(alphamap); j++)
440 if (src[0] == alphamap[j][0] &&
441 src[1] == alphamap[j][1] &&
442 src[2] == alphamap[j][2])
443 dst[3] = alphamap[j][3];
448 gboolean _conus_update_end(gpointer _conus)
450 RadarConus *conus = _conus;
451 g_debug("Conus: update_end");
453 /* Check error status */
454 if (conus->message) {
455 g_warning("Conus: update_end - %s", conus->message);
456 aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message));
460 /* Load and pixbuf */
461 GError *error = NULL;
462 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error);
463 if (!pixbuf || error) {
464 g_warning("Conus: update_end - error loading pixbuf: %s", conus->path);
465 aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf"));
466 g_remove(conus->path);
470 /* Split pixels into east/west parts */
471 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
472 gint width = gdk_pixbuf_get_width(pixbuf);
473 gint height = gdk_pixbuf_get_height(pixbuf);
474 gint pxsize = gdk_pixbuf_get_has_alpha(pixbuf) ? 4 : 3;
475 guchar *pixels_west = g_malloc(4*(width/2)*height);
476 guchar *pixels_east = g_malloc(4*(width/2)*height);
477 _conus_update_end_split(pixels, pixels_west, pixels_east,
478 width, height, pxsize);
479 g_object_unref(pixbuf);
481 /* Copy pixels to graphics memory */
482 _conus_update_end_copy(conus->tile[0], pixels_west);
483 _conus_update_end_copy(conus->tile[1], pixels_east);
488 gchar *label = g_path_get_basename(conus->path);
489 aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label));
490 grits_viewer_queue_draw(conus->viewer);
494 conus->idle_source = 0;
496 g_mutex_unlock(&conus->loading);
500 gpointer _conus_update_thread(gpointer _conus)
502 RadarConus *conus = _conus;
503 conus->message = NULL;
506 g_debug("Conus: update_thread - nearest");
507 gboolean offline = grits_viewer_get_offline(conus->viewer);
508 gchar *conus_url = "http://radar.weather.gov/Conus/RadarImg/";
510 if (time(NULL) - conus->time < 60*60*5 && !offline) {
511 /* radar.weather.gov is full of lies.
512 * the index pages get cached and out of date */
513 /* gmtime is not thread safe, but it's not used very often so
514 * hopefully it'll be alright for now... :-( */
515 struct tm *tm = gmtime(&conus->time);
516 time_t onthe8 = conus->time - 60*((tm->tm_min+1)%10+1);
517 tm = gmtime(&onthe8);
518 nearest = g_strdup_printf("Conus_%04d%02d%02d_%02d%02d_N0Ronly.gif",
519 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
520 tm->tm_hour, tm->tm_min);
522 GList *files = grits_http_available(conus->http,
523 "^Conus_[^\"]*_N0Ronly.gif$", "", NULL, NULL);
524 nearest = _find_nearest(conus->time, files, 6);
525 g_list_foreach(files, (GFunc)g_free, NULL);
528 conus->message = "No suitable files";
533 /* Fetch the image */
534 g_debug("Conus: update_thread - fetch");
535 gchar *uri = g_strconcat(conus_url, nearest, NULL);
536 conus->path = grits_http_fetch(conus->http, uri, nearest,
537 offline ? GRITS_LOCAL : GRITS_ONCE,
538 _conus_update_loading, conus);
542 conus->message = "Fetch failed";
547 g_debug("Conus: update_thread - done");
548 if (!conus->idle_source)
549 conus->idle_source = g_idle_add(_conus_update_end, conus);
553 void _conus_update(RadarConus *conus)
555 if (!g_mutex_trylock(&conus->loading))
557 conus->time = grits_viewer_get_time(conus->viewer);
558 g_debug("Conus: update - %d",
561 /* Add a progress bar */
562 GtkWidget *progress = gtk_progress_bar_new();
563 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
564 aweather_bin_set_child(GTK_BIN(conus->config), progress);
566 g_thread_new("conus-update-thread", _conus_update_thread, conus);
569 RadarConus *radar_conus_new(GtkWidget *pconfig,
570 GritsViewer *viewer, GritsHttp *http)
572 RadarConus *conus = g_new0(RadarConus, 1);
573 conus->viewer = g_object_ref(viewer);
575 conus->config = gtk_alignment_new(0, 0, 1, 1);
576 g_mutex_init(&conus->loading);
578 gdouble south = CONUS_NORTH - CONUS_DEG_PER_PX*CONUS_HEIGHT;
579 gdouble east = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH;
580 gdouble mid = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH/2;
581 conus->tile[0] = grits_tile_new(NULL, CONUS_NORTH, south, mid, CONUS_WEST);
582 conus->tile[1] = grits_tile_new(NULL, CONUS_NORTH, south, east, mid);
583 conus->tile[0]->zindex = 2;
584 conus->tile[1]->zindex = 1;
585 grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD+2, FALSE);
586 grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD+2, FALSE);
588 conus->time_id = g_signal_connect_swapped(viewer, "time-changed",
589 G_CALLBACK(_conus_update), conus);
590 conus->refresh_id = g_signal_connect_swapped(viewer, "refresh",
591 G_CALLBACK(_conus_update), conus);
593 g_object_set_data(G_OBJECT(conus->config), "conus", conus);
594 gtk_notebook_append_page(GTK_NOTEBOOK(pconfig), conus->config,
595 gtk_label_new("Conus"));
597 _conus_update(conus);
601 void radar_conus_free(RadarConus *conus)
603 g_signal_handler_disconnect(conus->viewer, conus->time_id);
604 g_signal_handler_disconnect(conus->viewer, conus->refresh_id);
605 if (conus->idle_source)
606 g_source_remove(conus->idle_source);
608 for (int i = 0; i < 2; i++) {
609 GritsTile *tile = conus->tile[i];
610 grits_viewer_remove(conus->viewer, GRITS_OBJECT(tile));
611 g_object_unref(tile);
614 g_object_unref(conus->viewer);
619 /********************
621 ********************/
622 static void _draw_hud(GritsCallback *callback, GritsOpenGL *opengl, gpointer _self)
624 g_debug("GritsPluginRadar: _draw_hud");
626 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
627 glMatrixMode(GL_PROJECTION); glLoadIdentity();
628 glDisable(GL_TEXTURE_2D);
629 glDisable(GL_ALPHA_TEST);
630 glDisable(GL_CULL_FACE);
631 glDisable(GL_LIGHTING);
632 glEnable(GL_COLOR_MATERIAL);
635 gpointer name, _site;
636 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(_self);
637 g_hash_table_iter_init(&iter, self->sites);
638 while (g_hash_table_iter_next(&iter, &name, &_site)) {
639 /* Pick correct colormaps */
640 RadarSite *site = _site;
641 if (site->hidden || !site->level2)
643 AWeatherColormap *colormap = site->level2->sweep_colors;
645 /* Print the color table */
647 int len = colormap->len;
648 for (int i = 0; i < len; i++) {
649 glColor4ubv(colormap->data[i]);
650 glVertex3f(-1.0, (float)((i ) - len/2)/(len/2), 0.0); // bot left
651 glVertex3f(-1.0, (float)((i+1) - len/2)/(len/2), 0.0); // top left
652 glVertex3f(-0.9, (float)((i+1) - len/2)/(len/2), 0.0); // top right
653 glVertex3f(-0.9, (float)((i ) - len/2)/(len/2), 0.0); // bot right
659 static void _load_colormap(gchar *filename, AWeatherColormap *cm)
661 g_debug("GritsPluginRadar: _load_colormap - %s", filename);
662 FILE *file = fopen(filename, "r");
664 g_error("GritsPluginRadar: open failed");
666 GArray *array = g_array_sized_new(FALSE, TRUE, sizeof(color), 256);
667 if (!fgets(cm->name, sizeof(cm->name), file)) goto out;
668 if (!fscanf(file, "%f\n", &cm->scale)) goto out;
669 if (!fscanf(file, "%f\n", &cm->shift)) goto out;
671 while (fscanf(file, "%d %d %d %d\n", &r, &g, &b, &a) == 4) {
676 g_array_append_val(array, color);
678 cm->len = (gint )array->len;
679 cm->data = (void*)array->data;
681 g_array_free(array, FALSE);
685 static void _update_hidden(GtkNotebook *notebook,
686 gpointer _, guint page_num, gpointer viewer)
688 g_debug("GritsPluginRadar: _update_hidden - 0..%d = %d",
689 gtk_notebook_get_n_pages(notebook), page_num);
691 for (gint i = 0; i < gtk_notebook_get_n_pages(notebook); i++) {
692 gboolean is_hidden = (i != page_num);
693 GtkWidget *config = gtk_notebook_get_nth_page(notebook, i);
694 RadarConus *conus = g_object_get_data(G_OBJECT(config), "conus");
695 RadarSite *site = g_object_get_data(G_OBJECT(config), "site");
699 grits_object_hide(GRITS_OBJECT(conus->tile[0]), is_hidden);
700 grits_object_hide(GRITS_OBJECT(conus->tile[1]), is_hidden);
702 site->hidden = is_hidden;
704 grits_object_hide(GRITS_OBJECT(site->level2), is_hidden);
706 g_warning("GritsPluginRadar: _update_hidden - no site or counus found");
709 grits_viewer_queue_draw(viewer);
713 GritsPluginRadar *grits_plugin_radar_new(GritsViewer *viewer, GritsPrefs *prefs)
715 /* TODO: move to constructor if possible */
716 g_debug("GritsPluginRadar: new");
717 GritsPluginRadar *self = g_object_new(GRITS_TYPE_PLUGIN_RADAR, NULL);
718 self->viewer = g_object_ref(viewer);
719 self->prefs = g_object_ref(prefs);
721 /* Setup page switching */
722 self->tab_id = g_signal_connect(self->config, "switch-page",
723 G_CALLBACK(_update_hidden), viewer);
726 self->hud = grits_callback_new(_draw_hud, self);
727 grits_viewer_add(viewer, GRITS_OBJECT(self->hud), GRITS_LEVEL_HUD, FALSE);
730 self->conus = radar_conus_new(self->config, self->viewer, self->conus_http);
732 /* Load radar sites */
733 for (city_t *city = cities; city->type; city++) {
734 if (city->type != LOCATION_CITY)
736 RadarSite *site = radar_site_new(city, self->config,
737 self->viewer, self->prefs, self->sites_http);
738 g_hash_table_insert(self->sites, city->code, site);
744 static GtkWidget *grits_plugin_radar_get_config(GritsPlugin *_self)
746 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(_self);
751 static void grits_plugin_radar_plugin_init(GritsPluginInterface *iface);
752 G_DEFINE_TYPE_WITH_CODE(GritsPluginRadar, grits_plugin_radar, G_TYPE_OBJECT,
753 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
754 grits_plugin_radar_plugin_init));
755 static void grits_plugin_radar_plugin_init(GritsPluginInterface *iface)
757 g_debug("GritsPluginRadar: plugin_init");
758 /* Add methods to the interface */
759 iface->get_config = grits_plugin_radar_get_config;
761 static void grits_plugin_radar_init(GritsPluginRadar *self)
763 g_debug("GritsPluginRadar: class_init");
765 self->sites_http = grits_http_new(G_DIR_SEPARATOR_S
766 "nexrad" G_DIR_SEPARATOR_S
767 "level2" G_DIR_SEPARATOR_S);
768 self->conus_http = grits_http_new(G_DIR_SEPARATOR_S
769 "nexrad" G_DIR_SEPARATOR_S
770 "conus" G_DIR_SEPARATOR_S);
771 self->sites = g_hash_table_new_full(g_str_hash, g_str_equal,
772 NULL, (GDestroyNotify)radar_site_free);
773 self->config = g_object_ref(gtk_notebook_new());
776 for (int i = 0; colormaps[i].file; i++) {
777 gchar *file = g_build_filename(PKGDATADIR,
778 "colors", colormaps[i].file, NULL);
779 _load_colormap(file, &colormaps[i]);
783 /* Need to position on the top because of Win32 bug */
784 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_LEFT);
786 static void grits_plugin_radar_dispose(GObject *gobject)
788 g_debug("GritsPluginRadar: dispose");
789 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
791 GritsViewer *viewer = self->viewer;
793 g_signal_handler_disconnect(self->config, self->tab_id);
794 grits_viewer_remove(viewer, GRITS_OBJECT(self->hud));
795 radar_conus_free(self->conus);
796 g_hash_table_destroy(self->sites);
797 g_object_unref(self->config);
798 g_object_unref(self->hud);
799 g_object_unref(self->prefs);
800 g_object_unref(viewer);
802 /* Drop references */
803 G_OBJECT_CLASS(grits_plugin_radar_parent_class)->dispose(gobject);
805 static void grits_plugin_radar_finalize(GObject *gobject)
807 g_debug("GritsPluginRadar: finalize");
808 GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
810 grits_http_free(self->conus_http);
811 grits_http_free(self->sites_http);
812 gtk_widget_destroy(self->config);
813 G_OBJECT_CLASS(grits_plugin_radar_parent_class)->finalize(gobject);
816 static void grits_plugin_radar_class_init(GritsPluginRadarClass *klass)
818 g_debug("GritsPluginRadar: class_init");
819 GObjectClass *gobject_class = (GObjectClass*)klass;
820 gobject_class->dispose = grits_plugin_radar_dispose;
821 gobject_class->finalize = grits_plugin_radar_finalize;