2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
21 #include <glib/gstdio.h>
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)
35 g_debug("AWeatherLevel2: _bscan_sweep - %p, %p, %p",
36 sweep, colormap, data);
37 /* Calculate max number of bins */
39 for (int i = 0; i < sweep->h.nrays; i++)
40 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
42 /* Allocate buffer using max number of bins for each ray */
43 guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
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
66 *height = sweep->h.nrays;
70 /* Load a sweep into an OpenGL texture */
71 static void _load_sweep_gl(Sweep *sweep, AWeatherColormap *colormap, guint *tex)
73 g_debug("AWeatherLevel2: _load_sweep_gl");
76 _bscan_sweep(sweep, colormap, &data, &width, &height);
77 glGenTextures(1, tex);
78 glBindTexture(GL_TEXTURE_2D, *tex);
79 glPixelStorei(GL_PACK_ALIGNMENT, 1);
80 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
81 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
82 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
83 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
84 GL_RGBA, GL_UNSIGNED_BYTE, data);
88 /* Decompress a radar file using wsr88dec */
89 static gboolean _decompress_radar(const gchar *file, const gchar *raw)
91 g_debug("AWeatherLevel2: _decompress_radar - \n\t%s\n\t%s", file, raw);
92 char *argv[] = {"wsr88ddec", (gchar*)file, (gchar*)raw, NULL};
96 NULL, // const gchar *working_directory
99 G_SPAWN_SEARCH_PATH, // GSpawnFlags flags
100 NULL, // GSpawnChildSetupFunc child_setup
101 NULL, // gpointer user_data
102 NULL, // gchar *standard_output
103 NULL, // gchar *standard_output
104 &status, // gint *exit_status
105 &error); // GError **error
107 g_warning("AWeatherLevel2: _decompress_radar - %s", error->message);
112 gchar *msg = g_strdup_printf("wsr88ddec exited with status %d", status);
113 g_warning("AWeatherLevel2: _decompress_radar - %s", msg);
121 /*********************
122 * Drawing functions *
123 *********************/
124 static gpointer _draw_radar(GisCallback *_self, gpointer _viewer)
126 AWeatherLevel2 *self = AWEATHER_LEVEL2(_self);
127 if (!self->sweep || !self->sweep_tex)
131 Sweep *sweep = self->sweep;
132 Radar_header *h = &self->radar->h;
133 gdouble lat = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
134 gdouble lon = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
135 gdouble elev = h->height;
136 gis_viewer_center_position(self->viewer, lat, lon, elev);
138 glDisable(GL_ALPHA_TEST);
139 glDisable(GL_CULL_FACE);
140 glDisable(GL_LIGHTING);
141 glEnable(GL_TEXTURE_2D);
142 glEnable(GL_POLYGON_OFFSET_FILL);
143 glPolygonOffset(1.0, -2.0);
147 glBindTexture(GL_TEXTURE_2D, self->sweep_tex);
148 g_message("Tex = %d", self->sweep_tex);
149 glBegin(GL_TRIANGLE_STRIP);
150 for (int ri = 0; ri <= sweep->h.nrays; ri++) {
153 if (ri < sweep->h.nrays) {
154 ray = sweep->ray[ri];
155 angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
157 /* Do the right side of the last sweep */
158 ray = sweep->ray[ri-1];
159 angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
162 double lx = sin(angle);
163 double ly = cos(angle);
165 double near_dist = ray->h.range_bin1;
166 double far_dist = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
168 /* (find middle of bin) / scale for opengl */
170 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
171 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
174 // todo: correct range-height function
175 double height = sin(deg2rad(ray->h.elev)) * far_dist;
176 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
177 glVertex3f(lx*far_dist, ly*far_dist, height);
180 //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
184 //glTexCoord2d( 0., 0.); glVertex3f(-500., 0., 0.); // bot left
185 //glTexCoord2d( 0., 1.); glVertex3f(-500., 500., 0.); // top left
186 //glTexCoord2d( 1., 1.); glVertex3f( 0., 500., 3.); // top right
187 //glTexCoord2d( 1., 0.); glVertex3f( 0., 0., 3.); // bot right
197 static gboolean _set_sweep_cb(gpointer _self)
199 g_debug("AWeatherLevel2: _set_sweep_cb");
200 AWeatherLevel2 *self = _self;
202 glDeleteTextures(1, &self->sweep_tex);
203 _load_sweep_gl(self->sweep, self->sweep_colors, &self->sweep_tex);
204 gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
207 void aweather_level2_set_sweep(AWeatherLevel2 *self,
208 int type, float elev)
210 g_debug("AWeatherLevel2: set_sweep - %d %f", type, elev);
213 Volume *volume = RSL_get_volume(self->radar, type);
215 self->sweep = RSL_get_closest_sweep(volume, elev, 90);
216 if (!self->sweep) return;
219 self->sweep_colors = NULL;
220 for (int i = 0; self->colormap[i].name; i++)
221 if (self->colormap[i].type == type)
222 self->sweep_colors = &self->colormap[i];
223 if (!self->sweep_colors) return;
226 g_idle_add(_set_sweep_cb, self);
229 AWeatherLevel2 *aweather_level2_new(GisViewer *viewer,
230 AWeatherColormap *colormap, Radar *radar)
232 g_debug("AWeatherLevel2: new");
233 AWeatherLevel2 *self = g_object_new(AWEATHER_TYPE_LEVEL2, NULL);
234 self->viewer = viewer;
236 self->colormap = colormap;
237 aweather_level2_set_sweep(self, DZ_INDEX, 0);
241 AWeatherLevel2 *aweather_level2_new_from_file(GisViewer *viewer,
242 AWeatherColormap *colormap,
243 const gchar *file, const gchar *site)
245 g_debug("AWeatherLevel2: new_from_file %s %s", site, file);
247 /* Decompress radar */
248 gchar *raw = g_strconcat(file, ".raw", NULL);
249 if (g_file_test(raw, G_FILE_TEST_EXISTS)) {
250 struct stat files, raws;
251 g_stat(file, &files);
253 if (files.st_mtime < raws.st_mtime)
254 if (!_decompress_radar(file, raw))
257 if (!_decompress_radar(file, raw))
261 /* Load the radar file */
262 RSL_read_these_sweeps("all", NULL);
263 Radar *radar = RSL_wsr88d_to_radar(raw, (gchar*)site);
268 return aweather_level2_new(viewer, colormaps, radar);
275 G_DEFINE_TYPE(AWeatherLevel2, aweather_level2, GIS_TYPE_CALLBACK);
276 static void aweather_level2_init(AWeatherLevel2 *self)
278 GIS_CALLBACK(self)->callback = _draw_radar;
279 GIS_CALLBACK(self)->user_data = self;
282 static void aweather_level2_class_init(AWeatherLevel2Class *klass)