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"
30 static char *nexrad_base = "http://mesonet.agron.iastate.edu/data/";
36 static void aweather_radar_plugin_init(AWeatherPluginInterface *iface);
37 static void _aweather_radar_expose(AWeatherPlugin *_radar);
38 G_DEFINE_TYPE_WITH_CODE(AWeatherRadar, aweather_radar, G_TYPE_OBJECT,
39 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
40 aweather_radar_plugin_init));
41 static void aweather_radar_plugin_init(AWeatherPluginInterface *iface)
43 g_debug("AWeatherRadar: plugin_init");
44 /* Add methods to the interface */
45 iface->expose = _aweather_radar_expose;
47 /* Class/Object init */
48 static void aweather_radar_init(AWeatherRadar *radar)
50 g_debug("AWeatherRadar: class_init");
55 static void aweather_radar_dispose(GObject *gobject)
57 g_debug("AWeatherRadar: dispose");
58 AWeatherRadar *self = AWEATHER_RADAR(gobject);
60 G_OBJECT_CLASS(aweather_radar_parent_class)->dispose(gobject);
62 static void aweather_radar_finalize(GObject *gobject)
64 g_debug("AWeatherRadar: finalize");
65 AWeatherRadar *self = AWEATHER_RADAR(gobject);
67 G_OBJECT_CLASS(aweather_radar_parent_class)->finalize(gobject);
70 static void aweather_radar_class_init(AWeatherRadarClass *klass)
72 g_debug("AWeatherRadar: class_init");
73 GObjectClass *gobject_class = (GObjectClass*)klass;
74 gobject_class->dispose = aweather_radar_dispose;
75 gobject_class->finalize = aweather_radar_finalize;
78 /**************************
79 * Data loading functions *
80 **************************/
81 /* Convert a sweep to an 2d array of data points */
82 static void bscan_sweep(AWeatherRadar *self, Sweep *sweep, colormap_t *colormap,
83 guint8 **data, int *width, int *height)
85 /* Calculate max number of bins */
87 for (i = 0; i < sweep->h.nrays; i++)
88 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
90 /* Allocate buffer using max number of bins for each ray */
91 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
95 for (ri = 0; ri < sweep->h.nrays; ri++) {
96 Ray *ray = sweep->ray[ri];
97 for (bi = 0; bi < ray->h.nbins; bi++) {
98 /* copy RGBA into buffer */
99 //guint val = dz_f(ray->range[bi]);
100 guint8 val = (guint8)ray->h.f(ray->range[bi]);
101 guint buf_i = (ri*max_bins+bi)*4;
102 buf[buf_i+0] = colormap->data[val][0];
103 buf[buf_i+1] = colormap->data[val][1];
104 buf[buf_i+2] = colormap->data[val][2];
105 buf[buf_i+3] = colormap->data[val][3];
106 if (val == BADVAL || val == RFVAL || val == APFLAG ||
107 val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
108 buf[buf_i+3] = 0x00; // transparent
115 *height = sweep->h.nrays;
119 /* Load a sweep as the active texture */
120 static void load_sweep(AWeatherRadar *self, Sweep *sweep)
122 aweather_gui_gl_begin(self->gui);
123 self->cur_sweep = sweep;
126 bscan_sweep(self, sweep, self->cur_colormap, &data, &width, &height);
127 glDeleteTextures(1, &self->cur_sweep_tex);
128 glGenTextures(1, &self->cur_sweep_tex);
129 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
130 glPixelStorei(GL_PACK_ALIGNMENT, 1);
131 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
132 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
134 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
135 GL_RGBA, GL_UNSIGNED_BYTE, data);
137 aweather_gui_gl_redraw(self->gui);
138 aweather_gui_gl_end(self->gui);
141 static void load_colormap(AWeatherRadar *self, gchar *table)
143 /* Set colormap so we can draw it on expose */
144 for (int i = 0; colormaps[i].name; i++)
145 if (g_str_equal(colormaps[i].name, table))
146 self->cur_colormap = &colormaps[i];
149 /* Add selectors to the config area for the sweeps */
150 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self);
151 static void load_radar_gui(AWeatherRadar *self, Radar *radar)
153 /* Clear existing items */
154 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
156 gtk_widget_destroy(child);
159 guint rows = 1, cols = 1, cur_cols;
160 gchar row_label_str[64], col_label_str[64], button_str[64];
161 GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
162 GtkWidget *table = gtk_table_new(rows, cols, FALSE);
164 for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
165 Volume *vol = radar->v[vi];
166 if (vol == NULL) continue;
167 rows++; cols = 1; elev = 0;
170 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
171 row_label = gtk_label_new(row_label_str);
172 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
173 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
174 gtk_table_attach(GTK_TABLE(table), row_label,
175 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
177 for (guint si = 0; si < vol->h.nsweeps; si++) {
178 Sweep *sweep = vol->sweep[si];
179 if (sweep == NULL || sweep->h.elev == 0) continue;
180 if (sweep->h.elev != elev) {
182 elev = sweep->h.elev;
185 g_object_get(table, "n-columns", &cur_cols, NULL);
186 if (cols > cur_cols) {
187 g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
188 col_label = gtk_label_new(col_label_str);
189 gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
190 gtk_widget_set_size_request(col_label, 40, -1);
191 gtk_table_attach(GTK_TABLE(table), col_label,
192 cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
195 elev_box = gtk_hbox_new(TRUE, 0);
196 gtk_table_attach(GTK_TABLE(table), elev_box,
197 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
202 g_snprintf(button_str, 64, "%3.2f", elev);
203 button = gtk_radio_button_new_with_label_from_widget(
204 GTK_RADIO_BUTTON(button), button_str);
205 gtk_widget_set_size_request(button, -1, 26);
206 //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
207 //gtk_widget_set_size_request(button, -1, 22);
208 g_object_set(button, "draw-indicator", FALSE, NULL);
209 gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
211 g_object_set_data(G_OBJECT(button), "type", vol->h.type_str);
212 g_object_set_data(G_OBJECT(button), "sweep", sweep);
213 g_signal_connect(button, "clicked", G_CALLBACK(on_sweep_clicked), self);
216 gtk_container_add(GTK_CONTAINER(self->config_body), table);
217 gtk_widget_show_all(table);
220 /* Load a radar from a decompressed file */
221 static void load_radar(AWeatherRadar *self, gchar *radar_file)
223 char *dir = g_path_get_dirname(radar_file);
224 char *site = g_path_get_basename(dir);
226 g_debug("AWeatherRadar: load_radar - Loading new radar");
227 RSL_read_these_sweeps("all", NULL);
228 Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site);
230 g_warning("fail to load radar: path=%s, site=%s", radar_file, site);
236 /* Load the first sweep by default */
237 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
238 g_warning("No sweeps found\n");
240 /* load first available sweep */
241 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
242 if (radar->v[vi]== NULL) continue;
243 for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
244 if (radar->v[vi]->sweep[si]== NULL) continue;
245 load_colormap(self, radar->v[vi]->h.type_str);
246 load_sweep(self, radar->v[vi]->sweep[si]);
253 load_radar_gui(self, radar);
256 /* TODO: These update times functions are getting ugly... */
257 static void update_times_gtk(AWeatherRadar *self, GList *times)
259 gchar *last_time = NULL;
260 GRegex *regex = g_regex_new("^[A-Z]{4}_([0-9]{8}_[0-9]{4})$", 0, 0, NULL); // KLSX_20090622_2113
263 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self->gui, "time"));
264 GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview));
265 gtk_list_store_clear(lstore);
267 times = g_list_reverse(times);
268 for (GList *cur = times; cur; cur = cur->next) {
269 g_message("trying time %s", (gchar*)cur->data);
270 if (g_regex_match(regex, cur->data, 0, &info)) {
271 gchar *time = g_match_info_fetch(info, 1);
272 g_message("adding time %s", (gchar*)cur->data);
273 gtk_list_store_insert(lstore, &iter, 0);
274 gtk_list_store_set(lstore, &iter, 0, time, -1);
279 AWeatherView *view = aweather_gui_get_view(self->gui);
280 aweather_view_set_time(view, last_time);
282 g_regex_unref(regex);
283 g_list_foreach(times, (GFunc)g_free, NULL);
286 static void update_times_online_cb(char *path, gboolean updated, gpointer _self)
291 g_file_get_contents(path, &data, &length, NULL);
292 gchar **lines = g_strsplit(data, "\n", -1);
293 for (int i = 0; lines[i] && lines[i][0]; i++) {
294 char **parts = g_strsplit(lines[i], " ", 2);
295 times = g_list_prepend(times, g_strdup(parts[1]));
301 update_times_gtk(_self, times);
303 static void update_times(AWeatherRadar *self, AWeatherView *view, char *site)
305 if (aweather_view_get_offline(view)) {
307 gchar *path = g_build_filename(g_get_user_cache_dir(), PACKAGE, "nexrd2", "raw", site, NULL);
308 GDir *dir = g_dir_open(path, 0, NULL);
311 while ((name = g_dir_read_name(dir))) {
312 times = g_list_prepend(times, g_strdup(name));
317 update_times_gtk(self, times);
319 gchar *path = g_strdup_printf("nexrd2/raw/%s/dir.list", site);
320 cache_file(nexrad_base, path, AWEATHER_REFRESH, NULL, update_times_online_cb, self);
321 /* update_times_gtk from update_times_online_cb */
333 static void decompressed_cb(GPid pid, gint status, gpointer _udata)
335 decompressed_t *udata = _udata;
337 g_warning("wsr88ddec exited with status %d", status);
340 load_radar(udata->self, udata->radar_file);
341 g_spawn_close_pid(pid);
342 g_free(udata->radar_file);
346 static void cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self)
348 AWeatherRadar *self = AWEATHER_RADAR(_self);
349 double percent = (double)cur/total;
351 g_message("AWeatherRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
352 cur, total, percent*100);
354 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
356 gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
357 percent*100, (double)cur/1000000, (double)total/1000000);
358 gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
362 static void cache_done_cb(char *path, gboolean updated, gpointer _self)
364 AWeatherRadar *self = AWEATHER_RADAR(_self);
365 char *decompressed = g_strconcat(path, ".raw", NULL);
367 load_radar(self, decompressed);
371 decompressed_t *udata = g_malloc(sizeof(decompressed_t));
373 udata->radar_file = decompressed;
374 g_debug("AWeatherRadar: cache_done_cb - File updated, decompressing..");
375 char *argv[] = {"wsr88ddec", path, decompressed, NULL};
377 GError *error = NULL;
379 NULL, // const gchar *working_directory,
380 argv, // gchar **argv,
381 NULL, // gchar **envp,
383 G_SPAWN_DO_NOT_REAP_CHILD,
384 // GSpawnFlags flags,
385 NULL, // GSpawnChildSetupFunc child_setup,
386 NULL, // gpointer user_data,
387 &pid, // GPid *child_pid,
388 &error); // GError **error
390 g_warning("failed to decompress WSR88D data: %s",
394 g_child_watch_add(pid, decompressed_cb, udata);
401 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self)
403 AWeatherRadar *self = AWEATHER_RADAR(_self);
404 load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
405 load_sweep (self, g_object_get_data(G_OBJECT(button), "sweep"));
408 static void on_time_changed(AWeatherView *view, const char *time, gpointer _self)
410 AWeatherRadar *self = AWEATHER_RADAR(_self);
411 g_debug("AWeatherRadar: on_time_changed - setting time=%s", time);
412 // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
413 char *site = aweather_view_get_site(view);
414 char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time);
416 /* Set up progress bar */
417 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
418 if (child) gtk_widget_destroy(child);
420 GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
421 gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
422 self->progress_bar = gtk_progress_bar_new();
423 self->progress_label = gtk_label_new("Loading radar...");
424 gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar, FALSE, FALSE, 0);
425 gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
426 gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
427 gtk_widget_show_all(self->config_body);
431 RSL_free_radar(self->cur_radar);
432 self->cur_radar = NULL;
433 self->cur_sweep = NULL;
434 aweather_gui_gl_redraw(self->gui);
436 /* Start loading the new radar */
438 soup_session_abort(self->soup);
441 if (aweather_view_get_offline(view))
442 self->soup = cache_file(nexrad_base, path, AWEATHER_ONCE,
443 cache_chunk_cb, cache_done_cb, self);
445 self->soup = cache_file(nexrad_base, path, AWEATHER_UPDATE,
446 cache_chunk_cb, cache_done_cb, self);
450 static void on_site_changed(AWeatherView *view, char *site, gpointer _self)
452 AWeatherRadar *self = AWEATHER_RADAR(_self);
453 g_debug("AWeatherRadar: on_site_changed - Loading wsr88d list for %s", site);
454 update_times(self, view, site), &time;
457 static void on_refresh(AWeatherView *view, gpointer _self)
459 AWeatherRadar *self = AWEATHER_RADAR(_self);
460 char *site = aweather_view_get_site(view);
461 update_times(self, view, site);
467 AWeatherRadar *aweather_radar_new(AWeatherGui *gui)
469 g_debug("AWeatherRadar: new");
470 AWeatherRadar *self = g_object_new(AWEATHER_TYPE_RADAR, NULL);
473 GtkWidget *config = aweather_gui_get_widget(gui, "tabs");
474 AWeatherView *view = aweather_gui_get_view(gui);
476 /* Add configuration tab */
477 self->config_body = gtk_alignment_new(0, 0, 1, 1);
478 gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
479 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
480 gtk_notebook_prepend_page(GTK_NOTEBOOK(config), self->config_body, gtk_label_new("Radar"));
482 /* Set up OpenGL Stuff */
483 g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
484 g_signal_connect(view, "time-changed", G_CALLBACK(on_time_changed), self);
485 g_signal_connect(view, "refresh", G_CALLBACK(on_refresh), self);
490 static void _aweather_radar_expose(AWeatherPlugin *_self)
492 AWeatherRadar *self = AWEATHER_RADAR(_self);
493 g_debug("AWeatherRadar: expose");
494 if (self->cur_sweep == NULL)
496 Sweep *sweep = self->cur_sweep;
500 glMatrixMode(GL_MODELVIEW);
502 glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
503 glEnable(GL_TEXTURE_2D);
504 glDisable(GL_ALPHA_TEST);
506 glBegin(GL_QUAD_STRIP);
507 for (int ri = 0; ri <= sweep->h.nrays; ri++) {
510 if (ri < sweep->h.nrays) {
511 ray = sweep->ray[ri];
512 angle = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0;
514 /* Do the right side of the last sweep */
515 ray = sweep->ray[ri-1];
516 angle = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0;
519 double lx = sin(angle);
520 double ly = cos(angle);
522 double near_dist = ray->h.range_bin1;
523 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
525 /* (find middle of bin) / scale for opengl */
527 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
528 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
531 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
532 glVertex3f(lx*far_dist, ly*far_dist, 2.0);
534 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
540 //glTexCoord2d( 0., 0.); glVertex3f(-500., 0., 0.); // bot left
541 //glTexCoord2d( 0., 1.); glVertex3f(-500., 500., 0.); // top left
542 //glTexCoord2d( 1., 1.); glVertex3f( 0., 500., 3.); // top right
543 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 3.); // bot right
546 /* Print the color table */
547 glDisable(GL_TEXTURE_2D);
548 glDisable(GL_DEPTH_TEST);
549 glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
550 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
553 for (i = 0; i < 256; i++) {
554 glColor4ub(self->cur_colormap->data[i][0],
555 self->cur_colormap->data[i][1],
556 self->cur_colormap->data[i][2],
557 self->cur_colormap->data[i][3]);
558 glVertex3f(-1.0, (float)((i ) - 256/2)/(256/2), 0.0); // bot left
559 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
560 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
561 glVertex3f(-0.9, (float)((i ) - 256/2)/(256/2), 0.0); // bot right
564 glEnable(GL_DEPTH_TEST);
565 glEnable(GL_ALPHA_TEST);
566 glMatrixMode(GL_PROJECTION); glPopMatrix();
567 glMatrixMode(GL_MODELVIEW ); glPopMatrix();