X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fplugin-radar.c;h=8d5fb4e3d58cd3f853a004a818e9b3771e3483e5;hp=07161c56af8b762f031b6389db114fd122162fc0;hb=1ac2b7c2d9465293d168c33555a43862320b5c04;hpb=40b17f742041c317915326c6f5ea6d92397c7363 diff --git a/src/plugin-radar.c b/src/plugin-radar.c index 07161c5..8d5fb4e 100644 --- a/src/plugin-radar.c +++ b/src/plugin-radar.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -29,24 +30,47 @@ /**************** * GObject code * ****************/ +/* Plugin init */ static void aweather_radar_plugin_init(AWeatherPluginInterface *iface); static void _aweather_radar_expose(AWeatherPlugin *_radar); G_DEFINE_TYPE_WITH_CODE(AWeatherRadar, aweather_radar, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN, aweather_radar_plugin_init)); -static void aweather_radar_class_init(AWeatherRadarClass *klass) -{ - GObjectClass *object_class = (GObjectClass*)klass; -} static void aweather_radar_plugin_init(AWeatherPluginInterface *iface) { + g_debug("AWeatherRadar: plugin_init"); /* Add methods to the interface */ iface->expose = _aweather_radar_expose; } +/* Class/Object init */ static void aweather_radar_init(AWeatherRadar *radar) { + g_debug("AWeatherRadar: class_init"); /* Set defaults */ - radar->gui = NULL; + radar->gui = NULL; + radar->soup = NULL; +} +static void aweather_radar_dispose(GObject *gobject) +{ + g_debug("AWeatherRadar: dispose"); + AWeatherRadar *self = AWEATHER_RADAR(gobject); + /* Drop references */ + G_OBJECT_CLASS(aweather_radar_parent_class)->dispose(gobject); +} +static void aweather_radar_finalize(GObject *gobject) +{ + g_debug("AWeatherRadar: finalize"); + AWeatherRadar *self = AWEATHER_RADAR(gobject); + /* Free data */ + G_OBJECT_CLASS(aweather_radar_parent_class)->finalize(gobject); + +} +static void aweather_radar_class_init(AWeatherRadarClass *klass) +{ + g_debug("AWeatherRadar: class_init"); + GObjectClass *gobject_class = (GObjectClass*)klass; + gobject_class->dispose = aweather_radar_dispose; + gobject_class->finalize = aweather_radar_finalize; } /************************** @@ -132,7 +156,7 @@ static void load_radar_gui(AWeatherRadar *self, Radar *radar) gdouble elev; guint rows = 1, cols = 1, cur_cols; gchar row_label_str[64], col_label_str[64], button_str[64]; - GtkWidget *row_label, *col_label, *button = NULL, *elev_box; + GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL; GtkWidget *table = gtk_table_new(rows, cols, FALSE); for (guint vi = 0; vi < radar->h.nvolumes; vi++) { @@ -197,12 +221,8 @@ static void load_radar(AWeatherRadar *self, gchar *radar_file) char *dir = g_path_get_dirname(radar_file); char *site = g_path_get_basename(dir); g_free(dir); - RSL_read_these_sweeps("all", NULL); - if (self->cur_radar) { - g_debug("AWeatherRadar: load_radar - Freeing old radar"); - RSL_free_radar(self->cur_radar); - } g_debug("AWeatherRadar: load_radar - Loading new radar"); + RSL_read_these_sweeps("all", NULL); Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site); if (radar == NULL) { g_warning("fail to load radar: path=%s, site=%s", radar_file, site); @@ -231,44 +251,69 @@ static void load_radar(AWeatherRadar *self, gchar *radar_file) load_radar_gui(self, radar); } -static void update_times(AWeatherRadar *self, char *site, char **last_time) +static void update_times(AWeatherRadar *self, AWeatherView *view, char *site, char **last_time) { - char *list_uri = g_strdup_printf( - "http://mesonet.agron.iastate.edu/data/nexrd2/raw/K%s/dir.list", - site); - GFile *list = g_file_new_for_uri(list_uri); - g_free(list_uri); - - gchar *data; - gsize length; - GError *error = NULL; - g_file_load_contents(list, NULL, &data, &length, NULL, &error); - g_object_unref(list); - if (error) { - g_warning("Error loading list for %s: %s", site, error->message); - g_error_free(error); - return; + GList *times = NULL; + if (aweather_view_get_offline(view)) { + gchar *path = g_build_filename(g_get_user_cache_dir(), PACKAGE, "nexrd2", "raw", site, NULL); + GDir *dir = g_dir_open(path, 0, NULL); + if (dir) { + const gchar *name; + while ((name = g_dir_read_name(dir))) { + times = g_list_prepend(times, g_strdup(name)); + } + g_dir_close(dir); + } + g_free(path); + } else { + gchar *data; + gsize length; + GError *error = NULL; + + char *list_uri = g_strdup_printf("http://mesonet.agron.iastate.edu/data/nexrd2/raw/%s/dir.list", site); + GFile *list = g_file_new_for_uri(list_uri); + g_file_load_contents(list, NULL, &data, &length, NULL, &error); + if (error) { + g_warning("Error loading list for %s: %s", site, error->message); + g_error_free(error); + } else { + gchar **lines = g_strsplit(data, "\n", -1); + for (int i = 0; lines[i] && lines[i][0]; i++) { + char **parts = g_strsplit(lines[i], " ", 2); + times = g_list_prepend(times, g_strdup(parts[1])); + g_strfreev(parts); + } + g_strfreev(lines); + g_free(data); + } + + g_free(list_uri); + g_object_unref(list); } - gchar **lines = g_strsplit(data, "\n", -1); + + GRegex *regex = g_regex_new("^[A-Z]{4}_([0-9]{8}_[0-9]{4})$", 0, 0, NULL); // KLSX_20090622_2113 + GMatchInfo *info; + GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(self->gui, "time")); GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview)); gtk_list_store_clear(lstore); GtkTreeIter iter; - for (int i = 0; lines[i] && lines[i][0]; i++) { - // format: `841907 KABR_20090510_0159' - //g_message("\tadding %p [%s]", lines[i], lines[i]); - char **parts = g_strsplit(lines[i], " ", 2); - char *time = parts[1]+5; - gtk_list_store_insert(lstore, &iter, 0); - gtk_list_store_set(lstore, &iter, 0, time, -1); - g_strfreev(parts); + times = g_list_reverse(times); + for (GList *cur = times; cur; cur = cur->next) { + g_message("trying time %s", (gchar*)cur->data); + if (g_regex_match(regex, cur->data, 0, &info)) { + gchar *time = g_match_info_fetch(info, 1); + g_message("adding time %s", (gchar*)cur->data); + gtk_list_store_insert(lstore, &iter, 0); + gtk_list_store_set(lstore, &iter, 0, time, -1); + if (last_time) + *last_time = time; + } } - if (last_time) - gtk_tree_model_get(GTK_TREE_MODEL(lstore), &iter, 0, last_time, -1); - - g_free(data); - g_strfreev(lines); + g_regex_unref(regex); + g_list_foreach(times, (GFunc)g_free, NULL); + g_list_free(times); } /***************** @@ -279,20 +324,36 @@ typedef struct { gchar *radar_file; } decompressed_t; -static void decompressed_cb(GPid pid, gint status, gpointer _self) +static void decompressed_cb(GPid pid, gint status, gpointer _udata) { - decompressed_t *udata = _self; + decompressed_t *udata = _udata; if (status != 0) { g_warning("wsr88ddec exited with status %d", status); return; } - // TODO: pass cur_file as params? load_radar(udata->self, udata->radar_file); + g_spawn_close_pid(pid); g_free(udata->radar_file); g_free(udata); } -static void cached_cb(char *path, gboolean updated, gpointer _self) +static void cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self) +{ + AWeatherRadar *self = AWEATHER_RADAR(_self); + double percent = (double)cur/total; + + g_message("AWeatherRadar: cache_chunk_cb - %lld/%lld = %.2f%%", + cur, total, percent*100); + + gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0)); + + gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)", + percent*100, (double)cur/1000000, (double)total/1000000); + gtk_label_set_text(GTK_LABEL(self->progress_label), msg); + g_free(msg); +} + +static void cache_done_cb(char *path, gboolean updated, gpointer _self) { AWeatherRadar *self = AWEATHER_RADAR(_self); char *decompressed = g_strconcat(path, ".raw", NULL); @@ -304,7 +365,7 @@ static void cached_cb(char *path, gboolean updated, gpointer _self) decompressed_t *udata = g_malloc(sizeof(decompressed_t)); udata->self = self; udata->radar_file = decompressed; - g_debug("AWeatherRadar: cached_cb - File updated, decompressing.."); + g_debug("AWeatherRadar: cache_done_cb - File updated, decompressing.."); char *argv[] = {"wsr88ddec", path, decompressed, NULL}; GPid pid; GError *error = NULL; @@ -325,6 +386,7 @@ static void cached_cb(char *path, gboolean updated, gpointer _self) g_error_free(error); } g_child_watch_add(pid, decompressed_cb, udata); + self->soup = NULL; } /************* @@ -337,20 +399,46 @@ static void on_sweep_clicked(GtkRadioButton *button, gpointer _self) load_sweep (self, g_object_get_data(G_OBJECT(button), "sweep")); } -static void on_time_changed(AWeatherView *view, char *time, gpointer _self) +static void on_time_changed(AWeatherView *view, const char *time, gpointer _self) { AWeatherRadar *self = AWEATHER_RADAR(_self); - g_debug("AWeatherRadar: on_time_changed - setting time"); + g_debug("AWeatherRadar: on_time_changed - setting time=%s", time); // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 char *site = aweather_view_get_site(view); char *base = "http://mesonet.agron.iastate.edu/data/"; - char *path = g_strdup_printf("nexrd2/raw/K%s/K%s_%s", site, site, time); + char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time); + /* Set up progress bar */ + GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body)); + if (child) gtk_widget_destroy(child); + + GtkWidget *vbox = gtk_vbox_new(FALSE, 10); + gtk_container_set_border_width(GTK_CONTAINER(vbox), 10); + self->progress_bar = gtk_progress_bar_new(); + self->progress_label = gtk_label_new("Loading radar..."); + gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0); + gtk_container_add(GTK_CONTAINER(self->config_body), vbox); + gtk_widget_show_all(self->config_body); + + /* Clear radar */ + if (self->cur_radar) + RSL_free_radar(self->cur_radar); self->cur_radar = NULL; - self->cur_sweep = NULL; // Clear radar + self->cur_sweep = NULL; aweather_gui_gl_redraw(self->gui); - cache_file(base, path, AWEATHER_AUTOMATIC, cached_cb, self); + /* Start loading the new radar */ + if (self->soup) { + soup_session_abort(self->soup); + self->soup = NULL; + } + if (aweather_view_get_offline(view)) + self->soup = cache_file(base, path, AWEATHER_ONCE, + cache_chunk_cb, cache_done_cb, self); + else + self->soup = cache_file(base, path, AWEATHER_UPDATE, + cache_chunk_cb, cache_done_cb, self); g_free(path); } @@ -359,18 +447,18 @@ static void on_site_changed(AWeatherView *view, char *site, gpointer _self) AWeatherRadar *self = AWEATHER_RADAR(_self); g_debug("AWeatherRadar: on_site_changed - Loading wsr88d list for %s", site); char *time = NULL; - update_times(self, site, &time); + update_times(self, view, site, &time); aweather_view_set_time(view, time); g_free(time); } -static void on_refresh(AWeatherView *view, gpointer user_data, gpointer _self) +static void on_refresh(AWeatherView *view, gpointer _self) { AWeatherRadar *self = AWEATHER_RADAR(_self); char *site = aweather_view_get_site(view); char *time = NULL; - update_times(self, site, &time); + update_times(self, view, site, &time); aweather_view_set_time(view, time); g_free(time); }