~ubuntu-branches/ubuntu/trusty/libgweather/trusty-updates

« back to all changes in this revision

Viewing changes to libgweather/weather-sun.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-05-27 11:58:15 UTC
  • mfrom: (13.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130527115815-l51yffuy20gbv9q0
Tags: 3.8.0-1ubuntu1
* Sync with Debian (LP: #1184168). Remaining changes: 
  - debian/control.in, debian/rules:
    + Run autoreconf
  - debian/rules:
    + Generate POT files on build
  - debian/patches/01_gettext_not_xml.patch: 
    + using gettext rather than add translations to the xml databases
  - debian/patches/02_no_external_gettext.patch:
    + Can't have both IT_PROG_INTLTOOL and AM_GNU_GETTEXT

Show diffs side-by-side

added added

removed removed

Lines of Context:
159
159
 
160
160
 
161
161
static gboolean
162
 
calc_sun2 (WeatherInfo *info, time_t t)
 
162
calc_sun (GWeatherInfo *info, time_t t)
163
163
{
164
 
    gdouble obsLat = info->location->latitude;
165
 
    gdouble obsLon = info->location->longitude;
 
164
    GWeatherInfoPrivate *priv;
 
165
    gdouble obsLat;
 
166
    gdouble obsLon;
166
167
    time_t gm_midn;
167
168
    time_t lcl_midn;
168
169
    gdouble gm_hoff, lambda;
175
176
    gdouble x, u, dt;
176
177
 
177
178
    /* Approximate preceding local midnight at observer's longitude */
178
 
    obsLat = info->location->latitude;
179
 
    obsLon = info->location->longitude;
 
179
    priv = info->priv;
 
180
    obsLat = priv->location.latitude;
 
181
    obsLon = priv->location.longitude;
180
182
    gm_midn = t - (t % 86400);
181
183
    gm_hoff = floor ((RADIANS_TO_DEGREES (obsLon) + 7.5) / 15.);
182
184
    lcl_midn = gm_midn - 3600. * gm_hoff;
186
188
        lcl_midn -= 86400;
187
189
 
188
190
    lambda = sunEclipLongitude (lcl_midn);
189
 
    
 
191
 
190
192
    /*
191
193
     * Calculate equitorial coordinates of sun at previous
192
194
     * and next local midnights
195
197
    ecl2equ (lcl_midn + 86400.,
196
198
             lambda + DEGREES_TO_RADIANS(SOL_PROGRESSION), 0.,
197
199
             &ra2, &decl2);
198
 
    
 
200
 
199
201
    /*
200
202
     * If the observer is within the Arctic or Antarctic Circles then
201
203
     * the sun may be above or below the horizon for the full day.
202
204
     */
203
205
    decl_midn = MIN(decl1,decl2);
204
206
    decl_noon = (decl1+decl2)/2.;
205
 
    info->midnightSun =
 
207
    priv->midnightSun =
206
208
        (obsLat > (M_PI/2.-decl_midn)) || (obsLat < (-M_PI/2.-decl_midn));
207
 
    info->polarNight =
 
209
    priv->polarNight =
208
210
        (obsLat > (M_PI/2.+decl_noon)) || (obsLat < (-M_PI/2.+decl_noon));
209
 
    if (info->midnightSun || info->polarNight) {
210
 
        info->sunriseValid = info->sunsetValid = FALSE;
 
211
    if (priv->midnightSun || priv->polarNight) {
 
212
        priv->sunriseValid = priv->sunsetValid = FALSE;
211
213
        return FALSE;
212
214
    }
213
215
 
220
222
 
221
223
    /* TODO: include calculations for regions near the poles. */
222
224
    if (isnan(rise1) || isnan(rise2)) {
223
 
        info->sunriseValid = info->sunsetValid = FALSE;
 
225
        priv->sunriseValid = priv->sunsetValid = FALSE;
224
226
        return FALSE;
225
227
    }
226
228
 
245
247
        set1  += 24.;
246
248
        set2  += 24.;
247
249
    }
248
 
   
 
250
 
249
251
    /*
250
252
     * Interpolate between the two to get a rise and set time
251
253
     * based on the sun's position at local noon (step 8)
261
263
    x = DEGREES_TO_RADIANS(0.830725);
262
264
    u = acos ( sin(obsLat) / cos(decl2) );
263
265
    dt = RADIANS_TO_HOURS ( asin ( sin(x) / sin(u) ) / cos(decl2) );
264
 
   
 
266
 
265
267
    /*
266
268
     * Subtract the correction value from sunrise and add to sunset,
267
269
     * then (step 11) convert sideral times to UT
271
273
        rise1 += 24;
272
274
    else if (rise1 >= 24.)
273
275
        rise1 -= 24.;
274
 
    info->sunriseValid = ((rise1 >= 0.) && (rise1 < 24.));
275
 
    info->sunrise = (rise1 * 3600.) + lcl_midn;
 
276
    priv->sunriseValid = ((rise1 >= 0.) && (rise1 < 24.));
 
277
    priv->sunrise = (rise1 * 3600.) + lcl_midn;
276
278
 
277
279
    set1  = (set1 + dt - tt) * 0.9972695661;
278
280
    if (set1 < 0.)
279
281
        set1 += 24;
280
282
    else if (set1 >= 24.)
281
283
        set1 -= 24.;
282
 
    info->sunsetValid = ((set1 >= 0.) && (set1 < 24.));
283
 
    info->sunset = (set1 * 3600.) + lcl_midn;
284
 
 
285
 
    return (info->sunriseValid || info->sunsetValid);
286
 
}
287
 
 
288
 
 
289
 
/**
290
 
 * calc_sun_time:
291
 
 * @info: #WeatherInfo structure containing the observer's latitude
292
 
 * and longitude in radians, fills in the sunrise and sunset times.
293
 
 * @t: time_t
294
 
 *
295
 
 * Returns: gboolean indicating if the results are valid.
296
 
 */
297
 
gboolean
298
 
calc_sun_time (WeatherInfo *info, time_t t)
299
 
{
300
 
    return info->location->latlon_valid && calc_sun2 (info, t);
301
 
}
302
 
 
303
 
/**
304
 
 * calc_sun:
305
 
 * @info: #WeatherInfo structure containing the observer's latitude
306
 
 * and longitude in radians, fills in the sunrise and sunset times.
307
 
 *
308
 
 * Returns: gboolean indicating if the results are valid.
309
 
 */
310
 
gboolean
311
 
calc_sun (WeatherInfo *info)
312
 
{
313
 
    return calc_sun_time(info, time(NULL));
314
 
}
315
 
 
 
284
    priv->sunsetValid = ((set1 >= 0.) && (set1 < 24.));
 
285
    priv->sunset = (set1 * 3600.) + lcl_midn;
 
286
 
 
287
    return (priv->sunriseValid || priv->sunsetValid);
 
288
}
 
289
 
 
290
void
 
291
_gweather_info_ensure_sun (GWeatherInfo *info)
 
292
{
 
293
    GWeatherInfoPrivate *priv;
 
294
 
 
295
    priv = info->priv;
 
296
 
 
297
    if (!priv->sunriseValid && !priv->sunsetValid)
 
298
        calc_sun (info, priv->current_time);
 
299
}
316
300
 
317
301
/**
318
302
 * weather_info_next_sun_event:
324
308
 *  - next sunset, when icon changes to nighttime version
325
309
 */
326
310
gint
327
 
weather_info_next_sun_event (WeatherInfo *info)
 
311
gweather_info_next_sun_event (GWeatherInfo *info)
328
312
{
329
313
    time_t    now = time (NULL);
330
314
    struct tm ltm;
331
315
    time_t    nxtEvent;
 
316
    GWeatherInfoPrivate *priv;
 
317
 
 
318
    priv = info->priv;
332
319
 
333
320
    g_return_val_if_fail (info != NULL, -1);
334
321
 
335
 
    if (!calc_sun (info))
336
 
        return -1;
 
322
    _gweather_info_ensure_sun (info);
337
323
 
338
324
    /* Determine when the next local midnight occurs */
339
325
    (void) localtime_r (&now, &ltm);
343
329
    ltm.tm_mday++;
344
330
    nxtEvent = mktime (&ltm);
345
331
 
346
 
    if (info->sunsetValid &&
347
 
        (info->sunset > now) && (info->sunset < nxtEvent))
348
 
        nxtEvent = info->sunset;
349
 
    if (info->sunriseValid &&
350
 
        (info->sunrise > now) && (info->sunrise < nxtEvent))
351
 
        nxtEvent = info->sunrise;
 
332
    if (priv->sunsetValid &&
 
333
        (priv->sunset > now) && (priv->sunset < nxtEvent))
 
334
        nxtEvent = priv->sunset;
 
335
    if (priv->sunriseValid &&
 
336
        (priv->sunrise > now) && (priv->sunrise < nxtEvent))
 
337
        nxtEvent = priv->sunrise;
352
338
    return (gint)(nxtEvent - now);
353
339
}