]> Pileus Git - aweather/blob - src/plugins/radar.c
Subjectively adding LODs to cities
[aweather] / src / plugins / radar.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 <glib/gstdio.h>
20 #include <gtk/gtk.h>
21 #include <gtk/gtkgl.h>
22 #include <gio/gio.h>
23 #include <GL/gl.h>
24 #include <math.h>
25 #include <rsl.h>
26
27 #include <gis.h>
28
29 #include "radar.h"
30 #include "level2.h"
31 #include "../aweather-location.h"
32
33 /* Drawing functions */
34 static void _draw_hud(GisCallback *callback, gpointer _self)
35 {
36         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
37         if (!self->colormap)
38                 return;
39
40         g_debug("GisPluginRadar: _draw_hud");
41         /* Print the color table */
42         glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
43         glMatrixMode(GL_PROJECTION); glLoadIdentity();
44         glDisable(GL_TEXTURE_2D);
45         glDisable(GL_ALPHA_TEST);
46         glDisable(GL_CULL_FACE);
47         glDisable(GL_LIGHTING);
48         glEnable(GL_COLOR_MATERIAL);
49         glBegin(GL_QUADS);
50         int i;
51         for (i = 0; i < 256; i++) {
52                 glColor4ubv(self->colormap->data[i]);
53                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
54                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
55                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
56                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
57         }
58         glEnd();
59 }
60
61
62 /***************
63  * GUI loading *
64  ***************/
65 /* Clear the config area */ 
66 static void _load_gui_clear(GisPluginRadar *self)
67 {
68         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
69         if (child)
70                 gtk_widget_destroy(child);
71 }
72
73 /* Setup a loading screen in the tab */
74 static void _load_gui_pre(GisPluginRadar *self)
75 {
76         g_debug("GisPluginRadar: _load_gui_pre");
77
78         /* Set up progress bar */
79         _load_gui_clear(self);
80         GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
81         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
82         self->progress_bar   = gtk_progress_bar_new();
83         self->progress_label = gtk_label_new("Loading radar...");
84         gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar,   FALSE, FALSE, 0);
85         gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
86         gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
87         gtk_widget_show_all(self->config_body);
88 }
89
90 /* Update pogress bar of loading screen */
91 static void _load_gui_update(char *path, goffset cur, goffset total, gpointer _self)
92 {
93         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
94         double percent = (double)cur/total;
95
96         //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
97         //              cur, total, percent*100);
98
99         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
100
101         gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
102                         percent*100, (double)cur/1000000, (double)total/1000000);
103         gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
104         g_free(msg);
105 }
106
107 /* Update pogress bar of loading screen */
108 static void _load_gui_error(GisPluginRadar *self, gchar *error)
109 {
110         gchar *msg = g_strdup_printf(
111                         "GisPluginRadar: error loading radar - %s", error);
112         g_warning("%s", msg);
113         _load_gui_clear(self);
114         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new(msg));
115         gtk_widget_show_all(self->config_body);
116         g_free(msg);
117 }
118
119 /* Clear loading screen and add sweep selectors */
120 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self);
121 static void _load_gui_success(GisPluginRadar *self, AWeatherLevel2 *level2)
122 {
123         Radar *radar = level2->radar;
124         g_debug("GisPluginRadar: _load_gui_success - %p", radar);
125         /* Clear existing items */
126         _load_gui_clear(self);
127
128         gdouble elev;
129         guint rows = 1, cols = 1, cur_cols;
130         gchar row_label_str[64], col_label_str[64], button_str[64];
131         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
132         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
133
134         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
135                 Volume *vol = radar->v[vi];
136                 if (vol == NULL) continue;
137                 rows++; cols = 1; elev = 0;
138
139                 /* Row label */
140                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
141                 row_label = gtk_label_new(row_label_str);
142                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
143                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
144                 gtk_table_attach(GTK_TABLE(table), row_label,
145                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
146
147                 for (guint si = 0; si < vol->h.nsweeps; si++) {
148                         Sweep *sweep = vol->sweep[si];
149                         if (sweep == NULL || sweep->h.elev == 0) continue;
150                         if (sweep->h.elev != elev) {
151                                 cols++;
152                                 elev = sweep->h.elev;
153
154                                 /* Column label */
155                                 g_object_get(table, "n-columns", &cur_cols, NULL);
156                                 if (cols >  cur_cols) {
157                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
158                                         col_label = gtk_label_new(col_label_str);
159                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
160                                         gtk_widget_set_size_request(col_label, 50, -1);
161                                         gtk_table_attach(GTK_TABLE(table), col_label,
162                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
163                                 }
164
165                                 elev_box = gtk_hbox_new(TRUE, 0);
166                                 gtk_table_attach(GTK_TABLE(table), elev_box,
167                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
168                         }
169
170
171                         /* Button */
172                         g_snprintf(button_str, 64, "%3.2f", elev);
173                         button = gtk_radio_button_new_with_label_from_widget(
174                                         GTK_RADIO_BUTTON(button), button_str);
175                         gtk_widget_set_size_request(button, -1, 26);
176                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
177                         //gtk_widget_set_size_request(button, -1, 22);
178                         g_object_set(button, "draw-indicator", FALSE, NULL);
179                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
180
181                         g_object_set_data(G_OBJECT(button), "level2", (gpointer)level2);
182                         g_object_set_data(G_OBJECT(button), "type",   (gpointer)vi);
183                         g_object_set_data(G_OBJECT(button), "elev",   (gpointer)(int)(elev*100));
184                         g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), self);
185                 }
186         }
187         gtk_container_add(GTK_CONTAINER(self->config_body), table);
188         gtk_widget_show_all(table);
189 }
190
191 static gchar *_download_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
192 {
193         g_debug("GisPluginRadar: _download_radar - %s, %s", site, time);
194
195         /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
196         gchar *base  = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
197         gchar *local = g_strdup_printf("%s/%s_%s", site, site, time);
198         gchar *uri   = g_strconcat(base, "/", local, NULL);
199         GisCacheType mode = gis_viewer_get_offline(self->viewer) ? GIS_LOCAL : GIS_UPDATE;
200         gchar *file  =  gis_http_fetch(self->http, uri, local, mode, _load_gui_update, self);
201         g_free(base);
202         g_free(local);
203         g_free(uri);
204         return file;
205 }
206
207 struct SetRadarData {
208         GisPluginRadar *self;
209         gchar *site;
210         gchar *time;
211 };
212 static gpointer _set_radar_cb(gpointer _data)
213 {
214         g_debug("GisPluginRadar: _set_radar_cb");
215         struct SetRadarData *data = _data;
216         GisPluginRadar *self = data->self;
217
218         _load_gui_pre(self);
219         if (self->radar) {
220                 gis_viewer_remove(self->viewer, self->radar);
221                 self->radar = NULL;
222         }
223         gchar *file = _download_radar(self, data->site, data->time);
224         if (!file) {
225                 _load_gui_error(self, "Download failed");
226                 goto out;
227         }
228         AWeatherLevel2 *level2 = aweather_level2_new_from_file(
229                         self->viewer, colormaps, file, data->site);
230         if (!level2) {
231                 _load_gui_error(self, "Loading radar failed");
232                 //g_remove(file);
233                 goto out;
234         }
235         self->colormap = level2->sweep_colors;
236         self->radar = gis_viewer_add(self->viewer, GIS_OBJECT(level2), GIS_LEVEL_WORLD, TRUE);
237         _load_gui_success(self, level2);
238
239 out:
240         g_free(file);
241         g_free(data->site);
242         g_free(data->time);
243         g_free(data);
244         g_mutex_unlock(self->load_mutex);
245         return NULL;
246 }
247 static void _set_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
248 {
249         if (!site || !time)
250                 return;
251         struct SetRadarData *data = g_new(struct SetRadarData, 1);
252         data->self = self;
253         data->site = g_strdup(site);
254         data->time = g_strdup(time);
255         if (g_mutex_trylock(self->load_mutex))
256                 g_thread_create(_set_radar_cb, data, FALSE, NULL);
257 }
258
259 /*************
260  * Callbacks *
261  *************/
262 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
263 {
264         GisPluginRadar *self   = GIS_PLUGIN_RADAR(_self);
265         AWeatherLevel2 *level2 = g_object_get_data(G_OBJECT(button), "level2");
266         gint type = (gint)g_object_get_data(G_OBJECT(button), "type");
267         gint elev = (gint)g_object_get_data(G_OBJECT(button), "elev");
268         aweather_level2_set_sweep(level2, type, (float)elev/100);
269         self->colormap = level2->sweep_colors;
270 }
271
272 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
273 {
274         g_debug("GisPluginRadar: _on_time_changed");
275         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
276         self->cur_time = g_strdup(time);
277         _set_radar(self, self->cur_site, self->cur_time);
278 }
279
280 static void _on_location_changed(GisViewer *viewer,
281                 gdouble lat, gdouble lon, gdouble elev,
282                 gpointer _self)
283 {
284         g_debug("GisPluginRadar: _on_location_changed");
285         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
286         gdouble min_dist = EARTH_R / 5;
287         city_t *city, *min_city = NULL;
288         for (city = cities; city->type; city++) {
289                 if (city->type != LOCATION_CITY)
290                         continue;
291                 gdouble city_loc[3] = {};
292                 gdouble eye_loc[3]  = {lat, lon, elev};
293                 lle2xyz(city->pos.lat, city->pos.lon, city->pos.elev,
294                                 &city_loc[0], &city_loc[1], &city_loc[2]);
295                 lle2xyz(lat, lon, elev,
296                                 &eye_loc[0], &eye_loc[1], &eye_loc[2]);
297                 gdouble dist = distd(city_loc, eye_loc);
298                 if (dist < min_dist) {
299                         min_dist = dist;
300                         min_city = city;
301                 }
302         }
303         static city_t *last_city = NULL;
304         if (min_city && min_city != last_city){
305                 self->cur_site = min_city->code;
306                 _set_radar(self, self->cur_site, self->cur_time);
307         }
308         last_city = min_city;
309 }
310
311
312 /***********
313  * Methods *
314  ***********/
315 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
316 {
317         /* TODO: move to constructor if possible */
318         g_debug("GisPluginRadar: new");
319         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
320         self->viewer = viewer;
321         self->prefs  = prefs;
322         self->time_changed_id = g_signal_connect(viewer, "time-changed",
323                         G_CALLBACK(_on_time_changed), self);
324         self->location_changed_id = g_signal_connect(viewer, "location-changed",
325                         G_CALLBACK(_on_location_changed), self);
326
327         for (city_t *city = cities; city->type; city++) {
328                 if (city->type != LOCATION_CITY)
329                         continue;
330                 g_debug("Adding marker for %s %s", city->code, city->label);
331                 GisMarker *marker = gis_marker_new(city->label);
332                 GIS_OBJECT(marker)->center = city->pos;
333                 GIS_OBJECT(marker)->lod    = EARTH_R*city->lod;
334                 gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, FALSE);
335         }
336
337         GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
338         gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
339
340         return self;
341 }
342
343 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
344 {
345         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
346         return self->config_body;
347 }
348
349
350 /****************
351  * GObject code *
352  ****************/
353 /* Plugin init */
354 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
355 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
356                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
357                         gis_plugin_radar_plugin_init));
358 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
359 {
360         g_debug("GisPluginRadar: plugin_init");
361         /* Add methods to the interface */
362         iface->get_config = gis_plugin_radar_get_config;
363 }
364 /* Class/Object init */
365 static void gis_plugin_radar_init(GisPluginRadar *self)
366 {
367         g_debug("GisPluginRadar: class_init");
368         /* Set defaults */
369         self->http = gis_http_new("/nexrad/level2/");
370         self->config_body = gtk_alignment_new(0, 0, 1, 1);
371         self->load_mutex = g_mutex_new();
372         gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
373         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
374 }
375 static void gis_plugin_radar_dispose(GObject *gobject)
376 {
377         g_debug("GisPluginRadar: dispose");
378         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
379         g_signal_handler_disconnect(self->viewer, self->time_changed_id);
380         /* Drop references */
381         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
382 }
383 static void gis_plugin_radar_finalize(GObject *gobject)
384 {
385         g_debug("GisPluginRadar: finalize");
386         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
387         /* Free data */
388         gis_http_free(self->http);
389         g_mutex_free(self->load_mutex);
390         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
391
392 }
393 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
394 {
395         g_debug("GisPluginRadar: class_init");
396         GObjectClass *gobject_class = (GObjectClass*)klass;
397         gobject_class->dispose  = gis_plugin_radar_dispose;
398         gobject_class->finalize = gis_plugin_radar_finalize;
399 }
400