]> Pileus Git - aweather/blob - src/radar.c
2fdf7d385c6df2825db3422e8266681fd381bd70
[aweather] / src / radar.c
1 #include <config.h>
2 #include <gtk/gtk.h>
3 #include <gtk/gtkgl.h>
4 #include <gdk/gdkkeysyms.h>
5 #include <GL/gl.h>
6 #include <math.h>
7
8 #include "rsl.h"
9
10 #include "radar.h"
11
12 GtkWidget *drawing;
13 static Sweep *cur_sweep; // make this not global
14 static int nred, ngreen, nblue;
15 static char red[256], green[256], blue[256];
16
17 /* Convert a sweep to an 2d array of data points */
18 static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
19 {
20         /* Calculate max number of bins */
21         int i, max_bins = 0;
22         for (i = 0; i < sweep->h.nrays; i++)
23                 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
24
25         /* Allocate buffer using max number of bins for each ray */
26         guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 3);
27
28         /* Fill the data */
29         int ri, bi;
30         for (ri = 0; ri < sweep->h.nrays; ri++) {
31                 Ray   *ray  = sweep->ray[ri];
32                 for (bi = 0; bi < ray->h.nbins; bi++) {
33                         Range bin = ray->range[bi];
34                         /* copy RGB into buffer */
35                         buf[(ri*max_bins+bi)*3+0] =   red[(gint8)ray->h.f(bin)];
36                         buf[(ri*max_bins+bi)*3+1] = green[(gint8)ray->h.f(bin)];
37                         buf[(ri*max_bins+bi)*3+2] =  blue[(gint8)ray->h.f(bin)];
38                 }
39         }
40
41         /* set output */
42         *width  = max_bins;
43         *height = sweep->h.nrays;
44         *data   = buf;
45
46         /* debug */
47         //static int fi = 0;
48         //char fname[128];
49         //sprintf(fname, "image-%d.ppm", fi);
50         //FILE *fp = fopen(fname, "w+");
51         //fprintf(fp, "P6\n");
52         //fprintf(fp, "# Foo\n");
53         //fprintf(fp, "%d %d\n", *width, *height);
54         //fprintf(fp, "255\n");
55         //fwrite(buf, 3, *width * *height, fp);
56         //fclose(fp);
57         //fi++;
58 }
59
60 /* Load a sweep as the active texture */
61 static void load_sweep(Sweep *sweep)
62 {
63         cur_sweep = sweep;
64         int height, width;
65         guint8 *data;
66         bscan_sweep(sweep, &data, &width, &height);
67         glPixelStorei(GL_PACK_ALIGNMENT, 1);
68         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
69         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
70         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
71         g_free(data);
72         glEnable(GL_TEXTURE_2D);
73         gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
74 }
75
76 /* Load the default sweep */
77 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
78 {
79         GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
80         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
81
82         if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
83                 g_assert_not_reached();
84
85         /* Load the texture */
86         load_sweep(cur_sweep);
87
88         gdk_gl_drawable_gl_end(gldrawable);
89
90         return FALSE;
91 }
92
93 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
94 {
95         Sweep *sweep = cur_sweep;
96
97         //GdkGLContext *glcontext = gtk_widget_get_gl_context(da);
98         //GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da);
99
100         /* draw in here */
101         glPushMatrix();
102         glRotatef(0, 0, 0, 0);
103
104         /* Draw the rays */
105         glEnable(GL_TEXTURE_2D);
106         glBegin(GL_QUAD_STRIP);
107         int _ri; // not really used, creates strange fragments..
108         for (_ri = 0; _ri < sweep->h.nrays; _ri++) {
109                 /* Do the first sweep twice to complete the last Quad */
110                 int ri = _ri % sweep->h.nrays;
111                 Ray *ray = sweep->ray[ri];
112
113                 /* right and left looking out from radar */
114                 double left  = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0; 
115                 //double right = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0; 
116
117                 double lx = sin(left);
118                 double ly = cos(left);
119
120                 /* TODO: change this to meters instead of 0..1 */
121                 double max_dist  = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
122                 double near_dist = (double)(ray->h.range_bin1) / max_dist;
123                 double far_dist  = (double)(ray->h.nbins*ray->h.gate_size + ray->h.range_bin1) / max_dist;
124
125                 /* (find middle of bin) / scale for opengl */
126                 glTexCoord2d(0.0, ((double)ri)/sweep->h.nrays); glVertex3f(lx*near_dist, ly*near_dist, 0.); // near left
127                 glTexCoord2d(0.7, ((double)ri)/sweep->h.nrays); glVertex3f(lx*far_dist,  ly*far_dist,  0.); // far  left
128         }
129         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
130         glEnd();
131         /* Texture debug */
132         //glBegin(GL_QUADS);
133         //glTexCoord2d( 0.,  0.); glVertex3f(-1.,  0., 0.); // bot left
134         //glTexCoord2d( 0.,  1.); glVertex3f(-1.,  1., 0.); // top left
135         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,  1., 0.); // top right
136         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,  0., 0.); // bot right
137         //glEnd();
138         glDisable(GL_TEXTURE_2D);
139
140         /* Print the color table */
141         glBegin(GL_QUADS);
142         int i;
143         for (i = 0; i < nred; i++) {
144                 glColor3ub(red[i], green[i], blue[i]);
145                 glVertex3f(-1., (float)((i  ) - nred/2)/(nred/2), 0.); // bot left
146                 glVertex3f(-1., (float)((i+1) - nred/2)/(nred/2), 0.); // top left
147                 glVertex3f(-.9, (float)((i+1) - nred/2)/(nred/2), 0.); // top right
148                 glVertex3f(-.9, (float)((i  ) - nred/2)/(nred/2), 0.); // bot right
149         }
150         glEnd();
151         
152         glPopMatrix();
153
154         return FALSE;
155 }
156
157 gboolean radar_init(GtkDrawingArea *_drawing, GtkNotebook *config)
158 {
159         drawing = GTK_WIDGET(_drawing);
160         /* Set up OpenGL Stuff */
161         g_signal_connect(drawing, "expose-event",    G_CALLBACK(expose),    NULL);
162         g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
163
164         /* Parse hard coded file.. */
165         RSL_read_these_sweeps("all", NULL);
166         //RSL_read_these_sweeps("all", NULL);
167         Radar *radar = RSL_wsr88d_to_radar("/scratch/aweather/data/KNQA_20090501_1925.raw", "KNQA");
168         RSL_load_refl_color_table();
169         RSL_get_color_table(RSL_RED_TABLE,   red,   &nred);
170         RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
171         RSL_get_color_table(RSL_BLUE_TABLE,  blue,  &nblue);
172         if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1)
173                 g_print("No sweeps found\n");
174         cur_sweep = radar->v[0]->sweep[6];
175
176         /* Add configuration tab */
177         GtkWidget *hbox = gtk_hbox_new(TRUE, 0);
178         GtkWidget *button = NULL;
179         int vi = 0, si = 0;
180         for (vi = 0; vi < radar->h.nvolumes; vi++) {
181                 Volume *vol = radar->v[vi];
182                 if (vol == NULL) continue;
183                 GtkWidget *vbox = gtk_vbox_new(TRUE, 0);
184                 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
185                 for (si = 0; si < vol->h.nsweeps; si++) {
186                         Sweep *sweep = vol->sweep[si];
187                         if (sweep == NULL) continue;
188                         char *label = g_strdup_printf("Tilt: %.2f (%s)", sweep->h.elev, vol->h.type_str);
189                         button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), label);
190                         g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_sweep), sweep);
191                         gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
192                         g_free(label);
193                 }
194         }
195         GtkWidget *label = gtk_label_new("Radar");
196         gtk_notebook_append_page(GTK_NOTEBOOK(config), hbox, label);
197         return TRUE;
198 }