Utils#
- class Utils#
Bases:
objectCollection of general-purpose utility methods used throughout Scarabaeus.
Provides static helpers for array manipulation, file I/O, and Verification-and-Validation (V&V) trajectory comparison against SPICE ground-truth kernels.
See also
scarabaeus.SpiceManagerSPICE kernel interface used by the V&V methods.
scarabaeus.EpochArrayTime-tag array type accepted by several helpers.
Methods
VandV_compare(positions, velocities, times, ...)Compare a Scarabaeus-generated trajectory against a SPICE ground truth.
VandV_plot_errors(errorPosition, ...)Plot position and velocity errors in a standardised V&V layout.
build_reorder_operator(old_list, new_list)Build the
Qselection/reorder operator mapping the old state to the new.check_slices_equal(arr)Checks if all slices are equal across an array.
enumerate_state_blocks(state_list)Enumerate the scalar blocks of a state definition with a rich key.
equal_along_rows(arr)Check equality along rows of the given array.
find_date_indices_in_epochs(epochsTDB, ...)Find the index or indices of target date(s) in a numpy array of epochs, accounting for floating-point precision.
load_json(dir[, print_flag])Load and parse a JSON file from disk.
make_dir(dir)Generate a folder in the given directory.
max_of_last_slice(arr)Finds the maximum value of the last slice of an array.
open_pickle(dir)Return a pickle object handle from dir.
reorder_covariance(Q, P[, new_block_variances])Reorder a covariance:
P' = Q P Q^T, with a-priori variances for new states.reorder_stm(Q, STM[, new_blocks])Reorder a State Transition Matrix:
Phi' = Q Phi Q^T.reorder_vector(Q, x)Apply the reorder operator to a state / deviation vector:
X' = Q X.tile_array(arr, desired_shape)Construct an array by repeating A the number of times given by reps.
- static VandV_compare(positions: ndarray, velocities: ndarray, times: EpochArray, caseId: str, integratorName: str, flag_SUN: bool = False, leg1_period=None, leg2_period=None)#
Compare a Scarabaeus-generated trajectory against a SPICE ground truth.
Loads the SPICE BSP kernel for caseId, queries the reference state at each epoch in times, computes the component-wise and norm errors, prints a summary to stdout, and delegates plotting to
VandV_plot_errors().- Parameters:
positions (
numpy.ndarray) – Spacecraft positions from Scarabaeus, shape(N, 3)in km.velocities (
numpy.ndarray) – Spacecraft velocities from Scarabaeus, shape(N, 3)in km/s.times (
EpochArray) – Epoch array of lengthNcorresponding to each state sample.caseId (
str) – Scenario identifier; used to locate the BSP kernel at<cwd>/data/kernels/validation/<caseId>.bsp.integratorName (
str) – Integration scheme label, printed in the summary and plot titles.flag_SUN (
bool, optional) – WhenTrueuses the Sun as the inertial origin; otherwise uses Earth. Defaults toFalse.leg1_period (
ArrayWUnitsorNone, optional) – Duration of the first trajectory leg in seconds. Required for multi-leg cases (MONTE_case17a/b/c);Noneotherwise.leg2_period (
ArrayWUnitsorNone, optional) – Duration of the second trajectory leg in seconds. Required for multi-leg cases;Noneotherwise.
- Returns:
figHandle (
matplotlib.figure.Figure) – Handle to the V&V error plot.max_state_error_norm_pos (
float) – Maximum position error norm over the trajectory in km.max_state_error_norm_vel (
float) – Maximum velocity error norm over the trajectory in km/s.
- static VandV_plot_errors(errorPosition: ndarray, errorVelocity: ndarray, times: EpochArray, caseId: str, integratorName: str)#
Plot position and velocity errors in a standardised V&V layout.
Produces a two-panel figure with position error (top) and velocity error (bottom) as functions of elapsed time in days.
- Parameters:
errorPosition (
numpy.ndarray) – Position error array of shape(N, 3)in km, computed aspositions_scb - positions_truth.errorVelocity (
numpy.ndarray) – Velocity error array of shape(N, 3)in km/s, computed asvelocities_scb - velocities_truth.times (
EpochArray) – Epoch array of lengthNcorresponding to each error sample.caseId (
str) – Scenario identifier used as the plot title prefix.integratorName (
str) – Integration scheme label appended to each subplot title.
- Returns:
figHandle – Handle to the generated figure.
- Return type:
- static build_reorder_operator(old_list, new_list)#
Build the
Qselection/reorder operator mapping the old state to the new.Blocks are matched on
(name, spice_id, occurrence). A new block with no old counterpart yields an all-zero row inQ(an expansion target).- Parameters:
- Returns:
Q (
numpy.ndarray) –(new_n, old_n)operator withQ[i, j] = 1when old scalarjmaps to new scalari.new_n (
int) – Dimension of the new state.new_blocks (
listof(str,int,int)) –(name, start, size)of blocks present innew_listbut absent fromold_list(expansion targets — zero rows ofQ).
- static check_slices_equal(arr: ndarray)#
Checks if all slices are equal across an array.
- Parameters:
arr (
numpy.ndarray) – The array to examine.- Returns:
are_equal – Indicates if all slices are equal or not.
- Return type:
- static enumerate_state_blocks(state_list)#
Enumerate the scalar blocks of a state definition with a rich key.
The key
(name, size, spice_id, frame_name, occurrence)fully disambiguates repeated parameters across bodies and occurrences, so it can project values between a global state and per-leg definitions with one consistent keying convention.- Parameters:
state_list (
list) – State-definition entries (6-tuples(name, size, est, dyn, body, awf)).- Yields:
- static equal_along_rows(arr: ndarray) bool#
Check equality along rows of the given array.
- Parameters:
arr (
numpy.ndarray) – The array to examine.- Returns:
are_equal – Returns
Trueif all values are equal along rows. ReturnsFalseif they are not.- Return type:
- static find_date_indices_in_epochs(epochsTDB, target_dates, rtol=1e-06)#
Find the index or indices of target date(s) in a numpy array of epochs, accounting for floating-point precision.
- Parameters:
epochsTDB (
EpochArray) – Reference epoch array to search within.target_dates (
EpochArrayorlistofEpochArray) – Target epoch(s) to locate inside epochsTDB.rtol (
float, optional) – Relative tolerance for comparing the dates. Defaults to1e-6.
- Returns:
ind – The index or indices of the target date(s), or
Noneif not found.- Return type:
- static max_of_last_slice(arr: ndarray)#
Finds the maximum value of the last slice of an array.
- Parameters:
arr (
numpy.ndarray) – The array to examine.- Returns:
max_vals – The max value of the last slice.
- Return type:
Any
- static reorder_covariance(Q, P, new_block_variances=None)#
Reorder a covariance:
P' = Q P Q^T, with a-priori variances for new states.- Parameters:
Q (
numpy.ndarray) – Reorder operator frombuild_reorder_operator().P (
array_like) – Old(old_n, old_n)covariance.new_block_variances (
listof(int,array_like), optional) –(start, variances)for each expansion block;variancesis the 1-D array of a-priori variances (\(\sigma^2\)) placed on that block’s diagonal. Off-diagonal (cross) terms stay zero (P_yz = 0).
- Returns:
The
(new_n, new_n)reordered covariance.- Return type:
- static reorder_stm(Q, STM, new_blocks=None)#
Reorder a State Transition Matrix:
Phi' = Q Phi Q^T.Newly augmented states (zero rows of
Q) receive a decoupled identity block, matching the convention that a fresh parameter starts uncorrelated with unit STM.- Parameters:
Q (
numpy.ndarray) – Reorder operator frombuild_reorder_operator().STM (
array_like) – Old(old_n, old_n)State Transition Matrix.new_blocks (
listof(str,int,int), optional) –(name, start, size)expansion blocks (as returned bybuild_reorder_operator()) to set to identity.
- Returns:
The
(new_n, new_n)reordered STM.- Return type:
- static reorder_vector(Q, x)#
Apply the reorder operator to a state / deviation vector:
X' = Q X.- Parameters:
Q (
numpy.ndarray) – Reorder operator frombuild_reorder_operator().x (
array_like) – Old state or deviation vector (old_nelements, any shape).
- Returns:
The reordered vector as an
(new_n, 1)column. Newly augmented states map to zero.- Return type:
- static tile_array(arr, desired_shape)#
Construct an array by repeating A the number of times given by reps.
- Parameters:
arr (
numpy.ndarray) – The array to examine.desired_shape (
tupleofint) – Target shape of the output array. The last dimension must equallen(arr); all preceding dimensions specify how many times to repeat the array along each axis.
- Returns:
tiled – The new tiled array.
- Return type: