9 #include "aweather-gui.h"
10 #include "plugin-radar.h"
13 static Sweep *cur_sweep = NULL; // make this not global
14 static int nred, ngreen, nblue;
15 static char red[256], green[256], blue[256];
16 static guint sweep_tex = 0;
18 static AWeatherGui *gui = NULL;
19 static Radar *radar = NULL;
21 static guint8 get_alpha(guint8 db)
23 if (db == BADVAL) return 0;
24 if (db == RFVAL ) return 0;
25 if (db == APFLAG) return 0;
26 if (db == NOECHO) return 0;
27 if (db == 0 ) return 0;
28 //if (db > 60) return 0;
29 //else if (db < 10) return 0;
30 //else if (db < 25) return (db-10)*(255.0/15);
34 /* Convert a sweep to an 2d array of data points */
35 static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
37 /* Calculate max number of bins */
39 for (i = 0; i < sweep->h.nrays; i++)
40 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
42 /* Allocate buffer using max number of bins for each ray */
43 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
47 for (ri = 0; ri < sweep->h.nrays; ri++) {
48 Ray *ray = sweep->ray[ri];
49 for (bi = 0; bi < ray->h.nbins; bi++) {
50 /* copy RGBA into buffer */
51 //guint val = dz_f(ray->range[bi]);
52 guint val = ray->h.f(ray->range[bi]);
53 guint buf_i = (ri*max_bins+bi)*4;
54 buf[buf_i+0] = red[val];
55 buf[buf_i+1] = green[val];
56 buf[buf_i+2] = blue[val];
57 buf[buf_i+3] = get_alpha(val);
63 *height = sweep->h.nrays;
67 /* Load a sweep as the active texture */
68 static void load_sweep(Sweep *sweep)
73 bscan_sweep(sweep, &data, &width, &height);
74 glGenTextures(1, &sweep_tex);
75 glBindTexture(GL_TEXTURE_2D, sweep_tex);
76 glPixelStorei(GL_PACK_ALIGNMENT, 1);
77 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
78 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
79 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
82 gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
83 gdk_window_process_updates(drawing->window, FALSE);
86 /* Load the default sweep */
87 static gboolean map(GtkWidget *da, GdkEvent *event, gpointer user_data)
89 g_message("radar:map");
90 aweather_gui_gl_begin(gui);
91 Sweep *first = radar->v[0]->sweep[0];
92 if (cur_sweep == NULL)
94 aweather_gui_gl_end(gui);
99 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
101 g_message("radar:expose");
102 Sweep *sweep = cur_sweep;
106 glMatrixMode(GL_MODELVIEW);
108 glBindTexture(GL_TEXTURE_2D, sweep_tex);
109 glEnable(GL_TEXTURE_2D);
111 glBegin(GL_QUAD_STRIP);
112 for (int ri = 0; ri <= sweep->h.nrays+1; ri++) {
113 /* Do the first sweep twice to complete the last Quad */
114 Ray *ray = sweep->ray[ri % sweep->h.nrays];
116 /* right and left looking out from radar */
117 double left = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0;
118 //double right = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0;
120 double lx = sin(left);
121 double ly = cos(left);
123 double near_dist = ray->h.range_bin1;
124 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
126 /* (find middle of bin) / scale for opengl */
128 glTexCoord2f(0.0, (double)ri/sweep->h.nrays);
129 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
132 glTexCoord2f(1.0, (double)ri/sweep->h.nrays);
133 glVertex3f(lx*far_dist, ly*far_dist, 2.0);
135 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
141 //glTexCoord2d( 0., 0.); glVertex3f(-1., 0., 0.); // bot left
142 //glTexCoord2d( 0., 1.); glVertex3f(-1., 1., 0.); // top left
143 //glTexCoord2d( 1., 1.); glVertex3f( 0., 1., 0.); // top right
144 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 0.); // bot right
147 /* Print the color table */
148 glDisable(GL_TEXTURE_2D);
149 glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
150 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
153 for (i = 0; i < nred; i++) {
154 glColor4ub(red[i], green[i], blue[i], get_alpha(i));
155 glVertex3f(-1.0, (float)((i ) - nred/2)/(nred/2), 0.0); // bot left
156 glVertex3f(-1.0, (float)((i+1) - nred/2)/(nred/2), 0.0); // top left
157 glVertex3f(-0.9, (float)((i+1) - nred/2)/(nred/2), 0.0); // top right
158 glVertex3f(-0.9, (float)((i ) - nred/2)/(nred/2), 0.0); // bot right
161 glMatrixMode(GL_PROJECTION); glPopMatrix();
162 glMatrixMode(GL_MODELVIEW ); glPopMatrix();
166 //static void set_site(AWeatherView *view, char *site, gpointer user_data)
168 // g_message("location changed to %s", site);
171 gboolean radar_init(AWeatherGui *_gui)
174 drawing = GTK_WIDGET(aweather_gui_get_drawing(gui));
175 GtkNotebook *config = aweather_gui_get_tabs(gui);
177 /* Parse hard coded file.. */
178 RSL_read_these_sweeps("all", NULL);
179 //RSL_read_these_sweeps("all", NULL);
180 radar = RSL_wsr88d_to_radar("/scratch/aweather/data/level2/KNQA_20090501_1925.raw", "KNQA");
181 RSL_load_refl_color_table();
182 RSL_get_color_table(RSL_RED_TABLE, red, &nred);
183 RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
184 RSL_get_color_table(RSL_BLUE_TABLE, blue, &nblue);
185 if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1)
186 g_print("No sweeps found\n");
188 /* Add configuration tab */
189 GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
190 GtkWidget *hbox = gtk_hbox_new(TRUE, 0);
191 GtkWidget *button = NULL;
193 for (vi = 0; vi < radar->h.nvolumes; vi++) {
194 Volume *vol = radar->v[vi];
195 if (vol == NULL) continue;
196 GtkWidget *vbox = gtk_vbox_new(TRUE, 0);
197 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
198 for (si = 0; si < vol->h.nsweeps; si++) {
199 Sweep *sweep = vol->sweep[si];
200 if (sweep == NULL) continue;
201 char *label = g_strdup_printf("Tilt: %.2f (%s)", sweep->h.elev, vol->h.type_str);
202 button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), label);
203 g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_sweep), sweep);
204 gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
208 GtkWidget *label = gtk_label_new("Radar");
209 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), hbox);
210 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
211 gtk_notebook_append_page(GTK_NOTEBOOK(config), scroll, label);
213 /* Set up OpenGL Stuff */
214 g_signal_connect(drawing, "map-event", G_CALLBACK(map), NULL);
215 g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);