2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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>
26 #include "aweather-gui.h"
27 #include "plugin-radar.h"
34 static void aweather_radar_plugin_init(AWeatherPluginInterface *iface);
35 static void _aweather_radar_expose(AWeatherPlugin *_radar);
36 G_DEFINE_TYPE_WITH_CODE(AWeatherRadar, aweather_radar, G_TYPE_OBJECT,
37 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
38 aweather_radar_plugin_init));
39 static void aweather_radar_plugin_init(AWeatherPluginInterface *iface)
41 g_debug("AWeatherRadar: plugin_init");
42 /* Add methods to the interface */
43 iface->expose = _aweather_radar_expose;
45 /* Class/Object init */
46 static void aweather_radar_init(AWeatherRadar *radar)
48 g_debug("AWeatherRadar: class_init");
53 static void aweather_radar_dispose(GObject *gobject)
55 g_debug("AWeatherRadar: dispose");
56 AWeatherRadar *self = AWEATHER_RADAR(gobject);
58 G_OBJECT_CLASS(aweather_radar_parent_class)->dispose(gobject);
60 static void aweather_radar_finalize(GObject *gobject)
62 g_debug("AWeatherRadar: finalize");
63 AWeatherRadar *self = AWEATHER_RADAR(gobject);
65 G_OBJECT_CLASS(aweather_radar_parent_class)->finalize(gobject);
68 static void aweather_radar_class_init(AWeatherRadarClass *klass)
70 g_debug("AWeatherRadar: class_init");
71 GObjectClass *gobject_class = (GObjectClass*)klass;
72 gobject_class->dispose = aweather_radar_dispose;
73 gobject_class->finalize = aweather_radar_finalize;
76 /**************************
77 * Data loading functions *
78 **************************/
79 /* Convert a sweep to an 2d array of data points */
80 static void bscan_sweep(AWeatherRadar *self, Sweep *sweep, colormap_t *colormap,
81 guint8 **data, int *width, int *height)
83 /* Calculate max number of bins */
85 for (i = 0; i < sweep->h.nrays; i++)
86 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
88 /* Allocate buffer using max number of bins for each ray */
89 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
93 for (ri = 0; ri < sweep->h.nrays; ri++) {
94 Ray *ray = sweep->ray[ri];
95 for (bi = 0; bi < ray->h.nbins; bi++) {
96 /* copy RGBA into buffer */
97 //guint val = dz_f(ray->range[bi]);
98 guint8 val = (guint8)ray->h.f(ray->range[bi]);
99 guint buf_i = (ri*max_bins+bi)*4;
100 buf[buf_i+0] = colormap->data[val][0];
101 buf[buf_i+1] = colormap->data[val][1];
102 buf[buf_i+2] = colormap->data[val][2];
103 buf[buf_i+3] = colormap->data[val][3];
104 if (val == BADVAL || val == RFVAL || val == APFLAG ||
105 val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
106 buf[buf_i+3] = 0x00; // transparent
113 *height = sweep->h.nrays;
117 /* Load a sweep as the active texture */
118 static void load_sweep(AWeatherRadar *self, Sweep *sweep)
120 aweather_gui_gl_begin(self->gui);
121 self->cur_sweep = sweep;
124 bscan_sweep(self, sweep, self->cur_colormap, &data, &width, &height);
125 glDeleteTextures(1, &self->cur_sweep_tex);
126 glGenTextures(1, &self->cur_sweep_tex);
127 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
128 glPixelStorei(GL_PACK_ALIGNMENT, 1);
129 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
130 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
131 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
133 GL_RGBA, GL_UNSIGNED_BYTE, data);
135 aweather_gui_gl_redraw(self->gui);
136 aweather_gui_gl_end(self->gui);
139 static void load_colormap(AWeatherRadar *self, gchar *table)
141 /* Set colormap so we can draw it on expose */
142 for (int i = 0; colormaps[i].name; i++)
143 if (g_str_equal(colormaps[i].name, table))
144 self->cur_colormap = &colormaps[i];
147 /* Add selectors to the config area for the sweeps */
148 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self);
149 static void load_radar_gui(AWeatherRadar *self, Radar *radar)
151 /* Clear existing items */
152 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
154 gtk_widget_destroy(child);
157 guint rows = 1, cols = 1, cur_cols;
158 gchar row_label_str[64], col_label_str[64], button_str[64];
159 GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
160 GtkWidget *table = gtk_table_new(rows, cols, FALSE);
162 for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
163 Volume *vol = radar->v[vi];
164 if (vol == NULL) continue;
165 rows++; cols = 1; elev = 0;
168 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
169 row_label = gtk_label_new(row_label_str);
170 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
171 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
172 gtk_table_attach(GTK_TABLE(table), row_label,
173 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
175 for (guint si = 0; si < vol->h.nsweeps; si++) {
176 Sweep *sweep = vol->sweep[si];
177 if (sweep == NULL || sweep->h.elev == 0) continue;
178 if (sweep->h.elev != elev) {
180 elev = sweep->h.elev;
183 g_object_get(table, "n-columns", &cur_cols, NULL);
184 if (cols > cur_cols) {
185 g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
186 col_label = gtk_label_new(col_label_str);
187 gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
188 gtk_widget_set_size_request(col_label, 40, -1);
189 gtk_table_attach(GTK_TABLE(table), col_label,
190 cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
193 elev_box = gtk_hbox_new(TRUE, 0);
194 gtk_table_attach(GTK_TABLE(table), elev_box,
195 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
200 g_snprintf(button_str, 64, "%3.2f", elev);
201 button = gtk_radio_button_new_with_label_from_widget(
202 GTK_RADIO_BUTTON(button), button_str);
203 gtk_widget_set_size_request(button, -1, 26);
204 //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
205 //gtk_widget_set_size_request(button, -1, 22);
206 g_object_set(button, "draw-indicator", FALSE, NULL);
207 gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
209 g_object_set_data(G_OBJECT(button), "type", vol->h.type_str);
210 g_object_set_data(G_OBJECT(button), "sweep", sweep);
211 g_signal_connect(button, "clicked", G_CALLBACK(on_sweep_clicked), self);
214 gtk_container_add(GTK_CONTAINER(self->config_body), table);
215 gtk_widget_show_all(table);
218 /* Load a radar from a decompressed file */
219 static void load_radar(AWeatherRadar *self, gchar *radar_file)
221 char *dir = g_path_get_dirname(radar_file);
222 char *site = g_path_get_basename(dir);
224 g_debug("AWeatherRadar: load_radar - Loading new radar");
225 RSL_read_these_sweeps("all", NULL);
226 Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site);
228 g_warning("fail to load radar: path=%s, site=%s", radar_file, site);
234 /* Load the first sweep by default */
235 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
236 g_warning("No sweeps found\n");
238 /* load first available sweep */
239 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
240 if (radar->v[vi]== NULL) continue;
241 for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
242 if (radar->v[vi]->sweep[si]== NULL) continue;
243 load_colormap(self, radar->v[vi]->h.type_str);
244 load_sweep(self, radar->v[vi]->sweep[si]);
251 load_radar_gui(self, radar);
254 static void update_times(AWeatherRadar *self, AWeatherView *view, char *site, char **last_time)
257 if (aweather_view_get_offline(view)) {
258 gchar *path = g_build_filename(g_get_user_cache_dir(), PACKAGE, "nexrd2", "raw", site, NULL);
259 GDir *dir = g_dir_open(path, 0, NULL);
262 while ((name = g_dir_read_name(dir))) {
263 times = g_list_prepend(times, g_strdup(name));
271 GError *error = NULL;
273 char *list_uri = g_strdup_printf("http://mesonet.agron.iastate.edu/data/nexrd2/raw/%s/dir.list", site);
274 GFile *list = g_file_new_for_uri(list_uri);
275 g_file_load_contents(list, NULL, &data, &length, NULL, &error);
277 g_warning("Error loading list for %s: %s", site, error->message);
280 gchar **lines = g_strsplit(data, "\n", -1);
281 for (int i = 0; lines[i] && lines[i][0]; i++) {
282 char **parts = g_strsplit(lines[i], " ", 2);
283 times = g_list_prepend(times, g_strdup(parts[1]));
291 g_object_unref(list);
294 GRegex *regex = g_regex_new("^[A-Z]{4}_([0-9]{8}_[0-9]{4})$", 0, 0, NULL); // KLSX_20090622_2113
297 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self->gui, "time"));
298 GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview));
299 gtk_list_store_clear(lstore);
301 times = g_list_reverse(times);
302 for (GList *cur = times; cur; cur = cur->next) {
303 g_message("trying time %s", (gchar*)cur->data);
304 if (g_regex_match(regex, cur->data, 0, &info)) {
305 gchar *time = g_match_info_fetch(info, 1);
306 g_message("adding time %s", (gchar*)cur->data);
307 gtk_list_store_insert(lstore, &iter, 0);
308 gtk_list_store_set(lstore, &iter, 0, time, -1);
314 g_regex_unref(regex);
315 g_list_foreach(times, (GFunc)g_free, NULL);
327 static void decompressed_cb(GPid pid, gint status, gpointer _udata)
329 decompressed_t *udata = _udata;
331 g_warning("wsr88ddec exited with status %d", status);
334 load_radar(udata->self, udata->radar_file);
335 g_spawn_close_pid(pid);
336 g_free(udata->radar_file);
340 static void cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self)
342 AWeatherRadar *self = AWEATHER_RADAR(_self);
343 double percent = (double)cur/total;
345 g_message("AWeatherRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
346 cur, total, percent*100);
348 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
350 gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
351 percent*100, (double)cur/1000000, (double)total/1000000);
352 gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
356 static void cache_done_cb(char *path, gboolean updated, gpointer _self)
358 AWeatherRadar *self = AWEATHER_RADAR(_self);
359 char *decompressed = g_strconcat(path, ".raw", NULL);
361 load_radar(self, decompressed);
365 decompressed_t *udata = g_malloc(sizeof(decompressed_t));
367 udata->radar_file = decompressed;
368 g_debug("AWeatherRadar: cache_done_cb - File updated, decompressing..");
369 char *argv[] = {"wsr88ddec", path, decompressed, NULL};
371 GError *error = NULL;
373 NULL, // const gchar *working_directory,
374 argv, // gchar **argv,
375 NULL, // gchar **envp,
377 G_SPAWN_DO_NOT_REAP_CHILD,
378 // GSpawnFlags flags,
379 NULL, // GSpawnChildSetupFunc child_setup,
380 NULL, // gpointer user_data,
381 &pid, // GPid *child_pid,
382 &error); // GError **error
384 g_warning("failed to decompress WSR88D data: %s",
388 g_child_watch_add(pid, decompressed_cb, udata);
395 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self)
397 AWeatherRadar *self = AWEATHER_RADAR(_self);
398 load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
399 load_sweep (self, g_object_get_data(G_OBJECT(button), "sweep"));
402 static void on_time_changed(AWeatherView *view, const char *time, gpointer _self)
404 AWeatherRadar *self = AWEATHER_RADAR(_self);
405 g_debug("AWeatherRadar: on_time_changed - setting time=%s", time);
406 // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
407 char *site = aweather_view_get_site(view);
408 char *base = "http://mesonet.agron.iastate.edu/data/";
409 char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time);
411 /* Set up progress bar */
412 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
413 if (child) gtk_widget_destroy(child);
415 GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
416 gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
417 self->progress_bar = gtk_progress_bar_new();
418 self->progress_label = gtk_label_new("Loading radar...");
419 gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar, FALSE, FALSE, 0);
420 gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
421 gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
422 gtk_widget_show_all(self->config_body);
426 RSL_free_radar(self->cur_radar);
427 self->cur_radar = NULL;
428 self->cur_sweep = NULL;
429 aweather_gui_gl_redraw(self->gui);
431 /* Start loading the new radar */
433 soup_session_abort(self->soup);
436 if (aweather_view_get_offline(view))
437 self->soup = cache_file(base, path, AWEATHER_ONCE,
438 cache_chunk_cb, cache_done_cb, self);
440 self->soup = cache_file(base, path, AWEATHER_UPDATE,
441 cache_chunk_cb, cache_done_cb, self);
445 static void on_site_changed(AWeatherView *view, char *site, gpointer _self)
447 AWeatherRadar *self = AWEATHER_RADAR(_self);
448 g_debug("AWeatherRadar: on_site_changed - Loading wsr88d list for %s", site);
450 update_times(self, view, site, &time);
451 aweather_view_set_time(view, time);
456 static void on_refresh(AWeatherView *view, gpointer _self)
458 AWeatherRadar *self = AWEATHER_RADAR(_self);
459 char *site = aweather_view_get_site(view);
461 update_times(self, view, site, &time);
462 aweather_view_set_time(view, time);
469 AWeatherRadar *aweather_radar_new(AWeatherGui *gui)
471 g_debug("AWeatherRadar: new");
472 AWeatherRadar *self = g_object_new(AWEATHER_TYPE_RADAR, NULL);
475 GtkWidget *config = aweather_gui_get_widget(gui, "tabs");
476 AWeatherView *view = aweather_gui_get_view(gui);
478 /* Add configuration tab */
479 self->config_body = gtk_alignment_new(0, 0, 1, 1);
480 gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
481 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
482 gtk_notebook_prepend_page(GTK_NOTEBOOK(config), self->config_body, gtk_label_new("Radar"));
484 /* Set up OpenGL Stuff */
485 g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
486 g_signal_connect(view, "time-changed", G_CALLBACK(on_time_changed), self);
487 g_signal_connect(view, "refresh", G_CALLBACK(on_refresh), self);
492 static void _aweather_radar_expose(AWeatherPlugin *_self)
494 AWeatherRadar *self = AWEATHER_RADAR(_self);
495 g_debug("AWeatherRadar: expose");
496 if (self->cur_sweep == NULL)
498 Sweep *sweep = self->cur_sweep;
502 glMatrixMode(GL_MODELVIEW);
504 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
505 glEnable(GL_TEXTURE_2D);
506 glDisable(GL_ALPHA_TEST);
508 glBegin(GL_QUAD_STRIP);
509 for (int ri = 0; ri <= sweep->h.nrays; ri++) {
512 if (ri < sweep->h.nrays) {
513 ray = sweep->ray[ri];
514 angle = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0;
516 /* Do the right side of the last sweep */
517 ray = sweep->ray[ri-1];
518 angle = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0;
521 double lx = sin(angle);
522 double ly = cos(angle);
524 double near_dist = ray->h.range_bin1;
525 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
527 /* (find middle of bin) / scale for opengl */
529 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
530 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
533 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
534 glVertex3f(lx*far_dist, ly*far_dist, 2.0);
536 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
542 //glTexCoord2d( 0., 0.); glVertex3f(-500., 0., 0.); // bot left
543 //glTexCoord2d( 0., 1.); glVertex3f(-500., 500., 0.); // top left
544 //glTexCoord2d( 1., 1.); glVertex3f( 0., 500., 3.); // top right
545 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 3.); // bot right
548 /* Print the color table */
549 glDisable(GL_TEXTURE_2D);
550 glDisable(GL_DEPTH_TEST);
551 glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
552 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
555 for (i = 0; i < 256; i++) {
556 glColor4ub(self->cur_colormap->data[i][0],
557 self->cur_colormap->data[i][1],
558 self->cur_colormap->data[i][2],
559 self->cur_colormap->data[i][3]);
560 glVertex3f(-1.0, (float)((i ) - 256/2)/(256/2), 0.0); // bot left
561 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
562 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
563 glVertex3f(-0.9, (float)((i ) - 256/2)/(256/2), 0.0); // bot right
566 glEnable(GL_DEPTH_TEST);
567 glEnable(GL_ALPHA_TEST);
568 glMatrixMode(GL_PROJECTION); glPopMatrix();
569 glMatrixMode(GL_MODELVIEW ); glPopMatrix();