skyfielders / python-skyfield

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

Observing a Moon location #837

Open livi099 opened 1 year ago

livi099 commented 1 year ago

Hi, I try to run the code from the skyfield documentation (Observing a Moon location) but with the tf, tpc and bpc files for the DE440. The code with the files for DE421 as it is in the documentation works, but when I use the files for DE440 I get an error.

Code:

from skyfield.api import PlanetaryConstants, load

ts = load.timescale()
t = ts.utc(2019, 12, 20, 11, 5)

eph = load('de421.bsp')
earth, moon = eph['earth'], eph['moon']

pc = PlanetaryConstants()
pc.read_text(load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/fk/satellites/moon_de440_220930.tf'))
pc.read_text(load(''https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/pck00011.tpc''))
pc.read_binary(load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/moon_pa_de440_200625.bpc'))

frame = pc.build_frame_named('MOON_ME_DE421')
aristarchus = moon + pc.build_latlon_degrees(frame, 26.3, -46.8)

apparent = earth.at(t).observe(aristarchus).apparent()
ra, dec, distance = apparent.radec(epoch='date')
print(ra)
print(dec)

Error message:

Traceback (most recent call last):
  File "C:\Users\me\PycharmProjects\Moon\surface_moon.py", line 17, in <module>
    apparent = earth.at(t).observe(aristarchus).apparent()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\skyfield\positionlib.py", line 700, in observe
    p, v, t, light_time = body._observe_from_bcrs(self)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\skyfield\vectorlib.py", line 106, in _observe_from_bcrs
    return _correct_for_light_travel_time(observer, self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\skyfield\vectorlib.py", line 243, in _correct_for_light_travel_time
    tposition, tvelocity, gcrs_position, message = target._at(t)
                                                   ^^^^^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\skyfield\vectorlib.py", line 216, in _at
    p2, v2, _, message = vf._at(t)
                         ^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\skyfield\planetarylib.py", line 232, in _at
    R, dRdt = self._frame.rotation_and_rate_at(t)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\skyfield\planetarylib.py", line 169, in rotation_and_rate_at
    components, rates = self._segment.compute(t.whole, t.tdb_fraction, True)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\PycharmProjects\Moon\venv\Lib\site-packages\jplephem\pck.py", line 144, in compute
    raise ValueError('segment only covers dates %.1f through %.1f'
ValueError: segment only covers dates 13447252800.0 through 20514081600.0

Thank you!

brandon-rhodes commented 1 year ago

I should really update that error message to display dates rather than just the raw numbers.

Are you perhaps running into issue #691? Could you show what prints out when you run the command to list the segments in an ephemeris? The command is shown here:

https://github.com/skyfielders/python-skyfield/issues/691#issuecomment-1017675291

livi099 commented 1 year ago

I found out, that the binary file (moon_pa_de440_200625.bpc) consists of two segments. So in planetarylib.py (line 70,71) only the second segment (starts at some point in year 2400) will be saved and used for calculations. I adapted the planetarylib.py so that segment one (longer date range than segment two) will be used. But in case you have an idea to connect both segments to use the whole time range, I'm I am very open to it.

brandon-rhodes commented 1 year ago

I just had the chance to sit down and try your sample code. I get this error (once I fix the slight syntax error caused by the double tick characters on line 11):

Traceback (most recent call last):
  File "tmp837.py", line 14, in <module>
    frame = pc.build_frame_named('MOON_ME_DE421')
  File "/home/brandon/skyfield/skyfield/planetarylib.py", line 84, in build_frame_named
    integer = self._get_assignment('FRAME_{0}'.format(name))
  File "/home/brandon/skyfield/skyfield/planetarylib.py", line 80, in _get_assignment
    raise e
ValueError: unknown planetary constant 'FRAME_MOON_ME_DE421'

You should either use this object's `.read_text()` method to load an
additional "*.tf" PCK text file that defines the missing name, or
manually provide a value by adding the name and value to the this
object's `.variables` dictionary.
--------------------

Do you know what I can do to move past this error, and see the expected output of your script.

livi099 commented 1 year ago
from skyfield.api import PlanetaryConstants, load

ts = load.timescale()
t = ts.utc(2019, 12, 20, 11, 5)

eph = load('de440.bsp')
earth, moon = eph['earth'], eph['moon']

pc = PlanetaryConstants()
pc.read_text(load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/fk/satellites/moon_de440_220930.tf'))
pc.read_text(load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/pck00011.tpc'))
pc.read_binary(load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/moon_pa_de440_200625.bpc'))

frame = pc.build_frame_named('MOON_ME_DE440_ME421')
aristarchus = moon + pc.build_latlon_degrees(frame, 26.3, -46.8)

apparent = earth.at(t).observe(aristarchus).apparent()
ra, dec, distance = apparent.radec(epoch='date')
print(ra)
print(dec)

This code generates my error.

brandon-rhodes commented 1 year ago

Thank you, I now see the same error message! I'm not sure what's going on, but when I have the chance I'll dig in and try to find out.