summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiovanni Campagna <gcampagna@src.gnome.org>2012-12-02 19:40:11 +0100
committerGiovanni Campagna <gcampagna@src.gnome.org>2012-12-02 20:07:43 +0100
commit3cb64a2e1285734214fea76cb999d11ffee4c49e (patch)
treed33b1fea0ff0095ec21261848a61237a7e5c994c
parente6a9a585e341c635edd6c3d03c032cb56d679f1d (diff)
downloadlibgweather-3cb64a2e1285734214fea76cb999d11ffee4c49e.tar.gz
GWeatherInfo: fix accessors for astronomical data
Introduce two new functions, ensure_sun() and ensure_moon(), that will try to compute the sun and moon data if not previously done, using a new field, current_time, that stores the wall clock time when constructed for master GWeatherInfos, and the update time for slaves (forecast).
-rw-r--r--libgweather/test_sun_moon.c10
-rw-r--r--libgweather/weather-iwin.c3
-rw-r--r--libgweather/weather-moon.c68
-rw-r--r--libgweather/weather-priv.h16
-rw-r--r--libgweather/weather-sun.c56
-rw-r--r--libgweather/weather-yahoo.c5
-rw-r--r--libgweather/weather-yrno.c10
-rw-r--r--libgweather/weather.c58
8 files changed, 93 insertions, 133 deletions
diff --git a/libgweather/test_sun_moon.c b/libgweather/test_sun_moon.c
index 2f03f67..de6a7b9 100644
--- a/libgweather/test_sun_moon.c
+++ b/libgweather/test_sun_moon.c
@@ -59,18 +59,18 @@ main (int argc, char **argv)
// printf(" gtime=%s\n", gtime);
g_date_set_parse(&gdate, gtime);
g_date_to_struct_tm(&gdate, &tm);
- info->priv->update = mktime(&tm);
+ priv->current_time = mktime(&tm);
} else {
- info->priv->update = time(NULL);
+ priv->current_time = time(NULL);
}
- calc_sun_time(info, info->priv->update);
- bmoon = calc_moon(info);
+ _gweather_info_ensure_sun (info);
+ _gweather_info_ensure_moon (info);
printf (" Latitude %7.3f %c Longitude %7.3f %c for %s All times UTC\n",
fabs(latitude), (latitude >= 0. ? 'N' : 'S'),
fabs(longitude), (longitude >= 0. ? 'E' : 'W'),
- asctime(gmtime(&info->priv->update)));
+ asctime(gmtime(&priv->current_time)));
printf("sunrise: %s",
(info->priv->sunriseValid ? ctime(&info->priv->sunrise) : "(invalid)\n"));
printf("sunset: %s",
diff --git a/libgweather/weather-iwin.c b/libgweather/weather-iwin.c
index 20add27..3c27a67 100644
--- a/libgweather/weather-iwin.c
+++ b/libgweather/weather-iwin.c
@@ -176,8 +176,7 @@ parseForecastXml (const char *buff, GWeatherInfo *master_info)
for (i = 0; i < 7; i++) {
GWeatherInfo *nfo = _gweather_info_new_clone (master_info);
- nfo->priv->update = update_times[i];
- calc_sun_time (nfo, nfo->priv->update);
+ nfo->priv->current_time = nfo->priv->update = update_times[i];
if (nfo)
res = g_slist_append (res, nfo);
diff --git a/libgweather/weather-moon.c b/libgweather/weather-moon.c
index db3ff8a..222eef4 100644
--- a/libgweather/weather-moon.c
+++ b/libgweather/weather-moon.c
@@ -50,6 +50,16 @@
#define LUNAR_PROGRESSION 13.176358
#define LUNAR_INCLINATION DEGREES_TO_RADIANS(5.145396)
+/*
+ * calc_moon_internal:
+ * @info: WeatherInfo containing time_t of interest. The
+ * values moonphase, moonlatitude and moonValid are updated
+ * on success.
+ *
+ * Returns: gboolean indicating success or failure.
+ * moonphase is expressed as degrees where '0' is a new moon,
+ * '90' is first quarter, etc.
+ */
static gboolean
calc_moon_internal (time_t update, gdouble *moonphase, gdouble *moonlatitude)
{
@@ -125,28 +135,20 @@ calc_moon_internal (time_t update, gdouble *moonphase, gdouble *moonlatitude)
return TRUE;
}
-/**
- * calc_moon:
- * @info: WeatherInfo containing time_t of interest. The
- * values moonphase, moonlatitude and moonValid are updated
- * on success.
- *
- * Returns: gboolean indicating success or failure.
- * moonphase is expressed as degrees where '0' is a new moon,
- * '90' is first quarter, etc.
- */
-
-gboolean
-calc_moon (GWeatherInfo *info)
+void
+_gweather_info_ensure_moon (GWeatherInfo *info)
{
- GWeatherInfoPrivate *priv = info->priv;
- return priv->moonValid = calc_moon_internal (priv->update,
- &priv->moonphase,
- &priv->moonlatitude);
-}
+ GWeatherInfoPrivate *priv;
+ priv = info->priv;
-/**
+ if (!priv->moonValid)
+ priv->moonValid = calc_moon_internal (priv->current_time,
+ &priv->moonphase,
+ &priv->moonlatitude);
+}
+
+/*
* calc_moon_phases:
* @info: WeatherInfo containing the time_t of interest
* @phases: An array of four time_t values that will hold the returned values.
@@ -155,8 +157,7 @@ calc_moon (GWeatherInfo *info)
*
* Returns: gboolean indicating success or failure
*/
-
-gboolean
+static gboolean
calc_moon_phases (GWeatherInfo *info, time_t *phases)
{
GWeatherInfoPrivate *priv;
@@ -169,15 +170,13 @@ calc_moon_phases (GWeatherInfo *info, time_t *phases)
int iter;
time_t dtime;
- g_return_val_if_fail (info != NULL &&
- (info->priv->moonValid || calc_moon (info)),
- FALSE);
+ _gweather_info_ensure_moon (info);
priv = info->priv;
ptime = phases;
for (idx = 0; idx < 4; idx++) {
- tmp_update = priv->update;
+ tmp_update = priv->current_time;
tmp_moonphase = priv->moonphase;
/*
@@ -207,3 +206,22 @@ calc_moon_phases (GWeatherInfo *info, time_t *phases)
return TRUE;
}
+
+/**
+ * gweather_info_get_upcoming_moonphases:
+ * @info: a #GWeatherInfo containing the time_t of interest
+ * @phases: (out) (array fixed-size=4) (element-type gulong): An array of four
+ * time_t values that will hold the returned values.
+ * The values are estimates of the time of the next new, quarter, full and
+ * three-quarter moons.
+ *
+ * Returns: gboolean indicating success or failure
+ */
+gboolean
+gweather_info_get_upcoming_moonphases (GWeatherInfo *info, time_t *phases)
+{
+ g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
+ g_return_val_if_fail (phases != NULL, FALSE);
+
+ return calc_moon_phases(info, phases);
+}
diff --git a/libgweather/weather-priv.h b/libgweather/weather-priv.h
index 985114c..84d94ce 100644
--- a/libgweather/weather-priv.h
+++ b/libgweather/weather-priv.h
@@ -119,6 +119,7 @@ struct _GWeatherInfoPrivate {
GWeatherLocation *world;
GWeatherLocation *glocation;
GWeatherUpdate update;
+ GWeatherUpdate current_time;
GWeatherSky sky;
GWeatherConditions cond;
GWeatherTemperature temp;
@@ -209,20 +210,13 @@ void ecl2equ (gdouble t,
gdouble *ra,
gdouble *decl);
gdouble sunEclipLongitude (time_t t);
-gboolean calc_sun (GWeatherInfo *info);
-gboolean calc_sun_time (GWeatherInfo *info, time_t t);
-gboolean calc_moon (GWeatherInfo *info);
-gboolean calc_moon_phases (GWeatherInfo *info, time_t *phases);
-void free_forecast_list (GWeatherInfo *info);
+void _gweather_info_ensure_sun (GWeatherInfo *info);
+void _gweather_info_ensure_moon (GWeatherInfo *info);
-GWeatherInfo *_gweather_info_new_clone (GWeatherInfo *info);
+void free_forecast_list (GWeatherInfo *info);
-GWeatherLocation *_gweather_location_new_detached (GWeatherLocation *nearest_station,
- const char *name,
- gboolean latlon_valid,
- gdouble latitude,
- gdouble longitude);
+GWeatherInfo *_gweather_info_new_clone (GWeatherInfo *info);
#endif /* __WEATHER_PRIV_H_ */
diff --git a/libgweather/weather-sun.c b/libgweather/weather-sun.c
index 781345d..7874fbe 100644
--- a/libgweather/weather-sun.c
+++ b/libgweather/weather-sun.c
@@ -159,11 +159,11 @@ t0 (time_t date)
static gboolean
-calc_sun2 (GWeatherInfo *info, time_t t)
+calc_sun (GWeatherInfo *info, time_t t)
{
- GWeatherInfoPrivate *priv = info->priv;
- gdouble obsLat = priv->location->latitude;
- gdouble obsLon = priv->location->longitude;
+ GWeatherInfoPrivate *priv;
+ gdouble obsLat;
+ gdouble obsLon;
time_t gm_midn;
time_t lcl_midn;
gdouble gm_hoff, lambda;
@@ -176,8 +176,9 @@ calc_sun2 (GWeatherInfo *info, time_t t)
gdouble x, u, dt;
/* Approximate preceding local midnight at observer's longitude */
- obsLat = priv->location->latitude;
- obsLon = priv->location->longitude;
+ priv = info->priv;
+ obsLat = priv->location.latitude;
+ obsLon = priv->location.longitude;
gm_midn = t - (t % 86400);
gm_hoff = floor ((RADIANS_TO_DEGREES (obsLon) + 7.5) / 15.);
lcl_midn = gm_midn - 3600. * gm_hoff;
@@ -187,7 +188,7 @@ calc_sun2 (GWeatherInfo *info, time_t t)
lcl_midn -= 86400;
lambda = sunEclipLongitude (lcl_midn);
-
+
/*
* Calculate equitorial coordinates of sun at previous
* and next local midnights
@@ -196,7 +197,7 @@ calc_sun2 (GWeatherInfo *info, time_t t)
ecl2equ (lcl_midn + 86400.,
lambda + DEGREES_TO_RADIANS(SOL_PROGRESSION), 0.,
&ra2, &decl2);
-
+
/*
* If the observer is within the Arctic or Antarctic Circles then
* the sun may be above or below the horizon for the full day.
@@ -246,7 +247,7 @@ calc_sun2 (GWeatherInfo *info, time_t t)
set1 += 24.;
set2 += 24.;
}
-
+
/*
* Interpolate between the two to get a rise and set time
* based on the sun's position at local noon (step 8)
@@ -262,7 +263,7 @@ calc_sun2 (GWeatherInfo *info, time_t t)
x = DEGREES_TO_RADIANS(0.830725);
u = acos ( sin(obsLat) / cos(decl2) );
dt = RADIANS_TO_HOURS ( asin ( sin(x) / sin(u) ) / cos(decl2) );
-
+
/*
* Subtract the correction value from sunrise and add to sunset,
* then (step 11) convert sideral times to UT
@@ -286,34 +287,16 @@ calc_sun2 (GWeatherInfo *info, time_t t)
return (priv->sunriseValid || priv->sunsetValid);
}
-
-/**
- * calc_sun_time:
- * @info: #WeatherInfo structure containing the observer's latitude
- * and longitude in radians, fills in the sunrise and sunset times.
- * @t: time_t
- *
- * Returns: gboolean indicating if the results are valid.
- */
-gboolean
-calc_sun_time (GWeatherInfo *info, time_t t)
+void
+_gweather_info_ensure_sun (GWeatherInfo *info)
{
- return info->priv->location->latlon_valid && calc_sun2 (info, t);
-}
+ GWeatherInfoPrivate *priv;
-/**
- * calc_sun:
- * @info: #WeatherInfo structure containing the observer's latitude
- * and longitude in radians, fills in the sunrise and sunset times.
- *
- * Returns: gboolean indicating if the results are valid.
- */
-gboolean
-calc_sun (GWeatherInfo *info)
-{
- return calc_sun_time(info, time(NULL));
-}
+ priv = info->priv;
+ if (!priv->sunriseValid && !priv->sunsetValid)
+ calc_sun (info, priv->current_time);
+}
/**
* weather_info_next_sun_event:
@@ -336,8 +319,7 @@ gweather_info_next_sun_event (GWeatherInfo *info)
g_return_val_if_fail (info != NULL, -1);
- if (!calc_sun (info))
- return -1;
+ _gweather_info_ensure_sun (info);
/* Determine when the next local midnight occurs */
(void) localtime_r (&now, &ltm);
diff --git a/libgweather/weather-yahoo.c b/libgweather/weather-yahoo.c
index 5bb864b..bd937b6 100644
--- a/libgweather/weather-yahoo.c
+++ b/libgweather/weather-yahoo.c
@@ -174,7 +174,7 @@ make_info_from_node (GWeatherInfo *master_info,
priv = info->priv;
val = xmlGetProp (node, XC("date"));
- priv->update = date_to_time_t (val);
+ priv->current_time = priv->update = date_to_time_t (val);
xmlFree (val);
val = xmlGetProp (node, XC("high"));
@@ -201,9 +201,6 @@ make_info_from_node (GWeatherInfo *master_info,
priv->valid = FALSE;
xmlFree (val);
- /* Calculate sun to get the right icon */
- calc_sun_time (info, info->priv->update);
-
return info;
}
diff --git a/libgweather/weather-yrno.c b/libgweather/weather-yrno.c
index 9b58f0a..b5a2f79 100644
--- a/libgweather/weather-yrno.c
+++ b/libgweather/weather-yrno.c
@@ -246,14 +246,11 @@ make_info_from_node_old (GWeatherInfo *master_info,
priv = info->priv;
val = xmlGetProp (node, XC("from"));
- priv->update = date_to_time_t (val, info->priv->location->tz_hint);
+ priv->current_time = priv->update = date_to_time_t (val, info->priv->location.tz_hint);
xmlFree (val);
fill_info_from_node (info, node);
- /* Calculate sun to get the right icon */
- calc_sun_time (info, info->priv->update);
-
return info;
}
@@ -385,7 +382,7 @@ parse_forecast_xml_new (GWeatherInfo *master_info,
*/
if (from_time == to_time) {
info = _gweather_info_new_clone (master_info);
- info->priv->update = from_time;
+ info->priv->current_time = info->priv->update = from_time;
for (location = node->children;
location && location->type != XML_ELEMENT_NODE;
@@ -404,9 +401,6 @@ parse_forecast_xml_new (GWeatherInfo *master_info,
fill_info_from_node (info, location);
}
- /* Calculate sun to get the right icon */
- calc_sun_time (info, info->priv->update);
-
priv->forecast_list = g_slist_append (priv->forecast_list, info);
}
}
diff --git a/libgweather/weather.c b/libgweather/weather.c
index 4952106..7a087f8 100644
--- a/libgweather/weather.c
+++ b/libgweather/weather.c
@@ -464,6 +464,7 @@ gweather_info_reset (GWeatherInfo *info)
}
priv->update = 0;
+ priv->current_time = time(NULL);
priv->sky = -1;
priv->cond.significant = FALSE;
priv->cond.phenomenon = GWEATHER_PHENOMENON_NONE;
@@ -958,15 +959,12 @@ gweather_info_get_sunrise (GWeatherInfo *info)
gchar *buf;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
- g_return_val_if_fail (info->priv->location, NULL);
priv = info->priv;
- if (!priv->location->latlon_valid)
- return g_strdup ("-");
- if (!priv->valid)
- return g_strdup ("-");
- if (!calc_sun (info))
+ _gweather_info_ensure_sun (info);
+
+ if (!priv->valid || !priv->sunriseValid)
return g_strdup ("-");
sunrise = g_date_time_new_from_unix_local (priv->sunrise);
@@ -987,15 +985,12 @@ gweather_info_get_sunset (GWeatherInfo *info)
gchar *buf;
g_return_val_if_fail (GWEATHER_IS_INFO (info), NULL);
- g_return_val_if_fail (info->priv->location, NULL);
priv = info->priv;
- if (!priv->location->latlon_valid)
- return g_strdup ("-");
- if (!priv->valid)
- return g_strdup ("-");
- if (!calc_sun (info))
+ _gweather_info_ensure_sun (info);
+
+ if (!priv->valid || !priv->sunsetValid)
return g_strdup ("-");
sunset = g_date_time_new_from_unix_local (priv->sunset);
@@ -1134,6 +1129,9 @@ gweather_info_get_icon_name (GWeatherInfo *info)
if (!priv->valid)
return NULL;
+ _gweather_info_ensure_sun (info);
+ _gweather_info_ensure_moon (info);
+
cond = priv->cond;
sky = priv->sky;
@@ -1187,7 +1185,7 @@ gweather_info_get_icon_name (GWeatherInfo *info)
else if (priv->polarNight)
daytime = FALSE;
else {
- current_time = priv->update;
+ current_time = priv->current_time;
daytime =
( !priv->sunriseValid || (current_time >= priv->sunrise) ) &&
( !priv->sunsetValid || (current_time < priv->sunset) );
@@ -1617,6 +1615,8 @@ gweather_info_get_value_sunrise (GWeatherInfo *info, time_t *value)
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
+ _gweather_info_ensure_sun (info);
+
if (!info->priv->valid || !info->priv->sunriseValid)
return FALSE;
@@ -1638,6 +1638,8 @@ gweather_info_get_value_sunset (GWeatherInfo *info, time_t *value)
g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
g_return_val_if_fail (value != NULL, FALSE);
+ _gweather_info_ensure_sun (info);
+
if (!info->priv->valid || !info->priv->sunsetValid)
return FALSE;
@@ -1663,6 +1665,8 @@ gweather_info_get_value_moonphase (GWeatherInfo *info,
g_return_val_if_fail (value != NULL, FALSE);
g_return_val_if_fail (lat != NULL, FALSE);
+ _gweather_info_ensure_moon (info);
+
if (!info->priv->valid || !info->priv->moonValid)
return FALSE;
@@ -1752,34 +1756,6 @@ gweather_info_get_value_visibility (GWeatherInfo *info,
return distance_value (info->priv->visibility, unit, value, info->priv->settings);
}
-/**
- * weather_info_get_upcoming_moonphases:
- * @info: a #GWeatherInfo containing the time_t of interest
- * @phases: (out) (array fixed-size=4) (element-type gulong): An array of four
- * time_t values that will hold the returned values.
- * The values are estimates of the time of the next new, quarter, full and
- * three-quarter moons.
- *
- * Returns: gboolean indicating success or failure
- */
-gboolean
-gweather_info_get_upcoming_moonphases (GWeatherInfo *info, time_t *phases)
-{
- g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
- g_return_val_if_fail (phases != NULL, FALSE);
-
- return calc_moon_phases(info, phases);
-}
-
-static void
-_weather_internal_check (void)
-{
- g_assert (G_N_ELEMENTS (wind_direction_str) == GWEATHER_WIND_LAST);
- g_assert (G_N_ELEMENTS (sky_str) == GWEATHER_SKY_LAST);
- g_assert (G_N_ELEMENTS (conditions_str) == GWEATHER_PHENOMENON_LAST);
- g_assert (G_N_ELEMENTS (conditions_str[0]) == GWEATHER_QUALIFIER_LAST);
-}
-
static void
gweather_info_set_location_internal (GWeatherInfo *info,
GWeatherLocation *location)