Skip to content

Commit

Permalink
Add irftrn (#361)
Browse files Browse the repository at this point in the history
* added 4 irf related functions
  • Loading branch information
AndrewAnnex committed Mar 26, 2020
1 parent e6c0b9e commit 238feed
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions appveyor/cspice.def
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ EXPORTS appndc_c
intmin_c
invert_c
invort_c
irfnam_
irfnum_
irfrot_
irftrn_
isordv_c
isrchc_c
isrchd_c
Expand Down
78 changes: 78 additions & 0 deletions spiceypy/spiceypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7444,6 +7444,84 @@ def invort(m: ndarray) -> ndarray:
return stypes.c_matrix_to_numpy(mout)


@spice_error_check
def irfnam(index: int) -> str:
"""
Return the name of one of the standard inertial reference
frames supported by :func:`irfrot`
https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/spicelib/irfnam.html
:param index: Index of a standard inertial reference frame.
:return: is the name of the frame.
"""
index = ctypes.c_int(index)
name = stypes.string_to_char_p(16) # just give enough space
name_len = ctypes.c_int(16)
libspice.irfnam_(ctypes.byref(index), name, name_len)
return stypes.to_python_string(name)


@spice_error_check
def irfnum(name: str) -> int:
"""
Return the index of one of the standard inertial reference
frames supported by :func:`irfrot`
https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/spicelib/irfnum.html
:param name: Name of standard inertial reference frame.
:return: is the index of the frame.
"""
index = ctypes.c_int()
name_len = ctypes.c_int(len(name))
name = stypes.string_to_char_p(name)
libspice.irfnum_(name, ctypes.byref(index), name_len)
return index.value


@spice_error_check
def irfrot(refa: int, refb: int) -> ndarray:
"""
Compute the matrix needed to rotate vectors between two
standard inertial reference frames.
https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/spicelib/irfrot.html
:param refa: index of first reference frame.
:param refb: index of second reference frame.
:return: rotation from frame A to frame B.
"""
refa = ctypes.c_int(refa)
refb = ctypes.c_int(refb)
rotab = stypes.empty_double_matrix()
libspice.irfrot_(ctypes.byref(refa), ctypes.byref(refb), rotab)
# make sure to transpose to get back into c order from fortran ordering
return stypes.c_matrix_to_numpy(rotab).T


@spice_error_check
def irftrn(refa: str, refb: str) -> ndarray:
"""
Return the matrix that transforms vectors from one specified
inertial reference frame to another.
https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/spicelib/irftrn.html
:param refa: Name of reference frame to transform vectors FROM.
:param refb: Name of reference frame to transform vectors TO.
:return: REFA-to-REFB transformation matrix.
"""
len_a = ctypes.c_int(len(refa))
len_b = ctypes.c_int(len(refb))
refa = stypes.string_to_char_p(refa)
refb = stypes.string_to_char_p(refb)
rotab = stypes.empty_double_matrix()
libspice.irftrn_(refa, refb, rotab, len_a, len_b)
# make sure to transpose to get back into c order from fortran ordering
return stypes.c_matrix_to_numpy(rotab).T


@spice_error_check
def isordv(array: Iterable[int], n: int) -> bool:
"""
Expand Down
30 changes: 30 additions & 0 deletions spiceypy/tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5144,6 +5144,36 @@ def test_invort():
npt.assert_array_almost_equal(m, mit)


def test_irfnam():
assert spice.irfnam(1) == "J2000"
assert spice.irfnam(13) == "GALACTIC"
assert spice.irfnam(21) == "DE-143"


def test_irfnum():
assert spice.irfnum("J2000") == 1
assert spice.irfnum("GALACTIC") == 13
assert spice.irfnum("DE-143") == 21


def test_irfrot():
# get the rotation matrix from pxform
expected_rotate = spice.pxform("B1950", "J2000", 0.0)
# get hopefully the same rotation matrix from irfrot
fromfrm = spice.irfnum("B1950")
tofrm = spice.irfnum("J2000")
actual_rotate = spice.irfrot(fromfrm, tofrm)
npt.assert_array_almost_equal(actual_rotate, expected_rotate, decimal=4)


def test_irftrn():
# get the rotation matrix from pxform
expected_rotate = spice.pxform("B1950", "J2000", 0.0)
# get hopefully the same rotation matrix from irfrot
actual_rotate = spice.irftrn("B1950", "J2000")
npt.assert_array_almost_equal(actual_rotate, expected_rotate, decimal=4)


def test_isordv():
assert spice.isordv([0, 1], 2)
assert spice.isordv([0, 1, 2], 3)
Expand Down
4 changes: 4 additions & 0 deletions spiceypy/utils/libspicehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,10 @@
libspice.intmin_c.restype = c_int
libspice.invert_c.argtypes = [(c_double * 3) * 3, (c_double * 3) * 3]
libspice.invort_c.argtypes = [(c_double * 3) * 3, (c_double * 3) * 3]
libspice.irfnam_.argtypes = [c_int_p, c_char_p, c_int]
libspice.irfnum_.argtypes = [c_char_p, c_int_p, c_int]
libspice.irfrot_.argtypes = [c_int_p, c_int_p, (c_double * 3) * 3]
libspice.irftrn_.argtypes = [c_char_p, c_char_p, (c_double * 3) * 3, c_int, c_int]
libspice.isordv_c.argtypes = [c_int_p, c_int]
libspice.isordv_c.restype = c_int
libspice.isrchc_c.argtypes = [c_char_p, c_int, c_int, c_void_p]
Expand Down

0 comments on commit 238feed

Please sign in to comment.