Post · 6 min read· pp. 135–139

Utils

Spice Utils

pykep.utils.spice_version() : Retrieves the installed version of the NAIF spice toolkit.

code
Returns:
:   `string`: SPICE toolbox version installed in the system.

pykep.utils.load_spice_kernels(paths) : Loads one or more kernels for use with the NAIF spice toolkit.

code
Args:
:   paths (`string` or [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)): The path (or paths) of the kernels to load

pykep.utils.unload_spice_kernels(paths) : Unloads from memory one or more kernels for use with the NAIF spice toolkit.

code
Args:
:   paths (`string` or [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)): The path (or paths) of the kernels to load

pykep.utils.inspect_spice_kernel(path) : Detects the objects contained in a specific kernel. This is useful to understand the possibile queries to make for a certain kernel.

code
Args:
:   path (`string`): The path (or paths) of the kernels to load

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list): the NAIF ids of the objects found in the kernels.

pykep.utils.name2naifid(name) : Retreives the NAIF id of some planet/comet/spacecraft

code
Args:
:   name (`string`): The name of the planet/comet/spacecraft to be found

Returns:
:   [`int`](https://docs.python.org/3/builtins/functions.html#int) the NAIF id of the the planet/comet/spacecraft

pykep.utils.extract_coverage_window(path, naifid, window_n=0) : Extracts the n-th SPK coverage window for a given NAIF ID and returns it as PyKEP epochs.

code
The SPK coverage start/end times returned by SPICE are ET (TDB) seconds past J2000. Here we map them to a **uniform**
seconds-from-2000-01-01T00:00:00 (UTC-labeled origin) representation, so that the resulting PyKEP epoch is strictly
1:1 with ET everywhere (this is not “UTC elapsed seconds” with leap seconds).

The offset constant 43135.816087188054 [s] is ET0 = str2et(“2000-01-01 00:00:00 UTC”), i.e. the ET value
of the UTC-labeled origin of pykep mjd2000; thus mjd2000\_days = (et - ET0) / 86400, implemented
as (et + 43135.816087188054) \* SEC2DAY.

Args:
:   path (`string`): The path (or paths) of the kernel where the ephemerides are defined in windows.
    naifid ([`int`](https://docs.python.org/3/builtins/functions.html#int)): The NAIF id.
    window\_n ([`int`](https://docs.python.org/3/builtins/functions.html#int)): The window to consider (in case ephs are defined in multiple windows of validity)

Returns:
:   `pk.epoch`, `pk.epoch`: Starting and final epoch of the *n*-th window

pykep.utils.epoch2utc(pykep_epoch) : Converts a PyKEP epoch (mjd2000) into a SPICE-formatted UTC calendar string.

code
The input epoch is assumed to be a uniform time variable expressed as days since the UTC-labeled origin
2000-01-01 00:00:00 (i.e. strictly 1:1 with SPICE ET everywhere; it is not “UTC elapsed seconds” with leap seconds).

The constant 43135.816087188054 [s] is ET0 = str2et(“2000-01-01 00:00:00 UTC”). We first reconstruct the ET value
via et = mjd2000\*86400 - ET0, then use SPICE et2utc to obtain the UTC calendar representation. A leapseconds kernel
is loaded to enable correct ET↔UTC conversion.

Args:
:   pykep\_epoch (`pk.epoch`): PyKEP epoch whose .mjd2000 is days since 2000-01-01 00:00:00 (UTC-labeled origin).

Returns:
:   `string`: UTC calendar string corresponding to the input epoch.

pykep.utils.framename2naifid(name) : Retreives the NAIF id of some reference frame

code
Args:
:   name (`string`): The name of the reference frame to be found

Returns:
:   [`int`](https://docs.python.org/3/builtins/functions.html#int) the NAIF id of the reference frame

pykep.utils.rotation_matrix(origin, destination, ep=_pk.epoch(0)) : Rotation matrix between frames at epoch.

code
The epoch is converted to SPICE ET with the same convention used by [`spice`](/u/6fb305ae73ecab80#pykep-udpla-spice)
and [`epoch2utc()`](#pykep.utils.epoch2utc), i.e. et = mjd2000\*86400 + str2et(“2000-01-01 00:00:00 UTC”).

Args:
:   origin (`string`): The name of the origin reference frame.

    destination (`string`): The name of the destination reference frame.

    ep ([`epoch`](/u/cc87604183a08dd4#pykep-epoch)): Epoch. Defaults to pk.epoch(0).

Returns:
:   `npumpy.ndarray`: The rotation matrix.

pykep.utils.naifid2name(naifid) : Retreives the planet/comet/spacecraft name from its NAIF id.

code
Args:
:   name ([`int`](https://docs.python.org/3/builtins/functions.html#int)): The NAIF id.

Returns:
:   `string` the name of the the planet/comet/spacecraft

Encoding Utils

pykep.alpha2direct(alphas) : Converts from alpha encoded to transfer times.

code
Args:
:   *alphas* ([`list`](https://docs.python.org/3/builtins/stdtypes.html#list)): a sequence of transfer times encoded using the alpha encoding.

    *T* ([`float`](https://docs.python.org/3/builtins/functions.html#float)): the total transfer time.

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list):: The encoded transfer times

pykep.direct2alpha(tofs) : Converts from transfer times to alpha encoded.

code
Args:
:   *tofs* ([`list`](https://docs.python.org/3/builtins/stdtypes.html#list)): a sequence of transfer times.

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list):, [`float`](https://docs.python.org/3/builtins/functions.html#float): The alpha-encoded transfer times, the total transfer time (for cenvenience)

pykep.eta2direct(etas, max_tof) : Converts from eta encoded to transfer times.

code
Args:
:   *etas* ([`list`](https://docs.python.org/3/builtins/stdtypes.html#list)): a sequence of transfer times encoded using the eta encoding.

    *max\_tof* ([`float`](https://docs.python.org/3/builtins/functions.html#float)): maximum time of flight (might actually be less according to the value of the last eta.)

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list): The encoded transfer times

pykep.direct2eta(tofs, max_tof) : > Converts from transfer times to eta encoded.

code
Args:
:   *tofs* ([`list`](https://docs.python.org/3/builtins/stdtypes.html#list)): a sequence of transfer times

    *max\_tof* ([`float`](https://docs.python.org/3/builtins/functions.html#float)): maximum time of flight (might actually be less according to the value of the last eta.)

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list): The eta-encoded transfer times

pykep.utils.uvV2cartesian(uvV) : This function converts the uvV encoding of a vector to cartesian coordinates

code
Args:
:   *uvV* (`array-like`): a sequence of 3 floats representing the vector in uvV encoding.

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list):: The vector in cartesian coordinates

pykep.utils.cartesian2uvV(V) : This function converts the cartesian coordinates of a vector to uvV encoding

code
Args:
:   *V* (`array-like`): a sequence of 3 floats representing the vector in cartesian coordinates.

Returns:
:   [`list`](https://docs.python.org/3/builtins/stdtypes.html#list):: The vector in uvV encoding

pykep.utils.compute_softmax_and_jacobian(logits) : This function computes softmax and its Jacobian using a numerically stable formulation. The softmax transforms an unconstrained real-valued vector into a probability simplex (all weights positive and summing to one).

code
Args:
:   *logits* (`array-like`): a sequence of floats representing the unnormalized
    :   log-weights (logits) to be transformed via softmax.

Returns:
:   `tuple`: A pair `(weights, J)` where:

    - *weights* (`numpy.ndarray`): the softmax-normalized weights,
      each in (0, 1) and summing to 1.
    - *J* (`numpy.ndarray`): the `(n, n)` Jacobian matrix of the
      softmax map, where `J[i, j] = weights[i] * (delta_ij - weights[j])`.

Miscellanea

pykep.utils.planet_to_keplerian(pla, when: epoch, mu=None) : Transforms a planet to its Keplerian version

code
The [`planet`](/u/b8b3c9975f4a752a#pykep-planet) returned will be Keplerian, with elements identical to
the osculating elements at *t0* of the input planet. Hence its ephemerides can be computed
at any epoch, but will only be matching the original ones at *t0*.

This utility is useful to extend the validity range of some [`planet`](/u/b8b3c9975f4a752a#pykep-planet)’s ephemerides.

Args:
:   *pla* ([`planet`](/u/b8b3c9975f4a752a#pykep-planet)): the input planet.

    *when* ([`epoch`](/u/cc87604183a08dd4#pykep-epoch)): the epoch to match the osculating elements.

    *mu* ([`float`](https://docs.python.org/3/builtins/functions.html#float), optional): the central body parameter. Defaults to the one computed from the input [`planet`](/u/b8b3c9975f4a752a#pykep-planet).

Returns:
:   [`planet`](/u/b8b3c9975f4a752a#pykep-planet): a Keplerian planet.