]> Pileus Git - grits/blob - src/radar.c
Whooo, radar displays! (but it's really slow ):
[grits] / 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 static GtkWidget *rotate_button;
13 static Radar *radar;
14 static Sweep *sweep;
15 static int nred, ngreen, nblue;
16 static unsigned char red[256], green[256], blue[256];
17
18 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
19 {
20         GdkGLContext *glcontext = gtk_widget_get_gl_context (da);
21         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (da);
22
23         /* draw in here */
24         glPushMatrix();
25         
26         glRotatef(0, 0, 0, 0);
27
28         glShadeModel(GL_FLAT);
29
30         glBegin(GL_QUADS);
31
32         int rayi;
33         for (rayi = 0; rayi < sweep->h.nrays; rayi++) {
34                 Ray *ray = sweep->ray[rayi];
35
36                 /* right and left with respect to north */
37                 double right = ((ray->h.azimuth + ray->h.beam_width)*M_PI)/180.0; 
38                 double left  = ((ray->h.azimuth - ray->h.beam_width)*M_PI)/180.0; 
39
40                 double rx = sin(right), ry = cos(right);
41                 double lx = sin(left),  ly = cos(left);
42
43                 int nbins = ray->h.nbins;
44                 //int nbins = 20;
45                 int max_dist = nbins * ray->h.gate_size + ray->h.range_bin1;
46                 int bini, dist = ray->h.range_bin1;
47                 for (bini = 0; bini < nbins; bini++, dist += ray->h.gate_size) {
48                         if (ray->range[bini] == BADVAL) continue;
49
50                         /* (find middle of bin) / scale for opengl */
51                         double nd = ((double)dist - ((double)ray->h.gate_size)/2.) / (double)max_dist;
52                         double fd = ((double)dist + ((double)ray->h.gate_size)/2.) / (double)max_dist;
53                 
54                         glColor3ub(
55                                   red[(unsigned char)ray->h.f(ray->range[bini])],
56                                 green[(unsigned char)ray->h.f(ray->range[bini])],
57                                  blue[(unsigned char)ray->h.f(ray->range[bini])]
58                         );
59
60                         glVertex3f(lx*nd, ly*nd, 0.); // near left
61                         glVertex3f(lx*fd, ly*fd, 0.); // far  left
62                         glVertex3f(rx*fd, ry*fd, 0.); // far  right
63                         glVertex3f(rx*nd, ry*nd, 0.); // near right
64
65                 }
66
67                 g_print("\n");
68         }
69
70         glEnd();
71
72         /* Print the color table */
73         glBegin(GL_QUADS);
74         int i;
75         for (i = 0; i < nred; i++) {
76                 glColor3ub(red[i], green[i], blue[i]);
77                 glVertex3f(-1., (float)((i  ) - nred/2)/(nred/2), 0.); // bot left
78                 glVertex3f(-1., (float)((i+1) - nred/2)/(nred/2), 0.); // top left
79                 glVertex3f(-.9, (float)((i+1) - nred/2)/(nred/2), 0.); // top right
80                 glVertex3f(-.9, (float)((i  ) - nred/2)/(nred/2), 0.); // bot right
81         }
82         glEnd();
83         
84         glPopMatrix ();
85
86         return FALSE;
87 }
88
89 gboolean radar_init(GtkDrawingArea *drawing, GtkNotebook *config)
90 {
91         /* Add configuration tab */
92         GtkWidget *label = gtk_label_new("Radar");
93         rotate_button = gtk_toggle_button_new_with_label("Rotate");
94         gtk_notebook_append_page(GTK_NOTEBOOK(config), rotate_button, label);
95
96         /* Set up OpenGL Stuff */
97         g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
98
99         /* Parse hard coded file.. */
100         RSL_read_these_sweeps("0", NULL);
101         radar = RSL_wsr88d_to_radar("/scratch/aweather/src/KABR_20080609_0224", "KABR");
102         RSL_load_refl_color_table();
103         RSL_get_color_table(RSL_RED_TABLE,   red,   &nred);
104         RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
105         RSL_get_color_table(RSL_BLUE_TABLE,  blue,  &nblue);
106         if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1)
107                 g_print("No sweeps found\n");
108         sweep = radar->v[0]->sweep[0];
109         RSL_volume_to_gif(radar->v[DZ_INDEX], "dz_sweep", 1024, 1024, 200.0);
110 }