]> Pileus Git - aweather/blob - src/plugins/level2.c
Misc Grits/formatting updates
[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                         /* copy RGBA into buffer */
50                         //guint val   = dz_f(ray->range[bi]);
51                         guint8 val   = (guint8)ray->h.f(ray->range[bi]);
52                         guint  buf_i = (ri*max_bins+bi)*4;
53                         buf[buf_i+0] = colormap->data[val][0];
54                         buf[buf_i+1] = colormap->data[val][1];
55                         buf[buf_i+2] = colormap->data[val][2];
56                         buf[buf_i+3] = colormap->data[val][3]*0.75; // TESTING
57                         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
58                             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
59                                 buf[buf_i+3] = 0x00; // transparent
60                         }
61                 }
62         }
63
64         /* set output */
65         *width  = max_bins;
66         *height = sweep->h.nrays;
67         *data   = buf;
68 }
69
70 /* Load a sweep into an OpenGL texture */
71 static void _load_sweep_gl(AWeatherLevel2 *self)
72 {
73         g_debug("AWeatherLevel2: _load_sweep_gl");
74         guint8 *data;
75         gint width, height;
76         _bscan_sweep(self->sweep, self->sweep_colors, &data, &width, &height);
77         gint tex_width  = pow(2, ceil(log(width )/log(2)));
78         gint tex_height = pow(2, ceil(log(height)/log(2)));
79         self->sweep_coords[0] = (double)width  / tex_width;
80         self->sweep_coords[1] = (double)height / tex_height;
81
82         if (!self->sweep_tex)
83                  glGenTextures(1, &self->sweep_tex);
84         glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
85         glPixelStorei(GL_PACK_ALIGNMENT, 1);
86         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
87         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_width, tex_height, 0,
88                         GL_RGBA, GL_UNSIGNED_BYTE, NULL);
89         glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, width,height,
90                         GL_RGBA, GL_UNSIGNED_BYTE, data);
91         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
92         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
93
94         g_free(data);
95 }
96
97 /* Decompress a radar file using wsr88dec */
98 static gboolean _decompress_radar(const gchar *file, const gchar *raw)
99 {
100         g_debug("AWeatherLevel2: _decompress_radar - \n\t%s\n\t%s", file, raw);
101         char *argv[] = {"wsr88ddec", (gchar*)file, (gchar*)raw, NULL};
102         gint status;
103         GError *error = NULL;
104         g_spawn_sync(
105                 NULL,    // const gchar *working_directory
106                 argv,    // gchar **argv
107                 NULL,    // gchar **envp
108                 G_SPAWN_SEARCH_PATH, // GSpawnFlags flags
109                 NULL,    // GSpawnChildSetupFunc child_setup
110                 NULL,    // gpointer user_data
111                 NULL,    // gchar *standard_output
112                 NULL,    // gchar *standard_output
113                 &status, // gint *exit_status
114                 &error); // GError **error
115         if (error) {
116                 g_warning("AWeatherLevel2: _decompress_radar - %s", error->message);
117                 g_error_free(error);
118                 return FALSE;
119         }
120         if (status != 0) {
121                 gchar *msg = g_strdup_printf("wsr88ddec exited with status %d", status);
122                 g_warning("AWeatherLevel2: _decompress_radar - %s", msg);
123                 g_free(msg);
124                 return FALSE;
125         }
126         return TRUE;
127 }
128
129
130 /*********************
131  * Drawing functions *
132  *********************/
133 void aweather_level2_draw(GritsObject *_self, GritsOpenGL *opengl)
134 {
135         AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
136         if (!self->sweep || !self->sweep_tex)
137                 return;
138
139         /* Draw wsr88d */
140         Sweep *sweep = self->sweep;
141         glDisable(GL_ALPHA_TEST);
142         glDisable(GL_CULL_FACE);
143         glDisable(GL_LIGHTING);
144         glEnable(GL_TEXTURE_2D);
145         glEnable(GL_POLYGON_OFFSET_FILL);
146         glPolygonOffset(1.0, -2.0);
147         glColor4f(1,1,1,1);
148
149         /* Draw the rays */
150         gdouble xscale = self->sweep_coords[0];
151         gdouble yscale = self->sweep_coords[1];
152         glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
153         glBegin(GL_TRIANGLE_STRIP);
154         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
155                 Ray  *ray = NULL;
156                 double angle = 0;
157                 if (ri < sweep->h.nrays) {
158                         ray = sweep->ray[ri];
159                         angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
160                 } else {
161                         /* Do the right side of the last sweep */
162                         ray = sweep->ray[ri-1];
163                         angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
164                 }
165
166                 double lx = sin(angle);
167                 double ly = cos(angle);
168
169                 double near_dist = ray->h.range_bin1 - ((double)ray->h.gate_size/2.);
170                 double far_dist  = near_dist + (double)ray->h.nbins*ray->h.gate_size;
171
172                 /* (find middle of bin) / scale for opengl */
173                 // near left
174                 glTexCoord2f(0.0, ((double)ri/sweep->h.nrays)*yscale);
175                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
176
177                 // far  left
178                 // todo: correct range-height function
179                 double height = sin(deg2rad(ray->h.elev)) * far_dist;
180                 glTexCoord2f(xscale, ((double)ri/sweep->h.nrays)*yscale);
181                 glVertex3f(lx*far_dist,  ly*far_dist, height);
182         }
183         glEnd();
184         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
185
186         /* Texture debug */
187         //glBegin(GL_QUADS);
188         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
189         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
190         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
191         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
192         //glEnd();
193 }
194
195
196 /***********
197  * Methods *
198  ***********/
199 static gboolean _set_sweep_cb(gpointer _self)
200 {
201         g_debug("AWeatherLevel2: _set_sweep_cb");
202         AWeatherLevel2 *self = _self;
203         _load_sweep_gl(self);
204         grits_object_queue_draw(_self);
205         g_object_unref(self);
206         return FALSE;
207 }
208 void aweather_level2_set_sweep(AWeatherLevel2 *self,
209                 int type, float elev)
210 {
211         g_debug("AWeatherLevel2: set_sweep - %d %f", type, elev);
212
213         /* Find sweep */
214         Volume *volume = RSL_get_volume(self->radar, type);
215         if (!volume) return;
216         self->sweep = RSL_get_closest_sweep(volume, elev, 90);
217         if (!self->sweep) return;
218
219         /* Find colormap */
220         self->sweep_colors = NULL;
221         for (int i = 0; self->colormap[i].name; i++)
222                 if (self->colormap[i].type == type)
223                         self->sweep_colors = &self->colormap[i];
224         if (!self->sweep_colors) return;
225
226         /* Load data */
227         g_object_ref(self);
228         g_idle_add(_set_sweep_cb, self);
229 }
230
231 AWeatherLevel2 *aweather_level2_new(Radar *radar, AWeatherColormap *colormap)
232 {
233         g_debug("AWeatherLevel2: new - %s", radar->h.radar_name);
234         AWeatherLevel2 *self = g_object_new(AWEATHER_TYPE_LEVEL2, NULL);
235         self->radar    = radar;
236         self->colormap = colormap;
237         aweather_level2_set_sweep(self, DZ_INDEX, 0);
238
239         GritsPoint center;
240         Radar_header *h = &radar->h;
241         center.lat  = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
242         center.lon  = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
243         center.elev = h->height;
244         GRITS_OBJECT(self)->center = center;
245         return self;
246 }
247
248 AWeatherLevel2 *aweather_level2_new_from_file(const gchar *file, const gchar *site,
249                 AWeatherColormap *colormap)
250 {
251         g_debug("AWeatherLevel2: new_from_file %s %s", site, file);
252
253         /* Decompress radar */
254         gchar *raw = g_strconcat(file, ".raw", NULL);
255         if (g_file_test(raw, G_FILE_TEST_EXISTS)) {
256                 struct stat files, raws;
257                 g_stat(file, &files);
258                 g_stat(raw,  &raws);
259                 if (files.st_mtime > raws.st_mtime)
260                         if (!_decompress_radar(file, raw))
261                                 return NULL;
262         } else {
263                 if (!_decompress_radar(file, raw))
264                         return NULL;
265         }
266
267         /* Load the radar file */
268         RSL_read_these_sweeps("all", NULL);
269         g_message("read start");
270         Radar *radar = RSL_wsr88d_to_radar(raw, (gchar*)site);
271         g_message("read done");
272         g_free(raw);
273         if (!radar)
274                 return NULL;
275
276         return aweather_level2_new(radar, colormaps);
277 }
278
279 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _level2)
280 {
281         AWeatherLevel2 *level2 = _level2;
282         gint type = (gint)g_object_get_data(G_OBJECT(button), "type");
283         gint elev = (gint)g_object_get_data(G_OBJECT(button), "elev");
284         aweather_level2_set_sweep(level2, type, (float)elev/100);
285         //self->colormap = level2->sweep_colors;
286 }
287
288 GtkWidget *aweather_level2_get_config(AWeatherLevel2 *level2)
289 {
290         Radar *radar = level2->radar;
291         g_debug("AWeatherLevel2: get_config - %p, %p", level2, radar);
292         /* Clear existing items */
293         gfloat elev;
294         guint rows = 1, cols = 1, cur_cols;
295         gchar row_label_str[64], col_label_str[64], button_str[64];
296         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
297         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
298
299         /* Add date */
300         gchar *date_str = g_strdup_printf("<b><i>%04d-%02d-%02d %02d:%02d</i></b>",
301                         radar->h.year, radar->h.month, radar->h.day,
302                         radar->h.hour, radar->h.minute);
303         GtkWidget *date_label = gtk_label_new(date_str);
304         gtk_label_set_use_markup(GTK_LABEL(date_label), TRUE);
305         gtk_table_attach(GTK_TABLE(table), date_label,
306                         0,1, 0,1, GTK_FILL,GTK_FILL, 5,0);
307         g_free(date_str);
308
309         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
310                 Volume *vol = radar->v[vi];
311                 if (vol == NULL) continue;
312                 rows++; cols = 1; elev = 0;
313
314                 /* Row label */
315                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
316                 row_label = gtk_label_new(row_label_str);
317                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
318                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
319                 gtk_table_attach(GTK_TABLE(table), row_label,
320                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
321
322                 for (guint si = 0; si < vol->h.nsweeps; si++) {
323                         Sweep *sweep = vol->sweep[si];
324                         if (sweep == NULL || sweep->h.elev == 0) continue;
325                         if (sweep->h.elev != elev) {
326                                 cols++;
327                                 elev = sweep->h.elev;
328
329                                 /* Column label */
330                                 g_object_get(table, "n-columns", &cur_cols, NULL);
331                                 if (cols >  cur_cols) {
332                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
333                                         col_label = gtk_label_new(col_label_str);
334                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
335                                         gtk_widget_set_size_request(col_label, 50, -1);
336                                         gtk_table_attach(GTK_TABLE(table), col_label,
337                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
338                                 }
339
340                                 elev_box = gtk_hbox_new(TRUE, 0);
341                                 gtk_table_attach(GTK_TABLE(table), elev_box,
342                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
343                         }
344
345
346                         /* Button */
347                         g_snprintf(button_str, 64, "%3.2f", elev);
348                         button = gtk_radio_button_new_with_label_from_widget(
349                                         GTK_RADIO_BUTTON(button), button_str);
350                         gtk_widget_set_size_request(button, -1, 26);
351                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
352                         //gtk_widget_set_size_request(button, -1, 22);
353                         g_object_set(button, "draw-indicator", FALSE, NULL);
354                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
355
356                         g_object_set_data(G_OBJECT(button), "level2", (gpointer)level2);
357                         g_object_set_data(G_OBJECT(button), "type",   (gpointer)vi);
358                         g_object_set_data(G_OBJECT(button), "elev",   (gpointer)(int)(elev*100));
359                         g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), level2);
360                 }
361         }
362         return table;
363 }
364
365 /****************
366  * GObject code *
367  ****************/
368 G_DEFINE_TYPE(AWeatherLevel2, aweather_level2, GRITS_TYPE_OBJECT);
369 static void aweather_level2_init(AWeatherLevel2 *self)
370 {
371 }
372 static void aweather_level2_finalize(GObject *_self)
373 {
374         AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
375         g_debug("AWeatherLevel2: finalize - %p", _self);
376         RSL_free_radar(self->radar);
377         if (self->sweep_tex)
378                 glDeleteTextures(1, &self->sweep_tex);
379         G_OBJECT_CLASS(aweather_level2_parent_class)->finalize(_self);
380 }
381 static void aweather_level2_class_init(AWeatherLevel2Class *klass)
382 {
383         G_OBJECT_CLASS(klass)->finalize = aweather_level2_finalize;
384         GRITS_OBJECT_CLASS(klass)->draw   = aweather_level2_draw;
385 }