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