]> Pileus Git - grits/blobdiff - src/plugin-radar.c
better color maps
[grits] / src / plugin-radar.c
index 1000b6e5fbcc5cc01b9143013848c00135a67069..0ee67ba4528cbdd50720b4b075e6666a9b7a333b 100644 (file)
@@ -30,8 +30,9 @@
 GtkWidget *drawing;
 GtkWidget *config_body;
 static Sweep *cur_sweep = NULL;  // make this not global
-static int nred, ngreen, nblue;
-static char red[256], green[256], blue[256];
+//static int nred, ngreen, nblue;
+//static char red[256], green[256], blue[256];
+static colormap_t *colormap;
 static guint sweep_tex = 0;
 
 static AWeatherGui *gui = NULL;
@@ -40,20 +41,6 @@ static Radar *radar = NULL;
 /**************************
  * Data loading functions *
  **************************/
-/* return a GL alpha value for a radar pixle */
-static guint8 get_alpha(guint8 db)
-{
-       if (db == BADVAL) return 0;
-       if (db == RFVAL ) return 0;
-       if (db == APFLAG) return 0;
-       if (db == NOECHO) return 0;
-       if (db == 0     ) return 0;
-       if      (db > 60) return 0;
-       else if (db < 10) return 0;
-       else if (db < 25) return (db-10)*(255.0/15);
-       else              return 255;
-}
-
 /* Convert a sweep to an 2d array of data points */
 static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
 {
@@ -72,12 +59,16 @@ static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
                for (bi = 0; bi < ray->h.nbins; bi++) {
                        /* copy RGBA into buffer */
                        //guint val   = dz_f(ray->range[bi]);
-                       guint val   = ray->h.f(ray->range[bi]);
-                       guint buf_i = (ri*max_bins+bi)*4;
-                       buf[buf_i+0] =   red[val];
-                       buf[buf_i+1] = green[val];
-                       buf[buf_i+2] =  blue[val];
-                       buf[buf_i+3] = get_alpha(val);
+                       guint8 val   = (guint8)ray->h.f(ray->range[bi]);
+                       guint  buf_i = (ri*max_bins+bi)*4;
+                       buf[buf_i+0] = colormap->data[val][0];
+                       buf[buf_i+1] = colormap->data[val][1];
+                       buf[buf_i+2] = colormap->data[val][2];
+                       buf[buf_i+3] = colormap->data[val][3];
+                       if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
+                           val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
+                               buf[buf_i+3] = 0x00; // transparent
+                       }
                }
        }
 
@@ -87,6 +78,13 @@ static void bscan_sweep(Sweep *sweep, guint8 **data, int *width, int *height)
        *data   = buf;
 }
 
+static void load_color_table(char *table)
+{
+       for (int i = 0; colormaps[i].name; i++)
+               if (g_str_equal(colormaps[i].name, table))
+                       colormap = &colormaps[i];
+}
+
 /* Load a sweep as the active texture */
 static void load_sweep(Sweep *sweep)
 {
@@ -103,8 +101,7 @@ static void load_sweep(Sweep *sweep)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
        g_free(data);
-       gdk_window_invalidate_rect(drawing->window, &drawing->allocation, FALSE);
-       gdk_window_process_updates(drawing->window, FALSE);
+       gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
        aweather_gui_gl_end(gui);
 }
 
@@ -129,6 +126,7 @@ static void load_radar_gui(Radar *radar)
                        char *label = g_strdup_printf("Tilt: %.2f (%s)", sweep->h.elev, vol->h.type_str);
                        button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), label);
                        g_object_set(button, "draw-indicator", FALSE, NULL);
+                       g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_color_table), vol->h.type_str);
                        g_signal_connect_swapped(button, "clicked", G_CALLBACK(load_sweep), sweep);
                        gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, TRUE, 0);
                        g_free(label);
@@ -157,13 +155,6 @@ static void load_radar_rsl(GPid pid, gint status, gpointer _path)
                return;
        }
 
-       /* TODO: replace this with a better color table */
-       g_message("loading color table");
-       RSL_load_refl_color_table();
-       RSL_get_color_table(RSL_RED_TABLE,   red,   &nred);
-       RSL_get_color_table(RSL_GREEN_TABLE, green, &ngreen);
-       RSL_get_color_table(RSL_BLUE_TABLE,  blue,  &nblue);
-
        /* Load the first sweep by default */
        if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
                g_warning("No sweeps found\n");
@@ -173,6 +164,7 @@ static void load_radar_rsl(GPid pid, gint status, gpointer _path)
                        if (radar->v[vi]== NULL) continue;
                        for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
                                if (radar->v[vi]->sweep[si]== NULL) continue;
+                               load_color_table(radar->v[vi]->h.type_str);
                                load_sweep(radar->v[vi]->sweep[si]);
                                break;
                        }
@@ -271,12 +263,15 @@ static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
        glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
        glBegin(GL_QUADS);
        int i;
-       for (i = 0; i < nred; i++) {
-               glColor4ub(red[i], green[i], blue[i], get_alpha(i));
-               glVertex3f(-1.0, (float)((i  ) - nred/2)/(nred/2), 0.0); // bot left
-               glVertex3f(-1.0, (float)((i+1) - nred/2)/(nred/2), 0.0); // top left
-               glVertex3f(-0.9, (float)((i+1) - nred/2)/(nred/2), 0.0); // top right
-               glVertex3f(-0.9, (float)((i  ) - nred/2)/(nred/2), 0.0); // bot right
+       for (i = 0; i < 256; i++) {
+               glColor4ub(colormap->data[i][0],
+                          colormap->data[i][1],
+                          colormap->data[i][2],
+                          colormap->data[i][3]);
+               glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
+               glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
+               glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
+               glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
        }
        glEnd();
        glEnable(GL_DEPTH_TEST);
@@ -294,6 +289,8 @@ static void set_time(AWeatherView *view, char *time, gpointer user_data)
        char *base = "http://mesonet.agron.iastate.edu/data/";
        char *path = g_strdup_printf("nexrd2/raw/K%s/K%s_%s", site, site, time);
        //g_message("caching %s/%s", base, path);
+       cur_sweep = NULL; // Clear radar
+       gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
        cache_file(base, path, load_radar, NULL);
 }
 
@@ -305,6 +302,8 @@ static void set_site(AWeatherView *view, char *site, gpointer user_data)
        GError *error = NULL;
        char *list_uri = g_strdup_printf("http://mesonet.agron.iastate.edu/data/nexrd2/raw/K%s/dir.list", site);
        GFile *list    = g_file_new_for_uri(list_uri);
+       cur_sweep = NULL; // Clear radar
+       gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
        g_file_load_contents(list, NULL, &data, &length, NULL, &error);
        if (error) {
                g_warning("Error loading list for %s: %s", site, error->message);