skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.38k stars 208 forks source link

Add elongation vs separation to Examples page #841

Closed acmpo6ou closed 1 year ago

acmpo6ou commented 1 year ago

Hello!

I'm using separation_from to calculate the separation between 2 planets. Here's an example code:

from skyfield.api import load
from datetime import datetime
import pytz

ts = load.timescale()
d1 = pytz.utc.localize(datetime(2023, 3, 1))
d2 = pytz.utc.localize(datetime(2023, 3, 31, 23))
t1 = ts.from_datetime(d1)
t2 = ts.from_datetime(d2)
# positions for all hours of the month (31 * 24 = 744)
t = ts.linspace(t1, t2, 744)

planets = load('de440.bsp')
earth, mars, moon = planets['earth'], planets['mars barycenter'], planets['moon']

astrometric = earth.at(t).observe(mars)
astrometric2 = earth.at(t).observe(moon)
sep = astrometric.separation_from(astrometric2)
print(sep.degrees)

Output:

array([  9.68309682,  10.16291013,  10.64268545,  11.12239606,
        11.60202065,  12.08154224,  12.56094729,  13.04022512,
        13.51936738,  13.99836762,  14.47722101,  14.95592405,
        15.43447436,  15.91287052,  16.39111192,  16.86919861,
        17.34713128,  17.82491108,  18.30253961,  18.78001886,
        19.25735114,  19.73453903,  20.21158537,  20.68849321,
        21.16526577,  21.64190647,  22.11841883,  22.59480652,
        23.0710733 ,  23.54722304,  24.02325968,  24.49918723,
        24.97500975,  25.45073137,  25.92635625,  26.40188859,
        26.87733263,  27.35269261,  27.8279728 ,  28.30317751,
        28.77831102,  29.25337765,  29.72838171,  30.2033275 ,
        30.67821933,  31.15306152,  31.62785835,  32.10261412,
        32.57733311,  33.05201957,  33.52667776,  34.00131191,
        34.47592624,  34.95052495,  35.42511221,  35.89969219,
        36.37426902,  36.84884681,  37.32342966,  37.79802162,
        38.27262673,  38.74724902,  39.22189246,  39.69656101,
        40.17125861,  40.64598916,  41.12075653,  41.59556458,
        42.07041711,  42.54531791,  43.02027073,  43.49527931,
        43.97034733,  44.44547846,  44.92067632,  45.39594452,
        45.87128662,  46.34670616,  46.82220664,  47.29779153,
        47.77346426,  48.24922825,  48.72508686,  49.20104342,
        49.67710126,  50.15326363,  50.62953378,  51.1059149 ,
        51.58241019,  52.05902276,  52.53575574,  53.01261218,
        53.48959514,  53.96670761,  54.44395256,  54.92133294,
        55.39885166,  55.87651157,  56.35431554,  56.83226635,
        57.31036678,  57.78861959,  58.26702746,  58.74559309,
        59.2243191 ,  59.70320812,  60.18226273,  60.66148546,
        61.14087883,  61.62044533,  62.10018741,  62.58010749,
        63.06020796,  63.54049118,  64.02095946,  64.50161512,
        64.98246042,  65.46349758,  65.94472883,  66.42615633,
        66.90778224,  67.38960866,  67.8716377 ,  68.35387141,
        68.83631182,  69.31896094,  69.80182074,  70.28489317,
        70.76818015,  71.25168358,  71.73540533,  72.21934723,
        72.7035111 ,  73.18789872,  73.67251188,  74.15735229,
        74.64242168,  75.12772174,  75.61325413,  76.09902049,
        76.58502245,  77.07126159,  77.55773949,  78.0444577 ,
        78.53141776,  79.01862116,  79.50606939,  79.99376391,
        80.48170618,  80.96989762,  81.45833963,  81.9470336 ,
        82.43598089,  82.92518286,  83.41464083,  83.90435612,
        84.39433002,  84.88456382,  85.37505878,  85.86581614,
        86.35683713,  86.84812298,  87.33967488,  87.83149403,
        88.32358159,  88.81593872,  89.30856659,  89.80146631,
        90.29463902,  90.78808582,  91.28180782,  91.7758061 ,
        92.27008175,  92.76463584,  93.25946942,  93.75458355,
        94.24997927,  94.74565761,  95.24161961,  95.73786628,
        96.23439863,  96.73121768,  97.22832442,  97.72571985,
        98.22340496,  98.72138073,  99.21964814,  99.71820819,
       100.21706183, 100.71621004, 101.21565378, 101.71539403,
       102.21543175, 102.7157679 , 103.21640345, 103.71733934,
       104.21857656, 104.72011606, 105.22195879, 105.72410573,
       106.22655783, 106.72931607, 107.23238141, 107.73575482,
       108.23943727, 108.74342974, 109.24773321, 109.75234865,
       110.25727706, 110.76251942, 111.26807674, 111.77395   ,
       112.28014021, 112.78664839, 113.29347554, 113.80062269,
       114.30809086, 114.81588108, 115.3239944 , 115.83243186,
       116.34119451, 116.8502834 , 117.35969962, 117.86944421,
       118.37951827, 118.88992289, 119.40065915, 119.91172816,
       120.42313102, 120.93486886, 121.4469428 , 121.95935396,
       122.4721035 , 122.98519254, 123.49862225, 124.01239379,
       124.52650832, 125.04096703, 125.55577108, 126.07092167,
       126.58641999, 127.10226726, 127.61846466, 128.13501341,
       128.65191473, 129.16916984, 129.68677997, 130.20474634,
       130.7230702 , 131.24175276, 131.76079529, 132.280199  ,
       132.79996515, 133.32009497, 133.84058971, 134.3614506 ,
       134.88267888, 135.40427578, 135.92624253, 136.44858035,
       136.97129046, 137.49437406, 138.01783234, 138.5416665 ,
       139.0658777 , 139.59046711, 140.11543585, 140.64078506,
       141.16651583, 141.69262924, 142.21912634, 142.74600814,
       143.27327564, 143.80092978, 144.32897148, 144.8574016 ,
       145.38622098, 145.91543037, 146.44503049, 146.97502198,
       147.50540543, 148.03618134, 148.56735012, 149.09891211,
       149.63086753, 150.1632165 , 150.69595901, 151.22909493,
       151.76262397, 152.2965457 , 152.83085947, 153.36556448,
       153.90065968, 154.4361438 , 154.97201528, 155.50827229,
       156.04491263, 156.58193376, 157.11933273, 157.65710609,
       158.19524989, 158.7337596 , 159.27263   , 159.81185513,
       160.35142816, 160.89134129, 161.43158556, 161.97215076,
       162.51302515, 163.05419527, 163.59564563, 164.13735837,
       164.67931286, 165.22148514, 165.76384734, 166.30636687,
       166.84900547, 167.39171797, 167.93445076, 168.47713976,
       169.01970791, 169.56206173, 170.10408691, 170.64564227,
       171.18655159, 171.72659227, 172.26547935, 172.80284254,
       173.33819247, 173.87087002, 174.39996798, 174.92420673,
       175.44173055, 175.94976253, 176.44400117, 176.91753694,
       177.35889675, 177.74870568, 178.05527981, 178.23423652,
       178.24559281, 178.08592635, 177.79165373, 177.40790322,
       176.96818985, 176.49326451, 175.99542638, 175.48205762,
       174.95773291, 174.42539187, 173.88699073, 173.34387345,
       172.79698995, 172.24702873, 171.69449999, 171.13978935,
       170.58319338, 170.0249438 , 169.46522423, 168.90418203,
       168.34193688, 167.77858698, 167.21421375, 166.6488853 ,
       166.0826591 , 165.51558405, 164.94770205, 164.3790493 ,
       163.80965729, 163.23955358, 162.66876248, 162.09730556,
       161.52520206, 160.95246931, 160.37912293, 159.80517716,
       159.23064502, 158.6555385 , 158.0798687 , 157.50364595,
       156.92687992, 156.34957972, 155.77175398, 155.1934109 ,
       154.61455831, 154.03520375, 153.45535447, 152.87501753,
       152.29419975, 151.71290784, 151.13114833, 150.54892767,
       149.96625221, 149.38312822, 148.79956192, 148.21555949,
       147.63112709, 147.04627086, 146.46099692, 145.87531143,
       145.28922053, 144.70273039, 144.11584721, 143.52857723,
       142.94092673, 142.35290202, 141.76450947, 141.1757555 ,
       140.5866466 , 139.99718929, 139.40739018, 138.81725595,
       138.22679331, 137.63600907, 137.04491012, 136.45350339,
       135.86179591, 135.26979478, 134.67750716, 134.08494032,
       133.49210156, 132.89899831, 132.30563804, 131.71202831,
       131.11817676, 130.5240911 , 129.92977914, 129.33524873,
       128.74050782, 128.14556444, 127.55042669, 126.95510272,
       126.3596008 , 125.76392924, 125.16809641, 124.57211079,
       123.97598091, 123.37971534, 122.78332277, 122.1868119 ,
       121.59019153, 120.99347052, 120.39665776, 119.79976224,
       119.20279296, 118.60575902, 118.00866955, 117.41153371,
       116.81436076, 116.21715996, 115.61994064, 115.02271216,
       114.42548393, 113.82826539, 113.23106602, 112.63389532,
       112.03676285, 111.43967818, 110.8426509 , 110.24569064,
       109.64880703, 109.05200975, 108.45530848, 107.85871289,
       107.26223271, 106.66587765, 106.06965741, 105.47358173,
       104.87766034, 104.28190295, 103.68631928, 103.09091904,
       102.49571195, 101.90070768, 101.30591591, 100.7113463 ,
       100.11700849,  99.52291209,  98.9290667 ,  98.33548187,
        97.74216713,  97.14913198,  96.55638589,  95.96393826,
        95.3717985 ,  94.77997593,  94.18847985,  93.59731949,
        93.00650406,  92.41604268,  91.82594444,  91.23621836,
        90.64687341,  90.05791848,  89.4693624 ,  88.88121393,
        88.29348178,  87.70617456,  87.11930081,  86.53286901,
        85.94688755,  85.36136473,  84.77630877,  84.19172782,
        83.60762993,  83.02402306,  82.44091507,  81.85831375,
        81.27622678,  80.69466175,  80.11362613,  79.53312733,
        78.95317262,  78.37376918,  77.7949241 ,  77.21664434,
        76.63893676,  76.06180813,  75.48526508,  74.90931414,
        74.33396174,  73.75921417,  73.18507763,  72.61155819,
        72.03866181,  71.46639432,  70.89476144,  70.32376878,
        69.75342181,  69.18372588,  68.61468624,  68.04630799,
        67.47859613,  66.91155552,  66.3451909 ,  65.77950689,
        65.21450798,  64.65019854,  64.08658281,  63.52366491,
        62.96144883,  62.39993843,  61.83913746,  61.27904953,
        60.71967813,  60.16102662,  59.60309825,  59.04589612,
        58.48942323,  57.93368245,  57.37867652,  56.82440805,
        56.27087955,  55.7180934 ,  55.16605185,  54.61475703,
        54.06421096,  53.51441554,  52.96537256,  52.41708366,
        51.8695504 ,  51.32277422,  50.77675644,  50.23149825,
        49.68700077,  49.14326499,  48.60029177,  48.0580819 ,
        47.51663606,  46.9759548 ,  46.4360386 ,  45.89688781,
        45.35850273,  44.82088351,  44.28403025,  43.74794293,
        43.21262147,  42.67806567,  42.14427528,  41.61124995,
        41.07898926,  40.54749271,  40.01675974,  39.48678971,
        38.95758192,  38.42913562,  37.90144999,  37.37452417,
        36.84835724,  36.32294825,  35.79829621,  35.27440009,
        34.75125884,  34.2288714 ,  33.70723668,  33.18635361,
        32.66622109,  32.14683806,  31.62820347,  31.1103163 ,
        30.59317556,  30.07678034,  29.56112978,  29.0462231 ,
        28.53205961,  28.01863874,  27.50596006,  26.99402328,
        26.48282828,  25.97237514,  25.46266418,  24.95369597,
        24.44547135,  23.93799154,  23.43125808,  22.92527296,
        22.42003865,  21.91555814,  21.41183506,  20.90887368,
        20.40667909,  19.90525725,  19.40461512,  18.90476081,
        18.4057037 ,  17.90745469,  17.41002637,  16.91343326,
        16.41769215,  15.92282241,  15.42884642,  14.93579008,
        14.44368336,  13.95256102,  13.46246352,  12.97343799,
        12.48553957,  11.99883298,  11.51339448,  11.02931434,
        10.5467    ,  10.06567998,   9.58640907,   9.1090749 ,
         8.63390675,   8.16118708,   7.69126715,   7.22458823,
         6.76171088,   6.30335595,   5.85046275,   5.40427286,
         4.96645234,   4.53927198,   4.12587411,   3.7306652 ,
         3.35987556,   3.02229401,   2.73003664,   2.49880979,
         2.34646672,   2.28855536,   2.33187308,   2.47087287,
         2.69056121,   2.97292268,   3.3017575 ,   3.6644352 ,
         4.05175364,   4.45718343,   4.87611057,   5.30525043,
         5.7422344 ,   6.18532851,   6.63324342,   7.08500576,
         7.53987004,   7.99725767,   8.45671403,   8.91787788,
         9.38045925,   9.84422321,  10.30897779,  10.77456497,
        11.24085379,  11.70773502,  12.17511708,  12.64292283,
        13.111087  ,  13.57955415,  14.04827709,  14.51721551,
        14.98633493,  15.45560583,  15.92500287,  16.39450435,
        16.86409167,  17.33374893,  17.80346253,  18.27322093,
        18.74301434,  19.21283454,  19.68267466,  20.15252906,
        20.62239313,  21.09226324,  21.56213656,  22.03201104,
        22.50188528,  22.97175848,  23.44163037,  23.91150116,
        24.3813715 ,  24.85124241,  25.32111527,  25.79099177,
        26.2608739 ,  26.73076387,  27.20066417,  27.67057747,
        28.14050662,  28.61045468,  29.08042483,  29.55042041,
        30.02044488,  30.49050183,  30.96059494,  31.43072798,
        31.90090483,  32.37112941,  32.84140574,  33.3117379 ,
        33.78212999,  34.25258621,  34.72311076,  35.19370791,
        35.66438194,  36.13513717,  36.60597794,  37.07690862,
        37.54793358,  38.01905722,  38.49028395,  38.96161818])

You can see that as the separation between Mars and Moon is approaching 180°, it goes 177.3, 177.7, 178.0, 178.2, 178.3, 178.0, 177.79, ... So it skips 179 and 180.

Same problem happens with 0°: 2.7, 2.49, 2.3, 2.28, 2.3, 2.47, 2.69, 2.97. It skips 2.0, 1.0, 0.0.

Is there a way to calculate separation without skipping degrees on the edges? Thank you.

brandon-rhodes commented 1 year ago

Because planets are never exactly on the ecliptic, but always at least a few fractions of a degree above or below it, their separation never reaches 180°. You probably want a different value—try computing each planet's ecliptic longitude, and then subtract them, mapping the resulting angular difference into the range 0–360° or -180–+180° as best fits your needs.

I should add this to the Examples page in the docs, since it's not the first time it's been asked. Let's keep this issue open until I have something written up.

acmpo6ou commented 1 year ago

Big thanks for your quick reply! I'll try to figure out how to compute each planet's ecliptic longitude, etc... And otherwise I'll wait for you to update the tutorial.

Thank you for your amazing work :)

brandon-rhodes commented 1 year ago

@acmpo6ou — I think you'll want something similar to this code from the almanac module:

https://github.com/skyfielders/python-skyfield/blob/master/skyfield/almanac.py#L168

acmpo6ou commented 1 year ago

Thank you Brandon, you're a legend! 🥰 Let the stars and planets always shine your way!

I tried this:

e = planets['earth'].at(t)
_, slon1, _ = e.observe(mars).apparent().ecliptic_latlon()
_, slon2, _ = e.observe(moon).apparent().ecliptic_latlon()
arr = np.abs(slon1.degrees - slon2.degrees)
arr[arr > 180] = 360 - arr[arr > 180]
print(arr)
Output ``` 9.6 10.0 10.5 11.0 11.5 12.0 12.5 12.9 13.4 13.9 14.4 14.9 15.3 15.8 16.3 16.8 17.3 17.7 18.2 18.7 19.2 19.7 20.1 20.6 21.1 21.6 22.0 22.5 23.0 23.5 24.0 24.4 24.9 25.4 25.9 26.3 26.8 27.3 27.8 28.3 28.7 29.2 29.7 30.2 30.6 31.1 31.6 32.1 32.6 33.0 33.5 34.0 34.5 34.9 35.4 35.9 36.4 36.8 37.3 37.8 38.3 38.7 39.2 39.7 40.2 40.7 41.1 41.6 42.1 42.6 43.0 43.5 44.0 44.5 44.9 45.4 45.9 46.4 46.9 47.3 47.8 48.3 48.8 49.2 49.7 50.2 50.7 51.2 51.6 52.1 52.6 53.1 53.6 54.0 54.5 55.0 55.5 55.9 56.4 56.9 57.4 57.9 58.3 58.8 59.3 59.8 60.3 60.8 61.2 61.7 62.2 62.7 63.2 63.6 64.1 64.6 65.1 65.6 66.1 66.5 67.0 67.5 68.0 68.5 69.0 69.4 69.9 70.4 70.9 71.4 71.9 72.3 72.8 73.3 73.8 74.3 74.8 75.3 75.7 76.2 76.7 77.2 77.7 78.2 78.7 79.2 79.6 80.1 80.6 81.1 81.6 82.1 82.6 83.1 83.6 84.0 84.5 85.0 85.5 86.0 86.5 87.0 87.5 88.0 88.5 89.0 89.5 89.9 90.4 90.9 91.4 91.9 92.4 92.9 93.4 93.9 94.4 94.9 95.4 95.9 96.4 96.9 97.4 97.9 98.4 98.9 99.4 99.9 100.4 100.9 101.4 101.9 102.3 102.8 103.3 103.8 104.3 104.8 105.3 105.9 106.4 106.9 107.4 107.9 108.4 108.9 109.4 109.9 110.4 110.9 111.4 111.9 112.4 112.9 113.4 113.9 114.4 114.9 115.4 115.9 116.4 116.9 117.5 118.0 118.5 119.0 119.5 120.0 120.5 121.0 121.5 122.0 122.5 123.1 123.6 124.1 124.6 125.1 125.6 126.1 126.6 127.2 127.7 128.2 128.7 129.2 129.7 130.3 130.8 131.3 131.8 132.3 132.8 133.4 133.9 134.4 134.9 135.4 136.0 136.5 137.0 137.5 138.0 138.6 139.1 139.6 140.1 140.7 141.2 141.7 142.2 142.8 143.3 143.8 144.3 144.9 145.4 145.9 146.4 147.0 147.5 148.0 148.6 149.1 149.6 150.2 150.7 151.2 151.7 152.3 152.8 153.3 153.9 154.4 155.0 155.5 156.0 156.6 157.1 157.6 158.2 158.7 159.3 159.8 160.3 160.9 161.4 162.0 162.5 163.0 163.6 164.1 164.7 165.2 165.8 166.3 166.9 167.4 168.0 168.5 169.1 169.6 170.2 170.7 171.3 171.8 172.4 172.9 173.5 174.0 174.6 175.1 175.7 176.2 176.8 177.3 177.9 178.5 179.0 179.6 179.9 179.3 178.7 178.2 177.6 177.1 176.5 175.9 175.4 174.8 174.2 173.7 173.1 172.5 172.0 171.4 170.8 170.3 169.7 169.1 168.6 168.0 167.4 166.8 166.3 165.7 165.1 164.6 164.0 163.4 162.8 162.3 161.7 161.1 160.5 159.9 159.4 158.8 158.2 157.6 157.0 156.5 155.9 155.3 154.7 154.1 153.6 153.0 152.4 151.8 151.2 150.6 150.0 149.5 148.9 148.3 147.7 147.1 146.5 145.9 145.3 144.7 144.2 143.6 143.0 142.4 141.8 141.2 140.6 140.0 139.4 138.8 138.2 137.6 137.1 136.5 135.9 135.3 134.7 134.1 133.5 132.9 132.3 131.7 131.1 130.5 129.9 129.3 128.7 128.1 127.5 126.9 126.3 125.7 125.1 124.5 123.9 123.3 122.7 122.1 121.5 120.9 120.3 119.7 119.1 118.5 117.9 117.3 116.7 116.1 115.5 114.9 114.3 113.7 113.1 112.5 111.9 111.3 110.7 110.1 109.6 109.0 108.4 107.8 107.2 106.6 106.0 105.4 104.8 104.2 103.6 103.0 102.4 101.8 101.2 100.6 100.0 99.4 98.8 98.2 97.6 97.0 96.4 95.9 95.3 94.7 94.1 93.5 92.9 92.3 91.7 91.1 90.5 89.9 89.4 88.8 88.2 87.6 87.0 86.4 85.8 85.3 84.7 84.1 83.5 82.9 82.3 81.8 81.2 80.6 80.0 79.4 78.9 78.3 77.7 77.1 76.5 76.0 75.4 74.8 74.3 73.7 73.1 72.5 72.0 71.4 70.8 70.3 69.7 69.1 68.5 68.0 67.4 66.8 66.3 65.7 65.2 64.6 64.0 63.5 62.9 62.4 61.8 61.2 60.7 60.1 59.6 59.0 58.5 57.9 57.3 56.8 56.2 55.7 55.1 54.6 54.0 53.5 52.9 52.4 51.9 51.3 50.8 50.2 49.7 49.1 48.6 48.1 47.5 47.0 46.4 45.9 45.4 44.8 44.3 43.8 43.2 42.7 42.2 41.6 41.1 40.6 40.0 39.5 39.0 38.4 37.9 37.4 36.9 36.3 35.8 35.3 34.8 34.3 33.7 33.2 32.7 32.2 31.7 31.1 30.6 30.1 29.6 29.1 28.6 28.0 27.5 27.0 26.5 26.0 25.5 25.0 24.5 24.0 23.4 22.9 22.4 21.9 21.4 20.9 20.4 19.9 19.4 18.9 18.4 17.9 17.4 16.9 16.4 15.9 15.4 14.9 14.4 13.9 13.4 12.9 12.4 11.9 11.4 10.9 10.4 9.9 9.4 8.9 8.5 8.0 7.5 7.0 6.5 6.0 5.5 5.0 4.5 4.0 3.6 3.1 2.6 2.1 1.6 1.1 0.6 0.2 0.3 0.8 1.3 1.8 2.3 2.7 3.2 3.7 4.2 4.7 5.2 5.6 6.1 6.6 7.1 7.6 8.0 8.5 9.0 9.5 10.0 10.4 10.9 11.4 11.9 12.3 12.8 13.3 13.8 14.3 14.7 15.2 15.7 16.2 16.6 17.1 17.6 18.1 18.5 19.0 19.5 20.0 20.4 20.9 21.4 21.9 22.3 22.8 23.3 23.8 24.2 24.7 25.2 25.7 26.1 26.6 27.1 27.6 28.0 28.5 29.0 29.4 29.9 30.4 30.9 31.3 31.8 32.3 32.8 33.2 33.7 34.2 34.7 35.1 35.6 36.1 36.6 37.0 37.5 38.0 38.5 38.9 ```

The result is very good :) But a bit imprecise, is there a way to take latitude into account too?

brandon-rhodes commented 1 year ago

If you take latitude into account, then you will get exactly the angles shown in the list you started your question with—because if you take latitude into account, then the separation is never quite 180°.

acmpo6ou commented 1 year ago

That's exactly what I was thinking could happen. Thank you again Brandon :)

Also you're replying insanely quickly, what's the secret? Are you working full time on skyfield?

brandon-rhodes commented 1 year ago

I use a browser plugin that lights up with a notification count whenever I have unread notifications on GitHub:

https://github.com/sindresorhus/notifier-for-github

That way, if I happen to have my laptop open, I can respond quickly—which can save time: a quick response means that someone might still have the error message sitting in front of them, and can answer questions immediately instead of a few days later when they get back to it!

acmpo6ou commented 1 year ago

True, that's very nice!

brandon-rhodes commented 1 year ago

There! In 67e23e6424bc45af0ff7030e531adae1afcccb53 I have added a discussion of this topic to the documentation, on the Examples page. It will be published online the next time Skyfield is released. Hopefully it explains things well enough that users can figure out what's going on.

acmpo6ou commented 1 year ago

Perfect! Amazing work Brandon! I could understand everything :heart: