Skip to content

Commit

Permalink
Merge b4fd314 into 238feed
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAnnex committed Apr 22, 2020
2 parents 238feed + b4fd314 commit 327953e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 51 deletions.
8 changes: 8 additions & 0 deletions docs/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ spiceypy.utils.support_types module
:undoc-members:
:show-inheritance:

spiceypy.utils.callbacks module
-----------------------------------

.. automodule:: spiceypy.utils.callbacks
:members:
:undoc-members:
:show-inheritance:

spiceypy.utils.libspice module
------------------------------

Expand Down
1 change: 1 addition & 0 deletions docs/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ channels:
dependencies:
- python=3
- numpy
- sphinx==2.3.1
- pip:
- sphinx_autodoc_typehints
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
sphinx_autodoc_typehints
sphinx_autodoc_typehints
sphinx==2.3.1
36 changes: 26 additions & 10 deletions spiceypy/spiceypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from . import config
from .utils.callbacks import SpiceUDFUNS, SpiceUDFUNB
from .utils.callbacks import (
UDFUNC,
UDFUNS,
UDFUNB,
UDSTEP,
Expand All @@ -51,6 +52,7 @@
Ellipse,
Plane,
SpiceCell,
SpiceCellPointer,
SpiceDLADescr,
SpiceDSKDescr,
SpiceEKAttDsc,
Expand Down Expand Up @@ -244,7 +246,7 @@ def cell_bool(cell_size: int) -> SpiceCell:
return stypes.SPICEBOOL_CELL(cell_size)


def cell_time(cell_size):
def cell_time(cell_size) -> SpiceCell:
return stypes.SPICETIME_CELL(cell_size)


Expand Down Expand Up @@ -1366,7 +1368,7 @@ def ckw05(
)


def cleard():
def cleard() -> NotImplementedError:
raise NotImplementedError


Expand Down Expand Up @@ -6271,7 +6273,9 @@ def gfrepf() -> None:


@spice_error_check
def gfrepi(window: SpiceCell, begmss: str, endmss: str) -> None:
def gfrepi(
window: Union[SpiceCell, SpiceCellPointer], begmss: str, endmss: str
) -> None:
"""
This entry point initializes a search progress report.
Expand All @@ -6284,7 +6288,7 @@ def gfrepi(window: SpiceCell, begmss: str, endmss: str) -> None:
begmss = stypes.string_to_char_p(begmss)
endmss = stypes.string_to_char_p(endmss)
# don't do anything if we were given a pointer to a SpiceCell, like if we were in a callback
if not isinstance(window, ctypes.POINTER(stypes.SpiceCell)):
if not isinstance(window, SpiceCellPointer):
assert isinstance(window, stypes.SpiceCell)
assert window.is_double()
window = ctypes.byref(window)
Expand Down Expand Up @@ -6767,7 +6771,9 @@ def gftfov(


@spice_error_check
def gfudb(udfuns, udfunb, step, cnfine, result):
def gfudb(
udfuns: UDFUNS, udfunb: UDFUNB, step: float, cnfine: SpiceCell, result: SpiceCell
):
"""
Perform a GF search on a user defined boolean quantity.
Expand All @@ -6785,7 +6791,17 @@ def gfudb(udfuns, udfunb, step, cnfine, result):


@spice_error_check
def gfuds(udfuns, udqdec, relate, refval, adjust, step, nintvls, cnfine, result):
def gfuds(
udfuns: UDFUNS,
udqdec: UDFUNB,
relate: str,
refval: float,
adjust: float,
step: float,
nintvls: int,
cnfine: SpiceCell,
result: SpiceCell,
) -> SpiceCell:
"""
Perform a GF search on a user defined scalar quantity.
Expand Down Expand Up @@ -14099,7 +14115,7 @@ def trcnam(index: int, namlen: int = _default_len_out) -> str:


@spice_error_check
def trcoff():
def trcoff() -> None:
"""
Disable tracing.
Expand Down Expand Up @@ -14236,7 +14252,7 @@ def ucrss(v1: ndarray, v2: ndarray) -> ndarray:
return stypes.c_vector_to_python(vout)


def uddc(udfunc, x, dx):
def uddc(udfunc: UDFUNC, x: float, dx: float) -> bool:
"""
SPICE private routine intended solely for the support of SPICE
routines. Users should not call this routine directly due to the
Expand All @@ -14255,7 +14271,7 @@ def udfunc(et_in):
pos, new_et = spice.spkpos("MERCURY", et_in, "J2000", "LT+S", "MOON")
return new_et
deriv = spice.uddf(udfunc, et, 1.0)
is_negative = spice.uddc(udfunc, et, 1.0)
https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/uddc_c.html
Expand All @@ -14272,7 +14288,7 @@ def udfunc(et_in):


@spice_error_check
def uddf(udfunc, x, dx):
def uddf(udfunc: UDFUNC, x: float, dx: float) -> float:
"""
Routine to calculate the first derivative of a caller-specified
function using a three-point estimation.
Expand Down
2 changes: 1 addition & 1 deletion spiceypy/tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9774,7 +9774,7 @@ def test_uddc():
spice.furnsh(CoreKernels.testMetaKernel)
et = spice.str2et("JAN 1 2009")

@spiceypy.utils.callbacks.SpiceUDFUNS
@spiceypy.utils.callbacks.SpiceUDFUNC
def udfunc(et_in):
pos, new_et = spice.spkpos("MERCURY", et_in, "J2000", "LT+S", "MOON")
return new_et
Expand Down
97 changes: 58 additions & 39 deletions spiceypy/utils/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

import functools
from ctypes import c_int, c_double, c_char_p, POINTER, CFUNCTYPE, byref
from .support_types import SpiceCell
from .support_types import SpiceCell, SpiceCellPointer, to_python_string
from typing import Callable, Union

UDFUNC = CFUNCTYPE(None, c_double, POINTER(c_double))
UDFUNS = CFUNCTYPE(None, c_double, POINTER(c_double))
UDFUNB = CFUNCTYPE(None, UDFUNS, c_double, POINTER(c_int))
UDSTEP = CFUNCTYPE(None, c_double, POINTER(c_double))
Expand All @@ -36,149 +38,166 @@
UDBAIL = CFUNCTYPE(c_int)


def SpiceUDFUNS(f):
def SpiceUDFUNC(f: Callable[[float], float]) -> UDFUNC:
"""
Decorator for wrapping python functions in spice udfunc callback type
:param f: function that has one argument of type float, and returns a float
:return: wrapped udfunc function
"""

@functools.wraps(f)
def wrapping_udfunc(x: float, value: POINTER(c_double)) -> None:
result = f(x)
value[0] = c_double(result)

return UDFUNC(wrapping_udfunc)


def SpiceUDFUNS(f: Callable[[float], float]) -> UDFUNS:
"""
Decorator for wrapping python functions in spice udfuns callback type
:param f: function that has one argument of type float, and returns a float
:type f: builtins.function
:return: wrapped udfunc function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udfuns(x, value):
def wrapping_udfuns(x: float, value: POINTER(c_double)) -> None:
result = f(x)
value[0] = c_double(result)

return UDFUNS(wrapping_udfuns)


def SpiceUDFUNB(f):
def SpiceUDFUNB(f: Callable[[UDFUNS, float], int]) -> UDFUNB:
"""
Decorator for wrapping python functions in spice udfunb callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udfunb function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udfunb(udf, et, xbool):
result = f(udf, et) # the function takes a udffunc as a argument
def wrapping_udfunb(udf: UDFUNS, et: float, xbool: POINTER(c_int)) -> None:
result = f(udf, et)
xbool[0] = c_int(result) # https://github.com/numpy/numpy/issues/14397

return UDFUNB(wrapping_udfunb)


def SpiceUDSTEP(f):
def SpiceUDSTEP(f: Callable[[float], float]) -> UDSTEP:
"""
Decorator for wrapping python functions in spice udstep callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udstep function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udstep(x, value):
def wrapping_udstep(x: float, value: POINTER(c_double)) -> None:
result = f(x)
value[0] = c_double(result)

return UDSTEP(wrapping_udstep)


def SpiceUDREFN(f):
def SpiceUDREFN(
f: Callable[[float, float, Union[bool, int], Union[bool, int]], float]
) -> UDREFN:
"""
Decorator for wrapping python functions in spice udrefn callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrefn function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udrefn(t1, t2, s1, s2, t):
def wrapping_udrefn(
t1: float,
t2: float,
s1: Union[bool, int],
s2: Union[bool, int],
t: POINTER(c_double),
) -> None:
result = f(t1, t2, s1, s2)
t[0] = c_double(result)

return UDREFN(wrapping_udrefn)


def SpiceUDREPI(f):
def SpiceUDREPI(
f: Callable[[Union[SpiceCell, SpiceCellPointer], str, str], None]
) -> UDREPI:
"""
Decorator for wrapping python functions in spice udfrepi callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrepi function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udrepi(cnfine, srcpre, srcsurf):
f(cnfine, srcpre, srcsurf)
def wrapping_udrepi(
cnfine: Union[SpiceCell, SpiceCellPointer], srcpre: bytes, srcsurf: bytes
) -> None:
f(cnfine, to_python_string(srcpre), to_python_string(srcsurf))

return UDREPI(wrapping_udrepi)


def SpiceUDREPU(f):
def SpiceUDREPU(f: Callable[[float, float, float], None]) -> UDREPU:
"""
Decorator for wrapping python functions in spice udrepu callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrepu function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udrepu(beg, end, et):
def wrapping_udrepu(beg: float, end: float, et: float) -> None:
f(beg, end, et)

return UDREPU(wrapping_udrepu)


def SpiceUDREPF(f):
def SpiceUDREPF(f: Callable) -> UDREPF:
"""
Decorator for wrapping python functions in spice udrepf callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrepf function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udrepf():
def wrapping_udrepf() -> None:
f()

return UDREPF(wrapping_udrepf)


def SpiceUDBAIL(f):
def SpiceUDBAIL(f: Callable[[], Union[bool, int]]) -> UDBAIL:
"""
Decorator for wrapping python functions in spice udbail callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udbail function
:rtype: builtins.function
"""

@functools.wraps(f)
def wrapping_udbail():
def wrapping_udbail() -> int:
result = f()
return int(result)

return UDBAIL(wrapping_udbail)


def CallUDFUNS(f, x):
def CallUDFUNS(f: UDFUNS, x: float) -> float:
"""
We are given a UDF CFUNCTYPE and want to call it in python
:param f: SpiceUDFUNS
:type f: CFUNCTYPE
:param x: some scalar
:type x: float
:return: value
:rtype: float
"""
value = c_double()
f(x, byref(value))
Expand Down
3 changes: 3 additions & 0 deletions spiceypy/utils/support_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
cast,
Structure,
string_at,
POINTER,
)

import numpy
Expand Down Expand Up @@ -1028,6 +1029,8 @@ def __eq__(self, other):
return True


SpiceCellPointer = POINTER(SpiceCell)

# Spice Cell classes


Expand Down

0 comments on commit 327953e

Please sign in to comment.