/*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*/
#define _XOPEN_SOURCE
-#include <sys/time.h>
+#include <time.h>
#include <config.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
-#include <gtk/gtkgl.h>
#include <gio/gio.h>
-#include <GL/gl.h>
#include <math.h>
#include <rsl.h>
}
static gchar *_find_nearest(time_t time, GList *files,
- gsize offset, gchar *format)
+ gsize offset)
{
g_debug("RadarSite: find_nearest ...");
time_t nearest_time = 0;
struct tm tm = {};
for (GList *cur = files; cur; cur = cur->next) {
gchar *file = cur->data;
- strptime(file+offset, format, &tm);
+ sscanf(file+offset, "%4d%2d%2d_%2d%2d",
+ &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+ &tm.tm_hour, &tm.tm_min);
+ tm.tm_year -= 1900;
+ tm.tm_mon -= 1;
if (ABS(time - mktime(&tm)) <
ABS(time - nearest_time)) {
nearest_file = file;
/* Information */
city_t *city;
GritsMarker *marker; // Map marker for grits
- gpointer *marker_ref; // Reference to maker
/* Stuff from the parents */
GritsViewer *viewer;
RadarSiteStatus status; // Loading status for the site
GtkWidget *config;
AWeatherLevel2 *level2; // The Level2 structure for the current volume
- gpointer level2_ref; // GritsViewer reference to the added radar
/* Internal data */
time_t time; // Current timestamp of the level2
gchar *dir_list = g_strconcat(nexrad_url, "/", site->city->code,
"/", "dir.list", NULL);
GList *files = grits_http_available(site->http,
- "^K\\w{3}_\\d{8}_\\d{4}$", site->city->code,
+ "^\\w{4}_\\d{8}_\\d{4}$", site->city->code,
"\\d+ (.*)", (offline ? NULL : dir_list));
g_free(dir_list);
- gchar *nearest = _find_nearest(site->time, files, 5, "%Y%m%d_%H%M");
+ gchar *nearest = _find_nearest(site->time, files, 5);
g_list_foreach(files, (GFunc)g_free, NULL);
g_list_free(files);
if (!nearest) {
site->message = "Load failed";
goto out;
}
- GRITS_OBJECT(site->level2)->hidden = site->hidden;
- site->level2_ref = grits_viewer_add(site->viewer,
- GRITS_OBJECT(site->level2), GRITS_LEVEL_WORLD, TRUE);
+ grits_object_hide(GRITS_OBJECT(site->level2), site->hidden);
+ grits_viewer_add(site->viewer, GRITS_OBJECT(site->level2),
+ GRITS_LEVEL_WORLD+3, TRUE);
out:
g_idle_add(_site_update_end, site);
/* Remove old volume */
g_debug("RadarSite: update - remove - %s", site->city->code);
- if (site->level2_ref) {
- site->level2 = NULL;
- grits_viewer_remove(site->viewer, site->level2_ref);
- site->level2_ref = NULL;
+ if (site->level2) {
+ grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
+ site->level2 = NULL;
}
/* Fork loading right away so updating the
gtk_widget_destroy(site->config);
/* Remove radar */
- if (site->level2_ref) {
- site->level2 = NULL;
- grits_viewer_remove(site->viewer, site->level2_ref);
- site->level2_ref = NULL;
+ if (site->level2) {
+ grits_viewer_remove(site->viewer, GRITS_OBJECT(site->level2));
+ site->level2 = NULL;
}
site->status = STATUS_UNLOADED;
gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig), site->config,
gtk_label_new(site->city->name));
gtk_widget_show_all(site->config);
+ if (gtk_notebook_get_current_page(GTK_NOTEBOOK(site->pconfig)) == 0)
+ gtk_notebook_set_current_page(GTK_NOTEBOOK(site->pconfig), -1);
/* Set up radar loading */
site->time_id = g_signal_connect_swapped(site->viewer, "time-changed",
radar_site_unload(site);
}
-gboolean _site_add_marker(gpointer _site)
+gboolean _site_add_marker(RadarSite *site)
{
- RadarSite *site = _site;
site->marker = grits_marker_new(site->city->name);
GRITS_OBJECT(site->marker)->center = site->city->pos;
- GRITS_OBJECT(site->marker)->lod = EARTH_R*site->city->lod;
- site->marker_ref = grits_viewer_add(site->viewer,
- GRITS_OBJECT(site->marker), GRITS_LEVEL_OVERLAY, FALSE);
+ GRITS_OBJECT(site->marker)->lod = EARTH_R*0.75*site->city->lod;
+ grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker),
+ GRITS_LEVEL_OVERLAY, FALSE);
return FALSE;
}
RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig,
"level2" G_DIR_SEPARATOR_S);
site->city = city;
site->pconfig = pconfig;
+ site->hidden = TRUE;
+
+ /* Set initial location */
+ gdouble lat, lon, elev;
+ grits_viewer_get_location(viewer, &lat, &lon, &elev);
+ _site_on_location_changed(viewer, lat, lon, elev, site);
/* Add marker */
- g_idle_add_full(G_PRIORITY_LOW, _site_add_marker, site, NULL);
+ _site_add_marker(site);
/* Connect signals */
site->location_id = g_signal_connect(viewer, "location-changed",
void radar_site_free(RadarSite *site)
{
radar_site_unload(site);
- grits_viewer_remove(site->viewer, site->marker_ref);
+ grits_viewer_remove(site->viewer, GRITS_OBJECT(site->marker));
if (site->location_id)
g_signal_handler_disconnect(site->viewer, site->location_id);
grits_http_free(site->http);
gchar *path;
GritsTile *tile[2];
- gpointer *tile_ref[2];
guint time_id; // "time-changed" callback ID
guint refresh_id; // "refresh" callback ID
glGenTextures(1, tile->data);
}
+ gchar *clear = g_malloc0(2048*2048*4);
guint *tex = tile->data;
glBindTexture(GL_TEXTURE_2D, *tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 4, 2048, 2048, 0,
- GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ GL_RGBA, GL_UNSIGNED_BYTE, clear);
glTexSubImage2D(GL_TEXTURE_2D, 0, 1,1, CONUS_WIDTH/2,CONUS_HEIGHT,
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
tile->coords.n = 1.0/(CONUS_WIDTH/2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glFlush();
+ g_free(clear);
}
/* Split the pixbuf into east and west halves (with 2K sides)
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
- dst[3] = 0xff;
+ dst[3] = 0xff * 0.75;
for (int j = 0; j < G_N_ELEMENTS(alphamap); j++)
if (src[0] == alphamap[j][0] &&
src[1] == alphamap[j][1] &&
GError *error = NULL;
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error);
if (!pixbuf || error) {
- g_warning("Conus: update_end - error loading pixbuf");
+ g_warning("Conus: update_end - error loading pixbuf: %s", conus->path);
_gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf"));
g_remove(conus->path);
goto out;
g_debug("Conus: update_thread - nearest");
gboolean offline = grits_viewer_get_offline(conus->viewer);
gchar *conus_url = "http://radar.weather.gov/Conus/RadarImg/";
- GList *files = grits_http_available(conus->http,
- "^Conus_[^\"]*_N0Ronly.gif$", "",
- NULL, (offline ? NULL : conus_url));
- gchar *nearest = _find_nearest(conus->time, files, 6, "%Y%m%d_%H%M");
- g_list_foreach(files, (GFunc)g_free, NULL);
- g_list_free(files);
- if (!nearest) {
- conus->message = "No suitable files";
- goto out;
+ gchar *nearest;
+ if (time(NULL) - conus->time < 60*60*5 && !offline) {
+ /* radar.weather.gov is full of lies.
+ * the index pages get cached and out of date */
+ /* gmtime is not thread safe, but it's not used very often so
+ * hopefully it'll be alright for now... :-( */
+ struct tm *tm = gmtime(&conus->time);
+ time_t onthe8 = conus->time - 60*((tm->tm_min+1)%10+1);
+ tm = gmtime(&onthe8);
+ nearest = g_strdup_printf("Conus_%04d%02d%02d_%02d%02d_N0Ronly.gif",
+ tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
+ tm->tm_hour, tm->tm_min);
+ } else {
+ GList *files = grits_http_available(conus->http,
+ "^Conus_[^\"]*_N0Ronly.gif$", "", NULL, NULL);
+ nearest = _find_nearest(conus->time, files, 6);
+ g_list_foreach(files, (GFunc)g_free, NULL);
+ g_list_free(files);
+ if (!nearest) {
+ conus->message = "No suitable files";
+ goto out;
+ }
}
/* Fetch the image */
g_debug("Conus: update_thread - fetch");
gchar *uri = g_strconcat(conus_url, nearest, NULL);
- conus->path = grits_http_fetch(conus->http, uri, nearest, GRITS_ONCE,
+ conus->path = grits_http_fetch(conus->http, uri, nearest,
+ offline ? GRITS_LOCAL : GRITS_ONCE,
_conus_update_loading, conus);
g_free(nearest);
g_free(uri);
conus->tile[1] = grits_tile_new(NULL, CONUS_NORTH, south, east, mid);
conus->tile[0]->zindex = 2;
conus->tile[1]->zindex = 1;
- conus->tile_ref[0] = grits_viewer_add(viewer,
- GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD, TRUE);
- conus->tile_ref[1] = grits_viewer_add(viewer,
- GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD, TRUE);
+ grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD+2, FALSE);
+ grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD+2, FALSE);
conus->time_id = g_signal_connect_swapped(viewer, "time-changed",
G_CALLBACK(_conus_update), conus);
for (int i = 0; i < 2; i++) {
GritsTile *tile = conus->tile[i];
- gpointer ref = conus->tile_ref[i];
if (tile->data) {
glDeleteTextures(1, tile->data);
g_free(tile->data);
}
- grits_viewer_remove(conus->viewer, ref);
+ grits_viewer_remove(conus->viewer, GRITS_OBJECT(tile));
}
g_object_unref(conus->viewer);
static void _load_colormap(gchar *filename, AWeatherColormap *cm)
{
g_debug("GritsPluginRadar: _load_colormap - %s", filename);
- FILE *file = g_fopen(filename, "r");
+ FILE *file = fopen(filename, "r");
if (!file)
g_error("GritsPluginRadar: open failed");
guint8 color[4];
GArray *array = g_array_sized_new(FALSE, TRUE, sizeof(color), 256);
- fgets(cm->name, sizeof(cm->name), file);
- fscanf(file, "%f\n", &cm->scale);
- fscanf(file, "%f\n", &cm->shift);
- while (fscanf(file, "%hhd %hhd %hhd %hhd\n",
- &color[0], &color[1], &color[2], &color[3]) == 4)
+ if (!fgets(cm->name, sizeof(cm->name), file)) goto out;
+ if (!fscanf(file, "%f\n", &cm->scale)) goto out;
+ if (!fscanf(file, "%f\n", &cm->shift)) goto out;
+ int r, g, b, a;
+ while (fscanf(file, "%d %d %d %d\n", &r, &g, &b, &a) == 4) {
+ color[0] = r;
+ color[1] = g;
+ color[2] = b;
+ color[3] = a;
g_array_append_val(array, color);
+ }
cm->len = (gint )array->len;
cm->data = (void*)array->data;
+out:
g_array_free(array, FALSE);
+ fclose(file);
}
static void _update_hidden(GtkNotebook *notebook,
/* Conus */
if (conus) {
- GRITS_OBJECT(conus->tile[0])->hidden = is_hidden;
- GRITS_OBJECT(conus->tile[1])->hidden = is_hidden;
+ grits_object_hide(GRITS_OBJECT(conus->tile[0]), is_hidden);
+ grits_object_hide(GRITS_OBJECT(conus->tile[1]), is_hidden);
} else if (site) {
site->hidden = is_hidden;
if (site->level2)
- GRITS_OBJECT(site->level2)->hidden = is_hidden;
+ grits_object_hide(GRITS_OBJECT(site->level2), is_hidden);
} else {
g_warning("GritsPluginRadar: _update_hidden - no site or counus found");
}
self->prefs = prefs;
/* Setup page switching */
- g_signal_connect(self->config, "switch-page",
+ self->tab_id = g_signal_connect(self->config, "switch-page",
G_CALLBACK(_update_hidden), viewer);
/* Load HUD */
- GritsCallback *hud_cb = grits_callback_new(_draw_hud, self);
- self->hud_ref = grits_viewer_add(viewer, GRITS_OBJECT(hud_cb), GRITS_LEVEL_HUD, FALSE);
+ self->hud = grits_callback_new(_draw_hud, self);
+ grits_viewer_add(viewer, GRITS_OBJECT(self->hud), GRITS_LEVEL_HUD, FALSE);
/* Load Conus */
self->conus = radar_conus_new(self->config, self->viewer, self->conus_http);
}
/* Need to position on the top because of Win32 bug */
- gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_BOTTOM);
+ gtk_notebook_set_tab_pos(GTK_NOTEBOOK(self->config), GTK_POS_LEFT);
}
static void grits_plugin_radar_dispose(GObject *gobject)
{
g_debug("GritsPluginRadar: dispose");
GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject);
- grits_viewer_remove(self->viewer, self->hud_ref);
+ g_signal_handler_disconnect(self->config, self->tab_id);
+ grits_viewer_remove(self->viewer, GRITS_OBJECT(self->hud));
radar_conus_free(self->conus);
/* Drop references */
G_OBJECT_CLASS(grits_plugin_radar_parent_class)->dispose(gobject);