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/>.
19 #include <glib/gstdio.h>
21 #include <gtk/gtkgl.h>
30 #include "../aweather-location.h"
33 /**************************
34 * Data loading functions *
35 **************************/
36 /* Convert a sweep to an 2d array of data points */
37 static void _bscan_sweep(GisPluginRadar *self, Sweep *sweep, colormap_t *colormap,
38 guint8 **data, int *width, int *height)
40 g_debug("GisPluginRadar: _bscan_sweep - %p, %p, %p",
41 sweep, colormap, data);
42 /* Calculate max number of bins */
44 for (int i = 0; i < sweep->h.nrays; i++)
45 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
47 /* Allocate buffer using max number of bins for each ray */
48 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
51 for (int ri = 0; ri < sweep->h.nrays; ri++) {
52 Ray *ray = sweep->ray[ri];
53 for (int bi = 0; bi < ray->h.nbins; bi++) {
54 /* copy RGBA into buffer */
55 //guint val = dz_f(ray->range[bi]);
56 guint8 val = (guint8)ray->h.f(ray->range[bi]);
57 guint buf_i = (ri*max_bins+bi)*4;
58 buf[buf_i+0] = colormap->data[val][0];
59 buf[buf_i+1] = colormap->data[val][1];
60 buf[buf_i+2] = colormap->data[val][2];
61 buf[buf_i+3] = colormap->data[val][3]*0.75; // TESTING
62 if (val == BADVAL || val == RFVAL || val == APFLAG ||
63 val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
64 buf[buf_i+3] = 0x00; // transparent
71 *height = sweep->h.nrays;
75 /* Load a sweep as the active texture */
76 static gboolean _load_sweep(gpointer _self)
78 GisPluginRadar *self = _self;
81 g_debug("GisPluginRadar: _load_sweep - %p", self->cur_sweep);
84 _bscan_sweep(self, self->cur_sweep, self->cur_colormap, &data, &width, &height);
85 glDeleteTextures(1, &self->cur_sweep_tex);
86 glGenTextures(1, &self->cur_sweep_tex);
87 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
88 glPixelStorei(GL_PACK_ALIGNMENT, 1);
89 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
90 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
92 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
93 GL_RGBA, GL_UNSIGNED_BYTE, data);
95 gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
99 /* Load the colormap for a sweep */
100 static void _load_colormap(GisPluginRadar *self, gchar *table)
102 g_debug("GisPluginRadar: _load_colormap - %s", table);
103 /* Set colormap so we can draw it on expose */
104 for (int i = 0; colormaps[i].name; i++)
105 if (g_str_equal(colormaps[i].name, table))
106 self->cur_colormap = &colormaps[i];
113 /* Setup a loading screen in the tab */
114 static void _load_gui_pre(GisPluginRadar *self)
116 g_debug("GisPluginRadar: _load_gui_pre");
119 /* Set up progress bar */
120 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
122 gtk_widget_destroy(child);
124 GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
125 gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
126 self->progress_bar = gtk_progress_bar_new();
127 self->progress_label = gtk_label_new("Loading radar...");
128 gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar, FALSE, FALSE, 0);
129 gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
130 gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
131 gtk_widget_show_all(self->config_body);
135 RSL_free_radar(self->cur_radar);
136 self->cur_radar = NULL;
137 self->cur_sweep = NULL;
138 gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
142 /* Update pogress bar of loading screen */
143 static void _load_gui_update(char *path, goffset cur, goffset total, gpointer _self)
145 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
146 double percent = (double)cur/total;
148 //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
149 // cur, total, percent*100);
152 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
154 gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
155 percent*100, (double)cur/1000000, (double)total/1000000);
156 gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
161 /* Update pogress bar of loading screen */
162 static void _load_gui_error(GisPluginRadar *self, gchar *error)
164 gchar *msg = g_strdup_printf(
165 "GisPluginRadar: error loading radar - %s", error);
166 g_warning("%s", msg);
168 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
170 gtk_widget_destroy(child);
171 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new(msg));
172 gtk_widget_show_all(self->config_body);
177 /* Clear loading screen and add sweep selectors */
178 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self);
179 static void _load_gui_success(GisPluginRadar *self, Radar *radar)
181 g_debug("GisPluginRadar: _load_gui_success - %p", radar);
182 /* Clear existing items */
184 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
186 gtk_widget_destroy(child);
189 guint rows = 1, cols = 1, cur_cols;
190 gchar row_label_str[64], col_label_str[64], button_str[64];
191 GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
192 GtkWidget *table = gtk_table_new(rows, cols, FALSE);
194 for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
195 Volume *vol = radar->v[vi];
196 if (vol == NULL) continue;
197 rows++; cols = 1; elev = 0;
200 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
201 row_label = gtk_label_new(row_label_str);
202 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
203 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
204 gtk_table_attach(GTK_TABLE(table), row_label,
205 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
207 for (guint si = 0; si < vol->h.nsweeps; si++) {
208 Sweep *sweep = vol->sweep[si];
209 if (sweep == NULL || sweep->h.elev == 0) continue;
210 if (sweep->h.elev != elev) {
212 elev = sweep->h.elev;
215 g_object_get(table, "n-columns", &cur_cols, NULL);
216 if (cols > cur_cols) {
217 g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
218 col_label = gtk_label_new(col_label_str);
219 gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
220 gtk_widget_set_size_request(col_label, 50, -1);
221 gtk_table_attach(GTK_TABLE(table), col_label,
222 cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
225 elev_box = gtk_hbox_new(TRUE, 0);
226 gtk_table_attach(GTK_TABLE(table), elev_box,
227 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
232 g_snprintf(button_str, 64, "%3.2f", elev);
233 button = gtk_radio_button_new_with_label_from_widget(
234 GTK_RADIO_BUTTON(button), button_str);
235 gtk_widget_set_size_request(button, -1, 26);
236 //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
237 //gtk_widget_set_size_request(button, -1, 22);
238 g_object_set(button, "draw-indicator", FALSE, NULL);
239 gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
241 g_object_set_data(G_OBJECT(button), "type", vol->h.type_str);
242 g_object_set_data(G_OBJECT(button), "sweep", sweep);
243 g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), self);
246 gtk_container_add(GTK_CONTAINER(self->config_body), table);
247 gtk_widget_show_all(table);
255 /* Download a compressed radar file from the remote server */
256 static gchar *_download_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
258 g_debug("GisPluginRadar: _download_radar - %s, %s", site, time);
260 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
261 gchar *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
262 gchar *local = g_strdup_printf("%s/%s_%s", site, site, time);
263 gchar *uri = g_strconcat(base, "/", local, NULL);
264 GisCacheType mode = gis_viewer_get_offline(self->viewer) ? GIS_LOCAL : GIS_UPDATE;
265 return gis_http_fetch(self->http, uri, local, mode, _load_gui_update, self);
268 /* Decompress a radar file using wsr88dec */
269 static gchar *_decompress_radar(GisPluginRadar *self, char *compressed)
271 char *decompressed = g_strconcat(compressed, ".raw", NULL);
272 if (g_file_test(decompressed, G_FILE_TEST_EXISTS)) {
273 struct stat comp, dec;
274 g_stat(compressed, &comp);
275 g_stat(decompressed, &dec);
276 if (dec.st_mtime >= comp.st_mtime)
279 g_debug("GisPluginRadar: _decompress_radar - %s", decompressed);
280 char *argv[] = {"wsr88ddec", compressed, decompressed, NULL};
282 GError *error = NULL;
284 NULL, // const gchar *working_directory
285 argv, // gchar **argv
286 NULL, // gchar **envp
287 G_SPAWN_SEARCH_PATH, // GSpawnFlags flags
288 NULL, // GSpawnChildSetupFunc child_setup
289 NULL, // gpointer user_data
290 NULL, // gchar *standard_output
291 NULL, // gchar *standard_output
292 &status, // gint *exit_status
293 &error); // GError **error
295 g_warning("GisPluginRadar: _decompress_radar - %s", error->message);
300 gchar *msg = g_strdup_printf("wsr88ddec exited with status %d", status);
301 g_warning("GisPluginRadar: _decompress_radar - %s", msg);
312 /* Set the radar file based on cur_site andcur_time
313 * This should be run in a separatet hread */
314 static gboolean _set_radar_cb(GisPluginRadar *self)
316 g_debug("GisPluginRadar: _set_radar_cb");
320 /* Download and decompress the radar */
321 gchar *compressed = _download_radar(self,
322 self->cur_site, self->cur_time);
324 _load_gui_error(self, "Download failed");
328 /* Decompress radar */
329 gchar *decompressed = _decompress_radar(self, compressed);
332 _load_gui_error(self, "Decompression failed");
336 /* Load the radar file */
337 g_debug("GisPluginRadar: _set_radar_cb - loading %s", decompressed);
338 RSL_read_these_sweeps("all", NULL);
339 self->cur_radar = RSL_wsr88d_to_radar(decompressed, self->cur_site);
340 g_free(decompressed);
341 if (!self->cur_radar) {
342 _load_gui_error(self, "Loading failed");
346 /* Load the first sweep by default */
347 Radar *radar = self->cur_radar;
349 gchar *type_str = NULL;
350 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
351 if (radar->v[vi] == NULL)
353 for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
354 if (radar->v[vi]->sweep[si] == NULL)
356 sweep = radar->v[vi]->sweep[si];
357 type_str = radar->v[vi]->h.type_str;
363 _load_gui_error(self, "No sweeps found");
368 g_debug("GisPluginRadar: _set_radar_cb - setting sweep");
369 self->cur_sweep = sweep;
370 _load_colormap(self, type_str);
371 g_idle_add(_load_sweep, self);
372 _load_gui_success(self, radar);
374 /* Let other threads go */
375 g_mutex_unlock(self->load_mutex);
379 g_mutex_unlock(self->load_mutex);
383 static void _set_radar(GisPluginRadar *self,
384 gchar *site, gchar *time)
386 if (site) self->cur_site = site;
387 if (time) self->cur_time = time;
388 if (!self->cur_site || !self->cur_time)
391 /* Abort any current downloads */
392 soup_session_abort(self->http->soup);
394 g_mutex_lock(self->load_mutex);
396 g_thread_create((GThreadFunc)_set_radar_cb, self, FALSE, NULL);
403 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
405 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
406 _load_colormap(self, g_object_get_data(G_OBJECT(button), "type"));
407 self->cur_sweep = g_object_get_data(G_OBJECT(button), "sweep");
411 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
413 g_debug("GisPluginRadar: _on_time_changed");
414 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
415 _set_radar(self, self->cur_site, g_strdup(time));
418 static void _on_location_changed(GisViewer *viewer,
419 gdouble lat, gdouble lon, gdouble elev,
422 g_debug("GisPluginRadar: _on_location_changed");
423 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
424 gdouble min_dist = EARTH_R / 5;
425 city_t *city, *min_city = NULL;
426 for (city = cities; city->type; city++) {
427 if (city->type != LOCATION_CITY)
429 gdouble city_loc[3] = {};
430 gdouble eye_loc[3] = {lat, lon, elev};
431 lle2xyz(city->lat, city->lon, city->elev,
432 &city_loc[0], &city_loc[1], &city_loc[2]);
433 lle2xyz(lat, lon, elev,
434 &eye_loc[0], &eye_loc[1], &eye_loc[2]);
435 gdouble dist = distd(city_loc, eye_loc);
436 if (dist < min_dist) {
441 static city_t *last_city = NULL;
442 if (min_city && min_city != last_city)
443 _set_radar(self, min_city->code, self->cur_time);
444 last_city = min_city;
447 static gpointer _draw_radar(GisCallback *callback, gpointer _self)
449 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
452 if (self->cur_sweep == NULL)
454 g_debug("GisPluginRadar: _draw_radar");
455 Sweep *sweep = self->cur_sweep;
457 g_debug("GisPluginRadar: _draw_radar - setting camera");
458 Radar_header *h = &self->cur_radar->h;
459 gdouble lat = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
460 gdouble lon = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
461 gdouble elev = h->height;
462 gis_viewer_center_position(self->viewer, lat, lon, elev);
464 glDisable(GL_ALPHA_TEST);
465 glDisable(GL_CULL_FACE);
466 glDisable(GL_LIGHTING);
467 glEnable(GL_TEXTURE_2D);
468 glEnable(GL_POLYGON_OFFSET_FILL);
469 glPolygonOffset(1.0, -2.0);
473 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
474 g_message("Tex = %d", self->cur_sweep_tex);
475 glBegin(GL_TRIANGLE_STRIP);
476 for (int ri = 0; ri <= sweep->h.nrays; ri++) {
479 if (ri < sweep->h.nrays) {
480 ray = sweep->ray[ri];
481 angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
483 /* Do the right side of the last sweep */
484 ray = sweep->ray[ri-1];
485 angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
488 double lx = sin(angle);
489 double ly = cos(angle);
491 double near_dist = ray->h.range_bin1;
492 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
494 /* (find middle of bin) / scale for opengl */
496 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
497 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
500 // todo: correct range-height function
501 double height = sin(deg2rad(ray->h.elev)) * far_dist;
502 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
503 glVertex3f(lx*far_dist, ly*far_dist, height);
506 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
510 //glTexCoord2d( 0., 0.); glVertex3f(-500., 0., 0.); // bot left
511 //glTexCoord2d( 0., 1.); glVertex3f(-500., 500., 0.); // top left
512 //glTexCoord2d( 1., 1.); glVertex3f( 0., 500., 3.); // top right
513 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 3.); // bot right
519 static gpointer _draw_hud(GisCallback *callback, gpointer _self)
521 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
522 if (self->cur_sweep == NULL)
524 g_debug("GisPluginRadar: _draw_hud");
526 /* Print the color table */
527 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
528 glMatrixMode(GL_PROJECTION); glLoadIdentity();
529 glDisable(GL_TEXTURE_2D);
530 glDisable(GL_ALPHA_TEST);
531 glDisable(GL_CULL_FACE);
532 glDisable(GL_LIGHTING);
533 glEnable(GL_COLOR_MATERIAL);
536 for (i = 0; i < 256; i++) {
537 glColor4ubv(self->cur_colormap->data[i]);
538 glVertex3f(-1.0, (float)((i ) - 256/2)/(256/2), 0.0); // bot left
539 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
540 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
541 glVertex3f(-0.9, (float)((i ) - 256/2)/(256/2), 0.0); // bot right
552 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
554 /* TODO: move to constructor if possible */
555 g_debug("GisPluginRadar: new");
556 GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
557 self->viewer = viewer;
559 self->time_changed_id = g_signal_connect(viewer, "time-changed",
560 G_CALLBACK(_on_time_changed), self);
561 self->location_changed_id = g_signal_connect(viewer, "location-changed",
562 G_CALLBACK(_on_location_changed), self);
564 for (city_t *city = cities; city->type; city++) {
565 if (city->type != LOCATION_CITY)
567 g_debug("Adding marker for %s %s", city->code, city->label);
568 GisMarker *marker = gis_marker_new(city->label);
569 gis_point_set_lle(gis_object_center(GIS_OBJECT(marker)),
570 city->lat, city->lon, city->elev);
571 GIS_OBJECT(marker)->lod = EARTH_R/2;
572 gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, FALSE);
576 GisCallback *radar_cb = gis_callback_new(_draw_radar, self);
577 GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
579 gis_viewer_add(viewer, GIS_OBJECT(radar_cb), GIS_LEVEL_WORLD, TRUE);
580 gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
585 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
587 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
588 return self->config_body;
596 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
597 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
598 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
599 gis_plugin_radar_plugin_init));
600 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
602 g_debug("GisPluginRadar: plugin_init");
603 /* Add methods to the interface */
604 iface->get_config = gis_plugin_radar_get_config;
606 /* Class/Object init */
607 static void gis_plugin_radar_init(GisPluginRadar *self)
609 g_debug("GisPluginRadar: class_init");
611 self->http = gis_http_new("/nexrad/level2/");
612 self->config_body = gtk_alignment_new(0, 0, 1, 1);
613 self->load_mutex = g_mutex_new();
614 gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
615 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
617 static void gis_plugin_radar_dispose(GObject *gobject)
619 g_debug("GisPluginRadar: dispose");
620 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
621 g_signal_handler_disconnect(self->viewer, self->time_changed_id);
622 /* Drop references */
623 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
625 static void gis_plugin_radar_finalize(GObject *gobject)
627 g_debug("GisPluginRadar: finalize");
628 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
630 gis_http_free(self->http);
631 g_mutex_free(self->load_mutex);
632 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
635 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
637 g_debug("GisPluginRadar: class_init");
638 GObjectClass *gobject_class = (GObjectClass*)klass;
639 gobject_class->dispose = gis_plugin_radar_dispose;
640 gobject_class->finalize = gis_plugin_radar_finalize;