Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Context manager for SPICE kernels

See original GitHub issue

Note I’ve read the contributing guidelines and am aware that Kernels’ management interfaces are out of your scope. That being said, this could be implemented in less than a minute, and it has improved my personal experience using this library.

Is your feature request related to a problem? Please describe. I’ve been working with spicepy for the last year, and one of the only things that annoys me about it is having to use a try-except or try-finally scheme in order to assure that kernels are properly closed, even if an exception is raised. Let me show a litte example:

import spiceypy as spice

kpath = "path/to/metak"


    t0 = spice.str2et("28-06-2000")

Describe the solution you’d like I’d like to be able to use a context manager to automatically load and unload kernels.

with Kernels(kpath):
    t0 = spice.str2et("28-06-2000")

Describe alternatives you’ve considered I’ve been working with a custom, and pretty simple, context manager that looks like this:

class Kernels:

    def __init__(self, path: str) -> None:


    def __enter__(self) -> None:

        return None

    def __exit__(self, exc_type, exc_value, traceback) -> None:


What are your thoughts on this? I’d love to help to implement it if you were open to it.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

AndrewAnnexcommented, Nov 29, 2022

@alfonsoSR thanks for the contribution! I’ve merged your pr in #458, closing this issue.

AndrewAnnexcommented, Nov 18, 2022

hey @alfonsoSR thanks for the detailed response, I’ve left some comments on the PR that need to be addressed but I’ll address some of your response here first.

Agree about worrying about changing directories to be out of scope, users should ensure they pass in absolute paths to kernels in general.

Secondly after looking into it a bit more I agree now with using kclear, although we could detect the type of kernel file, text kernels are so commonly used calling unload on them would end up having the same effect as kclear.

These caveats should be explained in a new docs file and in the docstrings for these functions (see other context manager PR for example…) to make it very clear to users what things to keep in mind.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The KernelPool context manager
The KernelPool context manager provides a convenient way to load, and unload SPICE kernels, guaranteeing that the kernel database will still be cleared...
Read more >
Introduction to Kernels - NAIF
Introduction to Kernels. 4. The Family of SPICE Kernels. • SPK. – Spacecraft and Planet Ephemeris. • PCK. – Planetary Constants, for natural...
Read more >
SpiceyPy Documentation - Read the Docs
SpiceyPy is a python wrapper for the SPICE Toolkit. ... Each of the kernels used to initialize the context manager is known as...
Read more >
SpiceyPy: a Pythonic Wrapper for the SPICE Toolkit. - GitHub
SpiceyPy is a python wrapper for the SPICE Toolkit. SPICE is an essential tool for scientists and engineers alike in the planetary science...
Read more >
Introduction to SPICE | P I R L
Welcome... Welcome to the SPICE system. This system allows you to easily combine the most accurate space geometry and event data with your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found