

/*
                VSOP87 Planetary Theory for JavaScript

                            by John Walker
                       http://www.fourmilab.ch/

                This program is in the public domain.

    Note: this file does not contain the full VSOP87 theory
    which includes terms for all of the planets excluding
    Pluto.  Instead, this file suppplies terms only for Mercury
    and the Sun, which the only ones required for the Mercury
    elongation pages which include it.  Including all the
    unused terms for the other planets would balloon this
    file well beyond its already substantial size and needlessly
    waste bandwidth and increase download time.

*/

//  VSOP87 periodic terms for Mercury and Earth

var MercuryTermL0 = new Array(
    4.40250710144, 0, 0,
    0.40989414976, 1.48302034194, 26087.9031415742,
    0.05046294199, 4.4778548954, 52175.8062831484,
    0.00855346843, 1.16520322351, 78263.7094247226,
    0.00165590362, 4.11969163181, 104351.612566297,
    0.00034561897, 0.77930765817, 130439.515707871,
    7.583476e-005, 3.7134840051, 156527.418849445,
    3.55974e-005, 1.51202669419, 1109.3785520934,
    1.803463e-005, 4.1033317841, 5661.3320491522,
    1.726012e-005, 0.35832239908, 182615.321991019,
    1.589923e-005, 2.99510417815, 25028.521211385,
    1.364682e-005, 4.59918318745, 27197.2816936676,
    1.017332e-005, 0.8803143904, 31749.2351907264,
    7.14182e-006, 1.54144865265, 24978.5245894808,
    6.43759e-006, 5.30266110787, 21535.9496445154,
    4.51137e-006, 6.04989275289, 51116.4243529592,
    4.042e-006, 3.28228847025, 208703.225132594,
    3.52441e-006, 5.24156297101, 20426.571092422,
    3.45212e-006, 2.79211901539, 15874.6175953632,
    3.43313e-006, 5.76531885335, 955.5997416086,
    3.39214e-006, 5.86327765, 25558.2121764796,
    3.25335e-006, 1.3367433478, 53285.1848352418,
    2.72947e-006, 2.49451163975, 529.6909650946,
    2.64336e-006, 3.91705094013, 57837.1383323006,
    2.59587e-006, 0.98732428184, 4551.9534970588,
    2.38793e-006, 0.11343953378, 1059.3819301892,
    2.3483e-006, 0.266721189, 11322.6640983044,
    2.16645e-006, 0.65987207348, 13521.7514415914,
    2.08995e-006, 2.09178234008, 47623.8527860896,
    1.83359e-006, 2.62878670784, 27043.5028831828,
    1.81629e-006, 2.43413502466, 25661.3049506982,
    1.75965e-006, 4.53636829858, 51066.427731055,
    1.72643e-006, 2.45200164173, 24498.8302462904,
    1.42316e-006, 3.36003948842, 37410.5672398786,
    1.37942e-006, 0.29098447849, 10213.285546211,
    1.25219e-006, 3.72079804425, 39609.6545831656,
    1.18233e-006, 2.78149786369, 77204.3274945334,
    1.06422e-006, 4.20572116254, 19804.8272915828
);

var MercuryTermL1 = new Array(
    26088.1470622275, 0, 0,
    0.01126007832, 6.21703970996, 26087.9031415742,
    0.00303471395, 3.05565472363, 52175.8062831484,
    0.00080538452, 6.10454743366, 78263.7094247226,
    0.00021245035, 2.83531934452, 104351.612566297,
    5.592094e-005, 5.82675673328, 130439.515707871,
    1.472233e-005, 2.51845458395, 156527.418849445,
    3.88318e-006, 5.48039225891, 182615.321991019,
    3.52244e-006, 3.05238094403, 1109.3785520934,
    1.02743e-006, 2.14879173777, 208703.225132594,
    9.354e-007, 6.11791163931, 27197.2816936676,
    9.0579e-007, 0.00045481669, 24978.5245894808,
    5.1941e-007, 5.62107554052, 5661.3320491522,
    4.437e-007, 4.57348500464, 25028.521211385,
    2.807e-007, 3.04195430989, 51066.427731055,
    2.7295e-007, 5.09210138837, 234791.128274168
);

var MercuryTermL2 = new Array(
    0.00053049845, 0, 0,
    0.00016903658, 4.69072300649, 26087.9031415742,
    7.396711e-005, 1.34735624669, 52175.8062831484,
    3.018297e-005, 4.45643539705, 78263.7094247226,
    1.107419e-005, 1.26226537554, 104351.612566297,
    3.78173e-006, 4.319980559, 130439.515707871,
    1.22998e-006, 1.06868541052, 156527.418849445,
    3.8663e-007, 4.08011610182, 182615.321991019,
    1.4898e-007, 4.6334308581, 1109.3785520934,
    1.1861e-007, 0.79187646439, 208703.225132594
);

var MercuryTermL3 = new Array(
    1.88077e-006, 0.03466830117, 52175.8062831484,
    1.42152e-006, 3.125054526, 26087.9031415742,
    9.6877e-007, 3.00378171915, 78263.7094247226,
    4.3669e-007, 6.01867965826, 104351.612566297,
    3.5395e-007, 0, 0,
    1.8045e-007, 2.77538373991, 130439.515707871,
    6.971e-008, 5.81808665742, 156527.418849445,
    2.556e-008, 2.57014364454, 182615.321991019
);

var MercuryTermL4 = new Array(
    1.14078e-006, 3.14159265359, 0,
    3.247e-008, 2.02848007619, 26087.9031415742,
    1.914e-008, 1.41731803758, 78263.7094247226,
    1.727e-008, 4.50137643801, 52175.8062831484,
    1.237e-008, 4.49970181057, 104351.612566297,
    6.45e-009, 1.26591776986, 130439.515707871
);

var MercuryTermL5 = new Array(
    8.77e-009, 3.14159265359, 0
);

var MercuryTermB0 = new Array(
    0.11737528962, 1.98357498767, 26087.9031415742,
    0.02388076996, 5.03738959685, 52175.8062831484,
    0.01222839532, 3.14159265359, 0,
    0.0054325181, 1.79644363963, 78263.7094247226,
    0.0012977877, 4.83232503961, 104351.612566297,
    0.00031866927, 1.58088495667, 130439.515707871,
    7.963301e-005, 4.60972126348, 156527.418849445,
    2.014189e-005, 1.35324164694, 182615.321991019,
    5.13953e-006, 4.37835409309, 208703.225132594,
    2.08584e-006, 2.02020294153, 24978.5245894808,
    2.07674e-006, 4.91772564073, 27197.2816936676,
    1.32013e-006, 1.11908492283, 234791.128274168,
    1.21395e-006, 1.81271752059, 53285.1848352418,
    1.00454e-006, 5.65684734206, 20426.571092422
);

var MercuryTermB1 = new Array(
    0.00429151362, 3.50169780393, 26087.9031415742,
    0.00146233668, 3.14159265359, 0,
    0.00022675295, 0.0151536688, 52175.8062831484,
    0.00010894981, 0.48540174006, 78263.7094247226,
    6.353462e-005, 3.42943919982, 104351.612566297,
    2.495743e-005, 0.16051210665, 130439.515707871,
    8.59585e-006, 3.18452433647, 156527.418849445,
    2.77503e-006, 6.21020774184, 182615.321991019,
    8.6233e-007, 2.95244391822, 208703.225132594,
    2.7696e-007, 0.29068938889, 27197.2816936676,
    2.6133e-007, 5.97708962692, 234791.128274168
);

var MercuryTermB2 = new Array(
    0.00011830934, 4.79065585784, 26087.9031415742,
    1.913516e-005, 0, 0,
    1.044801e-005, 1.21216540536, 52175.8062831484,
    2.66213e-006, 4.43418336532, 78263.7094247226,
    1.7028e-006, 1.62255638714, 104351.612566297,
    9.63e-007, 4.80023692017, 130439.515707871,
    4.4692e-007, 1.60758267772, 156527.418849445,
    1.8316e-007, 4.66904655377, 182615.321991019,
    6.927e-008, 1.4340488893, 208703.225132594
);

var MercuryTermB3 = new Array(
    2.35423e-006, 0.35387524604, 26087.9031415742,
    1.60537e-006, 0, 0,
    1.8904e-007, 4.36275460261, 52175.8062831484,
    6.376e-008, 2.50715381439, 78263.7094247226,
    4.58e-008, 6.14257817571, 104351.612566297,
    3.061e-008, 3.12497552681, 130439.515707871,
    1.732e-008, 6.26642412058, 156527.418849445
);

var MercuryTermB4 = new Array(
    4.276e-008, 1.74579932115, 26087.9031415742,
    1.023e-008, 3.14159265359, 0
);

var MercuryTermR0 = new Array(
    0.39528271652, 0, 0,
    0.07834131817, 6.19233722599, 26087.9031415742,
    0.00795525557, 2.95989690096, 52175.8062831484,
    0.00121281763, 6.01064153805, 78263.7094247226,
    0.00021921969, 2.77820093975, 104351.612566297,
    4.354065e-005, 5.82894543257, 130439.515707871,
    9.18228e-006, 2.59650562598, 156527.418849445,
    2.89955e-006, 1.42441936951, 25028.521211385,
    2.60033e-006, 3.02817753482, 27197.2816936676,
    2.01855e-006, 5.6472504035, 182615.321991019,
    2.01499e-006, 5.59227724202, 31749.2351907264,
    1.4198e-006, 6.25264202645, 24978.5245894808,
    1.00144e-006, 3.73435608689, 21535.9496445154
);

var MercuryTermR1 = new Array(
    0.00217347739, 4.65617158663, 26087.9031415742,
    0.00044141826, 1.42385543975, 52175.8062831484,
    0.00010094479, 4.47466326316, 78263.7094247226,
    2.432804e-005, 1.24226083435, 104351.612566297,
    1.624367e-005, 0, 0,
    6.03996e-006, 4.29303116561, 130439.515707871,
    1.52851e-006, 1.0606077981, 156527.418849445,
    3.9202e-007, 4.11136751416, 182615.321991019
);

var MercuryTermR2 = new Array(
    3.117867e-005, 3.08231840296, 26087.9031415742,
    1.245396e-005, 6.15183317423, 52175.8062831484,
    4.24822e-006, 2.9258335296, 78263.7094247226,
    1.3613e-006, 5.97983925842, 104351.612566297,
    4.2175e-007, 2.74936980629, 130439.515707871,
    2.1759e-007, 3.14159265359, 0,
    1.2793e-007, 5.80143162209, 156527.418849445
);

var MercuryTermR3 = new Array(
    3.2676e-007, 1.67971635359, 26087.9031415742,
    2.4166e-007, 4.63403168997, 52175.8062831484,
    1.2133e-007, 1.38983781545, 78263.7094247226,
    5.14e-008, 4.4391538693, 104351.612566297,
    1.981e-008, 1.20733880274, 130439.515707871
);

var MercuryTerms = new Array(
    MercuryTermL0, 38,
    MercuryTermL1, 16,
    MercuryTermL2, 10,
    MercuryTermL3, 8,
    MercuryTermL4, 6,
    MercuryTermL5, 1,
    MercuryTermB0, 14,
    MercuryTermB1, 11,
    MercuryTermB2, 9,
    MercuryTermB3, 7,
    MercuryTermB4, 2,
    null, 0,
    MercuryTermR0, 13,
    MercuryTermR1, 8,
    MercuryTermR2, 7,
    MercuryTermR3, 5,
    null, 0,
    null, 0
);

/*  Earth  */

var EarthTermL0 = new Array(
    1.75347045673, 0, 0,
    0.03341656456, 4.66925680417, 6283.0758499914,
    0.00034894275, 4.62610241759, 12566.1516999828,
    3.497056e-005, 2.74411800971, 5753.3848848968,
    3.417571e-005, 2.82886579606, 3.523118349,
    3.135896e-005, 3.62767041758, 77713.7714681205,
    2.676218e-005, 4.41808351397, 7860.4193924392,
    2.342687e-005, 6.13516237631, 3930.2096962196,
    1.324292e-005, 0.74246356352, 11506.7697697936,
    1.273166e-005, 2.03709655772, 529.6909650946,
    1.199167e-005, 1.10962944315, 1577.3435424478,
    9.9025e-006, 5.23268129594, 5884.9268465832,
    9.01855e-006, 2.04505443513, 26.2983197998,
    8.57223e-006, 3.50849156957, 398.1490034082,
    7.79786e-006, 1.17882652114, 5223.6939198022,
    7.53141e-006, 2.53339053818, 5507.5532386674,
    5.05264e-006, 4.58292563052, 18849.2275499742,
    4.92379e-006, 4.20506639861, 775.522611324,
    3.56655e-006, 2.91954116867, 0.0673103028,
    3.17087e-006, 5.84901952218, 11790.6290886588,
    2.84125e-006, 1.89869034186, 796.2980068164,
    2.71039e-006, 0.31488607649, 10977.078804699,
    2.4281e-006, 0.34481140906, 5486.777843175,
    2.0616e-006, 4.80646606059, 2544.3144198834,
    2.05385e-006, 1.86947813692, 5573.1428014331,
    2.02261e-006, 2.45767795458, 6069.7767545534,
    1.55516e-006, 0.83306073807, 213.299095438,
    1.32212e-006, 3.41118275555, 2942.4634232916,
    1.26184e-006, 1.0830263021, 20.7753954924,
    1.15132e-006, 0.64544911683, 0.9803210682,
    1.02851e-006, 0.63599846727, 4694.0029547076,
    1.01895e-006, 0.97569221824, 15720.8387848784,
    1.01724e-006, 4.26679821365, 7.1135470008,
    9.9206e-007, 6.20992940258, 2146.1654164752,
    9.7607e-007, 0.6810127227, 155.4203994342,
    8.5803e-007, 5.98322631256, 161000.685737674,
    8.5128e-007, 1.29870743025, 6275.9623029906,
    8.4711e-007, 3.67080093025, 71430.6956181291,
    7.9637e-007, 1.807913307, 17260.1546546904,
    7.8756e-007, 3.03698313141, 12036.4607348882,
    7.4651e-007, 1.75508916159, 5088.6288397668,
    7.3874e-007, 3.50319443167, 3154.6870848956,
    7.3547e-007, 4.67926565481, 801.8209311238,
    6.9627e-007, 0.83297596966, 9437.762934887,
    6.2449e-007, 3.97763880587, 8827.3902698748,
    6.1148e-007, 1.81839811024, 7084.8967811152,
    5.6963e-007, 2.78430398043, 6286.5989683404,
    5.6116e-007, 4.38694880779, 14143.4952424306,
    5.5577e-007, 3.47006009062, 6279.5527316424,
    5.1992e-007, 0.18914945834, 12139.5535091068,
    5.1605e-007, 1.33282746983, 1748.016413067,
    5.1145e-007, 0.28306864501, 5856.4776591154,
    4.9e-007, 0.48735065033, 1194.4470102246,
    4.1036e-007, 5.36817351402, 8429.2412664666,
    4.0938e-007, 2.39850881707, 19651.048481098,
    3.92e-007, 6.16832995016, 10447.3878396044,
    3.677e-007, 6.04133859347, 10213.285546211,
    3.6596e-007, 2.56955238628, 1059.3819301892,
    3.5954e-007, 1.70876111898, 2352.8661537718,
    3.5566e-007, 1.77597314691, 6812.766815086,
    3.3291e-007, 0.59309499459, 17789.845619785,
    3.0412e-007, 0.44294464135, 83996.8473181119,
    3.0047e-007, 2.73975123935, 1349.8674096588,
    2.5352e-007, 3.16470953405, 4690.4798363586
);

var EarthTermL1 = new Array(
    6283.31966747491, 0, 0,
    0.00206058863, 2.67823455584, 6283.0758499914,
    4.30343e-005, 2.63512650414, 12566.1516999828,
    4.25264e-006, 1.59046980729, 3.523118349,
    1.19261e-006, 5.79557487799, 26.2983197998,
    1.08977e-006, 2.96618001993, 1577.3435424478,
    9.3478e-007, 2.59212835365, 18849.2275499742,
    7.2122e-007, 1.13846158196, 529.6909650946,
    6.7768e-007, 1.87472304791, 398.1490034082,
    6.7327e-007, 4.40918235168, 5507.5532386674,
    5.9027e-007, 2.8879703846, 5223.6939198022,
    5.5976e-007, 2.17471680261, 155.4203994342,
    4.5407e-007, 0.39803079805, 796.2980068164,
    3.6369e-007, 0.46624739835, 775.522611324,
    2.8958e-007, 2.64707383882, 7.1135470008,
    2.0844e-007, 5.34138275149, 0.9803210682,
    1.9097e-007, 1.84628332577, 5486.777843175,
    1.8508e-007, 4.96855124577, 213.299095438,
    1.7293e-007, 2.99116864949, 6275.9623029906,
    1.6233e-007, 0.03216483047, 2544.3144198834,
    1.5832e-007, 1.43049285325, 2146.1654164752,
    1.4615e-007, 1.20532366323, 10977.078804699,
    1.2461e-007, 2.83432285512, 1748.016413067,
    1.1877e-007, 3.25804815607, 5088.6288397668,
    1.1808e-007, 5.2737979048, 1194.4470102246,
    1.1514e-007, 2.07502418155, 4694.0029547076,
    1.0641e-007, 0.76614199202, 553.5694028424,
    9.969e-008, 1.30262991097, 6286.5989683404,
    9.721e-008, 4.23925472239, 1349.8674096588,
    9.452e-008, 2.69957062864, 242.728603974,
    8.577e-008, 5.64475868067, 951.7184062506,
    7.576e-008, 5.30062664886, 2352.8661537718,
    6.385e-008, 2.65033984967, 9437.762934887,
    6.101e-008, 4.66632584188, 4690.4798363586
);

var EarthTermL2 = new Array(
    0.0005291887, 0, 0,
    8.719837e-005, 1.07209665242, 6283.0758499914,
    3.09125e-006, 0.86728818832, 12566.1516999828,
    2.7339e-007, 0.05297871691, 3.523118349,
    1.6334e-007, 5.18826691036, 26.2983197998,
    1.5752e-007, 3.6845788943, 155.4203994342,
    9.541e-008, 0.75742297675, 18849.2275499742,
    8.937e-008, 2.05705419118, 77713.7714681205,
    6.952e-008, 0.8267330541, 775.522611324,
    5.064e-008, 4.66284525271, 1577.3435424478,
    4.061e-008, 1.03057162962, 7.1135470008,
    3.81e-008, 3.4405080349, 5573.1428014331,
    3.463e-008, 5.14074632811, 796.2980068164,
    3.169e-008, 6.05291851171, 5507.5532386674,
    3.02e-008, 1.19246506441, 242.728603974,
    2.886e-008, 6.11652627155, 529.6909650946,
    2.714e-008, 0.30637881025, 398.1490034082,
    2.538e-008, 2.27992810679, 553.5694028424,
    2.371e-008, 4.38118838167, 5223.6939198022,
    2.079e-008, 3.75435330484, 0.9803210682
);

var EarthTermL3 = new Array(
    2.89226e-006, 5.84384198723, 6283.0758499914,
    3.4955e-007, 0, 0,
    1.6819e-007, 5.48766912348, 12566.1516999828,
    2.962e-008, 5.19577265202, 155.4203994342,
    1.288e-008, 4.72200252235, 3.523118349,
    7.14e-009, 5.30045809128, 18849.2275499742,
    6.35e-009, 5.96925937141, 242.728603974
);

var EarthTermL4 = new Array(
    1.14084e-006, 3.14159265359, 0,
    7.717e-008, 4.13446589358, 6283.0758499914,
    7.65e-009, 3.83803776214, 12566.1516999828
);

var EarthTermL5 = new Array(
    8.78e-009, 3.14159265359, 0
);

var EarthTermB0 = new Array(
    2.7962e-006, 3.19870156017, 84334.6615813083,
    1.01643e-006, 5.42248619256, 5507.5532386674,
    8.0445e-007, 3.88013204458, 5223.6939198022,
    4.3806e-007, 3.70444689758, 2352.8661537718,
    3.1933e-007, 4.00026369781, 1577.3435424478
);

var EarthTermB1 = new Array(
    9.03e-008, 3.8972906189, 5507.5532386674,
    6.177e-008, 1.73038850355, 5223.6939198022
);

var EarthTermR0 = new Array(
    1.00013988799, 0, 0,
    0.01670699626, 3.09846350771, 6283.0758499914,
    0.00013956023, 3.0552460962, 12566.1516999828,
    3.08372e-005, 5.19846674381, 77713.7714681205,
    1.628461e-005, 1.17387749012, 5753.3848848968,
    1.575568e-005, 2.84685245825, 7860.4193924392,
    9.24799e-006, 5.45292234084, 11506.7697697936,
    5.42444e-006, 4.56409149777, 3930.2096962196,
    4.7211e-006, 3.66100022149, 5884.9268465832,
    3.45983e-006, 0.96368617687, 5507.5532386674,
    3.2878e-006, 5.89983646482, 5223.6939198022,
    3.06784e-006, 0.29867139512, 5573.1428014331,
    2.43189e-006, 4.27349536153, 11790.6290886588,
    2.11829e-006, 5.84714540314, 1577.3435424478,
    1.85752e-006, 5.02194447178, 10977.078804699,
    1.74844e-006, 3.01193636534, 18849.2275499742,
    1.09835e-006, 5.05510636285, 5486.777843175,
    9.8316e-007, 0.88681311277, 6069.7767545534,
    8.6499e-007, 5.68959778254, 15720.8387848784,
    8.5825e-007, 1.27083733351, 161000.685737674,
    6.4903e-007, 0.27250613787, 17260.1546546904,
    6.2916e-007, 0.92177108832, 529.6909650946,
    5.7056e-007, 2.01374292014, 83996.8473181119,
    5.5736e-007, 5.24159798933, 71430.6956181291,
    4.9384e-007, 3.24501240359, 2544.3144198834,
    4.6963e-007, 2.57805070386, 775.522611324,
    4.4661e-007, 5.53715807302, 9437.762934887,
    4.2515e-007, 6.01110242003, 6275.9623029906,
    3.8968e-007, 5.36071738169, 4694.0029547076,
    3.8245e-007, 2.39255343974, 8827.3902698748,
    3.749e-007, 0.82952922332, 19651.048481098,
    3.6957e-007, 4.90107591914, 12139.5535091068,
    3.566e-007, 1.67468058995, 12036.4607348882,
    3.4537e-007, 1.84270693282, 2942.4634232916,
    3.3193e-007, 0.24370300098, 7084.8967811152,
    3.1921e-007, 0.18368229781, 5088.6288397668,
    3.1846e-007, 1.77775642085, 398.1490034082,
    2.8464e-007, 1.21344868176, 6286.5989683404,
    2.7793e-007, 1.89934330904, 6279.5527316424,
    2.6275e-007, 4.58896850401, 10447.3878396044
);

var EarthTermR1 = new Array(
    0.00103018608, 1.10748969588, 6283.0758499914,
    1.721238e-005, 1.06442301418, 12566.1516999828,
    7.02215e-006, 3.14159265359, 0,
    3.2346e-007, 1.02169059149, 18849.2275499742,
    3.0799e-007, 2.84353804832, 5507.5532386674,
    2.4971e-007, 1.31906709482, 5223.6939198022,
    1.8485e-007, 1.42429748614, 1577.3435424478,
    1.0078e-007, 5.91378194648, 10977.078804699,
    8.654e-008, 1.42046854427, 6275.9623029906,
    8.634e-008, 0.27146150602, 5486.777843175
);

var EarthTermR2 = new Array(
    4.359385e-005, 5.78455133738, 6283.0758499914,
    1.23633e-006, 5.57934722157, 12566.1516999828,
    1.2341e-007, 3.14159265359, 0,
    8.792e-008, 3.62777733395, 77713.7714681205,
    5.689e-008, 1.86958905084, 5573.1428014331,
    3.301e-008, 5.47027913302, 18849.2275499742
);

var EarthTermR3 = new Array(
    1.44595e-006, 4.27319435148, 6283.0758499914,
    6.729e-008, 3.91697608662, 12566.1516999828
);

var EarthTermR4 = new Array(
    3.858e-008, 2.56384387339, 6283.0758499914
);

var EarthTerms = new Array(
    EarthTermL0, 64,
    EarthTermL1, 34,
    EarthTermL2, 20,
    EarthTermL3, 7,
    EarthTermL4, 3,
    EarthTermL5, 1,
    EarthTermB0, 5,
    EarthTermB1, 2,
    null, 0,
    null, 0,
    null, 0,
    null, 0,
    EarthTermR0, 40,
    EarthTermR1, 10,
    EarthTermR2, 6,
    EarthTermR3, 2,
    EarthTermR4, 1,
    null, 0
);

var pTerms = new Array(
    null,
    MercuryTerms,
    null,
    EarthTerms
);

/*  PLANETPOS  --  Calculate position of a single planet from the
                   terms defining it.  */

function planetPos(planet, jd, quickPlanetCalc)
{
    /* Result assembly variables */
    var l, b, r, ldyn, bdyn;

    /* Working variables */
    var i, j, k, nterms,
        x, Tn,
        y,
        ld,
        tau,
        abc,
        pt,
        ai,
        isSun = false;

    y = new Array();
    tau = (jd - 2451545.0) / 365250.0;

    if (planet == 0) {
        isSun = true;
        planet = 3;
    }
    pt = pTerms[planet];
    for (i = 0 ; i < 3 ; i++) {
        y[i] = 0.0;
        Tn = 1.0; /* T^0 = 1 */
        for (j = 0 ; j < 6 ; j++) {
            x = 0.0;
            abc = pt[i * 12 + (j * 2)];  /* Array containing terms */
            nterms = pt[i * 12 + (j * 2) + 1]; /* Number of terms in array */
//log += "i = " + i + " j = " + j + " nterms = " + nterms + "\n";
            if (quickPlanetCalc) {
                nterms = Math.min(nterms, 6);
            } 
            ai = 0;
            for (k = 0 ; k < nterms; k++) {
                x += abc[ai] * Math.cos(abc[ai + 1] + abc[ai + 2] * tau);
                ai += 3;
            }
            y[i] += x * Tn;
            Tn *= tau;
        }
    }
    ldyn = fixangr(y[0]);
    bdyn = y[1];
    r = y[2];

    /* Convert from Dynamic to FK5 equator & ecliptic */

    tau *= 10;
    ld = ldyn - dtr(1.397 * tau + 0.00031 * tau * tau);
    b = bdyn + astor(0.03916 * (Math.cos(ld) - Math.sin(ld)));
    l = ldyn + astor(-0.09033 + 0.03916 * Math.tan(bdyn) *
        (Math.cos(ld) + Math.sin(ld)));
    if (isSun) {
        l = fixangr(l + Math.PI);
    }

    return new Array(l, b, r, ldyn, bdyn);
}

/*  HELIOTOECLIP  --  Determine the geocentric ecliptical coordinates
                      of a body from its heliocentric longitude and
                      latitude and those of the Earth.  */

function heliotoeclip(jd, i, ld, bd, r, sunL, sunB, sunR)
{
    var epsilon, esin, ecos, nPsi, nEps, x, y, z,
        tau, ppos, glon, glat, theta, jc,
        aberrE, aberrPI, aberrDlambda, aberrDbeta,
        lprime, nPsi, nEps, tra, tdec;

    var Kappa = (20.49552 / 3600.0);

    epsilon = dtr(obliqeq(jd));
    ppos = nutation(jd);
    nPsi = ppos[0];
    nEps = ppos[1];
//log += "Nutation Psi = " + nPsi + "Eps = " + nEps + "\n";
    epsilon += nEps;                  /* Correct obliquity for nutation */
    esin = Math.sin(epsilon);
    ecos = Math.cos(epsilon);


    x = r * Math.cos(bd) * Math.cos(ld) - sunR * Math.cos(sunB) * Math.cos(sunL);
    y = r * Math.cos(bd) * Math.sin(ld) - sunR * Math.cos(sunB) * Math.sin(sunL);
    z = r * Math.sin(bd)                - sunR * Math.sin(sunB);

    /* Light travel time over true distance from Earth. */
    tau = 0.0057755183 * Math.sqrt(x * x + y * y + z * z);

    /* Recompute apparent position taking into count
       speed of light delay. */

    ppos = planetPos(i, jd - tau);
    bd = ppos[4];
    ld = ppos[3];
    r = ppos[2];
    if (i == 0) {
        x = -r * Math.cos(bd) * Math.cos(ld);
        y = -r * Math.cos(bd) * Math.sin(ld);
        z = -r * Math.sin(bd);
        r = 0;
    } else {
        x = r * Math.cos(bd) * Math.cos(ld) - sunR * Math.cos(sunB) * Math.cos(sunL);
        y = r * Math.cos(bd) * Math.sin(ld) - sunR * Math.cos(sunB) * Math.sin(sunL);
        z = r * Math.sin(bd)                - sunR * Math.sin(sunB);
    }
    tau = 0.0057755183 * Math.sqrt(x * x + y * y + z * z);

    /* Geocentric longitude and latitude, corrected for
       light travel time. */

    glon = Math.atan2(y, x);
    glat = Math.atan2(z, Math.sqrt(x * x + y * y));

    /* Compute aberration. */

    theta = fixangr(sunL + Math.PI);
    jc = (jd - J2000) / JulianCentury;
    aberrE = 0.016708617 - 0.000042037 * jc -
             0.0000001236 * (jc * jc);
    aberrPI = dtr(102.93735 + 0.71953 * jc + 0.00046 * (jc * jc));
    aberrDlambda = ((-Kappa * Math.cos(theta - glon)) +
        (aberrE * Kappa * Math.cos(aberrPI - glon))) /
            Math.cos(glat);
    aberrDbeta = (-Kappa) * Math.sin(glat) * (Math.sin(theta - glon) -
        aberrE * Math.sin(aberrPI - glon));

    /* Correct for aberration. */

    glon += dtr(aberrDlambda);
    glat += dtr(aberrDbeta);

    /* Reduce to the FK5 system. */

    lprime = ld - dtr(1.397 * jc + 0.00031 * jc * jc);
    glat += astor(0.03916 * (Math.cos(ld) - Math.sin(ld)));
    glon += astor((-0.09033 + 0.03916 * Math.tan(bd) * (Math.cos(ld) + Math.sin(ld))));
    
    /* Correct for nutation in longitude. */

    glon += nPsi;

    /* Transform into apparent right ascension and declination. */

    tra = fixangle(rtd(Math.atan2(Math.sin(glon) * ecos -
                   Math.tan(glat) * esin, Math.cos(glon))));
    tdec = rtd(Math.asin(Math.sin(glat) * ecos +
               Math.cos(glat) * esin * Math.sin(glon)));

    return new Array(tra, tdec);
}


