home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / Astro / astrolog / Source / placalc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  60.4 KB  |  1,737 lines

  1. /*
  2. ** Astrolog (Version 4.10) File: placalc.c
  3. **
  4. ** IMPORTANT NOTICE: the graphics database and chart display routines
  5. ** used in this program are Copyright (C) 1991-1994 by Walter D. Pullen
  6. ** (cruiser1@stein.u.washington.edu). Permission is granted to freely
  7. ** use and distribute these routines provided one doesn't sell,
  8. ** restrict, or profit from them in any way. Modification is allowed
  9. ** provided these notices remain with any altered or edited versions of
  10. ** the program.
  11. **
  12. ** The main planetary calculation routines used in this program have
  13. ** been Copyrighted and the core of this program is basically a
  14. ** conversion to C of the routines created by James Neely as listed in
  15. ** Michael Erlewine's 'Manual of Computer Programming for Astrologers',
  16. ** available from Matrix Software. The copyright gives us permission to
  17. ** use the routines for personal use but not to sell them or profit from
  18. ** them in any way.
  19. **
  20. ** The PostScript code within the core graphics routines are programmed
  21. ** and Copyright (C) 1992-1993 by Brian D. Willoughby
  22. ** (brianw@sounds.wa.com). Conditions are identical to those above.
  23. **
  24. ** The extended accurate ephemeris databases and formulas are from the
  25. ** calculation routines in the program "Placalc" and are programmed and
  26. ** Copyright (C) 1989,1991,1993 by Astrodienst AG and Alois Treindl
  27. ** (alois@azur.ch). The use of that source code is subject to
  28. ** regulations made by Astrodienst Zurich, and the code is not in the
  29. ** public domain. This copyright notice must not be changed or removed
  30. ** by any user of this program.
  31. **
  32. ** Initial programming 8/28,30, 9/10,13,16,20,23, 10/3,6,7, 11/7,10,21/1991.
  33. ** X Window graphics initially programmed 10/23-29/1991.
  34. ** PostScript graphics initially programmed 11/29-30/1992.
  35. ** Last code change made 3/19/1994.
  36. */
  37.  
  38. #include "placalc.h"
  39.  
  40. #ifdef PLACALC
  41. #ifdef ASTROLOG
  42. /* Begin contents of placalc.c */
  43. #endif
  44. /*****************************************************
  45. $Header: placalc.c,v 1.14 93/07/19 22:13:07 alois Exp $
  46.  
  47.  
  48.   ---------------------------------------------------------------
  49.   | Copyright Astrodienst AG and Alois Treindl, 1989,1991,1993  |
  50.   | The use of this source code is subject to regulations made    |
  51.   | by Astrodienst Zurich. The code is NOT in the public domain.|
  52.   |                                |
  53.   | This copyright notice must not be changed or removed    |
  54.   | by any user of this program.                |
  55.   ---------------------------------------------------------------
  56.  
  57. Important changes:
  58. 11-jun-93 revision 1.12: fixed error which affected Mercury between -2100 and
  59.             -3100 (it jumped wildly).
  60.  
  61. *******************************************************/
  62.  
  63. #ifndef ASTROLOG
  64. #include "placalc.h"    /* includes ourdef.h */
  65. #include "astrolib.h"    /* includes ourdef.h */
  66. #include "helconst.c"    /* orbital elements and disturbations
  67.             for inner planets and moon */
  68. #include "deltat.c"
  69. #else
  70. #include "placalc2.c"
  71. #endif
  72. #include <string.h>
  73.  
  74. #ifndef DIR_GLUE
  75. # if MSDOS
  76. # define DIR_GLUE "\\"
  77. # else
  78. # define DIR_GLUE "/"
  79. # endif
  80. #endif
  81.  
  82.  
  83. /************************************************************
  84. externally accessible globals, defined as extern in placalc.h 
  85. ************************************************************/
  86.  
  87.  
  88. REAL8 meanekl, ekl, nut;
  89. struct elements el [MARS + 1];
  90.  
  91. /*
  92.  * The global variable ephe_path indicates where the ephemeris files
  93.  * LRZ5_xx and CHI_xx are stored.
  94.  * By default it is set to the #defined EPHE_PATH, but the user of the
  95.  * placalc module can change it to any other location before he
  96.  * starts calling calc().
  97.  */
  98. char *ephe_path = EPHE_PATH;
  99. /*
  100.  * If there occurs an internal error in placalc, a message is 
  101.  * written to stderr or into the string variable placalc_err_text.
  102.  * By default it is written to stderr, but if placalc_err_text is
  103.  * not a NULL pointer, it is written to the string variable.
  104.  * The user must set this pointer to a string of at least 160 bytes long,
  105.  * if he/she wants to use it.
  106.  */
  107. char *placalc_err_text = NULL;
  108.  
  109.  
  110. #ifndef ASTROLOG
  111. /**********************************************************
  112. function nacalc    ()
  113. calculates an array of planet longitudes and speeds,
  114. as needed for complete nathan data records.
  115. The function knows itself how many planets and in which mode
  116. they have to be calculated for Nathan. 
  117.  
  118. return OK or ERR
  119.  
  120. The returned positions are in centiseconds, our standard
  121. coordinate format for fast mathematics with planetary positions.
  122.  
  123. This function is just a template of how the calc() package
  124. can be used. 
  125. **********************************************************/
  126. int nacalc (REAL8    jd_ad,    /* universal time relative  julian date */
  127.         centisec *plon,    /* returned longitudes */
  128.         centisec *pspe    /* returned speeds, if not NULL pointer */
  129.        )
  130. {
  131.   int planet, flag;
  132.   REAL8 rlng, rrad, rlat, rspeed;
  133.   int result = OK;
  134.   flag = CALC_BIT_SPEED;    /* same, with speed */
  135.   jd_ad += deltat( jd_ad );    /* ET = UT + Delta_T */
  136.   for (planet = SUN; planet <= TRUE_NODE; planet++) {
  137.     if (calc (planet, jd_ad, flag, &rlng, &rrad, &rlat, &rspeed) == OK) {
  138.       plon [planet] = d2l (rlng * DEG);
  139.       if (pspe != NULL) pspe [planet] = d2l (rspeed * DEG);
  140.     } else {
  141.       plon [planet] = -1;
  142.       if (pspe != NULL) pspe [planet] = 0;
  143.       result = ERR;
  144.     }
  145.   }
  146.   planet = TRUE_NODE + 1;    /* CHIRON may not be TRUE_NODE + 1 */
  147.   if (calc (CHIRON, jd_ad, flag, &rlng, &rrad, &rlat, &rspeed) == OK) {
  148.     plon [planet] = d2l (rlng * DEG);
  149.     if (pspe != NULL) pspe [planet] = d2l (rspeed * DEG);
  150.   } else {
  151.     plon [planet] = -1;
  152.     if (pspe != NULL) pspe [planet] = 0;
  153.     result = ERR;
  154.   }
  155.   return result;
  156. }    /* end nacalc */
  157. #endif /* !ASTROLOG */
  158.  
  159. #ifdef ASTROLOG
  160. /* Given an object index and a Julian Day time, get its zodiac and        */
  161. /* declination position (planetary longitude and latitude) of the object  */
  162. /* and its velocity and distance from the Earth or Sun. This basically    */
  163. /* just calls the Placalc calculation function to actually do it, but as  */
  164. /* this is the one routine called from Astrolog, this is the one routine  */
  165. /* which has knowledge of and uses both Astrolog and Placalc definitions, */
  166. /* and does things such as translation to Placalc indices and formats.    */
  167.  
  168. int PlacalcPlanet(ind, jd, helio, planet, planetalt, ret, space)
  169. int ind, helio;
  170. double jd;
  171. real *planet, *planetalt, *ret, *space;
  172. {
  173.   int iplanet, flag;
  174.   REAL8 jd_ad, rlng, rrad, rlat, rspeed;
  175.  
  176.   if (ind <= _PLU)    /* Convert Astrolog object index to Placalc index. */
  177.     iplanet = ind-1;
  178.   else if (ind == _CHI)
  179.     iplanet = CHIRON;
  180.   else if (ind == _NOD)
  181. #ifdef TRUENODE
  182.     iplanet = TRUE_NODE;
  183. #else
  184.     iplanet = MEAN_NODE;
  185. #endif
  186.   else
  187.     return FALSE;
  188.  
  189.   jd_ad = jd - JUL_OFFSET;
  190.   flag = helio ? CALC_BIT_SPEED | CALC_BIT_HELIO : CALC_BIT_SPEED;
  191.   jd_ad += deltat(jd_ad);
  192.   if (calc(iplanet, jd_ad, flag, &rlng, &rrad, &rlat, &rspeed) == OK) {
  193.     *planet    = rlng;
  194.     *planetalt = rlat;
  195.     *ret       = rspeed;
  196.     *space     = rrad;
  197.     return TRUE;
  198.   }
  199.   return FALSE;
  200. }
  201. #endif /* ASTROLOG */
  202.  
  203. #ifndef ASTROLOG
  204. /******************************************************************
  205.  * calculation server
  206.  * delivers positions in string format which can be sent easily
  207.  * over a communication line to the calculation client.
  208.  ******************************************************************/
  209. int calcserv(int id,    /* request id, random number to prevent phase err */
  210.         REAL8 jd_ad,    /* time as relative Astrodienst julian date */
  211.         int    flag,    /* a set of CALC_BIT_ bitflags */
  212.         int  plalist,/* bit list of planets to be computed, 0 = all */
  213.         char *so)    /* output string, MUST BE LONG ENOUGH (800 bytes)*/
  214. {
  215.   int p, planet, so_len;
  216.   REAL8 rlng, rrad, rlat, rspeed, rau[CALC_N];
  217.   centisec lcs[CALC_N], lpcs[CALC_N], betcs[CALC_N]; 
  218.   char s[MAXCHAR];
  219.   if (plalist == 0) plalist = CALC_ALL_PLANET_BITS;
  220.   /*
  221.    * flag determines whether deltat is added to t;
  222.    * if CALC_BIT_EPHE is set, jd_ad is considered as ephemeris time,
  223.    * otherwise as universal time.
  224.    */
  225.   if ((flag & CALC_BIT_EPHE) == 0) {
  226.     jd_ad += deltat (jd_ad);
  227.   }
  228.   for (p = SUN; p < CALC_N; p++) {
  229.     if (! check_bit(plalist, p)) continue;
  230.     if (calc (p, jd_ad, flag, &rlng, &rrad, &rlat, &rspeed) == OK) {
  231.       lcs [p] = d2l (rlng * DEG);
  232.       lpcs [p] = d2l (rspeed * DEG);
  233.       betcs [p] = d2l (rlat * DEG);
  234.       rau [p] = rrad;
  235.     } else {
  236.       sprintf(so,"error at planet %d", p);
  237.       return ( ERR);
  238.     }
  239.   }
  240.   /*
  241.    * format comma separated list: id,teph,flag,plalist,ekl,nut
  242.    * REAL8 is given with 8 digits precision after decimal point, 
  243.    * all angles are given in centiseconds.
  244.    * then for each requested planet: longitude (csec)
  245.    * then for each requested planet, if wanted: speed (csec/day)
  246.    * then for each requested planet, if wanted: latitude (csec)
  247.    * then for each requested planet, if wanted: rgeo (units 0..999)
  248.    * then for each requested planet, if wanted: rau  (A.U.)
  249.    */
  250.   sprintf (so, "%d,%.8lf,%d,%d,%ld,%ld", id, jd_ad, flag, plalist, 
  251.            d2l(ekl * DEG), d2l (nut * DEG) );
  252.   so_len = strlen (so);
  253.   for (planet = SUN; planet < CALC_N; planet++) {
  254.     if (! check_bit(plalist, planet)) continue;
  255.     sprintf (s ,",%ld", lcs[planet]);
  256.     strcat (so + so_len, s);
  257.     so_len += strlen (s);
  258.   }
  259.   if (flag & CALC_BIT_SPEED) {
  260.     for (planet = SUN; planet < CALC_N; planet++)  {
  261.       if (! check_bit(plalist, planet)) continue;
  262.       sprintf (s ,",%ld", lpcs[planet]);
  263.       strcat (so + so_len, s);
  264.       so_len += strlen (s);
  265.     }
  266.   }
  267.   if (flag & CALC_BIT_BETA) {
  268.     for (planet = SUN; planet < CALC_N; planet++)  {
  269.       if (! check_bit(plalist, planet)) continue;
  270.       sprintf (s ,",%ld", betcs[planet]);
  271.       strcat (so + so_len, s);
  272.       so_len += strlen (s);
  273.     }
  274.   }
  275.   if (flag & CALC_BIT_RGEO) {
  276.     for (planet = SUN; planet < CALC_N; planet++)  {
  277.       if (! check_bit(plalist, planet)) continue;
  278.       sprintf (s ,",%ld", rel_geo(planet,rau[planet]));
  279.       strcat (so + so_len, s);
  280.       so_len += strlen (s);
  281.     }
  282.   }
  283.   if (flag & CALC_BIT_RAU) {
  284.     for (planet = SUN; planet < CALC_N; planet++)  {
  285.       if (! check_bit(plalist, planet)) continue;
  286.       sprintf (s ,",%.8lf", rau[planet]);
  287.       strcat (so + so_len, s);
  288.       so_len += strlen (s);
  289.     }
  290.   }
  291.   return (OK);
  292. }    /* end calcserv */
  293. #endif /* !ASTROLOG */
  294.  
  295. /******************************************************************
  296.    function calc(): 
  297.    This is the main routine for computing a planets position.
  298.    The function has several modes, which are controlled by bits in
  299.    the parameter 'flag'. The normal mode (flag == 0) computes
  300.    a planets apparent geocentric position in ecliptic coordinates relative to
  301.    the true equinox of date, without speed
  302.  
  303.    Explanation of the arguments: see the functions header.
  304.  
  305.    Returns OK or ERR (if some planet out of time range). OK and ERR are
  306.    defined in ourdef.h and must not be confused with TRUE and FALSE.
  307.    OK and ERR are of type int, not of type BOOLEAN.
  308.  
  309.    Bits used in flag:
  310.    CALC_BIT_HELIO          0 = geocentric, 1 = heliocentric
  311.    CALC_BIT_NOAPP            0 = apparent positions, 1 = true positions
  312.    CALC_BIT_NONUT         0 = do nutation (true equinox of date)
  313.                 1 = don't do nutation (mean equinox of date).
  314.  
  315.    CALC_BIT_SPEED         0 = don't calc speed,
  316.                 1 = calc speed, takes quite long for moon
  317.                 (is observed only for moon, with other
  318.                 planets speed is cheap)
  319.  
  320.    Side effects and local memory:
  321.    For doing heliocentric positions the fucntion must know the
  322.    earth's position for the desired time t. It remembers the earth
  323.    position so it does not have to recompute it each time a planet
  324.    position is wanted for the same time t.
  325.    It calls helup(t), which leaves as a side effect the global
  326.    variables meanekl, ekl and nut for the time t.
  327.  
  328.    Functions called by calc():
  329.    helup(t)
  330.    hel(t)
  331.    moon(t)
  332.    togeo()
  333.  
  334.    Time range:
  335.    The function can be used savely in the time range 5000 BC to
  336.    3000 AD. The stored ephemeris is available only for this time
  337.    range, so Jupiter ... Pluto cannot be computed outside. The
  338.    function will return results for the other planets also outside
  339.    of this time range, but they become meaningless pretty soon
  340.    before 5000 BC, because Newcombs time series expansions for the
  341.    elements will not work anymore.
  342.  
  343. ******************************************************************/
  344. #ifndef ASTROLOG
  345. int calc(int  planet,      /* planet index as defined in placalc.h,
  346.              SUN = 0, MOON = 1 etc.
  347.              planet == -1 calc calculates only nut and ecl */
  348.      REAL8 jd_ad,    /* relative Astrodienst Juldate, ephemeris time.
  349.              Astrodienst Juldate is relative 31 Dec 1949, noon. */
  350.      int  flag,    /* See definition of flag bits above */
  351.      REAL8 *alng,
  352.      REAL8 *arad,
  353.      REAL8 *alat,
  354.      REAL8 *alngspeed)
  355. #else
  356. int calc(planet, jd_ad,    flag,    alng, arad, alat, alngspeed)
  357. int planet;
  358. REAL8 jd_ad;
  359. int flag;
  360. REAL8 *alng;
  361. REAL8 *arad;
  362. REAL8 *alat;
  363. REAL8 *alngspeed;
  364. #endif
  365.       /* pointers to the return variables:
  366.      alng = ecliptic longitude in degrees
  367.      arad = radius vector in AU (astronomic units)
  368.      alat = ecliptic latitude in degrees
  369.      alngspeed = speed of planet in degrees per day
  370.      
  371.      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  372.      The precision of the speed is quite limited.
  373.      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  374.      For Sun, Mercury, Venus and Mars we take only the speed from
  375.      the undisturbed Kepler orbit. For the Moon there is no
  376.      reasonable undisturbed orbit and we derive the speed from
  377.      its position at t + dt and t - dt. We need these 
  378.      moon positions anyway for the true node calculation.
  379.      For the outer planets and Chiron we derive the precise
  380.      speed from the stored ephemeris by high order inter-
  381.      polation; the precision is limited for the geocentric
  382.      case due to the limited precision of the earth's/sun's
  383.      speed.
  384.      Applications who need precise speeds should
  385.      get them by calling calc() with slightly different times.
  386.      */
  387.   /*
  388.    * Comment 7 May 1991 by Alois Treindl:
  389.    * Center of Earth versus Barycenter Earth-Moon:
  390.    * Brown's theory of the moon gives the moon's coordinates relative
  391.    * to the center of the earth. Newcomb's theory of the Sun gives the
  392.    * coordinates of the earth's center relative to the center of the Sun.
  393.    * This is what we need.
  394.    *
  395.    * How about the Mean Lunar Node?
  396.    * The orbital elements of the Sun in Newcomb's theory are given
  397.    * relative to the barycenter Earth-Moon; the reduction to geocentric
  398.    * is only applied after doing the Kepler ellipse calculation.
  399.    * Are the Lunar elements also relative to the barycenter??
  400.    * If yes:
  401.    * When we use the moon's mean node out of the elements, it is still
  402.    * as seen from the barycenter. Because the node is close to the
  403.    * earth, we would have to apply a considerable correction, which is of
  404.    * the order of 4000/384000 km or 35' (minutes of arc).
  405.    * Nobody has ever applied such a correction to the mean node.
  406.    *
  407.    * And the True Node?
  408.    * When we calculate the osculating orbital elements of the Moon (true node),
  409.    * are they relative to the barycenter or to the Earth's center?
  410.    * Our derivation of true node from the actual Moon positions considers
  411.    * the earth's center as the focal point of the osculating lunar ellipse.
  412.    * A more correct approach would first reduce the lunar position from
  413.    * geocentric to barycentric, then compute the orbital elements from
  414.    * the reduced positions, and then reduce the desired items
  415.    * (node, apogaeum, 'dark moon') to geocentric positions.
  416.    * No known astrological ephemeris has ever used such a correction, which is
  417.    * of the same order of magnitude as the correction to the meannode above.
  418.    * When the moon is going through the ecliptic, the geocenter, barycenter
  419.    * moon (and the node identical to the moon itself) line up; this is why
  420.    * the error does not show up in normal considerations.
  421.    */
  422. {
  423.   struct rememberdat  /* time for which the datas are calculated */
  424.     { REAL8  calculation_time, lng, rad, zet, lngspeed, radspeed, zetspeed; };
  425.   static struct rememberdat earthrem = 
  426.     { HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8 };
  427.   static struct rememberdat moonrem  = 
  428.     { HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8 };
  429.   REAL8 c, s, x, knn, knv; 
  430.   REAL8 rp, zp; /* needed to call hel! */
  431.   REAL8 *azet = alat; 
  432.   BOOLEAN calc_geo, calc_helio, calc_apparent, calc_speed,
  433.       calc_nut;
  434.  
  435.   helup (jd_ad); /* helup checks whether it was already called with same time*/
  436.   if ( planet == CALC_ONLY_ECL_NUT ) return (OK);  
  437.  
  438.   calc_helio =  flag & CALC_BIT_HELIO;
  439.   calc_geo = ! calc_helio;
  440.   calc_apparent = ! (flag & CALC_BIT_NOAPP);
  441.   calc_nut = ! (flag & CALC_BIT_NONUT);
  442.   calc_speed = flag & CALC_BIT_SPEED;
  443.   /*
  444.    * it is necessary to compute EARTH in the following cases:
  445.    * heliocentric MOON or EARTH
  446.    * geocentric any planet except MOON or nodes or LILITH
  447.    */
  448.   if (calc_helio && (planet == MOON || planet == EARTH)
  449.   || calc_geo && planet != MOON
  450.           && planet != MEAN_NODE
  451.           && planet != TRUE_NODE
  452.               && planet != LILITH) {
  453.     if ( earthrem.calculation_time != jd_ad ) {     
  454.       hel (  EARTH, jd_ad, alng, arad, azet, alngspeed, &rp, &zp );
  455.       /* store earthdata for geocentric calculation: */
  456.       earthrem.lng = *alng;  
  457.       earthrem.rad = *arad;
  458.       earthrem.zet = *azet;
  459.       earthrem.lngspeed = *alngspeed;
  460.       earthrem.radspeed = rp;
  461.       earthrem.zetspeed = zp;
  462.       earthrem.calculation_time = jd_ad;
  463.     }
  464.   }
  465.   switch(planet) {
  466.   case EARTH:    /* has been already computed */
  467.     *alng = earthrem.lng;
  468.     *arad = earthrem.rad;
  469.     *azet = earthrem.zet;
  470.     *alngspeed = earthrem.lngspeed;
  471.     rp = earthrem.radspeed;
  472.     zp = earthrem.zetspeed;
  473.     if ( calc_geo ) {    /* SUN seen from earth */
  474.       *alng = smod8360( *alng + 180.0 );    
  475.       *azet = - *azet;
  476.     }
  477.     if (calc_apparent)
  478.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  479.     break; 
  480.   case MOON:
  481.     moon( alng, arad, azet );  
  482.     moonrem.lng = *alng;  /* moonrem will be used for TRUE_NODE */
  483.     moonrem.rad = *arad;
  484.     moonrem.zet = *azet;
  485.     *alngspeed = 12;
  486.     moonrem.calculation_time = jd_ad;
  487.     if ( calc_helio || calc_speed ) {/* get a second moon position */
  488.       REAL8 lng2, rad2, zet2;
  489.       helup( jd_ad + MOON_SPEED_INTERVAL );        
  490.       moon( &lng2, &rad2, &zet2 );  
  491.       helup( jd_ad );
  492.       if ( calc_helio ) { /* moon as seen from sun */
  493.     togeo( earthrem.lng, -earthrem.rad, moonrem.lng, moonrem.rad,
  494.          moonrem.zet, alng, arad );
  495.     togeo( earthrem.lng + MOON_SPEED_INTERVAL * earthrem.lngspeed, 
  496.         -( earthrem.rad + MOON_SPEED_INTERVAL * earthrem.radspeed ), 
  497.         lng2, rad2, zet2, &lng2, &rad2 );
  498.       }
  499.       *alngspeed =  diff8360( lng2, *alng ) / MOON_SPEED_INTERVAL;
  500.       /* rp = ( rad2 - *arad ) / MOON_SPEED_INTERVAL;
  501.      zp = ( zet2 - moonrem.zet ) / MOON_SPEED_INTERVAL; */
  502.     }
  503.     *alat = RADTODEG * ASIN8( *azet / *arad );
  504.     /*
  505.      * light time correction, not applied for moon or nodes;
  506.      * moon would have only term of ca. 0.02", see Expl.Sup.1961 p.109
  507.      */
  508.     break;
  509.   case MERCURY:
  510.   case VENUS:
  511.   case MARS:
  512.   case JUPITER:
  513.   case SATURN:
  514.   case URANUS:
  515.   case NEPTUNE:
  516.   case PLUTO:
  517.   case CHIRON:
  518.     if (hel ( planet, jd_ad, alng, arad, azet, alngspeed, &rp, &zp ) != OK)
  519.       return (ERR);    /* outer planets can fail if out of ephemeris range */
  520.     if ( calc_geo ) {       /* geocentric */
  521.       REAL8 lng1, rad1, lng2, rad2;
  522.       togeo( earthrem.lng, earthrem.rad, *alng, *arad, *azet, &lng1, &rad1 );
  523.       togeo( earthrem.lng + earthrem.lngspeed, 
  524.        earthrem.rad + earthrem.radspeed, 
  525.        *alng + *alngspeed, *arad + rp, *azet + zp, &lng2, &rad2 ); 
  526.       *alng = lng1;
  527.       *arad = rad1; 
  528.       *alngspeed = diff8360( lng2, lng1 );
  529.       /* rp = rad2 - rad1; */
  530.     }
  531.     *alat = RADTODEG * ASIN8( *azet / *arad );
  532.     if (calc_apparent)
  533.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  534.     break;
  535.   case MEAN_NODE:
  536.     *alng = smod8360( el[MOON].kn);
  537.     /*
  538.      * the distance of the node is the 'orbital parameter' p = a (1-e^2);
  539.      * Our current use of the axis a is wrong, but is never used.
  540.      */
  541.     *arad = pd[MOON].axis;
  542.     *alat = 0.0;
  543.     *alngspeed = -0.053;
  544.     break;
  545.   case TRUE_NODE: {
  546.     /* see comment 'Note 7 May 1991' above */
  547.     REAL8  ln, rn, zn, 
  548.        lv, rv, zv, 
  549.        l1, r1, z1, 
  550.        xn, yn, xv, yv, r0, x0, y0;
  551.     helup( jd_ad + NODE_INTERVAL );
  552.     moon( &ln, &rn, &zn );
  553.     helup( jd_ad - NODE_INTERVAL );
  554.     moon( &lv, &rv, &zv );
  555.     helup( jd_ad );
  556.     if ( moonrem.calculation_time != jd_ad ) 
  557.       moon( &l1, &r1, &z1 );  
  558.     else {     /* moon is already calculated */
  559.       l1 = moonrem.lng;
  560.       r1 = moonrem.rad;
  561.       z1 = moonrem.zet;
  562.     }
  563.     rn = sqrt( rn * rn - zn * zn );
  564.     rv = sqrt( rv * rv - zv * zv );
  565.     r0 = sqrt( r1 * r1 - z1 * z1 );
  566.     xn = rn * COS8( DEGTORAD * ln );
  567.     yn = rn * SIN8( DEGTORAD * ln );
  568.     xv = rv * COS8( DEGTORAD * lv );
  569.     yv = rv * SIN8( DEGTORAD * lv );
  570.     x0 = r0 * COS8( DEGTORAD * l1 );   
  571.     y0 = r0 * SIN8( DEGTORAD * l1 );   
  572.     x = test_near_zero( x0 * yn - xn * y0 );
  573.     s = ( y0 * zn - z1 * yn ) / x;
  574.     c = test_near_zero( ( x0 * zn - z1 * xn ) / x );
  575.     knn =  smod8360( RADTODEG * ATAN28( s, c )); /* = ATAN8( s / c ) */
  576.     x = test_near_zero( y0 * xv - x0 * yv );
  577.     s = ( yv * z1 - zv * y0 ) / x;
  578.     c = test_near_zero( ( xv * z1 - zv * x0 ) / x );
  579.     knv =  smod8360( RADTODEG * ATAN28( s, c ));        
  580.     *alng = smod8360( ( knv + knn ) / 2 );
  581.     /*
  582.      * the distance of the node is the 'orbital parameter' p = a (1-e^2);
  583.      * Our current use of the axis a is wrong.
  584.      */
  585.     *arad = pd[MOON].axis;    
  586.     *alat = 0.0;
  587.     *alngspeed = diff8360( knn, knv ) / NODE_INTERVAL;
  588.     }
  589.     break;
  590.   case LILITH: {
  591.     /*
  592.      * Added 22-Jun-93
  593.      * Lilith or Dark Moon is the empty focal point of the mean lunar ellipse.
  594.      * This is 180 degrees from the perihel.
  595.      * Because the lunar orbit is not in the ecliptic, it must be
  596.      * projected onto the ecliptic in the same way as the planetary orbits
  597.      * are (see for example Montenbruck, Grundlagen der Ephemeridenrechnung).
  598.      *
  599.      * We compute the MEAN Lilith, not the TRUE one which would have to be
  600.      * derived in a similar way as the true node.
  601.      * For the radius vector of Lilith we use a simple formula; 
  602.      * to get a precise value, the fact that the focal point of the ellipse
  603.      * is not at the center of the earth but at the barycenter moon-earth
  604.      * would have to be accounted for.
  605.      * For the speed we always return a constant: the T term from the
  606.      * lunar perihel.
  607.      * Joelle de Gravelaine publishes in her book "Lilith der schwarze Mond"
  608.      * (Astrodata, 1990) an ephemeris which gives noon (12.00) positions
  609.      * but does not project onto the ecliptic.
  610.      * This creates deviations 
  611.      */
  612.     double arg_lat, lon, cosi;
  613.     struct elements *e = &el[MOON];
  614.     arg_lat = degnorm(e->pe - e->kn + 180.0);
  615.     cosi = COSDEG(e->in);
  616.     if (e->in == 0 || ABS8( arg_lat -  90.0 ) < TANERRLIMIT 
  617.     || ABS8( arg_lat - 270.0 ) < TANERRLIMIT ) {
  618.       lon = arg_lat;
  619.     } else {
  620.       lon = ATAN8( TANDEG( arg_lat ) * cosi );
  621.       lon = RADTODEG * lon;
  622.       if ( arg_lat > 90.0 && arg_lat < 270.0 )  lon += 180.0;
  623.     }
  624.     lon = degnorm(lon + e->kn);
  625.     *alng = lon;
  626.     *alngspeed = 0.111404;    /* 6'41.05" per day */
  627.     *arad = 2 * pd[MOON].axis * e->ex;
  628.     /*
  629.      *  To test Gravalaines error, return unprojected long in alat.
  630.      *  the correct latitude would be:
  631.      *   *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  632.      */
  633.     *alat = degnorm(arg_lat + e->kn);    /* unprojected longitude, no nut */
  634.     }
  635.     break;
  636.   default:
  637.     fprintf(stderr, "calc() called with illegal planet %d\n", planet);
  638.     return ERR;
  639.   } /* end switch */ 
  640.   if (calc_nut)
  641.     *alng += nut;
  642.   *alng = smod8360( *alng);    /* normalize to circle */
  643.   return (OK);
  644. } /* end calc */
  645.  
  646. #ifndef ASTROLOG
  647. int rel_geo(int planet, double rau)
  648. #else
  649. int rel_geo(planet, rau)
  650. int planet;
  651. double rau;
  652. #endif
  653. {
  654.   /*
  655.    * get relative earth distance in range 0..1000:
  656.    * To compute the relative distance we use fixed values of 
  657.    * mimimum and maximum distance measured empirically between
  658.    * 1300 AD and 2300 AD (see helconst.c). 
  659.    * This approach is certainly fine for the
  660.    * outer planets, but not the best for Sun and Moon, where it
  661.    * would be better to look at the mean anomaly, i.e. the progress
  662.    * the planet makes on it's Kepler orbit.
  663.    * Considering the low importance astrologers give to the relative
  664.    * distance, we consider the effort not worthwile.
  665.    * Now we compare real radius with longtime-averaged distances.
  666.    */
  667.   int rgeo;
  668.   if (planet == MEAN_NODE || planet == TRUE_NODE || planet == LILITH) {
  669.     return 0;
  670.   } else {
  671. #ifndef ASTROLOG
  672.     rgeo = 1000 * (1.0 - (rau - rmima[planet][0]) / (rmima[planet][1] - rmima[planet][0]));
  673. #else
  674.     rgeo = 1000 * (int)(1.0 - (rau - rmima[planet][0]) / (rmima[planet][1] - rmima[planet][0]));
  675. #endif
  676.   }
  677.   if (rgeo < 0)
  678.     rgeo = 0;
  679.   else if (rgeo > 999)
  680.     rgeo = 999;
  681.   return rgeo;
  682. }
  683.  
  684. /******************************************************************
  685. helio to geocentric conversion
  686. ******************************************************************/
  687. #ifndef ASTROLOG
  688. void togeo(REAL8 lngearth,
  689.      REAL8 radearth,
  690.      REAL8 lng,
  691.      REAL8 rad,
  692.      REAL8 zet,
  693.      REAL8 *alnggeo,
  694.      REAL8 *aradgeo )
  695. #else
  696. void togeo(lngearth, radearth, lng, rad, zet, alnggeo, aradgeo)
  697. REAL8 lngearth;
  698. REAL8 radearth;
  699. REAL8 lng;
  700. REAL8 rad;
  701. REAL8 zet;
  702. REAL8 *alnggeo;
  703. REAL8 *aradgeo;
  704. #endif
  705. {
  706.   REAL8 r1, x, y;
  707.   r1 = sqrt( rad * rad - zet * zet );
  708.   x = r1 * COS8( DEGTORAD * lng ) - radearth * COS8( DEGTORAD * lngearth );
  709.   y = r1 * SIN8( DEGTORAD * lng ) - radearth * SIN8( DEGTORAD * lngearth );
  710.   *aradgeo = sqrt( x * x + y * y + zet * zet );
  711.   x = test_near_zero( x );
  712.   *alnggeo =  smod8360( RADTODEG * ATAN28( y, x ) );
  713. } /* end togeo */
  714.  
  715. /******************************************************************
  716. helup()
  717. prepares the orbital elements and the disturbation arguments for the
  718. inner planets and the moon. helup(t) is called by hel() and by calc().
  719. helup() returns its results in global variables.
  720. helup() remembers the t it has been called with before and does
  721. not recalculate its results when it is called more than once with
  722. the same t.
  723. ******************************************************************/
  724. #ifndef ASTROLOG
  725. void helup (REAL8  jd_ad ) /* relative julian date, ephemeris time */
  726. #else
  727. void helup(jd_ad)
  728. REAL8 jd_ad;
  729. #endif
  730. {
  731.   int i;
  732.   static REAL8 thelup = HUGE8;    /* is initialized only once at load time */
  733.   struct elements *e = el;      /* pointer to el[i] */  
  734.   struct elements *ee = el;     /* pointer to el[EARTH] */  
  735.   struct eledata  *d = pd;      /* pointer to pd[i] */
  736.   REAL8 td, ti, ti2, tj1, tj2, tj3;
  737.  
  738.   if ( thelup == jd_ad ) return;    /* if already calculated then return */
  739.   for ( i = SUN; i <= MARS; i++, d++, e++ ) 
  740.   {
  741.     td = jd_ad - d->epoch;
  742.     ti = e->tj = td / 36525.0;  /* julian centuries from epoch */
  743.     ti2 = ti * ti;
  744.     tj1 = ti / 3600.0;    /* used when coefficients are in seconds of arc */
  745.     tj2 = ti * tj1;
  746.     tj3 = ti * tj2;
  747.     e->lg = mod8360( d->lg0 + d->lg1 * td  + d->lg2 * tj2 + d->lg3 * tj3 );
  748.       /* also with moon lg1 *td is exact to 10e-8 degrees within 5000 years */
  749.     e->pe = mod8360( d->pe0 + d->pe1 * tj1 + d->pe2 * tj2 + d->pe3 * tj3 );
  750.     e->ex = d->ex0 + d->ex1 * ti + d->ex2 * ti2;
  751.     e->kn = mod8360( d->kn0 + d->kn1 * tj1 + d->kn2 * tj2 + d->kn3 * tj3 );
  752.     e->in = d->in0 + d->in1 * tj1 + d->in2 * tj2;
  753.     e->ma = smod8360( e->lg - e->pe );
  754.     if ( i == MOON ) {  /* calculate ekliptic according Newcomb, APAE VI,
  755.             and nutation according Exp.Suppl. 1961, identical
  756.             with Mark Potttenger elemnut()
  757.             all terms >= 0.01" only .
  758.             The 1984 IAU Theory of Nutation, as published in
  759.             AE 1984 suppl. has not yet been implemented
  760.             because it would mean to use other elements of
  761.             moon and sun */
  762.       REAL8 mnode, mlong2, slong2, mg, sg, d2;
  763.       mnode  = DEGTORAD * e->kn;    /* moon's mean node */
  764.       mlong2 = DEGTORAD * 2.0 * e->lg;     /* 2 x moon's mean longitude */
  765.       mg     = DEGTORAD * e->ma;    /* moon's mean anomaly (g1) */
  766.       slong2 = DEGTORAD * 2.0 * ee->lg; /* 2 x sun's mean longitude (L), with
  767.                     the phase 180 deg earth-sun irrelevant
  768.                     because 2 x 180 = 360 deg */
  769.       sg     = DEGTORAD * ee->ma;    /* sun's mean anomaly = earth's */
  770.       d2     = mlong2 - slong2;        /* 2 x elongation of moon from sun */
  771.       meanekl = ekld[0] + ekld[1] * tj1 + ekld[2] * tj2 + ekld[3] * tj3;
  772.       ekl = meanekl + 
  773.         ( 9.2100 * COS8( mnode ) 
  774.         - 0.0904 * COS8( 2.0 * mnode ) 
  775.         + 0.0183 * COS8( mlong2 - mnode )
  776.         + 0.0884 * COS8( mlong2 ) 
  777.         + 0.0113 * COS8( mlong2 + mg )
  778.         + 0.5522 * COS8( slong2 )
  779.         + 0.0216 * COS8( slong2 + sg ) ) / 3600.0;
  780.       nut = ( ( -17.2327 - 0.01737 * ti ) * SIN8( mnode ) 
  781.              + 0.2088 * SIN8( 2.0 * mnode )
  782.         + 0.0675 * SIN8( mg )
  783.         - 0.0149 * SIN8( mg - d2 )
  784.         - 0.0342 * SIN8( mlong2 - mnode)
  785.         + 0.0114 * SIN8( mlong2 - mg)
  786.             - 0.2037 * SIN8( mlong2 ) 
  787.         - 0.0261 * SIN8( mlong2 + mg )
  788.         + 0.0124 * SIN8( slong2 - mnode)
  789.         + 0.0214 * SIN8( slong2 - sg)
  790.         - 1.2729 * SIN8( slong2 ) 
  791.         - 0.0497 * SIN8( slong2 + sg)
  792.             + 0.1261 * SIN8( sg ) ) / 3600.0; 
  793.     }      
  794.   }  /* for i */
  795.   
  796.   /* calculate the arguments sa[] for the disturbation terms */ 
  797.   ti = (jd_ad - EPOCH1850) / 365.25;    /* julian years from 1850 */
  798.   for ( i = 0; i < SDNUM; i++ ) 
  799.     sa [i] = mod8360 (_sd [i].sd0 + _sd [i].sd1 * ti);
  800.   /*
  801.     sa[2] += 0.3315 * SIN8 (DEGTORAD *(133.9099 + 38.39365 * el[SUN].tj));
  802.   */
  803.   /* correction of jupiter perturbation argument for sun from Pottenger;
  804.   creates only .03" and 1e-7 rad, not applied because origin unclear */
  805.   thelup = jd_ad;               /* note the last helup time */
  806. }    /* end helup() */
  807.  
  808. /******************************************************************
  809. hel()
  810. Computes the heliocentric positions for all planets except the moon.
  811. The outer planets from Jupiter onwards, including Chiron, are 
  812. actually done by a subsequent call to outer_hel() which takes
  813. exactly the same parameters.
  814. hel() does true position relative to the mean ecliptic and equinox
  815. of date. Nutation is not added and must be done so by the caller.
  816. The latitude of the Sun (max. 0.5") is neglected and always returned
  817. as zero.
  818.  
  819. return: OK or ERR
  820. ******************************************************************/
  821. #ifndef ASTROLOG
  822. int hel( int    planet,        /* planet index as defined by placalc.h */
  823.     REAL8    t,        /* relative juliand date, ephemeris time */
  824.                 /* Now come 6 pointers to return values. */
  825.     REAL8    *al,        /* longitude in degrees */
  826.     REAL8   *ar,        /* radius in AU */
  827.     REAL8   *az,        /* distance from ecliptic in AU */
  828.     REAL8   *alp,         /* speed in longitude, degrees per day */
  829.     REAL8   *arp,        /* speed in radius, AU per day */
  830.     REAL8   *azp)       /* speed in z, AU per day */
  831. #else
  832. int hel(planet, t, al, ar, az, alp, arp, azp)
  833. int    planet;
  834. REAL8    t;
  835. REAL8    *al;
  836. REAL8 *ar;
  837. REAL8 *az;
  838. REAL8 *alp;
  839. REAL8 *arp;
  840. REAL8 *azp;
  841. #endif
  842. {
  843.   void  disturb();
  844.   REAL8 fnu();
  845.   register struct elements *e;
  846.   register struct eledata  *d;
  847.   REAL8 lk = 0.0;
  848.   REAL8 rk = 0.0;
  849.   REAL8 b,  h1,  sini, sinv, cosi, cosu, cosv, man, truanom, esquare, 
  850.     k8,  u, up, v, vp;
  851.       
  852.   if (planet >= JUPITER ) 
  853.     return ( outer_hel( planet, t, al, ar, az, alp, arp, azp ));
  854.   if (planet < SUN || planet == MOON)
  855.     return (ERR);
  856.  
  857.   e = &el[planet];
  858.   d = &pd[planet];
  859.   sini = SIN8( DEGTORAD * e->in );
  860.   cosi = COS8( DEGTORAD * e->in );
  861.   esquare = sqrt( ( 1.0 + e->ex ) / ( 1.0 - e->ex ) ); /* H6 in old version */
  862.   man = e->ma;
  863.   if ( planet == EARTH ) /* some longperiodic terms in mean longitude */
  864.     man += (    0.266 * SIN8 ( DEGTORAD * ( 31.8 + 119.0 * e->tj ) )
  865.         + 6.40  * SIN8 ( DEGTORAD * ( 231.19 + 20.2 * e->tj ) )
  866.         + (1.882-0.016*e->tj) * SIN8( DEGTORAD * (57.24 + 150.27 * e->tj))
  867.        ) / 3600.0;
  868.   if ( planet == MARS )   /* some longperiodic terms */
  869.     man += ( 0.606 * SIN8( DEGTORAD * (212.87 + e->tj * 119.051) )
  870.       + 52.490 * SIN8( DEGTORAD * (47.48 + e->tj * 19.771) )
  871.       +  0.319 * SIN8( DEGTORAD * (116.88 + e->tj * 773.444) )
  872.       +  0.130 * SIN8( DEGTORAD * (74 + e->tj * 163) )
  873.       +  0.280 * SIN8( DEGTORAD * (300 + e->tj * 40.8) )
  874.       -  ( 37.05 +13.5 * e->tj )
  875.       ) / 3600.0;
  876.   u = fnu ( man, e->ex, 0.0000003 ); /* error 0.001" returns radians */  
  877.   cosu = COS8( u );
  878.   h1 = 1 - e->ex * cosu;
  879.   *ar = d->axis * h1;
  880. #ifndef ASTROLOG
  881.   if ( ABS8( M_PI - u ) < TANERRLIMIT ) 
  882. #else
  883.   if (ABS8(PI - u) < TANERRLIMIT)
  884. #endif
  885.     truanom = u; /* very close to aphel */
  886.   else
  887.     truanom = 2.0 * ATAN8( esquare * TAN8( u * 0.5 ) ); /* true anomaly, rad*/
  888.   v = smod8360( truanom * RADTODEG + e->pe - e->kn ); /* argument of latitude */
  889.   if ( sini == 0.0 || ABS8( v -  90.0 ) < TANERRLIMIT 
  890.                    || ABS8( v - 270.0 ) < TANERRLIMIT ) {
  891.     *al = v;
  892.   } else {
  893.     *al = RADTODEG * ATAN8( TAN8( v * DEGTORAD ) * cosi );
  894.     if ( v > 90.0 && v < 270.0 )  *al += 180.0;
  895.   }
  896.   *al = smod8360( *al + e->kn );
  897.   sinv = SIN8( v * DEGTORAD );
  898.   cosv = COS8( v * DEGTORAD );
  899.   *az = *ar * sinv * sini;
  900.   b = ASIN8( sinv * sini );       /* latitude in radians */
  901.   k8 = cosv / COS8( b ) * sini;
  902.   up = 360.0 / d->period / h1;    /* du/dt degrees/day */
  903. #ifndef ASTROLOG
  904.   if ( ABS8 ( M_PI - u ) < TANERRLIMIT ) 
  905. #else
  906.   if (ABS8(PI - u) < TANERRLIMIT)
  907. #endif
  908.     vp = up / esquare;    /* speed at aphel */
  909.   else
  910.     vp = up * esquare * ( 1 + COS8 ( truanom ) ) / ( 1 + cosu ); 
  911.     /* dv/dt degrees/day */
  912.   *arp = d->axis * up * DEGTORAD * SIN8( u ) * e->ex; 
  913.     /* dr/dt  AU/day */
  914.   *azp = *arp * sinv * sini + *ar * vp * DEGTORAD * cosv * sini;    /* dz/dt */
  915.   *alp = vp / cosi * ( 1 - k8 * k8 );
  916.   /* now come the disturbations */
  917.   switch ( planet ) {
  918.   REAL8 am, mma, ema, u2;
  919.   case EARTH:  
  920.   /* 
  921.    * earth has some special moon values and a disturbation series due to the
  922.    * planets. The moon stuff is due to the fact, that the mean elements
  923.    * give the coordinates of the earth-moon barycenter. By adding the
  924.    * corrections we effectively reduce to the center of the earth.
  925.    * We neglect the correction in latitude, which is about 0.5", because
  926.    * for astrological purposes we want the Sun to have latitude zero.
  927.    */
  928.       am = DEGTORAD * smod8360( el[MOON].lg - e->lg + 180.0 ); /* degrees */
  929.       mma = DEGTORAD * el[MOON].ma;
  930.       ema = DEGTORAD * e->ma;
  931.       u2 = 2.0 * DEGTORAD * (e->lg - 180.0 - el[MOON].kn); /* 2u' */
  932.       lk =   6.454 * SIN8( am ) 
  933.        + 0.013 * SIN8( 3.0 * am )
  934.        + 0.177 * SIN8( am + mma )
  935.        - 0.424 * SIN8( am - mma )
  936.        + 0.039 * SIN8( 3.0 * am - mma )
  937.        - 0.064 * SIN8( am + ema )
  938.        + 0.172 * SIN8( am - ema )
  939.        - 0.013 * SIN8( am - mma - ema)
  940.        - 0.013 * SIN8( u2 );
  941.       rk =   13360 * COS8( am ) 
  942.        + 30    * COS8( 3.0 * am )
  943.        + 370   * COS8( am + mma )
  944.        - 1330  * COS8( am - mma )
  945.        + 80    * COS8( 3.0 * am - mma )
  946.        - 140   * COS8( am + ema )
  947.        + 360   * COS8( am - ema )
  948.        - 30    * COS8( am - mma - ema)
  949.        + 30    * COS8( u2 );
  950.       /* long periodic term from mars 15g''' - 8g'', Vol 6 p19, p24 */
  951.       lk +=  0.202 * SIN8( DEGTORAD * (315.6 + 893.3 * e->tj));
  952.       disturb( earthkor, al, ar, lk, rk, man );
  953.       break;
  954.   case MERCURY:    /* only normal disturbation series */
  955.       disturb( mercurykor, al, ar, 0.0, 0.0, man );
  956.       break;
  957.   case VENUS:  /* some longperiod terms and normal series */
  958.       lk = (2.761 - 0.22*e->tj) * SIN8( DEGTORAD * (237.24 + 150.27 * e->tj))
  959.               + 0.269 * SIN8( DEGTORAD * (212.2  + 119.05 * e->tj))
  960.           - 0.208 * SIN8( DEGTORAD * (175.8  + 1223.5 * e->tj));
  961.           /* make seconds */
  962.       disturb( venuskor, al, ar, lk, 0.0, man );
  963.       break;
  964.   case MARS:   /* only normal disturbation series */
  965.       disturb( marskor, al, ar, 0.0, 0.0, man );
  966.       break;
  967.   }    /* switch planet */
  968.   return (OK);
  969. }    /* hel */
  970.  
  971. /******************************************************************/
  972. void disturb( k, al, ar, lk, rk, man )
  973. register struct kor *k;  /* ENDMARK-terminated array of struct kor */
  974. REAL8  *al,     /* longitude in degrees, use a pointer to return value */
  975.        *ar;     /* radius in AU */
  976. REAL8  lk,      /* longitude correction in SECONDS OF ARC */
  977.         /* function can be called with an lk and rk already
  978.            != 0, but no value is returned */
  979.        rk,      /* radius correction in units of 9th place of log r */
  980.        man;     /* mean anomaly of planet */
  981. {
  982.   REAL8 arg;
  983.   while ( k->j != ENDMARK ) {
  984.     arg = k->j * sa[k->k] + k->i * man;
  985.     lk += k->lampl * COS8( DEGTORAD * ( k->lphase - arg ) );
  986.     rk += k->rampl * COS8( DEGTORAD * ( k->rphase - arg ) );
  987.     k++;
  988.   }  /* while */
  989.   *ar *=  EXP10 ( rk * 1.0E-9 );   /* 10^ rk */
  990.   *al += lk / 3600.0;
  991. }    /* disturb() */
  992.  
  993. /******************************************************************/
  994. #ifndef ASTROLOG
  995. int moon(REAL8 *al, REAL8 *ar, REAL8 *az )    /* return OK or ERR */
  996. #else
  997. int moon(al, ar, az)
  998. REAL8 *al;
  999. REAL8 *ar;
  1000. REAL8 *az;
  1001. #endif
  1002. {
  1003.   REAL8 a1,a2,a3,a4,a5,a6,a7,a8,a9,c2,c4,arg,b,d,f,dgc,dlm,dpm,dkm,dls;
  1004.   REAL8 ca, cb, cd, f_2d, f_4d, g1c,lk,lk1,man,ms,nib,s,sinarg,sinp,sk;
  1005.   REAL8 t, tb, t2c, r2rad, i1corr, i2corr, dlid;
  1006. #ifndef ASTROLOG
  1007.   int i, j;
  1008. #else
  1009.   int i;
  1010. #endif
  1011.   struct elements *e;
  1012.   struct m45dat   *mp;
  1013. # if MOON_TEST_CORR
  1014.   struct m5dat    *m5p;
  1015. # endif
  1016.   e = &el[MOON];
  1017.   t = e->tj * 36525;    /* days from epoch 1900 */
  1018.  
  1019.   /* new format table II, parameters in full rotations of 360 degrees */
  1020.   r2rad = 360.0 * DEGTORAD;
  1021.   tb  = t * 1e-12;    /* units of 10^12 */
  1022.   t2c = t * t * 1e-16;    /* units of 10^16 */
  1023.   a1 = SIN8( r2rad * (0.53733431 -  10104982 * tb + 191 * t2c ));
  1024.   a2 = SIN8( r2rad * (0.71995354 - 147094228 * tb +  43 * t2c ));
  1025.   c2 = COS8( r2rad * (0.71995354 - 147094228 * tb +  43 * t2c ));
  1026.   a3 = SIN8( r2rad * (0.14222222 +   1536238 * tb ));
  1027.   a4 = SIN8( r2rad * (0.48398132 - 147269147 * tb +  43 * t2c ));
  1028.   c4 = COS8( r2rad * (0.48398132 - 147269147 * tb +  43 * t2c ));
  1029.   a5 = SIN8( r2rad * (0.52453688 - 147162675 * tb +  43 * t2c ));
  1030.   a6 = SIN8( r2rad * (0.84536324 -  11459387 * tb ));
  1031.   a7 = SIN8( r2rad * (0.23363774 +   1232723 * tb + 191 * t2c ));
  1032.   a8 = SIN8( r2rad * (0.58750000 +   9050118 * tb ));
  1033.   a9 = SIN8( r2rad * (0.61043085 -  67718733 * tb ));
  1034.  
  1035.   dlm = 0.84 * a3 + 0.31 * a7 + 14.27 * a1 + 7.261  * a2 + 0.282 * a4 
  1036.     + 0.237 * a6;
  1037.   dpm = -2.1  * a3 - 2.076  * a2 - 0.840 * a4 - 0.593 * a6;
  1038.   dkm = 0.63 * a3 + 95.96 * a2 + 15.58 * a4 + 1.86 * a5;
  1039.   dls = -6.4  * a3 - 0.27 * a8 - 1.89  * a6 + 0.20 * a9;
  1040.   dgc = (-4.318 * c2 - 0.698 * c4) / 3600.0 / 360.0;    /* in revolutions */
  1041.   dgc = (1.000002708 + 139.978 * dgc);    /* in this form used later */
  1042.   man = DEGTORAD * (e->ma + ( dlm - dpm ) / 3600.0); 
  1043.     /* man with periodic and secular corr. */
  1044.   ms  = DEGTORAD * (el[EARTH].ma + dls / 3600.0);
  1045.   f   = DEGTORAD * (e->lg - e->kn + ( dlm - dkm ) / 3600.0);
  1046.   d   = DEGTORAD * (e->lg + 180 - el[EARTH].lg + (dlm - dls) / 3600.0);
  1047.  
  1048.   lk = lk1 = sk = sinp = nib = g1c = 0;
  1049.   i1corr = 1.0 - 6.8320E-8 * t;
  1050.   i2corr = dgc * dgc;    /* i2 occurs only as -2, 2 */
  1051.   for ( i = 0, mp = m45; i < NUM_MOON_CORR; i++, mp++ ) {
  1052.     /* arg = mp->i0 * man + mp->i1 * ms + mp->i2 * f + mp->i3 * d; */
  1053.     arg = mp->i0 * man;
  1054.     arg += mp->i3 * d;
  1055.     arg += mp->i2 * f;
  1056.     arg += mp->i1 * ms;
  1057.     sinarg = SIN8( arg );
  1058.     /* now apply corrections due to changes in constants;
  1059.        we correct only terms in l' (i1) and F (i2), not in l (i0), because
  1060.        the latter are < 0.05" 
  1061.        We don't apply corrections  for cos(arg), i.e. for parallax 
  1062.      */
  1063.     if (mp->i1 != 0) {    /* i1 can be -2, -1, 0, 1, 2 */
  1064.       sinarg *= i1corr;
  1065.       if  (mp->i1 == 2 || mp->i1 == -2) 
  1066.     sinarg *= i1corr;
  1067.     }
  1068.     if (mp->i2 != 0)     /* i2 can be -2, 0, 2 */
  1069.       sinarg *= i2corr;
  1070.     lk += mp->lng * sinarg;
  1071.     sk += mp->lat * sinarg;
  1072.     sinp += mp->par * COS8 (arg) ;
  1073.   }  /* for i */
  1074.  
  1075. # if MOON_TEST_CORR    /* optionally add more  lunar longitudes  */
  1076.   for ( m5p = m5; m5p->i0 != 99; m5p++ ) {    /* i0 = 99 is end mark */
  1077.     arg = m5p->i0 * man + m5p->i1 * ms + m5p->i2 * f + m5p->i3 * d;
  1078.     sinarg = SIN8( arg );
  1079.     lk1 += m5p->lng * sinarg;
  1080.   }  
  1081. # endif
  1082.  
  1083.   /*now compute some planetary terms in longitude, list i delta;
  1084.     we take all > 0.5" and neglect secular terms in the arguments. These
  1085.     produce phase errors > 10 degrees only after 3000 years.
  1086.   */
  1087.   dlid =  0.822 * SIN8 ( r2rad * (0.32480 - 0.0017125594 * t ));
  1088.   dlid += 0.307 * SIN8 ( r2rad * (0.14905 - 0.0034251187 * t ));
  1089.   dlid += 0.348 * SIN8 ( r2rad * (0.68266 - 0.0006873156 * t ));
  1090.   dlid += 0.662 * SIN8 ( r2rad * (0.65162 + 0.0365724168 * t ));
  1091.   dlid += 0.643 * SIN8 ( r2rad * (0.88098 - 0.0025069941 * t ));
  1092.   dlid += 1.137 * SIN8 ( r2rad * (0.85823 + 0.0364487270 * t ));
  1093.   dlid += 0.436 * SIN8 ( r2rad * (0.71892 + 0.0362179180 * t ));
  1094.   dlid += 0.327 * SIN8 ( r2rad * (0.97639 + 0.0001734910 * t ));
  1095.  
  1096.   *al = smod8360(e->lg + (dlm + lk + lk1 + dlid) / 3600.0); /* without nutation */
  1097.  
  1098.   /* solar Terms in latitude Nibeta */
  1099.   f_2d = f - 2.0 * d;
  1100.   f_4d = f - 4.0 * d;
  1101.   nib += -526.069 * SIN8(                   f_2d );
  1102.   nib +=   -3.352 * SIN8(                   f_4d );
  1103.   nib +=   44.297 * SIN8( man             + f_2d );
  1104.   nib +=   -6.000 * SIN8( man             + f_4d );
  1105.   nib +=   20.599 * SIN8(-man             + f    );
  1106.   nib +=  -30.598 * SIN8(-man             + f_2d );
  1107.   nib +=  -24.649 * SIN8(-2*man           + f    );
  1108.   nib +=   -2.000 * SIN8(-2*man           + f_2d );
  1109.   nib +=  -22.571 * SIN8(          ms     + f_2d ); 
  1110.   nib +=   10.985 * SIN8(         -ms     + f_2d ); 
  1111.  
  1112.   /* new gamma1C from 29 Jul 88, all terms > 0.4 " in table III, code 2 */
  1113.   g1c += -0.725 * COS8(                 d);
  1114.   g1c +=  0.601 * COS8(             2 * d);
  1115.   g1c +=  0.394 * COS8(             3 * d);
  1116.   g1c += -0.445 * COS8( man                   + 4 * d);
  1117.   g1c +=  0.455 * COS8( man                   + 1 * d);
  1118.   g1c +=  5.679 * COS8( 2 * man               - 2 * d);
  1119.   g1c += -1.300 * COS8( 3 * man                      );
  1120.   g1c += -1.302 * COS8(             ms               );
  1121.   g1c += -0.416 * COS8(             ms        - 4 * d);
  1122.   g1c += -0.740 * COS8(         2 * ms        - 2 * d);
  1123.   g1c +=  0.787 * COS8(     man +   ms        + 2 * d);
  1124.   g1c +=  0.461 * COS8(     man +   ms               );
  1125.   g1c +=  2.056 * COS8(     man +   ms        - 2 * d);
  1126.   g1c += -0.471 * COS8(     man +   ms        - 4 * d);
  1127.   g1c += -0.443 * COS8(    -man +   ms        + 2 * d);
  1128.   g1c +=  0.679 * COS8(    -man +   ms               );
  1129.   g1c += -1.540 * COS8(    -man +   ms        - 2 * d);
  1130.  
  1131.   s =  f + sk / 3600.0 * DEGTORAD;
  1132.   ca = 18519.7 + g1c;
  1133.   cb = -0.000336992 * ca * dgc * dgc * dgc; 
  1134.   cd = ca / 18519.7;
  1135. # ifdef MS_C
  1136.   /*
  1137.    * Microsoft C 5.0 runs out of heap space with this expression.
  1138.    */
  1139.   b = ca * SIN8( s ) * dgc;
  1140.   b += cb * SIN8( 3.0 * s );
  1141.   b += cd * nib;
  1142.   b = b / 3600.0;
  1143. # else
  1144.   b = ( ca * SIN8( s ) * dgc  + cb * SIN8( 3.0 * s ) + cd * nib ) / 3600.0;
  1145. # endif
  1146.     /* we neglect the planetary terms in latitude, code  4 in table III */
  1147.  
  1148.   sinp = ( sinp + 3422.451);    
  1149.   /* Improved lunar ephemeris and APAE until ca. 1970 had here
  1150.      3422.54 as constant of moon's sine parallax.
  1151.      The difference can be applied by direct addition of 0.089" to
  1152.      our parallax results.
  1153.  
  1154.      To get the radius in A.U. from the sine parallax,
  1155.      we use 1964 IAU value 8.794" for solar parallax.
  1156.      sinp is still in seconds of arc.
  1157.      To calculate moon parallax in " it would be:
  1158.      p = sinp ( 1  + sinp * sinp * 3.917405E-12) 
  1159.      based on the formula p = sinp + 1/6 sinp^3 
  1160.      and taking into account the conversion of " to radians.
  1161.      The  semidiameter of the moon is: (Expl.Suppl. 61, p 109)
  1162.      s = 0.0796 + 0.272446 * p  
  1163.   */ 
  1164.  
  1165.   *ar = 8.794 / sinp;
  1166.   *az = *ar * SIN8( DEGTORAD * b );
  1167.   return (OK);
  1168. }    /* end moon() */
  1169.  
  1170. /******************************************************************/
  1171. #ifndef ASTROLOG
  1172. REAL8 fnu(REAL8 t,REAL8 ex,REAL8 err )
  1173. #else
  1174. REAL8 fnu(t, ex, err)
  1175. REAL8 t;
  1176. REAL8 ex;
  1177. REAL8 err;
  1178. #endif
  1179.         /* solution of the kepler equation, return rad*/
  1180.         /* t = mean anomaly in degrees */
  1181.         /* ex = excentricity of orbit  */
  1182.         /* err = maximum error in degrees */
  1183. {
  1184.   REAL8 u0, delta;
  1185.   t *= DEGTORAD;
  1186.   u0 = t;
  1187.   err *= DEGTORAD;
  1188.   delta = 1;
  1189.   while ( ABS8( delta ) >= err ) {
  1190.     delta = ( t + ex * SIN8( u0 ) - u0 ) / ( 1 - ex * COS8( u0 ) );
  1191.     u0 += delta;    
  1192.   }
  1193.   return( u0 );  
  1194. }    /* end fnu() */
  1195.  
  1196. /************************************************************************
  1197. outer_hel()
  1198. Computes the position of Jupiter, Saturn, Uranus, Neptune, Pluto and
  1199. Chiron by reading our stored ephemeris in steps of 80 (!) days and
  1200. applying a high order interpolation to it. The interpolation errors are
  1201. less than 0.01" seconds or arc.
  1202. The stored ephemeris is  packed in a special format consisting of
  1203. 32 bit numbers; it has been created on the Astrodienst Unix system
  1204. by numerical integration with routines provided originally by Marc
  1205. Pottenger, USA, which we improved for better long term precision.
  1206. Because the Unix system uses a different byte order than the MSDOS
  1207. systems, the bytes must be reordered for MSDOS after reading from
  1208. the binary files. 
  1209.  
  1210. outer_hel() takes the same parameters as hel().
  1211. It returns the same type of values.
  1212.  
  1213. The access to the ephemeris files is done in the functions chi_file_posit()
  1214. and lrz_file_posit().
  1215. ****************************************************************************/
  1216. #ifndef ASTROLOG
  1217. int outer_hel( int planet, REAL8 jd_ad, REAL8 *al,  REAL8 *ar,  REAL8 *az, 
  1218.                     REAL8 *alp, REAL8 *arp, REAL8 *azp )
  1219. #else
  1220. int outer_hel(planet, jd_ad, al, ar, az, alp, arp, azp)
  1221. int planet;
  1222. REAL8 jd_ad;
  1223. REAL8 *al;
  1224. REAL8 *ar;
  1225. REAL8 *az;
  1226. REAL8 *alp;
  1227. REAL8 *arp;
  1228. REAL8 *azp;
  1229. #endif
  1230.     /* jd_ad Astrodienst relative Julian ephemeris time */
  1231. {
  1232.   static FILE *outerfp, *chironfp;
  1233.   static double last_j0_outer = HUGE8;
  1234.   static double last_j0_chiron = HUGE8;
  1235.   static long  icoord[6][5][3], chicoord[6][3];
  1236.   REAL8 j0, jd, jfrac;
  1237.   REAL8 l[6], r[6], z[6];
  1238.   int n, order, p;
  1239.   if (planet < JUPITER || planet > PLUTO && planet != CHIRON)
  1240.     return (ERR);
  1241.   jd = jd_ad + JUL_OFFSET;
  1242.   j0 = floor ( (jd - 0.5) / EPHE_STEP) * EPHE_STEP + 0.5;
  1243.   jfrac = (jd - j0) / EPHE_STEP;
  1244.   if (planet == CHIRON ) {
  1245.     if (last_j0_chiron != j0) {
  1246.       for ( n = 0; n < 6; n++) { /* read 6 days */
  1247.     jd = j0 + (n - 2) * EPHE_STEP;
  1248.     if (chi_file_posit (jd, &chironfp) != OK) return (ERR);
  1249.     fread (&chicoord[n][0], sizeof(long), 3, chironfp); 
  1250. #ifndef ASTROLOG
  1251.         longreorder (&chicoord[n][0], 3 * sizeof(long));
  1252. #else
  1253.         longreorder((UCHAR *)&chicoord[n][0], 3*4);
  1254. #endif
  1255.       }
  1256.       last_j0_chiron = j0;
  1257.     }
  1258.     for ( n = 0; n < 6; n++) {
  1259.       l[n] = chicoord[n][0] / DEG2MSEC;
  1260.       r[n] = chicoord[n][1] / AU2INT;
  1261.       z[n] = chicoord[n][2] / AU2INT;
  1262.     }     /* for n */
  1263.   } else {    /* an outerplanet */
  1264.     if (last_j0_outer != j0) { /* read all 5 planets for 6 steps */
  1265.       for ( n = 0; n < 6; n++) { 
  1266.     jd = j0 + (n - 2) * EPHE_STEP;
  1267.     if (lrz_file_posit (jd, &outerfp) != OK) return (ERR);
  1268.     fread (&icoord[n][0][0], sizeof(long), 15, outerfp); 
  1269. #ifndef ASTROLOG
  1270.         longreorder (&icoord[n][0][0], 15 * sizeof(long));
  1271. #else
  1272.         longreorder((UCHAR *)&icoord[n][0][0], 15*4);
  1273. #endif
  1274.       }
  1275.       last_j0_outer = j0;
  1276.     }
  1277.     p = planet - JUPITER;
  1278.     for ( n = 0; n < 6; n++) {
  1279.       l[n] = icoord[n][p][0] / DEG2MSEC;
  1280.       r[n] = icoord[n][p][1] / AU2INT;
  1281.       z[n] = icoord[n][p][2] / AU2INT;
  1282.     }     /* for n */
  1283.   }
  1284.   if  (planet > SATURN)
  1285.     order = 3;
  1286.   else
  1287.     order = 5;
  1288.   inpolq(2, order, jfrac, l, al, alp);
  1289.   *alp /= EPHE_STEP;
  1290.   inpolq(2, order, jfrac, r, ar, arp);
  1291.   *arp /= EPHE_STEP;
  1292.   inpolq(2, order, jfrac, z, az, azp);
  1293.   *azp /= EPHE_STEP;
  1294.   return OK;
  1295. }
  1296.  
  1297. /*****************************************************
  1298. quicker Everett interpolation, after Pottenger
  1299. version  9 Jul 1988    by Alois Treindl
  1300.  
  1301. return OK or ERR.
  1302. *****************************************************/
  1303. int inpolq(n,o,p,x,axu,adxu)
  1304. int n,     /* interpolate between x[n] and x[n-1], at argument n+p */
  1305.     o;    /* order of interpolation, maximum 5 */
  1306. double    p,    /* argument , intervall [0..1] */
  1307.     x[],    /* array of function values, x[n-o]..x[n+o] must exist */
  1308.     *axu,     /* pointer for storage of result */
  1309.     *adxu;    /* pointer for storage of dx/dt  */
  1310. {
  1311. static double    q,q2,q3,q4,q5,
  1312.       p2,p3,p4,p5,
  1313.     u,u0,u1,u2;
  1314. static double lastp = 9999;
  1315. double    dm2,dm1,d0,dp1,dp2,
  1316.     d2m1,d20,d2p1,d2p2,
  1317.     d30,d3p1,d3p2,
  1318.     d4p1,d4p2;
  1319. double offset = 0.0;
  1320.  
  1321. if (lastp != p) {
  1322.   q=1.0-p;
  1323.   q2 = q*q;
  1324.   q3 = (q+1.0)*q*(q-1.0)/6.0;    /* q - 1  over 3; u5 */
  1325.   p2 = p*p;
  1326.   p3 = (p+1.0)*p*(p-1.0)/6.0;    /* p - 1  over 3; u8 */
  1327.   u = (3.0*p2-1.0)/6.0;
  1328.   u0 = (3.0*q2-1.0)/6.0;
  1329.   q4 = q2*q2;        /* f5 */
  1330.   p4 = p2*p2;        /* f4 */
  1331.   u1 = (5.0*p4-15.0*p2+4.0)/120.0;    /* u1 */
  1332.   u2 = (5.0*q4-15.0*q2+4.0)/120.0;    /* u2 */
  1333.   q5 = q3*(q+2.0)*(q-2.0)/20.0;   /* q - 2  over 5; u6 */
  1334.   p5 = (p+2.0)*p3*(p-2.0)/20.0;    /* p - 2  over 5; u9 */
  1335.   lastp = p;
  1336. }
  1337. dm1 = x[n]   - x[n-1];
  1338. if (dm1 > 180.0) dm1 -= 360.0;
  1339. if (dm1 < -180.0) dm1 += 360.0;
  1340. d0  = x[n+1] - x[n];
  1341. if (d0 > 180.0) {
  1342.   d0 -= 360.0;
  1343.   offset = 360.0;
  1344. }
  1345. if (d0 < -180.0) {
  1346.   d0 += 360.0;
  1347.   offset = -360.0;
  1348. }
  1349. dp1 = x[n+2] - x[n+1];
  1350. if (dp1 > 180.0) dp1 -= 360.0;
  1351. if (dp1 < -180.0) dp1 += 360.0;
  1352. d20  = d0 - dm1;    /* f8 */
  1353. d2p1 = dp1 - d0;    /* f9 */
  1354.  
  1355. /* Everett interpolation 3rd order */
  1356. *axu = q*(x[n] + offset)   + q3*d20
  1357.      + p*x[n+1]  + p3*d2p1;
  1358. *adxu  =  d0 + u*d2p1  - u0*d20;
  1359. if ( o > 3 ) {    /* 5th order */
  1360.   dm2 = x[n-1] - x[n-2];
  1361.   if (dm2 > 180.0) dm2 -= 360.0;
  1362.   if (dm2 < -180.0) dm2 += 360.0;
  1363.   dp2 = x[n+3] - x[n+2];
  1364.   if (dp2 > 180.0) dp2 -= 360.0;
  1365.   if (dp2 < -180.0) dp2 += 360.0;
  1366.   d2m1 = dm1 - dm2;
  1367.   d2p2 = dp2 - dp1;
  1368.   d30  = d20 - d2m1;
  1369.   d3p1 = d2p1 - d20;
  1370.   d3p2 = d2p2 - d2p1;
  1371.   d4p1 = d3p1 - d30;    /* f7 */
  1372.   d4p2 = d3p2 - d3p1;    /* f */
  1373.   *axu  += p5*d4p2 + q5*d4p1;
  1374.   *adxu += u1*d4p2 - u2*d4p1;
  1375. }
  1376. return (OK);
  1377. }    /* end inpolq() */
  1378.  
  1379. /*********************************************************
  1380.   position lrz file at proper position for julian date jd; 
  1381.   Return OK or ERR.  Version for outer planets.
  1382.   The path where the ephemeris files are looked for is defined
  1383.   by ephe_path.
  1384. **********************************************************/
  1385. int lrz_file_posit (jd, lrzfpp)
  1386. double    jd;        /* full Julian day number, not Astrodienst relative */
  1387. FILE      **lrzfpp;    /* pointer to file pointer; this function
  1388.             opens or closes the ephemeris file, and caller
  1389.             should keep it open while using it. The caller
  1390.             should close it when he is finished with using
  1391.             the placalc() package. */
  1392. {
  1393.   int filenr;
  1394.   long posit, jlong;
  1395.   static char fname[80];
  1396.   static int open_lrznr = -10000;    /* local memory to remember whether
  1397.                 an already open file is the one with
  1398.                 the correct number for this date */
  1399. #ifndef ASTROLOG
  1400.   jlong = floor (jd);    
  1401.   filenr = jlong / EPHE_DAYS_PER_FILE;   
  1402. #else
  1403.   jlong = (long)floor (jd);    
  1404.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);   
  1405. #endif
  1406.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong) filenr--;
  1407.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1408.   posit = (posit / (int)  EPHE_STEP) * EPHE_OUTER_BSIZE;
  1409.   if (*lrzfpp == NULL || open_lrznr  != filenr) { /* no or wrong open file */
  1410.     open_lrznr = -10000;
  1411. #ifndef ASTROLOG
  1412.     if (filenr >= 0)
  1413.       sprintf (fname, "%s%s%s%d", ephe_path, DIR_GLUE, EPHE_OUTER, filenr);
  1414.     else
  1415.       sprintf (fname, "%s%s%sM%d", ephe_path, DIR_GLUE, EPHE_OUTER, -filenr);
  1416.     if (*lrzfpp == NULL)
  1417.       *lrzfpp = fopen (fname, OPEN_EPHE);    /* open for read */
  1418.     else
  1419.       *lrzfpp = freopen (fname, OPEN_EPHE, *lrzfpp);
  1420. #else
  1421.     sprintf(fname, "%s%s%d", EPHE_OUTER, filenr < 0 ? "M" : "", abs(filenr));
  1422.     if (*lrzfpp != NULL)
  1423.       fclose(*lrzfpp);
  1424.     *lrzfpp = OpenFile(fname, 2);
  1425. #endif /* ASTROLOG */
  1426.     if (*lrzfpp == NULL) {
  1427.       if (placalc_err_text != NULL)
  1428.     sprintf (placalc_err_text,"lrz_file_posit: file %s does not exist", fname);
  1429.       else
  1430.     fprintf (stderr,"lrz_file_posit: file %s does not exist\n", fname);
  1431.       return (ERR);    
  1432.     }
  1433.     open_lrznr = filenr;
  1434.   }
  1435.   if  (fseek (*lrzfpp, posit, 0) == 0)
  1436.     return (OK);
  1437.   if (placalc_err_text != NULL)
  1438.     sprintf (placalc_err_text,"lrz_file_posit: fseek error %s posit %ld", fname, posit);
  1439.   else
  1440.     fprintf (stderr,"lrz_file_posit: fseek error %s posit %ld\n", fname, posit);
  1441.   return (ERR);    /* this fseek error occurs only with incomplete files */
  1442. }    /* end lrz_file_posit */
  1443.  
  1444. /*********************************************************
  1445.   position chiron file at proper position for julian date jd; 
  1446.   Return OK or ERR.  Version for Chiron.
  1447.   Sister function to lrz_file_posit().
  1448. **********************************************************/
  1449. int chi_file_posit (jd, lrzfpp)
  1450. double    jd;    /* full Julian day number, not Astrodienst relative */
  1451. FILE      **lrzfpp;    /* pointer to file pointer; this function
  1452.             opens or closes the ephemeris file, and caller
  1453.             should keep it open while using it */
  1454. {
  1455.   int filenr;
  1456.   long posit, jlong;
  1457.   char fname[80];
  1458.   static int open_lrznr = -10000;    /* local memory to remember whether
  1459.                 an already open file is the one with
  1460.                 the correct number for this date */
  1461. #ifndef ASTROLOG
  1462.   jlong = floor (jd);    
  1463.   filenr = jlong / EPHE_DAYS_PER_FILE;  
  1464. #else
  1465.   jlong = (long)floor (jd);    
  1466.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);  
  1467. #endif 
  1468.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong) filenr--;
  1469.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1470.   posit = (posit / (int)  EPHE_STEP) * EPHE_CHIRON_BSIZE;
  1471.   if (*lrzfpp == NULL || open_lrznr  != filenr) { /* no or wrong open file */
  1472.     open_lrznr = -10000;
  1473. #ifndef ASTROLOG
  1474.     if (filenr >= 0)
  1475.       sprintf (fname, "%s%s%s%d", ephe_path, DIR_GLUE, EPHE_CHIRON, filenr);
  1476.     else
  1477.       sprintf (fname, "%s%s%sM%d", ephe_path, DIR_GLUE, EPHE_CHIRON, -filenr);
  1478.     if (*lrzfpp == NULL)
  1479.       *lrzfpp = fopen (fname, OPEN_EPHE);    /* open for read */
  1480.     else
  1481.       *lrzfpp = freopen (fname, OPEN_EPHE, *lrzfpp);    /* open for read */
  1482. #else
  1483.     sprintf(fname, "%s%s%d", EPHE_CHIRON, filenr < 0 ? "M" : "", abs(filenr));
  1484.     if (*lrzfpp != NULL)
  1485.       fclose(*lrzfpp);
  1486.     *lrzfpp = OpenFile(fname, 2);
  1487. #endif /* ASTROLOG */
  1488.     if (*lrzfpp == NULL) {
  1489.       if (placalc_err_text != NULL)
  1490.     sprintf (placalc_err_text,"chi_file_posit: file %s does not exist", fname);
  1491.       else
  1492.     fprintf (stderr,"chi_file_posit: file %s does not exist\n", fname);
  1493.       return (ERR);    
  1494.     }
  1495.     open_lrznr = filenr;
  1496.   }
  1497.   if  (fseek (*lrzfpp, posit, 0) == 0)
  1498.     return (OK);
  1499.   if (placalc_err_text != NULL)
  1500.     sprintf (placalc_err_text,"chi_file_posit: fseek error %s posit %ld", fname, posit);
  1501.   else
  1502.   fprintf (stderr,"chi_file_posit: fseek error %s posit %ld\n", fname, posit);
  1503.   return (ERR);    /* this fseek error occurs only with incomplete files */
  1504. }    /* end chi_file_posit */
  1505.  
  1506.  
  1507. #ifndef ASTROLOG
  1508. /***********************************************************************/
  1509. REAL8 fraction (REAL8 x)    /* positive fraction: 3.4 -> 0.4, -3.7 -> 0.7 */
  1510. {
  1511.   return (x - floor (x));
  1512. }
  1513.  
  1514. /***********************************************************
  1515. function sidtime (t): returns sidereal time at greenwich;
  1516. Parameters differ from ASYS version! after AESuppl. 1961, page 75
  1517. version 24-oct-87
  1518. ***********************************************************/
  1519. REAL8  sidtime (REAL8 jd_ad, REAL8 ecl, REAL8 nuta)
  1520.     /* jd_ad relative julian date */
  1521.         /* ecl, nuta  ecliptic and nutation of date, in degrees */
  1522. {
  1523.   REAL8 tj, sec, x;
  1524.   tj = (jd_ad + 18262.0) / 36525.0;    /* julian centuries from epoch 1900.0 */
  1525.   sec = 23925.836 + 8640184.542 * tj + 0.0929 * tj * tj;
  1526.   x = sec / 3600.0 / 24.0 + fraction (jd_ad - 0.5) 
  1527.       + nuta / 360.0 * COS8 (DEGTORAD * ecl);    
  1528.   return fraction(x) * 24.0;   
  1529. }
  1530. #endif
  1531.  
  1532. /******************************************************************/
  1533. #ifndef ASTROLOG
  1534. REAL8 smod8360( REAL8 x )  /* x MOD 360.0, so that x in 0..<360 */
  1535. #else
  1536. REAL8 smod8360(x)
  1537. REAL8 x;
  1538. #endif
  1539. {
  1540.   while ( x >= 360.0 ) x -= 360.0;
  1541.   while ( x < 0.0) x += 360.0;
  1542.   return  x;
  1543. }    /* smod8360 */    
  1544.  
  1545. /******************************************************************/
  1546. #ifndef ASTROLOG
  1547. REAL8 mod8360( REAL8  x )           /* x MOD 360.0, so that x in 0..360 */
  1548. #else
  1549. REAL8 mod8360(x)
  1550. REAL8 x;
  1551. #endif
  1552. {
  1553.   if ( x >= 0 && x < 360.0 ) return( x );
  1554.   return( x - 360.0 * floor ( x / 360.0 ) );
  1555. }    /* mod8360 */    
  1556.  
  1557. /******************************************************************/
  1558. #ifndef ASTROLOG
  1559. REAL8 diff8360 (REAL8 a, REAL8 b) 
  1560. #else
  1561. REAL8 diff8360(a, b) 
  1562. REAL8 a;
  1563. REAL8 b;
  1564. #endif
  1565.      /* a - b on a 360 degree circle, result -180..180*/
  1566. {
  1567.   REAL8 d;
  1568.   d = a - b;
  1569.   if ( d >= 180.0 ) return( d - 360.0 );
  1570.   if ( d < -180.0 ) return( d + 360.0 );
  1571.   return( d );
  1572. }    /* diff8360 */
  1573.   
  1574. /******************************************************************/
  1575. #ifndef ASTROLOG
  1576. REAL8 test_near_zero(REAL8 x )
  1577. #else
  1578. REAL8 test_near_zero(x)
  1579. REAL8 x;
  1580. #endif
  1581. {
  1582.   if ( ABS8( x ) >= NEAR_ZERO ) return( x );
  1583.   if ( x < 0 ) return( -NEAR_ZERO ); 
  1584.            return(  NEAR_ZERO );
  1585. }    /* test_near_zero */
  1586.  
  1587. /********************************************************************/
  1588. #ifndef ASTROLOG
  1589. longreorder (UCHAR *p, int n) 
  1590. #else
  1591. void longreorder(p, n) 
  1592. UCHAR *p;
  1593. int n;
  1594. #endif
  1595.                /* p points to memory filled with long values; for
  1596.                            each of the values the seqeuence of the four bytes
  1597.                            has to be reversed, to translate HP-UX and VAX
  1598.                ordering to MSDOS/Turboc ordering */
  1599. {
  1600.   int i;
  1601.   unsigned char c0, c1, c2, c3;
  1602. #ifdef ASTROLOG
  1603.   static int orderinit = 0;
  1604.   unsigned short test;
  1605.  
  1606.   if (!orderinit) {
  1607.     test = 0x0001;
  1608.     orderinit = (*(unsigned char *)(&test)) ? 1 : -1;
  1609.   }
  1610.   if (orderinit < 0)
  1611.     return;
  1612. #endif
  1613.   for (i = 0; i < n; i += 4, p += 4) {
  1614.     c0 = *p;
  1615.     c1 = *(p + 1);
  1616.     c2 = *(p + 2);
  1617.     c3 = *(p + 3);
  1618.     *p = c3;
  1619.     *(p + 1) = c2;
  1620.     *(p + 2) = c1;
  1621.     *(p + 3) = c0;
  1622.   }
  1623. }
  1624.  
  1625. #ifndef ASTROLOG
  1626. /*
  1627.  * get the planet index for an AFL letter
  1628.  * returns -1 if the letter does not correspond to a planet.
  1629.  */
  1630. int afl2planet(int afl)
  1631. {
  1632.   int p;
  1633.   switch (afl) {
  1634.     case AFL_SUN : p = SUN; break;
  1635.     case AFL_MON : p = MOON; break;
  1636.     case AFL_MER : p = MERCURY; break;
  1637.     case AFL_VEN : p = VENUS; break;
  1638.     case AFL_MAR : p = MARS; break;
  1639.     case AFL_JUP : p = JUPITER; break;
  1640.     case AFL_SAT : p = SATURN; break;
  1641.     case AFL_URA : p = URANUS; break;
  1642.     case AFL_NEP : p = NEPTUNE; break;
  1643.     case AFL_PLU : p = PLUTO; break;
  1644.     case AFL_MNODE :  p = MEAN_NODE; break;
  1645.     case AFL_TNODE :  p = TRUE_NODE; break;
  1646.     case AFL_CHI : p = CHIRON; break;
  1647.     case AFL_LIL : p = LILITH; break;
  1648.     case AFL_AC  : p = AC; break;
  1649.     case AFL_MC  : p = MC; break;
  1650.     default : p = -1; break;
  1651.   }  
  1652.   return p;
  1653. }
  1654.  
  1655. /*******************************************************************
  1656. precession direction cosines from equator of date to 1950.0
  1657. correct non-symmetric version from  Suppl. 1961, p 30 
  1658. and AA (Astr.Almanach) 1983, p B18
  1659. (pre-1984 precession must be used for transformation of Vol.22 data!)
  1660. ********************************************************************/
  1661. void getdc50(double j, double dceqdt50[3][3])
  1662. {
  1663.   double t,t2,t3;
  1664.   double zeta, zet, th, sinz0, cosz0, sinz, cosz, sinth, costh;
  1665.   t=(j-2433282.423)/36524.22;
  1666.   t2=t*t;
  1667.   t3=t2*t;
  1668.   zeta = (0.6402633 * t + 0.0000839 * t2 + 0.0000050 * t3) * DEGTORAD;
  1669.   zet  = zeta + 0.0002197 * t2 * DEGTORAD;
  1670.   th   = (0.5567376 * t - 0.0001183 * t2 - 0.0000117 * t3) * DEGTORAD;
  1671.   cosz0 = cos(zeta);
  1672.   sinz0 = sin(zeta);
  1673.   cosz  = cos(zet);
  1674.   sinz  = sin(zet);
  1675.   costh = cos(th);
  1676.   sinth = sin(th);
  1677.   dceqdt50[0][0] = cosz0 * costh * cosz - sinz0 * sinz;    /* Xx */
  1678.   dceqdt50[1][0] = -sinz0 * costh * cosz - cosz0 * sinz;    /* Yx */
  1679.   dceqdt50[2][0] = -sinth * cosz;                /* Zx */
  1680.   dceqdt50[0][1] = cosz0 * costh * sinz + sinz0 * cosz;    /* Xy */
  1681.   dceqdt50[1][1] = -sinz0 * costh * sinz + cosz0 * cosz;    /* Yy */
  1682.   dceqdt50[2][1] = -sinth * sinz;                /* Zy */
  1683.   dceqdt50[0][2] = cosz0 * sinth;                /* Xz */
  1684.   dceqdt50[1][2] = -sinz0 * sinth;            /* Yz */
  1685.   dceqdt50[2][2] = costh;                    /* Zz */
  1686. }
  1687.  
  1688. void to_mean_ekl (double jd, double xyz[], double lrz[])
  1689. /*
  1690.  * jd =  absolute julian day 
  1691.  * xyz[0..2] array with x, y, z equator 1950.0 
  1692.  * Transform equatorial coordinates 1950.0
  1693.  * to ecliptic coordinates mean equinox of date.
  1694.  * Return values are stored in lrz[0..2]
  1695.  * as lrz[0] = mean longitude, lrz[1] = radius, lrz[2] = r * sin(lat).
  1696.  *
  1697.  * This function is not used within placalc itself and can be removed;
  1698.  * it was used to transform the coordinates from the numerical integration
  1699.  * program into the format as stored in the ephemeris files.
  1700.  */
  1701. {
  1702.   double ex, ey, ez, ex0, ey0, ez0, ix, iy, iz, il, ir;
  1703.   double cosobl, sinobl;
  1704.   double ti, tj1, tj2;
  1705.   double dceqdt50[3][3];
  1706.   getdc50(jd, dceqdt50);    /* calc precession matrix to equator of date */
  1707.   ti = (jd - 2415020) / 36525.0;    /* julian centuries from 1900 */
  1708.   tj1 = ti / 3600.0;    
  1709.   tj2 = ti * tj1;
  1710.   /* should agree with what is in ekld[], see helconst.c */
  1711.   meanekl = 23.452294  - 46.845 * tj1 -0.0059 * tj2 + 0.00181 * tj2 * ti;
  1712.   ex0 = xyz[0];
  1713.   ey0 = xyz[1];
  1714.   ez0 = xyz[2];
  1715.   ex=dceqdt50[0][0]*ex0+dceqdt50[1][0]*ey0+dceqdt50[2][0]*ez0;
  1716.   ey=dceqdt50[0][1]*ex0+dceqdt50[1][1]*ey0+dceqdt50[2][1]*ez0;
  1717.   ez=dceqdt50[0][2]*ex0+dceqdt50[1][2]*ey0+dceqdt50[2][2]*ez0;
  1718.   /* now we have equator of  date and go to mean ekl of date */
  1719.   cosobl = cos(meanekl * DEGTORAD);
  1720.   sinobl = sin(meanekl * DEGTORAD);
  1721.   ix = ex;
  1722.   iy = ey * cosobl + ez * sinobl;
  1723.   iz = -ey * sinobl + ez * cosobl;
  1724.   /* convert xyz to longitude = il and radius = ir */
  1725.   ir = sqrt(ix * ix + iy * iy + iz * iz);
  1726.   il = atan2(iy, ix) * RADTODEG;    /* returns range -pi .. pi */
  1727.   if ( il < 0) il += 360.0;
  1728.   lrz[0] = il;
  1729.   lrz[1] = ir;
  1730.   lrz[2] = iz;
  1731. }
  1732. #endif /* ASTROLOG */
  1733. #endif /* PLACALC */
  1734. #ifdef ASTROLOG
  1735. /* End contents of placalc.c */
  1736. #endif
  1737.