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/>.
19 #include <glib/gstdio.h>
21 #include <gtk/gtkgl.h>
31 #include "../aweather-location.h"
33 /* Drawing functions */
34 static gpointer _draw_hud(GisCallback *callback, gpointer _self)
36 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
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);
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
67 /* Clear the config area */
68 static void _load_gui_clear(GisPluginRadar *self)
70 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
72 gtk_widget_destroy(child);
75 /* Setup a loading screen in the tab */
76 static void _load_gui_pre(GisPluginRadar *self)
78 g_debug("GisPluginRadar: _load_gui_pre");
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);
92 /* Update pogress bar of loading screen */
93 static void _load_gui_update(char *path, goffset cur, goffset total, gpointer _self)
95 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
96 double percent = (double)cur/total;
98 //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
99 // cur, total, percent*100);
101 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
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);
109 /* Update pogress bar of loading screen */
110 static void _load_gui_error(GisPluginRadar *self, gchar *error)
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);
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)
125 Radar *radar = level2->radar;
126 g_debug("GisPluginRadar: _load_gui_success - %p", radar);
127 /* Clear existing items */
128 _load_gui_clear(self);
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);
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;
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);
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) {
154 elev = sweep->h.elev;
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);
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);
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);
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);
189 gtk_container_add(GTK_CONTAINER(self->config_body), table);
190 gtk_widget_show_all(table);
193 static gchar *_download_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
195 g_debug("GisPluginRadar: _download_radar - %s, %s", site, time);
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);
209 struct SetRadarData {
210 GisPluginRadar *self;
214 static gpointer _set_radar_cb(gpointer _data)
216 g_debug("GisPluginRadar: _set_radar_cb");
217 struct SetRadarData *data = _data;
218 GisPluginRadar *self = data->self;
223 gis_viewer_remove(self->viewer, self->radar);
226 gchar *file = _download_radar(self, data->site, data->time);
228 _load_gui_error(self, "Download failed");
231 AWeatherLevel2 *level2 = aweather_level2_new_from_file(
232 self->viewer, colormaps, file, data->site);
234 _load_gui_error(self, "Loading radar failed");
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);
250 static void _set_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
254 //soup_session_abort(self->http->soup);
255 //g_mutex_lock(self->load_mutex);
256 struct SetRadarData *data = g_new(struct SetRadarData, 1);
258 data->site = g_strdup(site);
259 data->time = g_strdup(time);
260 g_thread_create(_set_radar_cb, data, FALSE, NULL);
266 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
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;
276 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
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);
284 static void _on_location_changed(GisViewer *viewer,
285 gdouble lat, gdouble lon, gdouble elev,
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)
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) {
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);
312 last_city = min_city;
319 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
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;
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);
331 for (city_t *city = cities; city->type; city++) {
332 if (city->type != LOCATION_CITY)
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);
342 GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
343 gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
348 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
350 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
351 return self->config_body;
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)
365 g_debug("GisPluginRadar: plugin_init");
366 /* Add methods to the interface */
367 iface->get_config = gis_plugin_radar_get_config;
369 /* Class/Object init */
370 static void gis_plugin_radar_init(GisPluginRadar *self)
372 g_debug("GisPluginRadar: class_init");
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"));
380 static void gis_plugin_radar_dispose(GObject *gobject)
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);
388 static void gis_plugin_radar_finalize(GObject *gobject)
390 g_debug("GisPluginRadar: finalize");
391 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
393 gis_http_free(self->http);
394 //g_mutex_free(self->load_mutex);
395 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
398 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
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;