Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

introduce MDASegment for SPK type 1 and 21 #973

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Tontyna
Copy link

@Tontyna Tontyna commented Jun 10, 2024

Resolves #157

Injecting the MDASegment into skyfield.jpllib.SpiceKernel is ... just a workaround. Might/should become a part of jplephem.spk.SPK

@brandon-rhodes
Copy link
Member

I also haven't had time yet to read through this pull request, but I'll try to find time this month or next, so that I can respond before summer is fully over. Before a feature like this could become part of Skyfield, I'd need a sample file that it could be shown to work successfully on, and also documentation and tests. Also, could you describe the audience who will be using this, so we can think about where in the docs it would be described? Thanks!

@Tontyna
Copy link
Author

Tontyna commented Jul 17, 2024

There is already a chapter about Type 1 and Type 21 ephemeris formats in the documentation. It recommends to install Shushi Uetsuki's spktype01 resp, spktype21 and implement a helper class.
The MDASegment is based on those libraries -- I'd never managed to translate the FORTAN source of the SPICE Toolkit into Python 😏

For 'small bodies' like Chiron, Pholus and other not-so-popular asteroids there are no pre-built binary SPK files on the JPL- or NAIF-Server.
A comfortable way to generate them is via the HORIZONS Web Application

Those Small-Body SPK files will most probably be

  • of Type 21
  • heliocentric

Example code

from skyfield.api import load

ts = load.timescale()
t = ts.utc(2024, 7, 12)

chiron_kernel = load('20002060.bsp')
planets_kernel = load('de440.bsp')

earth = planets_kernel['earth']
sun = planets_kernel['sun']

# add the sun to make it barycentric
chiron = sun + chiron_kernel[20002060]

ra, dec, distance = earth.at(t).observe(chiron).radec()
print(ra)
print(dec)
01h 23m 47.83s
+10deg 01' 20.9"

@Tontyna
Copy link
Author

Tontyna commented Jul 17, 2024

Depending on the time span you selected the ephemeris file will be multi-segmented.
Unless you're only interested in the last one, you must explicitly pick the appropriate segment.

Instead of chiron = sun + chiron_kernel[20002060] do sth. like this:

def pick_segment(kernel, target, time):
    target = kernel.decode(target)
    tdb = time.tdb
    segments = kernel.segments
    # last segment should win
    segments = segments[::-1]
    for segment in segments:
        if (segment.target == target and
            segment.spk_segment.start_jd <= tdb and tdb < segment.spk_segment.end_jd):
            return segment

    format_datetime = '{0}-{1:02}-{2:02} {3:02}:{4:02}:{5:02.0f}'.format
    raise ValueError('kernel {0!r} has no segment for target "{1}" at TBD {2}'
                        .format(
                        kernel.filename,
                         _jpl_name(target) ,
                        format_datetime(*calendar_tuple(tdb))
                    ))

chiron = sun + pick_segment(chiron_kernel, 20002060, t)

Admittedly, without a 'MultiKernel' (cf. #975) it's easier to use the spktype21 library mentioned above. It picks the time-segment automatically.

@Tontyna
Copy link
Author

Tontyna commented Jul 17, 2024

@brandon-rhodes As said before, I think the proper place for the MDASegment is jplephem/spk.py. Shall I close this PR and move it tho jplephem instead?

@Tontyna
Copy link
Author

Tontyna commented Jul 17, 2024

As for tests:
Requires at least one MDA-SPK file. Is there a recommended max. size for data files?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Skyfield should support Type 21 ephemeris segments
2 participants