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/>.
20 #include <gtk/gtkgl.h>
29 #include "../aweather-location.h"
32 /**************************
33 * Data loading functions *
34 **************************/
35 /* Convert a sweep to an 2d array of data points */
36 static void _bscan_sweep(GisPluginRadar *self, Sweep *sweep, colormap_t *colormap,
37 guint8 **data, int *width, int *height)
39 /* Calculate max number of bins */
41 for (int i = 0; i < sweep->h.nrays; i++)
42 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
44 /* Allocate buffer using max number of bins for each ray */
45 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
48 for (int ri = 0; ri < sweep->h.nrays; ri++) {
49 Ray *ray = sweep->ray[ri];
50 for (int bi = 0; bi < ray->h.nbins; bi++) {
51 /* copy RGBA into buffer */
52 //guint val = dz_f(ray->range[bi]);
53 guint8 val = (guint8)ray->h.f(ray->range[bi]);
54 guint buf_i = (ri*max_bins+bi)*4;
55 buf[buf_i+0] = colormap->data[val][0];
56 buf[buf_i+1] = colormap->data[val][1];
57 buf[buf_i+2] = colormap->data[val][2];
58 buf[buf_i+3] = colormap->data[val][3]*0.75; // TESTING
59 if (val == BADVAL || val == RFVAL || val == APFLAG ||
60 val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
61 buf[buf_i+3] = 0x00; // transparent
68 *height = sweep->h.nrays;
72 /* Load a sweep as the active texture */
73 static void _load_sweep(GisPluginRadar *self, Sweep *sweep)
75 GisViewer *viewer = self->viewer;
76 self->cur_sweep = sweep;
79 _bscan_sweep(self, sweep, self->cur_colormap, &data, &width, &height);
80 glDeleteTextures(1, &self->cur_sweep_tex);
81 glGenTextures(1, &self->cur_sweep_tex);
82 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
83 glPixelStorei(GL_PACK_ALIGNMENT, 1);
84 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
85 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
86 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
87 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
88 GL_RGBA, GL_UNSIGNED_BYTE, data);
90 gtk_widget_queue_draw(GTK_WIDGET(viewer));
93 static void _load_colormap(GisPluginRadar *self, gchar *table)
95 /* Set colormap so we can draw it on expose */
96 for (int i = 0; colormaps[i].name; i++)
97 if (g_str_equal(colormaps[i].name, table))
98 self->cur_colormap = &colormaps[i];
101 /* Add selectors to the config area for the sweeps */
102 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self);
103 static void _load_radar_gui(GisPluginRadar *self, Radar *radar)
105 /* Clear existing items */
106 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
108 gtk_widget_destroy(child);
111 guint rows = 1, cols = 1, cur_cols;
112 gchar row_label_str[64], col_label_str[64], button_str[64];
113 GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
114 GtkWidget *table = gtk_table_new(rows, cols, FALSE);
116 for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
117 Volume *vol = radar->v[vi];
118 if (vol == NULL) continue;
119 rows++; cols = 1; elev = 0;
122 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
123 row_label = gtk_label_new(row_label_str);
124 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
125 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
126 gtk_table_attach(GTK_TABLE(table), row_label,
127 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
129 for (guint si = 0; si < vol->h.nsweeps; si++) {
130 Sweep *sweep = vol->sweep[si];
131 if (sweep == NULL || sweep->h.elev == 0) continue;
132 if (sweep->h.elev != elev) {
134 elev = sweep->h.elev;
137 g_object_get(table, "n-columns", &cur_cols, NULL);
138 if (cols > cur_cols) {
139 g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
140 col_label = gtk_label_new(col_label_str);
141 gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
142 gtk_widget_set_size_request(col_label, 50, -1);
143 gtk_table_attach(GTK_TABLE(table), col_label,
144 cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
147 elev_box = gtk_hbox_new(TRUE, 0);
148 gtk_table_attach(GTK_TABLE(table), elev_box,
149 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
154 g_snprintf(button_str, 64, "%3.2f", elev);
155 button = gtk_radio_button_new_with_label_from_widget(
156 GTK_RADIO_BUTTON(button), button_str);
157 gtk_widget_set_size_request(button, -1, 26);
158 //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
159 //gtk_widget_set_size_request(button, -1, 22);
160 g_object_set(button, "draw-indicator", FALSE, NULL);
161 gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
163 g_object_set_data(G_OBJECT(button), "type", vol->h.type_str);
164 g_object_set_data(G_OBJECT(button), "sweep", sweep);
165 g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), self);
168 gtk_container_add(GTK_CONTAINER(self->config_body), table);
169 gtk_widget_show_all(table);
172 /* Load a radar from a decompressed file */
173 static void _load_radar(GisPluginRadar *self, gchar *radar_file)
175 char *dir = g_path_get_dirname(radar_file);
176 char *site = g_path_get_basename(dir);
178 g_debug("GisPluginRadar: load_radar - Loading new radar");
179 RSL_read_these_sweeps("all", NULL);
180 Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site);
182 g_warning("fail to load radar: path=%s, site=%s", radar_file, site);
188 /* Load the first sweep by default */
189 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
190 g_warning("No sweeps found\n");
192 /* load first available sweep */
193 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
194 if (radar->v[vi]== NULL) continue;
195 for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
196 if (radar->v[vi]->sweep[si]== NULL) continue;
197 _load_colormap(self, radar->v[vi]->h.type_str);
198 _load_sweep(self, radar->v[vi]->sweep[si]);
205 _load_radar_gui(self, radar);
209 GisPluginRadar *self;
213 static void _decompressed_cb(GPid pid, gint status, gpointer _udata)
215 g_debug("GisPluginRadar: decompressed_cb");
216 decompressed_t *udata = _udata;
218 g_warning("wsr88ddec exited with status %d", status);
221 _load_radar(udata->self, udata->radar_file);
222 g_spawn_close_pid(pid);
223 g_free(udata->radar_file);
227 static void _cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self)
229 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
230 double percent = (double)cur/total;
232 //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
233 // cur, total, percent*100);
235 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
237 gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
238 percent*100, (double)cur/1000000, (double)total/1000000);
239 gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
243 static void _cache_done_cb(char *path, gboolean updated, gpointer _self)
245 g_debug("GisPluginRadar: cache_done_cb - updated = %d", updated);
246 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
247 char *decompressed = g_strconcat(path, ".raw", NULL);
248 if (!updated && g_file_test(decompressed, G_FILE_TEST_EXISTS)) {
249 _load_radar(self, decompressed);
253 decompressed_t *udata = g_malloc(sizeof(decompressed_t));
255 udata->radar_file = decompressed;
256 g_debug("GisPluginRadar: cache_done_cb - File updated, decompressing..");
257 char *argv[] = {"wsr88ddec", path, decompressed, NULL};
259 GError *error = NULL;
261 NULL, // const gchar *working_directory,
262 argv, // gchar **argv,
263 NULL, // gchar **envp,
265 G_SPAWN_DO_NOT_REAP_CHILD,
266 // GSpawnFlags flags,
267 NULL, // GSpawnChildSetupFunc child_setup,
268 NULL, // gpointer user_data,
269 &pid, // GPid *child_pid,
270 &error); // GError **error
272 gchar *message = g_strdup_printf("Unable to decompress WSR88D data: %s",
274 g_warning("%s", message);
275 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
277 gtk_widget_destroy(child);
278 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new(message));
279 gtk_widget_show_all(self->config_body);
283 g_child_watch_add(pid, _decompressed_cb, udata);
286 static void _set_radar(GisPluginRadar *self, const char *site, const char *time)
290 g_debug("GisPluginRadar: set_radar - %s - %s", site, time);
292 /* Set up progress bar */
293 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
294 if (child) gtk_widget_destroy(child);
296 GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
297 gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
298 self->progress_bar = gtk_progress_bar_new();
299 self->progress_label = gtk_label_new("Loading radar...");
300 gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar, FALSE, FALSE, 0);
301 gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
302 gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
303 gtk_widget_show_all(self->config_body);
307 RSL_free_radar(self->cur_radar);
308 self->cur_radar = NULL;
309 self->cur_sweep = NULL;
310 gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
312 /* Start loading the new radar */
313 soup_session_abort(self->http->soup);
314 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
315 gchar *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
316 gchar *local = g_strdup_printf("%s/%s_%s", site, site, time);
317 gchar *uri = g_strconcat(base, "/", local, NULL);
318 GisCacheType mode = gis_viewer_get_offline(self->viewer) ? GIS_LOCAL : GIS_UPDATE;
319 gchar *path = gis_http_fetch(self->http, uri, local, mode, _cache_chunk_cb, self);
321 _cache_done_cb(path, TRUE, self);
330 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
332 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
333 _load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
334 _load_sweep (self, g_object_get_data(G_OBJECT(button), "sweep"));
337 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
339 g_debug("GisPluginRadar: _on_time_changed");
340 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
341 g_free(self->cur_time);
342 self->cur_time = g_strdup(time);
343 _set_radar(self, self->cur_site, self->cur_time);
346 static void _on_location_changed(GisViewer *viewer,
347 gdouble lat, gdouble lon, gdouble elev,
350 g_debug("GisPluginRadar: _on_location_changed");
351 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
352 gdouble min_dist = EARTH_R / 5;
353 city_t *city, *min_city = NULL;
354 for (city = cities; city->type; city++) {
355 if (city->type != LOCATION_CITY)
357 gdouble city_loc[3] = {};
358 gdouble eye_loc[3] = {lat, lon, elev};
359 lle2xyz(city->lat, city->lon, city->elev,
360 &city_loc[0], &city_loc[1], &city_loc[2]);
361 lle2xyz(lat, lon, elev,
362 &eye_loc[0], &eye_loc[1], &eye_loc[2]);
363 gdouble dist = distd(city_loc, eye_loc);
364 if (dist < min_dist) {
369 static city_t *last_city = NULL;
370 if (min_city && min_city != last_city) {
371 self->cur_site = min_city->code;
372 _set_radar(self, self->cur_site, self->cur_time);
374 last_city = min_city;
377 static gpointer _draw_radar(GisCallback *callback, gpointer _self)
379 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
382 if (self->cur_sweep == NULL)
384 g_debug("GisPluginRadar: _draw_radar");
385 Sweep *sweep = self->cur_sweep;
387 g_debug("GisPluginRadar: _draw_radar - setting camera");
388 Radar_header *h = &self->cur_radar->h;
389 gdouble lat = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
390 gdouble lon = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
391 gdouble elev = h->height;
392 gis_viewer_center_position(self->viewer, lat, lon, elev);
394 glDisable(GL_ALPHA_TEST);
395 glDisable(GL_CULL_FACE);
396 glDisable(GL_LIGHTING);
397 glEnable(GL_TEXTURE_2D);
398 glEnable(GL_POLYGON_OFFSET_FILL);
399 glPolygonOffset(1.0, -2.0);
403 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
404 g_message("Tex = %d", self->cur_sweep_tex);
405 glBegin(GL_TRIANGLE_STRIP);
406 for (int ri = 0; ri <= sweep->h.nrays; ri++) {
409 if (ri < sweep->h.nrays) {
410 ray = sweep->ray[ri];
411 angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
413 /* Do the right side of the last sweep */
414 ray = sweep->ray[ri-1];
415 angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
418 double lx = sin(angle);
419 double ly = cos(angle);
421 double near_dist = ray->h.range_bin1;
422 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
424 /* (find middle of bin) / scale for opengl */
426 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
427 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
430 // todo: correct range-height function
431 double height = sin(deg2rad(ray->h.elev)) * far_dist;
432 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
433 glVertex3f(lx*far_dist, ly*far_dist, height);
436 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
440 //glTexCoord2d( 0., 0.); glVertex3f(-500., 0., 0.); // bot left
441 //glTexCoord2d( 0., 1.); glVertex3f(-500., 500., 0.); // top left
442 //glTexCoord2d( 1., 1.); glVertex3f( 0., 500., 3.); // top right
443 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 3.); // bot right
449 static gpointer _draw_hud(GisCallback *callback, gpointer _self)
451 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
452 if (self->cur_sweep == NULL)
454 g_debug("GisPluginRadar: _draw_hud");
456 /* Print the color table */
457 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
458 glMatrixMode(GL_PROJECTION); glLoadIdentity();
459 glDisable(GL_TEXTURE_2D);
460 glDisable(GL_ALPHA_TEST);
461 glDisable(GL_CULL_FACE);
462 glDisable(GL_LIGHTING);
463 glEnable(GL_COLOR_MATERIAL);
466 for (i = 0; i < 256; i++) {
467 glColor4ubv(self->cur_colormap->data[i]);
468 glVertex3f(-1.0, (float)((i ) - 256/2)/(256/2), 0.0); // bot left
469 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
470 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
471 glVertex3f(-0.9, (float)((i ) - 256/2)/(256/2), 0.0); // bot right
482 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
484 /* TODO: move to constructor if possible */
485 g_debug("GisPluginRadar: new");
486 GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
487 self->viewer = viewer;
489 self->time_changed_id = g_signal_connect(viewer, "time-changed",
490 G_CALLBACK(_on_time_changed), self);
491 self->location_changed_id = g_signal_connect(viewer, "location-changed",
492 G_CALLBACK(_on_location_changed), self);
494 for (city_t *city = cities; city->type; city++) {
495 if (city->type != LOCATION_CITY)
497 g_debug("Adding marker for %s %s", city->code, city->label);
498 GisMarker *marker = gis_marker_new(city->label);
499 gis_point_set_lle(gis_object_center(GIS_OBJECT(marker)),
500 city->lat, city->lon, city->elev);
501 GIS_OBJECT(marker)->lod = EARTH_R/2;
502 gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, FALSE);
506 GisCallback *radar_cb = gis_callback_new(_draw_radar, self);
507 GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
509 gis_viewer_add(viewer, GIS_OBJECT(radar_cb), GIS_LEVEL_WORLD, TRUE);
510 gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
515 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
517 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
518 return self->config_body;
526 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
527 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
528 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
529 gis_plugin_radar_plugin_init));
530 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
532 g_debug("GisPluginRadar: plugin_init");
533 /* Add methods to the interface */
534 iface->get_config = gis_plugin_radar_get_config;
536 /* Class/Object init */
537 static void gis_plugin_radar_init(GisPluginRadar *self)
539 g_debug("GisPluginRadar: class_init");
541 self->http = gis_http_new("/nexrad/level2/");
542 self->config_body = gtk_alignment_new(0, 0, 1, 1);
543 gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
544 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
546 static void gis_plugin_radar_dispose(GObject *gobject)
548 g_debug("GisPluginRadar: dispose");
549 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
550 g_signal_handler_disconnect(self->viewer, self->time_changed_id);
551 /* Drop references */
552 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
554 static void gis_plugin_radar_finalize(GObject *gobject)
556 g_debug("GisPluginRadar: finalize");
557 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
559 gis_http_free(self->http);
560 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
563 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
565 g_debug("GisPluginRadar: class_init");
566 GObjectClass *gobject_class = (GObjectClass*)klass;
567 gobject_class->dispose = gis_plugin_radar_dispose;
568 gobject_class->finalize = gis_plugin_radar_finalize;