2 * Copyright (C) 2009-2010 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>
23 #include <gtk/gtkgl.h>
33 #include "../aweather-location.h"
35 void _gtk_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,
45 gsize offset, gchar *format)
47 g_debug("GisPluginRadar: _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 strptime(file+offset, format, &tm);
55 if (ABS(time - mktime(&tm)) <
56 ABS(time - nearest_time)) {
58 nearest_time = mktime(&tm);
62 g_debug("GisPluginRadar: _find_nearest = %s", nearest_file);
64 return g_strdup(nearest_file);
81 GisMarker *marker; // Map marker for libgis
82 gpointer *marker_ref; // Reference to maker
84 /* Stuff from the parents */
91 RadarSiteStatus status; // Loading status for the site
93 AWeatherLevel2 *level2; // The Level2 structure for the current volume
94 gpointer level2_ref; // GisViewer reference to the added radar
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
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)
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);
117 gboolean _site_update_end(gpointer _site)
119 RadarSite *site = _site;
121 g_warning("GisPluginRadar: _update_end - %s", site->message);
122 _gtk_bin_set_child(GTK_BIN(site->config), gtk_label_new(site->message));
124 _gtk_bin_set_child(GTK_BIN(site->config),
125 aweather_level2_get_config(site->level2));
127 site->status = STATUS_LOADED;
130 gpointer _site_update_thread(gpointer _site)
132 RadarSite *site = _site;
133 g_debug("GisPluginRadar: _update - %s", site->city->code);
134 site->status = STATUS_LOADING;
135 site->message = NULL;
137 gboolean offline = gis_viewer_get_offline(site->viewer);
138 gchar *nexrad_url = gis_prefs_get_string(site->prefs,
139 "aweather/nexrad_url", NULL);
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;
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));
156 gchar *nearest = _find_nearest(site->time, files, 5, "%Y%m%d_%H%M");
157 g_list_foreach(files, (GFunc)g_free, NULL);
160 site->message = "No suitable files found";
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);
176 site->message = "Fetch failed";
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);
186 site->message = "Load failed";
189 site->level2_ref = gis_viewer_add(site->viewer,
190 GIS_OBJECT(site->level2), GIS_LEVEL_WORLD, TRUE);
193 g_idle_add(_site_update_end, site);
196 void _site_update(RadarSite *site)
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);
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);
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);
212 /* RadarSite methods */
213 void radar_site_unload(RadarSite *site)
215 if (site->status != STATUS_LOADED)
216 return; // Abort if it's still loading
218 g_debug("GisPluginRadar: radar_site_unload %s", site->city->code);
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);
227 gtk_widget_destroy(site->config);
230 if (site->level2_ref) {
231 gis_viewer_remove(site->viewer, site->level2_ref);
232 site->level2_ref = NULL;
235 site->status = STATUS_UNLOADED;
238 void radar_site_load(RadarSite *site)
240 g_debug("GisPluginRadar: radar_site_load %s", site->city->code);
241 site->status = STATUS_LOADING;
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),
258 gtk_widget_show_all(site->config);
259 gtk_widget_show_all(tab);
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);
269 void _site_on_location_changed(GisViewer *viewer,
270 gdouble lat, gdouble lon, gdouble elev,
273 static gdouble min_dist = EARTH_R / 20;
274 RadarSite *site = _site;
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);
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);
290 gboolean _site_add_marker(gpointer _site)
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);
300 RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
301 GisViewer *viewer, GisPrefs *prefs, GisHttp *http)
303 RadarSite *site = g_new0(RadarSite, 1);
304 site->viewer = g_object_ref(viewer);
305 site->prefs = g_object_ref(prefs);
308 site->pconfig = pconfig;
311 g_idle_add_full(G_PRIORITY_LOW, _site_add_marker, site, NULL);
314 /* Connect signals */
315 site->location_id = g_signal_connect(viewer, "location-changed",
316 G_CALLBACK(_site_on_location_changed), site);
320 void radar_site_free(RadarSite *site)
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);
343 const gchar *message;
346 guint time_id; // "time-changed" callback ID
347 guint refresh_id; // "refresh" callback ID
350 void _conus_update_loading(gchar *file, goffset cur,
351 goffset total, gpointer _conus)
353 RadarConus *conus = _conus;
354 GtkWidget *progress_bar = gtk_bin_get_child(GTK_BIN(conus->config));
355 double percent = (double)cur/total;
356 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), MIN(percent, 1.0));
357 gchar *msg = g_strdup_printf("Loading... %5.1f%% (%.2f/%.2f MB)",
358 percent*100, (double)cur/1000000, (double)total/1000000);
359 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), msg);
363 gboolean _conus_update_end(gpointer _conus)
365 RadarConus *conus = _conus;
367 guchar *pixels = gdk_pixbuf_get_pixels(conus->pixbuf);
368 gboolean alpha = gdk_pixbuf_get_has_alpha(conus->pixbuf);
369 gint width = gdk_pixbuf_get_width(conus->pixbuf);
370 gint height = gdk_pixbuf_get_height(conus->pixbuf);
372 if (!conus->tile->data) {
373 conus->tile->data = g_new0(guint, 1);
374 glGenTextures(1, conus->tile->data);
377 guint *tex = conus->tile->data;
378 glBindTexture(GL_TEXTURE_2D, *tex);
380 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
381 glPixelStorei(GL_PACK_ALIGNMENT, 1);
382 glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
383 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
384 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
385 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
391 _gtk_bin_set_child(GTK_BIN(conus->config),
392 gtk_label_new(conus->nearest));
393 gtk_widget_queue_draw(GTK_WIDGET(conus->viewer));
396 g_object_unref(conus->pixbuf);
397 g_free(conus->nearest);
402 gpointer _conus_update_thread(gpointer _conus)
404 RadarConus *conus = _conus;
407 gboolean offline = gis_viewer_get_offline(conus->viewer);
408 gchar *conus_url = "http://radar.weather.gov/Conus/RadarImg/";
409 GList *files = gis_http_available(conus->http,
410 "^Conus_[^\"]*_N0Ronly.gif$", "",
411 NULL, (offline ? NULL : conus_url));
412 conus->nearest = _find_nearest(conus->time, files, 6, "%Y%m%d_%H%M");
413 g_list_foreach(files, (GFunc)g_free, NULL);
415 if (!conus->nearest) {
416 conus->message = "No suitable files";
420 /* Fetch the image */
421 gchar *uri = g_strconcat(conus_url, conus->nearest, NULL);
422 gchar *path = gis_http_fetch(conus->http, uri, conus->nearest, GIS_ONCE,
423 _conus_update_loading, conus);
426 conus->message = "Fetch failed";
430 /* Load the image to a pixbuf */
431 GError *error = NULL;
432 conus->pixbuf = gdk_pixbuf_new_from_file(path, &error);
434 if (!gdk_pixbuf_get_has_alpha(conus->pixbuf)) {
435 GdkPixbuf *tmp = gdk_pixbuf_add_alpha(conus->pixbuf, TRUE, 0xff, 0xff, 0xff);
436 g_object_unref(conus->pixbuf);
440 /* Map the pixbuf's alpha values */
441 const guchar colormap[][2][4] = {
442 {{0x04, 0xe9, 0xe7}, {0x04, 0xe9, 0xe7, 0x30}},
443 {{0x01, 0x9f, 0xf4}, {0x01, 0x9f, 0xf4, 0x60}},
444 {{0x03, 0x00, 0xf4}, {0x03, 0x00, 0xf4, 0x90}},
446 guchar *pixels = gdk_pixbuf_get_pixels(conus->pixbuf);
447 gint height = gdk_pixbuf_get_height(conus->pixbuf);
448 gint width = gdk_pixbuf_get_width(conus->pixbuf);
449 for (int i = 0; i < width*height; i++) {
450 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
451 if (pixels[i*4+0] > 0xe0 &&
452 pixels[i*4+1] > 0xe0 &&
453 pixels[i*4+2] > 0xe0) {
454 pixels[i*4+3] = 0x00;
457 if (pixels[i*4+0] == colormap[j][0][0] &&
458 pixels[i*4+1] == colormap[j][0][1] &&
459 pixels[i*4+2] == colormap[j][0][2]) {
460 pixels[i*4+0] = colormap[j][1][0];
461 pixels[i*4+1] = colormap[j][1][1];
462 pixels[i*4+2] = colormap[j][1][2];
463 pixels[i*4+3] = colormap[j][1][3];
470 g_idle_add(_conus_update_end, conus);
474 void _conus_update(RadarConus *conus)
476 conus->time = gis_viewer_get_time(conus->viewer);
477 g_debug("GisPluginRadar: _conus_update - %d",
480 /* Add a progress bar */
481 GtkWidget *progress = gtk_progress_bar_new();
482 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading...");
483 _gtk_bin_set_child(GTK_BIN(conus->config), progress);
485 g_thread_create(_conus_update_thread, conus, FALSE, NULL);
488 RadarConus *radar_conus_new(GtkWidget *pconfig,
489 GisViewer *viewer, GisHttp *http)
491 RadarConus *conus = g_new0(RadarConus, 1);
492 conus->viewer = g_object_ref(viewer);
494 conus->config = gtk_alignment_new(0, 0, 1, 1);
495 conus->tile = gis_tile_new(NULL,
496 50.406626367301044, 50.406626367301044-0.017971305190311*1600,
497 -127.620375523875420+0.017971305190311*3400, -127.620375523875420);
498 conus->tile->zindex = 1;
499 conus->tile_ref = gis_viewer_add(viewer,
500 GIS_OBJECT(conus->tile), GIS_LEVEL_WORLD, TRUE);
502 conus->time_id = g_signal_connect_swapped(viewer, "time-changed",
503 G_CALLBACK(_conus_update), conus);
504 conus->refresh_id = g_signal_connect_swapped(viewer, "refresh",
505 G_CALLBACK(_conus_update), conus);
507 gtk_notebook_append_page(GTK_NOTEBOOK(pconfig), conus->config, gtk_label_new("Conus"));
508 _conus_update(conus);
512 void radar_conus_free(RadarConus *conus)
514 g_signal_handler_disconnect(conus->viewer, conus->time_id);
515 g_signal_handler_disconnect(conus->viewer, conus->refresh_id);
517 if (conus->tile->data) {
518 glDeleteTextures(1, conus->tile->data);
519 g_free(conus->tile->data);
521 gis_viewer_remove(conus->viewer, conus->tile_ref);
523 g_object_unref(conus->viewer);
531 static void _draw_hud(GisCallback *callback, gpointer _self)
534 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
538 g_debug("GisPluginRadar: _draw_hud");
539 /* Print the color table */
540 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
541 glMatrixMode(GL_PROJECTION); glLoadIdentity();
542 glDisable(GL_TEXTURE_2D);
543 glDisable(GL_ALPHA_TEST);
544 glDisable(GL_CULL_FACE);
545 glDisable(GL_LIGHTING);
546 glEnable(GL_COLOR_MATERIAL);
549 for (i = 0; i < 256; i++) {
550 glColor4ubv(self->colormap->data[i]);
551 glVertex3f(-1.0, (float)((i ) - 256/2)/(256/2), 0.0); // bot left
552 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
553 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
554 glVertex3f(-0.9, (float)((i ) - 256/2)/(256/2), 0.0); // bot right
560 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
562 /* TODO: move to constructor if possible */
563 g_debug("GisPluginRadar: new");
564 GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
565 self->viewer = viewer;
569 GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
570 self->hud_ref = gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
573 self->conus = radar_conus_new(self->config, self->viewer, self->conus_http);
575 /* Load radar sites */
576 for (city_t *city = cities; city->type; city++) {
577 if (city->type != LOCATION_CITY)
579 RadarSite *site = radar_site_new(city, self->config,
580 self->viewer, self->prefs, self->sites_http);
581 g_hash_table_insert(self->sites, city->code, site);
587 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
589 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
594 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
595 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
596 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
597 gis_plugin_radar_plugin_init));
598 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
600 g_debug("GisPluginRadar: plugin_init");
601 /* Add methods to the interface */
602 iface->get_config = gis_plugin_radar_get_config;
604 static void gis_plugin_radar_init(GisPluginRadar *self)
606 g_debug("GisPluginRadar: class_init");
608 self->sites_http = gis_http_new(G_DIR_SEPARATOR_S "nexrad" G_DIR_SEPARATOR_S "level2" G_DIR_SEPARATOR_S);
609 self->conus_http = gis_http_new(G_DIR_SEPARATOR_S "nexrad" G_DIR_SEPARATOR_S "conus" G_DIR_SEPARATOR_S);
610 self->sites = g_hash_table_new_full(g_str_hash, g_str_equal,
611 NULL, (GDestroyNotify)radar_site_free);
612 self->config = gtk_notebook_new();
613 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_TOP);
615 static void gis_plugin_radar_dispose(GObject *gobject)
617 g_debug("GisPluginRadar: dispose");
618 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
619 gis_viewer_remove(self->viewer, self->hud_ref);
620 radar_conus_free(self->conus);
621 /* Drop references */
622 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
624 static void gis_plugin_radar_finalize(GObject *gobject)
626 g_debug("GisPluginRadar: finalize");
627 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
629 gis_http_free(self->conus_http);
630 gis_http_free(self->sites_http);
631 g_hash_table_destroy(self->sites);
632 gtk_widget_destroy(self->config);
633 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
636 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
638 g_debug("GisPluginRadar: class_init");
639 GObjectClass *gobject_class = (GObjectClass*)klass;
640 gobject_class->dispose = gis_plugin_radar_dispose;
641 gobject_class->finalize = gis_plugin_radar_finalize;