9 #include "aweather-gui.h"
10 #include "plugin-radar.h"
14 GtkWidget *config_body;
15 static Sweep *cur_sweep = NULL; // make this not global
16 static int nred, ngreen, nblue;
17 static char red[256], green[256], blue[256];
18 static guint sweep_tex = 0;
20 static AWeatherGui *gui = NULL;
21 static Radar *radar = NULL;
23 /**************************
24 * Data loading functions *
25 **************************/
26 /* return a GL alpha value for a radar pixle */
27 static guint8 get_alpha(guint8 db)
29 if (db == BADVAL) return 0;
30 if (db == RFVAL ) return 0;
31 if (db == APFLAG) return 0;
32 if (db == NOECHO) return 0;
33 if (db == 0 ) return 0;
34 //if (db > 60) return 0;
35 //else if (db < 10) return 0;
36 //else if (db < 25) return (db-10)*(255.0/15);
40 /* Convert a sweep to an 2d array of data points */
41 static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
43 /* Calculate max number of bins */
45 for (i = 0; i < sweep->h.nrays; i++)
46 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
48 /* Allocate buffer using max number of bins for each ray */
49 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
53 for (ri = 0; ri < sweep->h.nrays; ri++) {
54 Ray *ray = sweep->ray[ri];
55 for (bi = 0; bi < ray->h.nbins; bi++) {
56 /* copy RGBA into buffer */
57 //guint val = dz_f(ray->range[bi]);
58 guint val = ray->h.f(ray->range[bi]);
59 guint buf_i = (ri*max_bins+bi)*4;
60 buf[buf_i+0] = red[val];
61 buf[buf_i+1] = green[val];
62 buf[buf_i+2] = blue[val];
63 buf[buf_i+3] = get_alpha(val);
69 *height = sweep->h.nrays;
73 /* Load a sweep as the active texture */
74 static void load_sweep(Sweep *sweep)
76 aweather_gui_gl_begin(gui);
80 bscan_sweep(sweep, &data, &width, &height);
81 glGenTextures(1, &sweep_tex);
82 glBindTexture(GL_TEXTURE_2D, 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_LINEAR);
87 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
89 gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
90 gdk_window_process_updates(drawing->window, FALSE);
91 aweather_gui_gl_end(gui);
94 /* Add selectors to the config area for the sweeps */
95 static void load_radar_gui(Radar *radar)
97 /* Clear existing items */
98 GtkWidget *child = gtk_bin_get_child(GTK_BIN(config_body));
100 gtk_widget_destroy(child);
102 GtkWidget *hbox = gtk_hbox_new(TRUE, 0);
103 GtkWidget *button = NULL;
105 for (vi = 0; vi < radar->h.nvolumes; vi++) {
106 Volume *vol = radar->v[vi];
107 if (vol == NULL) continue;
108 GtkWidget *vbox = gtk_vbox_new(TRUE, 0);
109 for (si = vol->h.nsweeps-1; si >= 0; si--) {
110 Sweep *sweep = vol->sweep[si];
111 if (sweep == NULL) continue;
112 char *label = g_strdup_printf("Tilt: %.2f (%s)", sweep->h.elev, vol->h.type_str);
113 button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), label);
114 g_object_set(button, "draw-indicator", FALSE, NULL);
115 g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_sweep), sweep);
116 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 0);
119 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
120 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
122 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(config_body), hbox);
123 gtk_widget_show_all(hbox);
126 /* Load a radar from a file */
127 static void load_radar_rsl(GPid pid, gint status, gpointer _path)
131 g_warning("wsr88ddec exited with status %d", status);
134 char *site = g_path_get_basename(g_path_get_dirname(path));
135 if (radar) RSL_free_radar(radar);
136 RSL_read_these_sweeps("all", NULL);
137 radar = RSL_wsr88d_to_radar(path, site);
139 g_warning("fail to load radar: path=%s, site=%s", path, site);
143 /* TODO: replace this with a better color table */
144 g_message("loading color table");
145 RSL_load_refl_color_table();
146 RSL_get_color_table(RSL_RED_TABLE, red, &nred);
147 RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
148 RSL_get_color_table(RSL_BLUE_TABLE, blue, &nblue);
150 /* Load the first sweep by default */
151 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
152 g_warning("No sweeps found\n");
154 /* load first available sweep */
155 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
156 if (radar->v[vi]== NULL) continue;
157 for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
158 if (radar->v[vi]->sweep[si]== NULL) continue;
159 load_sweep(radar->v[vi]->sweep[si]);
166 load_radar_gui(radar);
169 /* decompress a radar file, then chain to the actuall loading function */
170 static void load_radar(char *path, gpointer user_data)
172 char *raw = g_strconcat(path, ".raw", NULL);
173 if (g_file_test(raw, G_FILE_TEST_EXISTS)) {
174 load_radar_rsl(0, 0, raw);
176 char *argv[] = {"./wsr88ddec", path, raw, NULL};
178 GError *error = NULL;
180 NULL, // const gchar *working_directory,
181 argv, // gchar **argv,
182 NULL, // gchar **envp,
183 G_SPAWN_DO_NOT_REAP_CHILD, // GSpawnFlags flags,
184 NULL, // GSpawnChildSetupFunc child_setup,
185 NULL, // gpointer user_data,
186 &pid, // GPid *child_pid,
187 &error); // GError **error
189 g_warning("failed to decompress WSR88D data: %s", error->message);
190 g_child_watch_add(pid, load_radar_rsl, raw);
197 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
199 g_message("radar:expose");
200 if (cur_sweep == NULL)
202 Sweep *sweep = cur_sweep;
206 glMatrixMode(GL_MODELVIEW);
208 glBindTexture(GL_TEXTURE_2D, sweep_tex);
209 glEnable(GL_TEXTURE_2D);
211 glBegin(GL_QUAD_STRIP);
212 for (int ri = 0; ri <= sweep->h.nrays+1; ri++) {
213 /* Do the first sweep twice to complete the last Quad */
214 Ray *ray = sweep->ray[ri % sweep->h.nrays];
216 /* right and left looking out from radar */
217 double left = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0;
218 //double right = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0;
220 double lx = sin(left);
221 double ly = cos(left);
223 double near_dist = ray->h.range_bin1;
224 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
226 /* (find middle of bin) / scale for opengl */
228 glTexCoord2f(0.0, (double)ri/sweep->h.nrays);
229 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
232 glTexCoord2f(1.0, (double)ri/sweep->h.nrays);
233 glVertex3f(lx*far_dist, ly*far_dist, 2.0);
235 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
241 //glTexCoord2d( 0., 0.); glVertex3f(-1., 0., 0.); // bot left
242 //glTexCoord2d( 0., 1.); glVertex3f(-1., 1., 0.); // top left
243 //glTexCoord2d( 1., 1.); glVertex3f( 0., 1., 0.); // top right
244 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 0.); // bot right
247 /* Print the color table */
248 glDisable(GL_TEXTURE_2D);
249 glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
250 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
253 for (i = 0; i < nred; i++) {
254 glColor4ub(red[i], green[i], blue[i], get_alpha(i));
255 glVertex3f(-1.0, (float)((i ) - nred/2)/(nred/2), 0.0); // bot left
256 glVertex3f(-1.0, (float)((i+1) - nred/2)/(nred/2), 0.0); // top left
257 glVertex3f(-0.9, (float)((i+1) - nred/2)/(nred/2), 0.0); // top right
258 glVertex3f(-0.9, (float)((i ) - nred/2)/(nred/2), 0.0); // bot right
261 glMatrixMode(GL_PROJECTION); glPopMatrix();
262 glMatrixMode(GL_MODELVIEW ); glPopMatrix();
266 static void set_time(AWeatherView *view, char *time, gpointer user_data)
268 g_message("radar:setting time");
269 // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
270 char *site = aweather_view_get_location(view);
271 char *base = "http://mesonet.agron.iastate.edu/data/";
272 char *path = g_strdup_printf("nexrd2/raw/K%s/K%s_%s", site, site, time);
273 //g_message("caching %s/%s", base, path);
274 cache_file(base, path, load_radar, NULL);
277 static void set_site(AWeatherView *view, char *site, gpointer user_data)
279 g_message("Loading wsr88d list for %s", site);
282 GError *error = NULL;
283 char *list_uri = g_strdup_printf("http://mesonet.agron.iastate.edu/data/nexrd2/raw/K%s/dir.list", site);
284 GFile *list = g_file_new_for_uri(list_uri);
285 g_file_load_contents(list, NULL, &data, &length, NULL, &error);
287 g_warning("Error loading list for %s: %s", site, error->message);
290 gchar **lines = g_strsplit(data, "\n", -1);
291 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
292 GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview));
293 gtk_list_store_clear(lstore);
296 for (int i = 0; lines[i] && lines[i][0]; i++) {
297 // format: `841907 KABR_20090510_0159'
298 //g_message("\tadding %p [%s]", lines[i], lines[i]);
299 char **parts = g_strsplit(lines[i], " ", 2);
302 gtk_list_store_insert(lstore, &iter, 0);
303 gtk_list_store_set(lstore, &iter, 0, time, -1);
306 aweather_view_set_time(view, time);
310 gboolean radar_init(AWeatherGui *_gui)
313 drawing = aweather_gui_get_widget(gui, "drawing");
314 GtkNotebook *config = GTK_NOTEBOOK(aweather_gui_get_widget(gui, "tabs"));
315 AWeatherView *view = aweather_gui_get_view(gui);
317 /* Add configuration tab */
318 config_body = gtk_scrolled_window_new(NULL, NULL);
319 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(config_body), gtk_label_new("No radar loaded"));
320 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(config_body), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
321 gtk_notebook_append_page(GTK_NOTEBOOK(config), config_body, gtk_label_new("Radar"));
323 /* Set up OpenGL Stuff */
324 g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
325 g_signal_connect(view, "location-changed", G_CALLBACK(set_site), NULL);
326 g_signal_connect(view, "time-changed", G_CALLBACK(set_time), NULL);