Ticket #502: daynight.diff

File daynight.diff, 24.9 KB (added by akashihi, 11 years ago)
  • layout.c

     
    1919
    2020#include <glib.h>
    2121#include <string.h>
     22#include <math.h>
    2223#include "item.h"
    2324#include "attr.h"
    2425#include "layout.h"
    2526#include "coord.h"
    2627#include "debug.h"
     28#include "vehicle.h"
     29#include "util.h"
     30#include "sunriset.h"
     31#include "navit.h"
    2732
     33struct attr_iter {
     34        union {
     35                GList *list;
     36        } u;
     37};
    2838
     39
    2940struct layout * layout_new(struct attr *parent, struct attr **attrs)
    3041{
    3142        struct layout *l;
    3243        struct color def_color = {0xffff, 0xefef, 0xb7b7, 0xffff};
    33         struct attr *name_attr,*color_attr,*order_delta_attr,*font_attr;
     44        struct attr *name_attr,*color_attr,*order_delta_attr,*font_attr,*day_attr,*night_attr;
    3445
    3546        if (! (name_attr=attr_search(attrs, NULL, attr_name)))
    3647                return NULL;
     
    3950        if ((font_attr=attr_search(attrs, NULL, attr_font))) {
    4051                l->font = g_strdup(font_attr->u.str);
    4152        }
     53        if ((day_attr=attr_search(attrs, NULL, attr_dayprofile))) {
     54                l->dayname = g_strdup(day_attr->u.str);
     55        }
     56        if ((night_attr=attr_search(attrs, NULL, attr_nightprofile))) {
     57                l->nightname = g_strdup(night_attr->u.str);
     58        }
    4259        if ((color_attr=attr_search(attrs, NULL, attr_color)))
    4360                l->color = *color_attr->u.color;
    4461        else
     
    4865        return l;
    4966}
    5067
     68void
     69layout_switch(struct navit *n,struct vehicle *v)
     70{
     71
     72    static int prevTs=0x00;
     73    int currTs;
     74    struct attr iso8601_attr,geo_attr,layout_attr;
     75    double trise,tset;
     76    struct layout *l;
     77   
     78    if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
     79        return; //No layout - nothing to switch
     80    }
     81    l=layout_attr.u.layout;
     82   
     83    if (l->dayname || l->nightname) {
     84        //Ok, we know that we have profile to switch
     85       
     86        //Check that we aren't calculating too fast
     87        if (vehicle_get_attr(v, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
     88                currTs=iso8601_to_secs(iso8601_attr.u.str);
     89        } else { //Precise time is unavailable for some reason
     90                currTs=time(NULL);
     91        }
     92        if (currTs-prevTs<60) {
     93            //We've have to wait a little
     94            return;
     95        }
     96       
     97        prevTs=currTs;
     98       
     99        if (vehicle_get_attr(v, attr_position_coord_geo,&geo_attr,NULL)!=1) {
     100                //No position - no sun
     101                return;
     102        }
     103       
     104        if (l->dayname) {
     105                if (__sunriset__(currTs,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,35,1,&trise,&tset)!=0) {
     106                //near the pole sun never rises/sets, so we should never switch profiles
     107                return;
     108            }
     109       
     110            if (HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) {
     111                //The sun is rising now!
     112                if (strcmp(l->name,l->dayname)) {
     113                    layout_set_by_name(n,l->dayname);
     114                }
     115            }
     116        }
     117        if (l->nightname) {
     118            if (__sunriset__(currTs,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,-12,0,&trise,&tset)!=0) {
     119                //near the pole sun never rises/sets, so we should never switch profiles
     120                return;
     121            }
     122           
     123            if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)) {
     124                //Time to sleep
     125                if (strcmp(l->name,l->nightname)) {
     126                    layout_set_by_name(n,l->nightname);
     127                }
     128            }   
     129        }
     130    }
     131}
     132
     133void
     134layout_set_by_name(struct navit *n,char *name)
     135{
     136    struct layout *l;
     137    struct attr_iter iter;
     138    struct attr layout_attr;
     139
     140    iter.u.list=0x00;
     141
     142    if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
     143        return; //No layouts - nothing to do
     144    }
     145    if (iter.u.list==NULL) {
     146        return;
     147    }
     148   
     149    iter.u.list=g_list_first(iter.u.list);
     150   
     151    while(iter.u.list) {
     152        l=(struct layout*)iter.u.list->data;
     153        if (!strcmp(name,l->name)) {
     154            layout_attr.u.layout=l;
     155            layout_attr.type=attr_layout;
     156            navit_set_attr(n,&layout_attr);
     157            iter.u.list=g_list_first(iter.u.list);
     158            return;
     159        }
     160        iter.u.list=g_list_next(iter.u.list);
     161    }
     162
     163    iter.u.list=g_list_first(iter.u.list);
     164    return;
     165}
     166
    51167int
    52168layout_add_attr(struct layout *layout, struct attr *attr)
    53169{
  • layout.h

     
    7171        int interval;
    7272};
    7373
    74 struct layout { char *name; char *font; struct color color; GList *layers; GList *cursors; int order_delta; };
     74struct layout { char *name; char* dayname; char* nightname; char *font; struct color color; GList *layers; GList *cursors; int order_delta; };
    7575
    7676/* prototypes */
    7777struct layout *layout_new(struct attr *parent, struct attr **attrs);
    7878int layout_add_attr(struct layout *layout, struct attr *attr);
     79void layout_switch(struct navit *n,struct vehicle *v);
     80void layout_set_by_name(struct navit *n, char* name);
    7981struct cursor *layout_get_cursor(struct layout *this_, char *name);
    8082struct cursor *cursor_new(struct attr *parent, struct attr **attrs);
    8183void cursor_destroy(struct cursor *this_);
  • navit_shipped.xml

     
    340340            <map type="garmin" enabled="yes" data="/path/to/img" debug="4"/>
    341341        </mapset>
    342342
    343         <layout name="Car" color="#ffefb7" font="Liberation Sans">
     343        <layout name="Car" dayprofile="Car" nightprofile="Car-dark" color="#ffefb7" font="Liberation Sans">
    344344
    345345            <cursor w="26" h="26">
    346346                <itemgra>
     
    19451945            </layer>
    19461946        </layout>
    19471947
    1948         <layout name="Car-dark" color="#011001">
     1948        <layout name="Car-dark" dayprofile="Car" nightprofile="Car-dark" color="#011001">
    19491949
    19501950            <cursor w="26" h="26">
    19511951                <itemgra>
  • Makefile.am

     
    4141        map_data.h mapset.h maptype.h menu.h messages.h navigation.h navit.h osd.h \
    4242        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 \
    4343        transform.h track.h util.h vehicle.h vehicleprofile.h window.h xmlconfig.h zipfile.h \
    44         navit_nls.h
     44        navit_nls.h sunriset.c
    4545
    4646comma=,
    4747XSLTS=@XSLTS@
  • attr_def.h

     
    224224ATTR(src)
    225225ATTR(path)
    226226ATTR(font)
     227ATTR(dayprofile)
     228ATTR(nightprofile)
    227229ATTR(url_local)
    228230ATTR(gc_size)
    229231ATTR(gc_difficulty)
  • sunriset.c

     
     1/*
     2
     3SUNRISET.C - computes Sun rise/set times, start/end of twilight, and
     4             the length of the day at any date and latitude
     5
     6Written as DAYLEN.C, 1989-08-16
     7
     8Modified to SUNRISET.C, 1992-12-01
     9
     10(c) Paul Schlyter, 1989, 1992
     11
     12Released to the public domain by Paul Schlyter, December 1992
     13
     14*/
     15
     16
     17#include <stdio.h>
     18#include <math.h>
     19#include <time.h>
     20
     21#include "sunriset.h"
     22
     23/* The "workhorse" function for sun rise/set times */
     24
     25int __sunriset__( time_t ts, double lon, double lat,
     26                  double altit, int upper_limb, double *trise, double *tset )
     27/***************************************************************************/
     28/* Note: year,month,date = calendar date, 1801-2099 only.             */
     29/*       Eastern longitude positive, Western longitude negative       */
     30/*       Northern latitude positive, Southern latitude negative       */
     31/*       The longitude value IS critical in this function!            */
     32/*       altit = the altitude which the Sun should cross              */
     33/*               Set to -35/60 degrees for rise/set, -6 degrees       */
     34/*               for civil, -12 degrees for nautical and -18          */
     35/*               degrees for astronomical twilight.                   */
     36/*         upper_limb: non-zero -> upper limb, zero -> center         */
     37/*               Set to non-zero (e.g. 1) when computing rise/set     */
     38/*               times, and to zero when computing start/end of       */
     39/*               twilight.                                            */
     40/*        *rise = where to store the rise time                        */
     41/*        *set  = where to store the set  time                        */
     42/*                Both times are relative to the specified altitude,  */
     43/*                and thus this function can be used to comupte       */
     44/*                various twilight times, as well as rise/set times   */
     45/* Return value:  0 = sun rises/sets this day, times stored at        */
     46/*                    *trise and *tset.                               */
     47/*               +1 = sun above the specified "horizon" 24 hours.     */
     48/*                    *trise set to time when the sun is at south,    */
     49/*                    minus 12 hours while *tset is set to the south  */
     50/*                    time plus 12 hours. "Day" length = 24 hours     */
     51/*               -1 = sun is below the specified "horizon" 24 hours   */
     52/*                    "Day" length = 0 hours, *trise and *tset are    */
     53/*                    both set to the time when the sun is at south.  */
     54/*                                                                    */
     55/**********************************************************************/
     56{
     57      int year, month, day;
     58      struct tm ymd;
     59      double  d,  /* Days since 2000 Jan 0.0 (negative before) */
     60      sr,         /* Solar distance, astronomical units */
     61      sRA,        /* Sun's Right Ascension */
     62      sdec,       /* Sun's declination */
     63      sradius,    /* Sun's apparent radius */
     64      t,          /* Diurnal arc */
     65      tsouth,     /* Time when Sun is at south */
     66      sidtime;    /* Local sidereal time */
     67
     68      int rc = 0; /* Return cde from function - usually 0 */
     69
     70      //Split ts to y/m/d
     71      gmtime_r(&ts,&ymd);
     72      year=ymd.tm_year+1900;
     73      month=ymd.tm_mon+1;
     74      day=ymd.tm_mday+1;
     75
     76      /* Compute d of 12h local mean solar time */
     77      d = days_since_2000_Jan_0(year,month,day) + 0.5 - lon/360.0;
     78
     79      /* Compute local sideral time of this moment */
     80      sidtime = revolution( GMST0(d) + 180.0 + lon );
     81
     82      /* Compute Sun's RA + Decl at this moment */
     83      sun_RA_dec( d, &sRA, &sdec, &sr );
     84
     85      /* Compute time when Sun is at south - in hours UT */
     86      tsouth = 12.0 - rev180(sidtime - sRA)/15.0;
     87
     88      /* Compute the Sun's apparent radius, degrees */
     89      sradius = 0.2666 / sr;
     90
     91      /* Do correction to upper limb, if necessary */
     92      if ( upper_limb )
     93            altit -= sradius;
     94
     95      /* Compute the diurnal arc that the Sun traverses to reach */
     96      /* the specified altitide altit: */
     97      {
     98            double cost;
     99            cost = ( sind(altit) - sind(lat) * sind(sdec) ) /
     100                  ( cosd(lat) * cosd(sdec) );
     101            if ( cost >= 1.0 )
     102                  rc = -1, t = 0.0;       /* Sun always below altit */
     103            else if ( cost <= -1.0 )
     104                  rc = +1, t = 12.0;      /* Sun always above altit */
     105            else
     106                  t = acosd(cost)/15.0;   /* The diurnal arc, hours */
     107      }
     108
     109      /* Store rise and set times - in hours UT */
     110      *trise = tsouth - t;
     111      *tset  = tsouth + t;
     112
     113      return rc;
     114}  /* __sunriset__ */
     115
     116
     117
     118/* The "workhorse" function */
     119
     120
     121double __daylen__( int year, int month, int day, double lon, double lat,
     122                   double altit, int upper_limb )
     123/**********************************************************************/
     124/* Note: year,month,date = calendar date, 1801-2099 only.             */
     125/*       Eastern longitude positive, Western longitude negative       */
     126/*       Northern latitude positive, Southern latitude negative       */
     127/*       The longitude value is not critical. Set it to the correct   */
     128/*       longitude if you're picky, otherwise set to to, say, 0.0     */
     129/*       The latitude however IS critical - be sure to get it correct */
     130/*       altit = the altitude which the Sun should cross              */
     131/*               Set to -35/60 degrees for rise/set, -6 degrees       */
     132/*               for civil, -12 degrees for nautical and -18          */
     133/*               degrees for astronomical twilight.                   */
     134/*         upper_limb: non-zero -> upper limb, zero -> center         */
     135/*               Set to non-zero (e.g. 1) when computing day length   */
     136/*               and to zero when computing day+twilight length.      */
     137/**********************************************************************/
     138{
     139      double  d,  /* Days since 2000 Jan 0.0 (negative before) */
     140      obl_ecl,    /* Obliquity (inclination) of Earth's axis */
     141      sr,         /* Solar distance, astronomical units */
     142      slon,       /* True solar longitude */
     143      sin_sdecl,  /* Sine of Sun's declination */
     144      cos_sdecl,  /* Cosine of Sun's declination */
     145      sradius,    /* Sun's apparent radius */
     146      t;          /* Diurnal arc */
     147
     148      /* Compute d of 12h local mean solar time */
     149      d = days_since_2000_Jan_0(year,month,day) + 0.5 - lon/360.0;
     150
     151      /* Compute obliquity of ecliptic (inclination of Earth's axis) */
     152      obl_ecl = 23.4393 - 3.563E-7 * d;
     153
     154      /* Compute Sun's position */
     155      sunpos( d, &slon, &sr );
     156
     157      /* Compute sine and cosine of Sun's declination */
     158      sin_sdecl = sind(obl_ecl) * sind(slon);
     159      cos_sdecl = sqrt( 1.0 - sin_sdecl * sin_sdecl );
     160
     161      /* Compute the Sun's apparent radius, degrees */
     162      sradius = 0.2666 / sr;
     163
     164      /* Do correction to upper limb, if necessary */
     165      if ( upper_limb )
     166            altit -= sradius;
     167
     168      /* Compute the diurnal arc that the Sun traverses to reach */
     169      /* the specified altitide altit: */
     170      {
     171            double cost;
     172            cost = ( sind(altit) - sind(lat) * sin_sdecl ) /
     173                  ( cosd(lat) * cos_sdecl );
     174            if ( cost >= 1.0 )
     175                  t = 0.0;                      /* Sun always below altit */
     176            else if ( cost <= -1.0 )
     177                  t = 24.0;                     /* Sun always above altit */
     178            else  t = (2.0/15.0) * acosd(cost); /* The diurnal arc, hours */
     179      }
     180      return t;
     181}  /* __daylen__ */
     182
     183
     184/* This function computes the Sun's position at any instant */
     185
     186void sunpos( double d, double *lon, double *r )
     187/******************************************************/
     188/* Computes the Sun's ecliptic longitude and distance */
     189/* at an instant given in d, number of days since     */
     190/* 2000 Jan 0.0.  The Sun's ecliptic latitude is not  */
     191/* computed, since it's always very near 0.           */
     192/******************************************************/
     193{
     194      double M,         /* Mean anomaly of the Sun */
     195             w,         /* Mean longitude of perihelion */
     196                        /* Note: Sun's mean longitude = M + w */
     197             e,         /* Eccentricity of Earth's orbit */
     198             E,         /* Eccentric anomaly */
     199             x, y,      /* x, y coordinates in orbit */
     200             v;         /* True anomaly */
     201
     202      /* Compute mean elements */
     203      M = revolution( 356.0470 + 0.9856002585 * d );
     204      w = 282.9404 + 4.70935E-5 * d;
     205      e = 0.016709 - 1.151E-9 * d;
     206
     207      /* Compute true longitude and radius vector */
     208      E = M + e * RADEG * sind(M) * ( 1.0 + e * cosd(M) );
     209      x = cosd(E) - e;
     210      y = sqrt( 1.0 - e*e ) * sind(E);
     211      *r = sqrt( x*x + y*y );              /* Solar distance */
     212      v = atan2d( y, x );                  /* True anomaly */
     213      *lon = v + w;                        /* True solar longitude */
     214      if ( *lon >= 360.0 )
     215            *lon -= 360.0;                   /* Make it 0..360 degrees */
     216}
     217
     218void sun_RA_dec( double d, double *RA, double *dec, double *r )
     219{
     220  double lon, obl_ecl;
     221  double xs, ys, zs;
     222  double xe, ye, ze;
     223 
     224  /* Compute Sun's ecliptical coordinates */
     225  sunpos( d, &lon, r );
     226 
     227  /* Compute ecliptic rectangular coordinates */
     228  xs = *r * cosd(lon);
     229  ys = *r * sind(lon);
     230  zs = 0; /* because the Sun is always in the ecliptic plane! */
     231
     232  /* Compute obliquity of ecliptic (inclination of Earth's axis) */
     233  obl_ecl = 23.4393 - 3.563E-7 * d;
     234 
     235  /* Convert to equatorial rectangular coordinates - x is unchanged */
     236  xe = xs;
     237  ye = ys * cosd(obl_ecl);
     238  ze = ys * sind(obl_ecl);
     239 
     240  /* Convert to spherical coordinates */
     241  *RA = atan2d( ye, xe );
     242  *dec = atan2d( ze, sqrt(xe*xe + ye*ye) );
     243     
     244}  /* sun_RA_dec */
     245
     246
     247/******************************************************************/
     248/* This function reduces any angle to within the first revolution */
     249/* by subtracting or adding even multiples of 360.0 until the     */
     250/* result is >= 0.0 and < 360.0                                   */
     251/******************************************************************/
     252
     253#define INV360    ( 1.0 / 360.0 )
     254
     255double revolution( double x )
     256/*****************************************/
     257/* Reduce angle to within 0..360 degrees */
     258/*****************************************/
     259{
     260      return( x - 360.0 * floor( x * INV360 ) );
     261}  /* revolution */
     262
     263double rev180( double x )
     264/*********************************************/
     265/* Reduce angle to within -180..+180 degrees */
     266/*********************************************/
     267{
     268      return( x - 360.0 * floor( x * INV360 + 0.5 ) );
     269}  /* revolution */
     270
     271
     272/*******************************************************************/
     273/* This function computes GMST0, the Greenwhich Mean Sidereal Time */
     274/* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at  */
     275/* 0h UT).  GMST is then the sidereal time at Greenwich at any     */
     276/* time of the day.  I've generelized GMST0 as well, and define it */
     277/* as:  GMST0 = GMST - UT  --  this allows GMST0 to be computed at */
     278/* other times than 0h UT as well.  While this sounds somewhat     */
     279/* contradictory, it is very practical:  instead of computing      */
     280/* GMST like:                                                      */
     281/*                                                                 */
     282/*  GMST = (GMST0) + UT * (366.2422/365.2422)                      */
     283/*                                                                 */
     284/* where (GMST0) is the GMST last time UT was 0 hours, one simply  */
     285/* computes:                                                       */
     286/*                                                                 */
     287/*  GMST = GMST0 + UT                                              */
     288/*                                                                 */
     289/* where GMST0 is the GMST "at 0h UT" but at the current moment!   */
     290/* Defined in this way, GMST0 will increase with about 4 min a     */
     291/* day.  It also happens that GMST0 (in degrees, 1 hr = 15 degr)   */
     292/* is equal to the Sun's mean longitude plus/minus 180 degrees!    */
     293/* (if we neglect aberration, which amounts to 20 seconds of arc   */
     294/* or 1.33 seconds of time)                                        */
     295/*                                                                 */
     296/*******************************************************************/
     297
     298double GMST0( double d )
     299{
     300      double sidtim0;
     301      /* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr  */
     302      /* L = M + w, as defined in sunpos().  Since I'm too lazy to */
     303      /* add these numbers, I'll let the C compiler do it for me.  */
     304      /* Any decent C compiler will add the constants at compile   */
     305      /* time, imposing no runtime or code overhead.               */
     306      sidtim0 = revolution( ( 180.0 + 356.0470 + 282.9404 ) +
     307                          ( 0.9856002585 + 4.70935E-5 ) * d );
     308      return sidtim0;
     309}  /* GMST0 */
  • navit.c

     
    20732073                profile(0,"return 1\n");
    20742074                return;
    20752075        }
     2076        layout_switch(this_,nv->vehicle);
    20762077        if (this_->vehicle == nv && this_->tracking_flag)
    20772078                tracking=this_->tracking;
    20782079        if (tracking) {
  • sunriset.h

     
     1extern const char* timezone_name;
     2extern long int timezone_offset;
     3
     4#define TMOD(x) ((x)<0?(x)+24:((x)>=24?(x)-24:(x)))
     5#define DAYSOFF(x) ((x)<0?"(-1) ":((x)>=24?"(+1) ":""))
     6
     7#define HOURS(h) ((int)(floor(h)))
     8#define MINUTES(h) ((int)(60*(h-floor(h))))
     9
     10#define ABS(x) ((x)<0?-(x):(x))
     11
     12/* A macro to compute the number of days elapsed since 2000 Jan 0.0 */
     13/* (which is equal to 1999 Dec 31, 0h UT)                           */
     14/* Dan R sez: This is some pretty fucking high magic. */
     15#define days_since_2000_Jan_0(y,m,d) \
     16    (367L*(y)-((7*((y)+(((m)+9)/12)))/4)+((275*(m))/9)+(d)-730530L)
     17
     18/* Some conversion factors between radians and degrees */
     19
     20#ifndef PI
     21 #define PI        3.1415926535897932384
     22#endif
     23
     24#define RADEG     ( 180.0 / PI )
     25#define DEGRAD    ( PI / 180.0 )
     26
     27/* The trigonometric functions in degrees */
     28
     29#define sind(x)  sin((x)*DEGRAD)
     30#define cosd(x)  cos((x)*DEGRAD)
     31#define tand(x)  tan((x)*DEGRAD)
     32
     33#define atand(x)    (RADEG*atan(x))
     34#define asind(x)    (RADEG*asin(x))
     35#define acosd(x)    (RADEG*acos(x))
     36#define atan2d(y,x) (RADEG*atan2(y,x))
     37
     38/* Following are some macros around the "workhorse" function __daylen__ */
     39/* They mainly fill in the desired values for the reference altitude    */
     40/* below the horizon, and also selects whether this altitude should     */
     41/* refer to the Sun's center or its upper limb.                         */
     42
     43
     44/* This macro computes the length of the day, from sunrise to sunset. */
     45/* Sunrise/set is considered to occur when the Sun's upper limb is    */
     46/* 50 arc minutes below the horizon (this accounts for the refraction */
     47/* of the Earth's atmosphere).                                        */
     48/* The original version of the program used the value of 35 arc mins, */
     49/* which is the accepted value in Sweden.                             */
     50#define day_length(year,month,day,lon,lat)  \
     51        __daylen__( year, month, day, lon, lat, -50.0/60.0, 1 )
     52
     53/* This macro computes the length of the day, including civil twilight. */
     54/* Civil twilight starts/ends when the Sun's center is 6 degrees below  */
     55/* the horizon.                                                         */
     56#define day_civil_twilight_length(year,month,day,lon,lat)  \
     57        __daylen__( year, month, day, lon, lat, -6.0, 0 )
     58
     59/* This macro computes the length of the day, incl. nautical twilight.  */
     60/* Nautical twilight starts/ends when the Sun's center is 12 degrees    */
     61/* below the horizon.                                                   */
     62#define day_nautical_twilight_length(year,month,day,lon,lat)  \
     63        __daylen__( year, month, day, lon, lat, -12.0, 0 )
     64
     65/* This macro computes the length of the day, incl. astronomical twilight. */
     66/* Astronomical twilight starts/ends when the Sun's center is 18 degrees   */
     67/* below the horizon.                                                      */
     68#define day_astronomical_twilight_length(year,month,day,lon,lat)  \
     69        __daylen__( year, month, day, lon, lat, -18.0, 0 )
     70
     71
     72/* This macro computes times for sunrise/sunset.                      */
     73/* Sunrise/set is considered to occur when the Sun's upper limb is    */
     74/* 35 arc minutes below the horizon (this accounts for the refraction */
     75/* of the Earth's atmosphere).                                        */
     76#define sun_rise_set(year,month,day,lon,lat,rise,set)  \
     77        __sunriset__( year, month, day, lon, lat, -35.0/60.0, 1, rise, set )
     78
     79/* This macro computes the start and end times of civil twilight.       */
     80/* Civil twilight starts/ends when the Sun's center is 6 degrees below  */
     81/* the horizon.                                                         */
     82#define civil_twilight(year,month,day,lon,lat,start,end)  \
     83        __sunriset__( year, month, day, lon, lat, -6.0, 0, start, end )
     84
     85/* This macro computes the start and end times of nautical twilight.    */
     86/* Nautical twilight starts/ends when the Sun's center is 12 degrees    */
     87/* below the horizon.                                                   */
     88#define nautical_twilight(year,month,day,lon,lat,start,end)  \
     89        __sunriset__( year, month, day, lon, lat, -12.0, 0, start, end )
     90
     91/* This macro computes the start and end times of astronomical twilight.   */
     92/* Astronomical twilight starts/ends when the Sun's center is 18 degrees   */
     93/* below the horizon.                                                      */
     94#define astronomical_twilight(year,month,day,lon,lat,start,end)  \
     95        __sunriset__( year, month, day, lon, lat, -18.0, 0, start, end )
     96
     97
     98/* Function prototypes */
     99
     100double __daylen__( int year, int month, int day, double lon, double lat,
     101                   double altit, int upper_limb );
     102
     103int __sunriset__( time_t ts, double lon, double lat,
     104                  double altit, int upper_limb, double *rise, double *set );
     105
     106void sunpos( double d, double *lon, double *r );
     107
     108void sun_RA_dec( double d, double *RA, double *dec, double *r );
     109
     110double revolution( double x );
     111
     112double rev180( double x );
     113
     114double GMST0( double d );
     115