]> Pileus Git - aweather/blob - src/plugins/level2.c
Add dual-pole support and improve colormap handling
[aweather] / src / plugins / level2.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <math.h>
20 #include <GL/gl.h>
21 #include <glib/gstdio.h>
22 #include <grits.h>
23 #include <rsl.h>
24
25 #include "level2.h"
26
27
28 /**************************
29  * Data loading functions *
30  **************************/
31 /* Convert a sweep to an 2d array of data points */
32 static void _bscan_sweep(Sweep *sweep, AWeatherColormap *colormap,
33                 guint8 **data, int *width, int *height)
34 {
35         g_debug("AWeatherLevel2: _bscan_sweep - %p, %p, %p",
36                         sweep, colormap, data);
37         /* Calculate max number of bins */
38         int max_bins = 0;
39         for (int i = 0; i < sweep->h.nrays; i++)
40                 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
41
42         /* Allocate buffer using max number of bins for each ray */
43         guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
44
45         /* Fill the data */
46         for (int ri = 0; ri < sweep->h.nrays; ri++) {
47                 Ray *ray  = sweep->ray[ri];
48                 for (int bi = 0; bi < ray->h.nbins; bi++) {
49                         guint  buf_i = (ri*max_bins+bi)*4;
50                         float  value = ray->h.f(ray->range[bi]);
51
52                         /* Check for bad values */
53                         if (value == BADVAL     || value == RFVAL      || value == APFLAG ||
54                             value == NOTFOUND_H || value == NOTFOUND_V || value == NOECHO) {
55                                 buf[buf_i+3] = 0x00; // transparent
56                                 continue;
57                         }
58
59                         /* Convert data value to index */
60                         gint idx = value * colormap->scale + colormap->shift;
61                         idx = CLAMP(idx, 0, colormap->len);
62
63                         /* Copy color to buffer */
64                         buf[buf_i+0] = colormap->data[idx][0];
65                         buf[buf_i+1] = colormap->data[idx][1];
66                         buf[buf_i+2] = colormap->data[idx][2];
67                         buf[buf_i+3] = colormap->data[idx][3]*0.75; // TESTING
68                 }
69         }
70
71         /* set output */
72         *width  = max_bins;
73         *height = sweep->h.nrays;
74         *data   = buf;
75 }
76
77 /* Load a sweep into an OpenGL texture */
78 static void _load_sweep_gl(AWeatherLevel2 *self)
79 {
80         g_debug("AWeatherLevel2: _load_sweep_gl");
81         guint8 *data;
82         gint width, height;
83         _bscan_sweep(self->sweep, self->sweep_colors, &data, &width, &height);
84         gint tex_width  = pow(2, ceil(log(width )/log(2)));
85         gint tex_height = pow(2, ceil(log(height)/log(2)));
86         self->sweep_coords[0] = (double)width  / tex_width;
87         self->sweep_coords[1] = (double)height / tex_height;
88
89         if (!self->sweep_tex)
90                  glGenTextures(1, &self->sweep_tex);
91         glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
92         glPixelStorei(GL_PACK_ALIGNMENT, 1);
93         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_width, tex_height, 0,
95                         GL_RGBA, GL_UNSIGNED_BYTE, NULL);
96         glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, width,height,
97                         GL_RGBA, GL_UNSIGNED_BYTE, data);
98         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
99         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
100
101         g_free(data);
102 }
103
104 /* Decompress a radar file using wsr88dec */
105 static gboolean _decompress_radar(const gchar *file, const gchar *raw)
106 {
107         g_debug("AWeatherLevel2: _decompress_radar - \n\t%s\n\t%s", file, raw);
108         char *argv[] = {"wsr88ddec", (gchar*)file, (gchar*)raw, NULL};
109         gint status;
110         GError *error = NULL;
111         g_spawn_sync(
112                 NULL,    // const gchar *working_directory
113                 argv,    // gchar **argv
114                 NULL,    // gchar **envp
115                 G_SPAWN_SEARCH_PATH, // GSpawnFlags flags
116                 NULL,    // GSpawnChildSetupFunc child_setup
117                 NULL,    // gpointer user_data
118                 NULL,    // gchar *standard_output
119                 NULL,    // gchar *standard_output
120                 &status, // gint *exit_status
121                 &error); // GError **error
122         if (error) {
123                 g_warning("AWeatherLevel2: _decompress_radar - %s", error->message);
124                 g_error_free(error);
125                 return FALSE;
126         }
127         if (status != 0) {
128                 gchar *msg = g_strdup_printf("wsr88ddec exited with status %d", status);
129                 g_warning("AWeatherLevel2: _decompress_radar - %s", msg);
130                 g_free(msg);
131                 return FALSE;
132         }
133         return TRUE;
134 }
135
136
137 /*********************
138  * Drawing functions *
139  *********************/
140 void aweather_level2_draw(GritsObject *_self, GritsOpenGL *opengl)
141 {
142         AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
143         if (!self->sweep || !self->sweep_tex)
144                 return;
145
146         /* Draw wsr88d */
147         Sweep *sweep = self->sweep;
148         glDisable(GL_ALPHA_TEST);
149         glDisable(GL_CULL_FACE);
150         glDisable(GL_LIGHTING);
151         glEnable(GL_TEXTURE_2D);
152         glEnable(GL_POLYGON_OFFSET_FILL);
153         glPolygonOffset(1.0, -2.0);
154         glColor4f(1,1,1,1);
155
156         /* Draw the rays */
157         gdouble xscale = self->sweep_coords[0];
158         gdouble yscale = self->sweep_coords[1];
159         glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
160         glBegin(GL_TRIANGLE_STRIP);
161         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
162                 Ray  *ray = NULL;
163                 double angle = 0;
164                 if (ri < sweep->h.nrays) {
165                         ray = sweep->ray[ri];
166                         angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
167                 } else {
168                         /* Do the right side of the last sweep */
169                         ray = sweep->ray[ri-1];
170                         angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
171                 }
172
173                 double lx = sin(angle);
174                 double ly = cos(angle);
175
176                 double near_dist = ray->h.range_bin1 - ((double)ray->h.gate_size/2.);
177                 double far_dist  = near_dist + (double)ray->h.nbins*ray->h.gate_size;
178
179                 /* (find middle of bin) / scale for opengl */
180                 // near left
181                 glTexCoord2f(0.0, ((double)ri/sweep->h.nrays)*yscale);
182                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
183
184                 // far  left
185                 // todo: correct range-height function
186                 double height = sin(deg2rad(ray->h.elev)) * far_dist;
187                 glTexCoord2f(xscale, ((double)ri/sweep->h.nrays)*yscale);
188                 glVertex3f(lx*far_dist,  ly*far_dist, height);
189         }
190         glEnd();
191         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
192
193         /* Texture debug */
194         //glBegin(GL_QUADS);
195         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
196         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
197         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
198         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
199         //glEnd();
200 }
201
202
203 /***********
204  * Methods *
205  ***********/
206 static gboolean _set_sweep_cb(gpointer _self)
207 {
208         g_debug("AWeatherLevel2: _set_sweep_cb");
209         AWeatherLevel2 *self = _self;
210         _load_sweep_gl(self);
211         grits_object_queue_draw(_self);
212         g_object_unref(self);
213         return FALSE;
214 }
215 void aweather_level2_set_sweep(AWeatherLevel2 *self,
216                 int type, float elev)
217 {
218         g_debug("AWeatherLevel2: set_sweep - %d %f", type, elev);
219
220         /* Find sweep */
221         Volume *volume = RSL_get_volume(self->radar, type);
222         if (!volume) return;
223         self->sweep = RSL_get_closest_sweep(volume, elev, 90);
224         if (!self->sweep) return;
225
226         /* Find colormap */
227         self->sweep_colors = NULL;
228         for (int i = 0; self->colormap[i].file; i++)
229                 if (self->colormap[i].type == type)
230                         self->sweep_colors = &self->colormap[i];
231         if (!self->sweep_colors) {
232                 g_warning("AWeatherLevel2: set_sweep - missing colormap[%d]", type);
233                 self->sweep_colors = &self->colormap[0];
234         }
235
236         /* Load data */
237         g_object_ref(self);
238         g_idle_add(_set_sweep_cb, self);
239 }
240
241 AWeatherLevel2 *aweather_level2_new(Radar *radar, AWeatherColormap *colormap)
242 {
243         g_debug("AWeatherLevel2: new - %s", radar->h.radar_name);
244         AWeatherLevel2 *self = g_object_new(AWEATHER_TYPE_LEVEL2, NULL);
245         self->radar    = radar;
246         self->colormap = colormap;
247         aweather_level2_set_sweep(self, DZ_INDEX, 0);
248
249         GritsPoint center;
250         Radar_header *h = &radar->h;
251         center.lat  = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
252         center.lon  = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
253         center.elev = h->height;
254         GRITS_OBJECT(self)->center = center;
255         return self;
256 }
257
258 AWeatherLevel2 *aweather_level2_new_from_file(const gchar *file, const gchar *site,
259                 AWeatherColormap *colormap)
260 {
261         g_debug("AWeatherLevel2: new_from_file %s %s", site, file);
262
263         /* Decompress radar */
264         gchar *raw = g_strconcat(file, ".raw", NULL);
265         if (g_file_test(raw, G_FILE_TEST_EXISTS)) {
266                 struct stat files, raws;
267                 g_stat(file, &files);
268                 g_stat(raw,  &raws);
269                 if (files.st_mtime > raws.st_mtime)
270                         if (!_decompress_radar(file, raw))
271                                 return NULL;
272         } else {
273                 if (!_decompress_radar(file, raw))
274                         return NULL;
275         }
276
277         /* Load the radar file */
278         RSL_read_these_sweeps("all", NULL);
279         g_message("read start");
280         Radar *radar = RSL_wsr88d_to_radar(raw, (gchar*)site);
281         g_message("read done");
282         g_free(raw);
283         if (!radar)
284                 return NULL;
285
286         return aweather_level2_new(radar, colormaps);
287 }
288
289 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _level2)
290 {
291         AWeatherLevel2 *level2 = _level2;
292         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
293                 gint type = (gint)g_object_get_data(G_OBJECT(button), "type");
294                 gint elev = (gint)g_object_get_data(G_OBJECT(button), "elev");
295                 aweather_level2_set_sweep(level2, type, (float)elev/100);
296                 //self->colormap = level2->sweep_colors;
297         }
298 }
299
300 GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
301 {
302         Radar *radar = level2->radar;
303         g_debug("AWeatherLevel2: get_config - %p, %p", level2, radar);
304         /* Clear existing items */
305         gfloat elev;
306         guint rows = 1, cols = 1, cur_cols;
307         gchar row_label_str[64], col_label_str[64], button_str[64];
308         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
309         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
310
311         /* Add date */
312         gchar *date_str = g_strdup_printf("<b><i>%04d-%02d-%02d %02d:%02d</i></b>",
313                         radar->h.year, radar->h.month, radar->h.day,
314                         radar->h.hour, radar->h.minute);
315         GtkWidget *date_label = gtk_label_new(date_str);
316         gtk_label_set_use_markup(GTK_LABEL(date_label), TRUE);
317         gtk_table_attach(GTK_TABLE(table), date_label,
318                         0,1, 0,1, GTK_FILL,GTK_FILL, 5,0);
319         g_free(date_str);
320
321         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
322                 Volume *vol = radar->v[vi];
323                 if (vol == NULL) continue;
324                 rows++; cols = 1; elev = 0;
325
326                 /* Row label */
327                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
328                 row_label = gtk_label_new(row_label_str);
329                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
330                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
331                 gtk_table_attach(GTK_TABLE(table), row_label,
332                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
333
334                 for (guint si = 0; si < vol->h.nsweeps; si++) {
335                         Sweep *sweep = vol->sweep[si];
336                         if (sweep == NULL || sweep->h.elev == 0) continue;
337                         if (sweep->h.elev != elev) {
338                                 cols++;
339                                 elev = sweep->h.elev;
340
341                                 /* Column label */
342                                 g_object_get(table, "n-columns", &cur_cols, NULL);
343                                 if (cols >  cur_cols) {
344                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
345                                         col_label = gtk_label_new(col_label_str);
346                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
347                                         gtk_widget_set_size_request(col_label, 50, -1);
348                                         gtk_table_attach(GTK_TABLE(table), col_label,
349                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
350                                 }
351
352                                 elev_box = gtk_hbox_new(TRUE, 0);
353                                 gtk_table_attach(GTK_TABLE(table), elev_box,
354                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
355                         }
356
357
358                         /* Button */
359                         g_snprintf(button_str, 64, "%3.2f", elev);
360                         button = gtk_radio_button_new_with_label_from_widget(
361                                         GTK_RADIO_BUTTON(button), button_str);
362                         gtk_widget_set_size_request(button, -1, 26);
363                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
364                         //gtk_widget_set_size_request(button, -1, 22);
365                         g_object_set(button, "draw-indicator", FALSE, NULL);
366                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
367
368                         g_object_set_data(G_OBJECT(button), "level2", (gpointer)level2);
369                         g_object_set_data(G_OBJECT(button), "type",   (gpointer)vi);
370                         g_object_set_data(G_OBJECT(button), "elev",   (gpointer)(int)(elev*100));
371                         g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), level2);
372                 }
373         }
374         return table;
375 }
376
377 /****************
378  * GObject code *
379  ****************/
380 G_DEFINE_TYPE(AWeatherLevel2, aweather_level2, GRITS_TYPE_OBJECT);
381 static void aweather_level2_init(AWeatherLevel2 *self)
382 {
383 }
384 static void aweather_level2_finalize(GObject *_self)
385 {
386         AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
387         g_debug("AWeatherLevel2: finalize - %p", _self);
388         RSL_free_radar(self->radar);
389         if (self->sweep_tex)
390                 glDeleteTextures(1, &self->sweep_tex);
391         G_OBJECT_CLASS(aweather_level2_parent_class)->finalize(_self);
392 }
393 static void aweather_level2_class_init(AWeatherLevel2Class *klass)
394 {
395         G_OBJECT_CLASS(klass)->finalize = aweather_level2_finalize;
396         GRITS_OBJECT_CLASS(klass)->draw   = aweather_level2_draw;
397 }