-
Notifications
You must be signed in to change notification settings - Fork 9
ISD to Kernel support #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2d08cd4
28ff7b7
b99b28a
4f36e66
e0a9fa4
6264fb9
7f3c545
86a0378
8ea9ecb
cb337ff
d2a1373
3c076bf
7e546aa
3398db3
1c275ab
1b2c11e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import ctypes | ||
| import numpy as np | ||
| import os | ||
| import sys | ||
|
|
||
| def _find_lib(): | ||
| # Load library | ||
| try: | ||
| from . import _pyspiceql as mod | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mention you did this to avoid latency, which is valid, but how bad was the latency? It will be awkward having a single function exposed this way.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The kernel would crash in a Jupyter Notebook but I'll check it out again.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the isd_to_kernel script to use writeCk() and tested with ALE's tests/pytest/data/isds/mexhrsc_isd.json which has ~1500 quats. It works randomly(?) and when it fails this is the error message |
||
|
|
||
| lib = ctypes.CDLL(mod.__file__) | ||
| if hasattr(lib, "writeCkFromBuffers"): | ||
| return lib, mod.__file__, [] | ||
| except (ImportError, AttributeError, OSError) as e: | ||
| print(f"DEBUG: Internal import failed: {e}") | ||
| pass | ||
|
|
||
| # Manual search and load library | ||
| try: | ||
| current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| # Look for the .so or .dylib file in the same folder as ck_writer.py | ||
| for f in os.listdir(current_dir): | ||
| if f.startswith("_pyspiceql") and (f.endswith(".so") or f.endswith(".dylib")): | ||
| path = os.path.join(current_dir, f) | ||
| lib = ctypes.CDLL(path) | ||
| if hasattr(lib, "writeCkFromBuffers"): | ||
| return lib, path, [] | ||
| except Exception as e: | ||
| print(f"DEBUG: Manual directory search failed: {e}") | ||
|
|
||
| return None, None, [] | ||
|
|
||
|
|
||
| _ck_lib, _ck_lib_path, _ck_lib_search_dirs = _find_lib() | ||
|
|
||
| if _ck_lib is not None: | ||
| try: | ||
| _ck_lib.writeCkFromBuffers.argtypes = [ | ||
| ctypes.c_char_p, | ||
| ctypes.POINTER(ctypes.c_double), | ||
| ctypes.c_size_t, | ||
| ctypes.POINTER(ctypes.c_double), | ||
| ctypes.c_size_t, | ||
| ctypes.c_int, | ||
| ctypes.c_char_p, | ||
| ctypes.c_char_p, | ||
| ctypes.c_char_p, | ||
| ctypes.c_char_p, | ||
| ctypes.POINTER(ctypes.c_double), | ||
| ctypes.c_size_t, | ||
| ctypes.c_char_p, | ||
| ] | ||
| _ck_lib.writeCkFromBuffers.restype = ctypes.c_int | ||
| if hasattr(_ck_lib, "writeCkFromBuffersLastError"): | ||
| _ck_lib.writeCkFromBuffersLastError.restype = ctypes.c_char_p | ||
| _ck_lib.writeCkFromBuffersLastError.argtypes = [] | ||
| _ck_lib_ok = True | ||
| except AttributeError: | ||
| _ck_lib_ok = False | ||
| else: | ||
| _ck_lib_ok = False | ||
|
|
||
|
|
||
| def write_ck( | ||
| path, | ||
| quats, | ||
| times, | ||
| body_code, | ||
| reference_frame, | ||
| segment_id, | ||
| sclk, | ||
| lsk, | ||
| angular_velocities=None, | ||
| comment="", | ||
| ): | ||
| if not _ck_lib_ok: | ||
| dirs = _ck_lib_search_dirs | ||
| hint = "" | ||
| if dirs: | ||
| hint = " Searched: " + ", ".join(dirs) + ". " | ||
| raise RuntimeError( | ||
| "writeCkFromBuffers not found. Rebuild SpiceQL and ensure libSpiceQL is on the library path." | ||
| + hint | ||
| ) | ||
|
|
||
| quats = np.ascontiguousarray(np.asarray(quats, dtype=np.float64)) | ||
| times = np.ascontiguousarray(np.asarray(times, dtype=np.float64)) | ||
| n = quats.shape[0] | ||
| if quats.ndim != 2 or quats.shape[1] != 4: | ||
| raise ValueError("quats must have shape (n, 4)") | ||
| if times.shape[0] != n: | ||
| raise ValueError("times length must match quats rows") | ||
|
|
||
| if angular_velocities is not None and len(angular_velocities) != 0: | ||
| angular_velocities = np.ascontiguousarray(np.asarray(angular_velocities, dtype=np.float64)) | ||
| if angular_velocities.shape[0] != n or angular_velocities.shape[1] != 3: | ||
| raise ValueError("angular_velocities must have shape (n, 3)") | ||
| av_ptr = angular_velocities.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) | ||
| n_av = n | ||
| else: | ||
| av_ptr = None | ||
| n_av = 0 | ||
|
|
||
| def _b(s): | ||
| """Convert value to bytes""" | ||
| if s is None: | ||
| return b"" | ||
| if isinstance(s, str): | ||
| return s.encode("utf-8") | ||
| return str(s).encode("utf-8") | ||
|
|
||
| # Accept str or list of str (multiple SCLK kernels) for sclk, comma deliminated | ||
| sclk_arg = ",".join(sclk) if isinstance(sclk, (list, tuple)) else sclk | ||
|
|
||
| rc = _ck_lib.writeCkFromBuffers( | ||
| _b(path), | ||
| quats.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), | ||
| n, | ||
| times.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), | ||
| n, | ||
| int(body_code), | ||
| _b(reference_frame), | ||
| _b(segment_id), | ||
| _b(sclk_arg), | ||
| _b(lsk), | ||
| av_ptr, | ||
| n_av, | ||
| _b(comment), | ||
| ) | ||
| if rc != 0: | ||
| err = "unknown error" | ||
| if hasattr(_ck_lib, "writeCkFromBuffersLastError"): | ||
| msg = _ck_lib.writeCkFromBuffersLastError() | ||
| if msg: | ||
| err = msg.decode("utf-8") if isinstance(msg, bytes) else msg | ||
| raise RuntimeError(f"writeCkFromBuffers failed: {err}") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ | |
| #include "io.h" | ||
| %} | ||
|
|
||
| %ignore writeCkFromBuffers; | ||
| %include "io.h" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why an anonymous namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I was debating on a name and I just forgot, I can name it
helper.