summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>2009-11-12 16:25:25 +0000
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>2009-11-12 16:25:25 +0000
commit770c0dae922ff5f79c8939d02081d718637ee980 (patch)
tree3c2b1e7efc89ac30371920ecba6a6f6ae92d6e85
parentb7e166eb030b3a07d89649288605f2b0d8be5fa9 (diff)
downloadnavit-770c0dae922ff5f79c8939d02081d718637ee980.tar.gz
Add:Core:Support for automatic day night switching|Thanks chollya
git-svn-id: http://svn.code.sf.net/p/navit/code/trunk/navit@2746 ffa7fe5e-494d-0410-b361-a75ebd5db220
-rw-r--r--navit/Makefile.am2
-rw-r--r--navit/attr_def.h2
-rw-r--r--navit/layout.c8
-rw-r--r--navit/layout.h2
-rw-r--r--navit/navit.c113
-rw-r--r--navit/navit.h2
-rw-r--r--navit/navit_shipped.xml4
-rw-r--r--navit/sunriset.c309
-rw-r--r--navit/sunriset.h115
9 files changed, 552 insertions, 5 deletions
diff --git a/navit/Makefile.am b/navit/Makefile.am
index 1797d2783..56ae0eb98 100644
--- a/navit/Makefile.am
+++ b/navit/Makefile.am
@@ -41,7 +41,7 @@ libnavit_la_SOURCES = announcement.c atom.c attr.c cache.c callback.c command.c
map_data.h mapset.h maptype.h menu.h messages.h navigation.h navit.h osd.h \
param.h phrase.h plugin.h point.h plugin_def.h projection.h popup.h route.h profile.h roadprofile.h search.h speech.h start_real.h \
transform.h track.h util.h vehicle.h vehicleprofile.h window.h xmlconfig.h zipfile.h \
- navit_nls.h
+ navit_nls.h sunriset.c sunriset.h
comma=,
XSLTS=@XSLTS@
diff --git a/navit/attr_def.h b/navit/attr_def.h
index 20f3322ef..77ca1d703 100644
--- a/navit/attr_def.h
+++ b/navit/attr_def.h
@@ -259,6 +259,8 @@ ATTR(street_name_match)
ATTR(language)
ATTR(subtype)
ATTR(filter)
+ATTR(daylayout)
+ATTR(nightlayout)
ATTR2(0x0003ffff,type_string_end)
ATTR2(0x00040000,type_special_begin)
ATTR(order)
diff --git a/navit/layout.c b/navit/layout.c
index 230fa354a..332a9f526 100644
--- a/navit/layout.c
+++ b/navit/layout.c
@@ -30,7 +30,7 @@ struct layout * layout_new(struct attr *parent, struct attr **attrs)
{
struct layout *l;
struct color def_color = {0xffff, 0xefef, 0xb7b7, 0xffff};
- struct attr *name_attr,*color_attr,*order_delta_attr,*font_attr;
+ struct attr *name_attr,*color_attr,*order_delta_attr,*font_attr,*day_attr,*night_attr;
if (! (name_attr=attr_search(attrs, NULL, attr_name)))
return NULL;
@@ -39,6 +39,12 @@ struct layout * layout_new(struct attr *parent, struct attr **attrs)
if ((font_attr=attr_search(attrs, NULL, attr_font))) {
l->font = g_strdup(font_attr->u.str);
}
+ if ((day_attr=attr_search(attrs, NULL, attr_daylayout))) {
+ l->dayname = g_strdup(day_attr->u.str);
+ }
+ if ((night_attr=attr_search(attrs, NULL, attr_nightlayout))) {
+ l->nightname = g_strdup(night_attr->u.str);
+ }
if ((color_attr=attr_search(attrs, NULL, attr_color)))
l->color = *color_attr->u.color;
else
diff --git a/navit/layout.h b/navit/layout.h
index 1b2f1c09d..ef5fd1062 100644
--- a/navit/layout.h
+++ b/navit/layout.h
@@ -71,7 +71,7 @@ struct cursor {
int interval;
};
-struct layout { char *name; char *font; struct color color; GList *layers; GList *cursors; int order_delta; };
+struct layout { char *name; char* dayname; char* nightname; char *font; struct color color; GList *layers; GList *cursors; int order_delta; };
/* prototypes */
struct layout *layout_new(struct attr *parent, struct attr **attrs);
diff --git a/navit/navit.c b/navit/navit.c
index 89ae60478..03c8059c6 100644
--- a/navit/navit.c
+++ b/navit/navit.c
@@ -60,6 +60,7 @@
#include "util.h"
#include "messages.h"
#include "vehicleprofile.h"
+#include "sunriset.h"
/**
* @defgroup navit the navit core instance. navit is the object containing nearly everything: A set of maps, one or more vehicle, a graphics object for rendering the map, a gui object for displaying the user interface, a route object, a navigation object and so on. Be warned that it is theoretically possible to have more than one navit object
@@ -134,6 +135,7 @@ struct navit {
GList *vehicleprofiles;
int pitch;
int follow_cursor;
+ int prevTs;
};
struct gui *main_loop_gui;
@@ -660,6 +662,8 @@ navit_new(struct attr *parent, struct attr **attrs)
center.x=co.x;
center.y=co.y;
center.pro = pro;
+
+ this_->prevTs=0;
transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
for (;*attrs; attrs++) {
@@ -2075,6 +2079,7 @@ navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
profile(0,"return 1\n");
return;
}
+ navit_layout_switch(this_);
if (this_->vehicle == nv && this_->tracking_flag)
tracking=this_->tracking;
if (tracking) {
@@ -2252,6 +2257,114 @@ navit_get_displaylist(struct navit *this_)
return this_->displaylist;
}
+void
+navit_layout_switch(struct navit *n)
+{
+
+ int currTs=0;
+ struct attr iso8601_attr,geo_attr,layout_attr;
+ double trise,tset,trise_actual;
+ struct layout *l;
+
+ if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
+ return; //No layout - nothing to switch
+ }
+ l=layout_attr.u.layout;
+
+ if (l->dayname || l->nightname) {
+ //Ok, we know that we have profile to switch
+
+ //Check that we aren't calculating too fast
+ if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
+ currTs=iso8601_to_secs(iso8601_attr.u.str);
+ }
+ if (currTs-(n->prevTs)<60) {
+ //We've have to wait a little
+ return;
+ }
+
+ if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
+ //No position - no sun
+ return;
+ }
+ if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &geo_attr,NULL) && geo_attr.u.num==attr_position_valid_invalid) {
+ return; //No valid fix yet
+ }
+
+ //We calculate sunrise anyway, cause it is need both for day and for night
+ if (__sunriset__(currTs,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,35,1,&trise,&tset)!=0) {
+ //near the pole sun never rises/sets, so we should never switch profiles
+ n->prevTs=currTs;
+ return;
+ }
+
+ trise_actual=trise;
+
+ if (l->dayname) {
+
+ if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
+ (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
+ //The sun is rising now!
+ if (strcmp(l->name,l->dayname)) {
+ navit_set_layout_by_name(n,l->dayname);
+ }
+ }
+ }
+ if (l->nightname) {
+ if (__sunriset__(currTs,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,-12,0,&trise,&tset)!=0) {
+ //near the pole sun never rises/sets, so we should never switch profiles
+ n->prevTs=currTs;
+ return;
+ }
+
+ if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
+ || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
+ ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
+ //Time to sleep
+ if (strcmp(l->name,l->nightname)) {
+ navit_set_layout_by_name(n,l->nightname);
+ }
+ }
+ }
+
+ n->prevTs=currTs;
+ }
+}
+
+int
+navit_set_layout_by_name(struct navit *n,char *name)
+{
+ struct layout *l;
+ struct attr_iter iter;
+ struct attr layout_attr;
+
+ iter.u.list=0x00;
+
+ if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
+ return 0; //No layouts - nothing to do
+ }
+ if (iter.u.list==NULL) {
+ return 0;
+ }
+
+ iter.u.list=g_list_first(iter.u.list);
+
+ while(iter.u.list) {
+ l=(struct layout*)iter.u.list->data;
+ if (!strcmp(name,l->name)) {
+ layout_attr.u.layout=l;
+ layout_attr.type=attr_layout;
+ navit_set_attr(n,&layout_attr);
+ iter.u.list=g_list_first(iter.u.list);
+ return 1;
+ }
+ iter.u.list=g_list_next(iter.u.list);
+ }
+
+ iter.u.list=g_list_first(iter.u.list);
+ return 0;
+}
+
int
navit_block(struct navit *this_, int block)
{
diff --git a/navit/navit.h b/navit/navit.h
index 5f71c62c6..8745889f3 100644
--- a/navit/navit.h
+++ b/navit/navit.h
@@ -97,6 +97,8 @@ struct route *navit_get_route(struct navit *this_);
struct navigation *navit_get_navigation(struct navit *this_);
struct displaylist *navit_get_displaylist(struct navit *this_);
int navit_block(struct navit *this_, int block);
+void navit_layout_switch(struct navit *n);
+int navit_set_layout_by_name(struct navit *n, char* name);
void navit_destroy(struct navit *this_);
/* end of prototypes */
#ifdef __cplusplus
diff --git a/navit/navit_shipped.xml b/navit/navit_shipped.xml
index efcf02b05..463f54e5b 100644
--- a/navit/navit_shipped.xml
+++ b/navit/navit_shipped.xml
@@ -340,7 +340,7 @@
<map type="garmin" enabled="yes" data="/path/to/img" debug="4"/>
</mapset>
- <layout name="Car" color="#ffefb7" font="Liberation Sans">
+ <layout name="Car" nightlayout="Car-dark" color="#ffefb7" font="Liberation Sans">
<cursor w="26" h="26">
<itemgra>
@@ -1945,7 +1945,7 @@
</layer>
</layout>
- <layout name="Car-dark" color="#011001">
+ <layout name="Car-dark" daylayout="Car" color="#011001">
<cursor w="26" h="26">
<itemgra>
diff --git a/navit/sunriset.c b/navit/sunriset.c
new file mode 100644
index 000000000..ee6a37b31
--- /dev/null
+++ b/navit/sunriset.c
@@ -0,0 +1,309 @@
+/*
+
+SUNRISET.C - computes Sun rise/set times, start/end of twilight, and
+ the length of the day at any date and latitude
+
+Written as DAYLEN.C, 1989-08-16
+
+Modified to SUNRISET.C, 1992-12-01
+
+(c) Paul Schlyter, 1989, 1992
+
+Released to the public domain by Paul Schlyter, December 1992
+
+*/
+
+
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+
+#include "sunriset.h"
+
+/* The "workhorse" function for sun rise/set times */
+
+int __sunriset__( time_t ts, double lon, double lat,
+ double altit, int upper_limb, double *trise, double *tset )
+/***************************************************************************/
+/* Note: year,month,date = calendar date, 1801-2099 only. */
+/* Eastern longitude positive, Western longitude negative */
+/* Northern latitude positive, Southern latitude negative */
+/* The longitude value IS critical in this function! */
+/* altit = the altitude which the Sun should cross */
+/* Set to -35/60 degrees for rise/set, -6 degrees */
+/* for civil, -12 degrees for nautical and -18 */
+/* degrees for astronomical twilight. */
+/* upper_limb: non-zero -> upper limb, zero -> center */
+/* Set to non-zero (e.g. 1) when computing rise/set */
+/* times, and to zero when computing start/end of */
+/* twilight. */
+/* *rise = where to store the rise time */
+/* *set = where to store the set time */
+/* Both times are relative to the specified altitude, */
+/* and thus this function can be used to comupte */
+/* various twilight times, as well as rise/set times */
+/* Return value: 0 = sun rises/sets this day, times stored at */
+/* *trise and *tset. */
+/* +1 = sun above the specified "horizon" 24 hours. */
+/* *trise set to time when the sun is at south, */
+/* minus 12 hours while *tset is set to the south */
+/* time plus 12 hours. "Day" length = 24 hours */
+/* -1 = sun is below the specified "horizon" 24 hours */
+/* "Day" length = 0 hours, *trise and *tset are */
+/* both set to the time when the sun is at south. */
+/* */
+/**********************************************************************/
+{
+ int year, month, day;
+ struct tm ymd;
+ double d, /* Days since 2000 Jan 0.0 (negative before) */
+ sr, /* Solar distance, astronomical units */
+ sRA, /* Sun's Right Ascension */
+ sdec, /* Sun's declination */
+ sradius, /* Sun's apparent radius */
+ t, /* Diurnal arc */
+ tsouth, /* Time when Sun is at south */
+ sidtime; /* Local sidereal time */
+
+ int rc = 0; /* Return cde from function - usually 0 */
+
+ //Split ts to y/m/d
+ gmtime_r(&ts,&ymd);
+ year=ymd.tm_year+1900;
+ month=ymd.tm_mon+1;
+ day=ymd.tm_mday+1;
+
+ /* Compute d of 12h local mean solar time */
+ d = days_since_2000_Jan_0(year,month,day) + 0.5 - lon/360.0;
+
+ /* Compute local sideral time of this moment */
+ sidtime = revolution( GMST0(d) + 180.0 + lon );
+
+ /* Compute Sun's RA + Decl at this moment */
+ sun_RA_dec( d, &sRA, &sdec, &sr );
+
+ /* Compute time when Sun is at south - in hours UT */
+ tsouth = 12.0 - rev180(sidtime - sRA)/15.0;
+
+ /* Compute the Sun's apparent radius, degrees */
+ sradius = 0.2666 / sr;
+
+ /* Do correction to upper limb, if necessary */
+ if ( upper_limb )
+ altit -= sradius;
+
+ /* Compute the diurnal arc that the Sun traverses to reach */
+ /* the specified altitide altit: */
+ {
+ double cost;
+ cost = ( sind(altit) - sind(lat) * sind(sdec) ) /
+ ( cosd(lat) * cosd(sdec) );
+ if ( cost >= 1.0 )
+ rc = -1, t = 0.0; /* Sun always below altit */
+ else if ( cost <= -1.0 )
+ rc = +1, t = 12.0; /* Sun always above altit */
+ else
+ t = acosd(cost)/15.0; /* The diurnal arc, hours */
+ }
+
+ /* Store rise and set times - in hours UT */
+ *trise = tsouth - t;
+ *tset = tsouth + t;
+
+ return rc;
+} /* __sunriset__ */
+
+
+
+/* The "workhorse" function */
+
+
+double __daylen__( int year, int month, int day, double lon, double lat,
+ double altit, int upper_limb )
+/**********************************************************************/
+/* Note: year,month,date = calendar date, 1801-2099 only. */
+/* Eastern longitude positive, Western longitude negative */
+/* Northern latitude positive, Southern latitude negative */
+/* The longitude value is not critical. Set it to the correct */
+/* longitude if you're picky, otherwise set to to, say, 0.0 */
+/* The latitude however IS critical - be sure to get it correct */
+/* altit = the altitude which the Sun should cross */
+/* Set to -35/60 degrees for rise/set, -6 degrees */
+/* for civil, -12 degrees for nautical and -18 */
+/* degrees for astronomical twilight. */
+/* upper_limb: non-zero -> upper limb, zero -> center */
+/* Set to non-zero (e.g. 1) when computing day length */
+/* and to zero when computing day+twilight length. */
+/**********************************************************************/
+{
+ double d, /* Days since 2000 Jan 0.0 (negative before) */
+ obl_ecl, /* Obliquity (inclination) of Earth's axis */
+ sr, /* Solar distance, astronomical units */
+ slon, /* True solar longitude */
+ sin_sdecl, /* Sine of Sun's declination */
+ cos_sdecl, /* Cosine of Sun's declination */
+ sradius, /* Sun's apparent radius */
+ t; /* Diurnal arc */
+
+ /* Compute d of 12h local mean solar time */
+ d = days_since_2000_Jan_0(year,month,day) + 0.5 - lon/360.0;
+
+ /* Compute obliquity of ecliptic (inclination of Earth's axis) */
+ obl_ecl = 23.4393 - 3.563E-7 * d;
+
+ /* Compute Sun's position */
+ sunpos( d, &slon, &sr );
+
+ /* Compute sine and cosine of Sun's declination */
+ sin_sdecl = sind(obl_ecl) * sind(slon);
+ cos_sdecl = sqrt( 1.0 - sin_sdecl * sin_sdecl );
+
+ /* Compute the Sun's apparent radius, degrees */
+ sradius = 0.2666 / sr;
+
+ /* Do correction to upper limb, if necessary */
+ if ( upper_limb )
+ altit -= sradius;
+
+ /* Compute the diurnal arc that the Sun traverses to reach */
+ /* the specified altitide altit: */
+ {
+ double cost;
+ cost = ( sind(altit) - sind(lat) * sin_sdecl ) /
+ ( cosd(lat) * cos_sdecl );
+ if ( cost >= 1.0 )
+ t = 0.0; /* Sun always below altit */
+ else if ( cost <= -1.0 )
+ t = 24.0; /* Sun always above altit */
+ else t = (2.0/15.0) * acosd(cost); /* The diurnal arc, hours */
+ }
+ return t;
+} /* __daylen__ */
+
+
+/* This function computes the Sun's position at any instant */
+
+void sunpos( double d, double *lon, double *r )
+/******************************************************/
+/* Computes the Sun's ecliptic longitude and distance */
+/* at an instant given in d, number of days since */
+/* 2000 Jan 0.0. The Sun's ecliptic latitude is not */
+/* computed, since it's always very near 0. */
+/******************************************************/
+{
+ double M, /* Mean anomaly of the Sun */
+ w, /* Mean longitude of perihelion */
+ /* Note: Sun's mean longitude = M + w */
+ e, /* Eccentricity of Earth's orbit */
+ E, /* Eccentric anomaly */
+ x, y, /* x, y coordinates in orbit */
+ v; /* True anomaly */
+
+ /* Compute mean elements */
+ M = revolution( 356.0470 + 0.9856002585 * d );
+ w = 282.9404 + 4.70935E-5 * d;
+ e = 0.016709 - 1.151E-9 * d;
+
+ /* Compute true longitude and radius vector */
+ E = M + e * RADEG * sind(M) * ( 1.0 + e * cosd(M) );
+ x = cosd(E) - e;
+ y = sqrt( 1.0 - e*e ) * sind(E);
+ *r = sqrt( x*x + y*y ); /* Solar distance */
+ v = atan2d( y, x ); /* True anomaly */
+ *lon = v + w; /* True solar longitude */
+ if ( *lon >= 360.0 )
+ *lon -= 360.0; /* Make it 0..360 degrees */
+}
+
+void sun_RA_dec( double d, double *RA, double *dec, double *r )
+{
+ double lon, obl_ecl;
+ double xs, ys, zs;
+ double xe, ye, ze;
+
+ /* Compute Sun's ecliptical coordinates */
+ sunpos( d, &lon, r );
+
+ /* Compute ecliptic rectangular coordinates */
+ xs = *r * cosd(lon);
+ ys = *r * sind(lon);
+ zs = 0; /* because the Sun is always in the ecliptic plane! */
+
+ /* Compute obliquity of ecliptic (inclination of Earth's axis) */
+ obl_ecl = 23.4393 - 3.563E-7 * d;
+
+ /* Convert to equatorial rectangular coordinates - x is unchanged */
+ xe = xs;
+ ye = ys * cosd(obl_ecl);
+ ze = ys * sind(obl_ecl);
+
+ /* Convert to spherical coordinates */
+ *RA = atan2d( ye, xe );
+ *dec = atan2d( ze, sqrt(xe*xe + ye*ye) );
+
+} /* sun_RA_dec */
+
+
+/******************************************************************/
+/* This function reduces any angle to within the first revolution */
+/* by subtracting or adding even multiples of 360.0 until the */
+/* result is >= 0.0 and < 360.0 */
+/******************************************************************/
+
+#define INV360 ( 1.0 / 360.0 )
+
+double revolution( double x )
+/*****************************************/
+/* Reduce angle to within 0..360 degrees */
+/*****************************************/
+{
+ return( x - 360.0 * floor( x * INV360 ) );
+} /* revolution */
+
+double rev180( double x )
+/*********************************************/
+/* Reduce angle to within -180..+180 degrees */
+/*********************************************/
+{
+ return( x - 360.0 * floor( x * INV360 + 0.5 ) );
+} /* revolution */
+
+
+/*******************************************************************/
+/* This function computes GMST0, the Greenwhich Mean Sidereal Time */
+/* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at */
+/* 0h UT). GMST is then the sidereal time at Greenwich at any */
+/* time of the day. I've generelized GMST0 as well, and define it */
+/* as: GMST0 = GMST - UT -- this allows GMST0 to be computed at */
+/* other times than 0h UT as well. While this sounds somewhat */
+/* contradictory, it is very practical: instead of computing */
+/* GMST like: */
+/* */
+/* GMST = (GMST0) + UT * (366.2422/365.2422) */
+/* */
+/* where (GMST0) is the GMST last time UT was 0 hours, one simply */
+/* computes: */
+/* */
+/* GMST = GMST0 + UT */
+/* */
+/* where GMST0 is the GMST "at 0h UT" but at the current moment! */
+/* Defined in this way, GMST0 will increase with about 4 min a */
+/* day. It also happens that GMST0 (in degrees, 1 hr = 15 degr) */
+/* is equal to the Sun's mean longitude plus/minus 180 degrees! */
+/* (if we neglect aberration, which amounts to 20 seconds of arc */
+/* or 1.33 seconds of time) */
+/* */
+/*******************************************************************/
+
+double GMST0( double d )
+{
+ double sidtim0;
+ /* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr */
+ /* L = M + w, as defined in sunpos(). Since I'm too lazy to */
+ /* add these numbers, I'll let the C compiler do it for me. */
+ /* Any decent C compiler will add the constants at compile */
+ /* time, imposing no runtime or code overhead. */
+ sidtim0 = revolution( ( 180.0 + 356.0470 + 282.9404 ) +
+ ( 0.9856002585 + 4.70935E-5 ) * d );
+ return sidtim0;
+} /* GMST0 */
diff --git a/navit/sunriset.h b/navit/sunriset.h
new file mode 100644
index 000000000..1ccad5797
--- /dev/null
+++ b/navit/sunriset.h
@@ -0,0 +1,115 @@
+extern const char* timezone_name;
+extern long int timezone_offset;
+
+#define TMOD(x) ((x)<0?(x)+24:((x)>=24?(x)-24:(x)))
+#define DAYSOFF(x) ((x)<0?"(-1) ":((x)>=24?"(+1) ":""))
+
+#define HOURS(h) ((int)(floor(h)))
+#define MINUTES(h) ((int)(60*(h-floor(h))))
+
+#define ABS(x) ((x)<0?-(x):(x))
+
+/* A macro to compute the number of days elapsed since 2000 Jan 0.0 */
+/* (which is equal to 1999 Dec 31, 0h UT) */
+/* Dan R sez: This is some pretty fucking high magic. */
+#define days_since_2000_Jan_0(y,m,d) \
+ (367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
+
+/* Some conversion factors between radians and degrees */
+
+#ifndef PI
+ #define PI 3.1415926535897932384
+#endif
+
+#define RADEG ( 180.0 / PI )
+#define DEGRAD ( PI / 180.0 )
+
+/* The trigonometric functions in degrees */
+
+#define sind(x) sin((x)*DEGRAD)
+#define cosd(x) cos((x)*DEGRAD)
+#define tand(x) tan((x)*DEGRAD)
+
+#define atand(x) (RADEG*atan(x))
+#define asind(x) (RADEG*asin(x))
+#define acosd(x) (RADEG*acos(x))
+#define atan2d(y,x) (RADEG*atan2(y,x))
+
+/* Following are some macros around the "workhorse" function __daylen__ */
+/* They mainly fill in the desired values for the reference altitude */
+/* below the horizon, and also selects whether this altitude should */
+/* refer to the Sun's center or its upper limb. */
+
+
+/* This macro computes the length of the day, from sunrise to sunset. */
+/* Sunrise/set is considered to occur when the Sun's upper limb is */
+/* 50 arc minutes below the horizon (this accounts for the refraction */
+/* of the Earth's atmosphere). */
+/* The original version of the program used the value of 35 arc mins, */
+/* which is the accepted value in Sweden. */
+#define day_length(year,month,day,lon,lat) \
+ __daylen__( year, month, day, lon, lat, -50.0/60.0, 1 )
+
+/* This macro computes the length of the day, including civil twilight. */
+/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
+/* the horizon. */
+#define day_civil_twilight_length(year,month,day,lon,lat) \
+ __daylen__( year, month, day, lon, lat, -6.0, 0 )
+
+/* This macro computes the length of the day, incl. nautical twilight. */
+/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
+/* below the horizon. */
+#define day_nautical_twilight_length(year,month,day,lon,lat) \
+ __daylen__( year, month, day, lon, lat, -12.0, 0 )
+
+/* This macro computes the length of the day, incl. astronomical twilight. */
+/* Astronomical twilight starts/ends when the Sun's center is 18 degrees */
+/* below the horizon. */
+#define day_astronomical_twilight_length(year,month,day,lon,lat) \
+ __daylen__( year, month, day, lon, lat, -18.0, 0 )
+
+
+/* This macro computes times for sunrise/sunset. */
+/* Sunrise/set is considered to occur when the Sun's upper limb is */
+/* 35 arc minutes below the horizon (this accounts for the refraction */
+/* of the Earth's atmosphere). */
+#define sun_rise_set(year,month,day,lon,lat,rise,set) \
+ __sunriset__( year, month, day, lon, lat, -35.0/60.0, 1, rise, set )
+
+/* This macro computes the start and end times of civil twilight. */
+/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
+/* the horizon. */
+#define civil_twilight(year,month,day,lon,lat,start,end) \
+ __sunriset__( year, month, day, lon, lat, -6.0, 0, start, end )
+
+/* This macro computes the start and end times of nautical twilight. */
+/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
+/* below the horizon. */
+#define nautical_twilight(year,month,day,lon,lat,start,end) \
+ __sunriset__( year, month, day, lon, lat, -12.0, 0, start, end )
+
+/* This macro computes the start and end times of astronomical twilight. */
+/* Astronomical twilight starts/ends when the Sun's center is 18 degrees */
+/* below the horizon. */
+#define astronomical_twilight(year,month,day,lon,lat,start,end) \
+ __sunriset__( year, month, day, lon, lat, -18.0, 0, start, end )
+
+
+/* Function prototypes */
+
+double __daylen__( int year, int month, int day, double lon, double lat,
+ double altit, int upper_limb );
+
+int __sunriset__( time_t ts, double lon, double lat,
+ double altit, int upper_limb, double *rise, double *set );
+
+void sunpos( double d, double *lon, double *r );
+
+void sun_RA_dec( double d, double *RA, double *dec, double *r );
+
+double revolution( double x );
+
+double rev180( double x );
+
+double GMST0( double d );
+