Source code for irfpy.juice.jspice

''' Interface of JUICE related SPICE module.

Replaces :mod:`irfpy.pep.pep_spice`, :mod:`irfpy.pep.juice_spice` and
:mod:`irfpy.pep.juice_spice1205`.

The PySpice extension is needed.  Refer to :ref:`pyspice_install` document.

.. note::

    This module is again deprecated (170719).
    Use :mod:`irfpy.juice.jspiceesa` module for further development.
    This module is kept for the backward compatibility.
'''
import datetime
import logging
logging.basicConfig()
logger = logging.getLogger('irfpy.juice.jspice')

import warnings
warnings.warn("irfpy.juice.jspace module is deprecated, and use irfpy.juice.jspiceesa for further work (170719)",
              DeprecationWarning)
logger.warning("irfpy.juice.jspace module is deprecated, and use irfpy.juice.jspiceesa for further work (170719)")

import numpy as np

import irfpy.util.irfpyrc
try:
    import spiceypy as spice
except ImportError:
    import spice

[docs]def furnsh(kernel_file): ''' To furnsh kernel additionally. ''' spice.furnsh(kernel_file)
__initialized = False
[docs]def isinit(): return __initialized
[docs]def reinit(*args, **kwds): global __initialized __initialized = False init(*args, **kwds)
[docs]def init(mkkernel=None): ''' To initialize the spice. Initialize the spice, i.e. load the kernels specified. :keyword mkkernel: The MK kernel file name. If None is specified, the default file is read. Default file is specified by ``.irfpyrc`` file, [pep]-juicemkkernel entry. ''' if isinit(): logger.warn('Already initialized. Do nothing. Restart program, or use reinit() if forcely initialize again') return if mkkernel == None: rc = irfpy.util.irfpyrc.Rc() mkkernel = rc.get('pep', 'juicemkkernel') if mkkernel == None: logger.error('Failed to load PEP kernel files') logger.error('Specify [pep] juicemkkernel in .irfpyrc') logger.error('Or use mkfile name as argument') raise RuntimeError('No kernel file specified.') furnsh(mkkernel) define_jgo() global __initialized __initialized = True
[docs]def define_jgo(): spice.boddef('JGO', -907)
[docs]def define_juice(): spice.boddef('JUICE', -907)
[docs]def get_position(t, target='JGO', origin='JUPITER', frame='JSE'): ''' A generic position getter. :param t: Time :type t: ``datetime.datetime`` :keyword target: The SPICE name of the body to calculate the position :type target: String :keyword origin: The SPICE name of the body at origin :type origin: String :keyword frame: Frame of the position vector :type frame: String :returns: Return the target's position with relative to origin in the given frame :rtype: ``np.array`` of (3,) shape. You can get the position of JUICE at 2031/06/05T12:55:15 as >>> init() >>> pos = get_position(datetime.datetime(2031, 6, 5, 12, 55, 15)) >>> print('%.2f %.2f %.2f' % (pos[0], pos[1], pos[2])) 97514.29 -1409055.30 550473.59 You can get the position of JUICE centered at GANYMEDE. >>> pos = get_position(datetime.datetime(2030, 9, 15, 12, 55, 15), origin='GANYMEDE', frame='IAU_GANYMEDE') >>> print('%.2f %.2f %.2f' % (pos[0], pos[1], pos[2])) 50428.32 -22917.62 2457.08 ''' posvel = get_posvel(t, target=target, frame=frame, origin=origin) return np.array(posvel[:3])
[docs]def get_velocity(t, target='JGO', origin='JUPITER', frame='JSE'): ''' A generic velocity getter. :param t: Time :type t: ``datetime.datetime`` :keyword target: The SPICE name of the body to calculate the position :type target: String :keyword origin: The SPICE name of the body at origin :type origin: String :keyword frame: Frame of the position vector :type frame: String :returns: Return the target's velocity with relative to origin in the given frame :rtype: ``np.array`` of (3,) shape. >>> init() >>> vel = get_velocity(datetime.datetime(2031, 6, 5, 12, 55, 15)) >>> print('%.2f %.2f %.2f' % (vel[0], vel[1], vel[2])) 8.98 3.59 2.44 ''' posvel = get_posvel(t, target=target, frame=frame, origin=origin) return np.array(posvel[3:])
[docs]def get_posvel(t, target='JGO', origin='JUPITER', frame='JSE'): ''' A generic position/velocity getter. :param t: Time :type t: ``datetime.datetime`` :keyword target: The SPICE name of the body to calculate the position :type target: String :keyword origin: The SPICE name of the body at origin :type origin: String :keyword frame: Frame of the position vector :type frame: String :returns: Return the target's position and velocity with relative to origin in the given frame :rtype: ``np.array`` of (6,) shape. ''' if target.upper() == "JUICE": target = "JGO" # Alias. et0 = spice.str2et(t.strftime('%FT%T')) posvel, lt = spice.spkezr(target, et0, frame, 'LT+S', origin) return np.array(posvel)
[docs]def get_conversion_matrix(t, from_frame='JSE', to_frame='IAU_JUPITER'): ''' Return conversion matrix at a given time. :param t: Time in datetime instance. :keyword from_frame: The original frame :keyword to_frame: The frame after conversion. ''' et0 = spice.str2et(t.strftime('%FT%T')) matx = spice.pxform(from_frame, to_frame, et0) return np.array(matx)
[docs]def sun_longitude_system3(t): ''' Gives Sun longitude in system 3. >>> print('%.2f' % sun_longitude_system3(datetime.datetime(2030, 1, 1))) 133.02 >>> print('%.2f' % sun_longitude_system3(datetime.datetime(2030, 1, 1, 1))) 96.75 ''' sundir_jse = [1, 0, 0] matx = get_conversion_matrix(t, from_frame='JSE', to_frame='IAU_JUPITER') #sundir_sys3 = matx.dot(sundir_jse) # sundir_sys3 = matx[:, 0] return np.rad2deg(np.arctan2(sundir_sys3[1], sundir_sys3[0]))
import unittest import doctest
[docs]def doctests(): return unittest.TestSuite(( doctest.DocTestSuite(), ))
if __name__ == '__main__': unittest.main(defaultTest='doctests')