DTrajectory¶
Dynamic-dimension trajectory container for N-dimensional state data.
DTrajectory
¶
Dynamic-dimension trajectory container.
Stores a sequence of N-dimensional states at specific epochs with support for interpolation and automatic state eviction policies. Dimension is determined at runtime.
Initialize instance.
__doc__
class-attribute
¶
__doc__ = 'Dynamic-dimension trajectory container.\n\nStores a sequence of N-dimensional states at specific epochs with support\nfor interpolation and automatic state eviction policies. Dimension is\ndetermined at runtime.'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to 'utf-8'. errors defaults to 'strict'.
__module__
class-attribute
¶
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to 'utf-8'. errors defaults to 'strict'.
length
property
¶
length: int
Get the number of states in the trajectory.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of states in the trajectory |
__new__
builtin
¶
Create and return a new object. See help(type) for accurate signature.
add
method descriptor
¶
dimension
method descriptor
¶
dimension() -> int
Get the trajectory dimension (method form).
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Dimension of the trajectory |
epoch_at_idx
method descriptor
¶
first
method descriptor
¶
first() -> Tuple
Get the first (epoch, state) tuple in the trajectory, if any exists.
Returns:
| Type | Description |
|---|---|
Tuple
|
tuple or None: Tuple of (Epoch, numpy.ndarray) for first state, or None if empty |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc, state)
first_epc, first_state = traj.first()
from_data
builtin
¶
from_data(epochs: list[Epoch], states: ndarray, interpolation_method: InterpolationMethod = None) -> DTrajectory
Create a trajectory from existing data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epochs
|
list[Epoch]
|
List of time epochs |
required |
states
|
ndarray
|
2D array of states with shape (num_epochs, dimension) where each row is a state vector |
required |
interpolation_method
|
InterpolationMethod
|
Interpolation method (default Linear) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DTrajectory |
DTrajectory
|
New trajectory instance populated with data |
get
method descriptor
¶
Get both epoch and state at a specific index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index to retrieve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple
|
Tuple of (Epoch, numpy.ndarray) for epoch and state at the index |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc, state)
ret_epc, ret_state = traj.get(0)
get_eviction_policy
method descriptor
¶
get_eviction_policy() -> str
Get current eviction policy.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
String representation of eviction policy |
get_interpolation_method
method descriptor
¶
get_interpolation_method() -> InterpolationMethod
Get interpolation method.
Returns:
| Name | Type | Description |
|---|---|---|
InterpolationMethod |
InterpolationMethod
|
Current interpolation method |
index_after_epoch
method descriptor
¶
Get the index of the state at or after the given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Index of the state at or after the target epoch |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 11, 59, 0.0, 0.0, bh.TimeSystem.UTC)
index = traj.index_after_epoch(epc2)
index_before_epoch
method descriptor
¶
Get the index of the state at or before the given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Index of the state at or before the target epoch |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 12, 1, 0.0, 0.0, bh.TimeSystem.UTC)
index = traj.index_before_epoch(epc2)
interpolate
method descriptor
¶
Interpolate state at a given epoch using the configured interpolation method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: Interpolated state vector |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state1 = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state1)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 12, 2, 0.0, 0.0, bh.TimeSystem.UTC)
state2 = np.array([bh.R_EARTH + 510e3, 0.0, 0.0, 0.0, 7650.0, 0.0])
traj.add(epc2, state2)
epc_mid = bh.Epoch.from_datetime(2024, 1, 1, 12, 1, 0.0, 0.0, bh.TimeSystem.UTC)
state_interp = traj.interpolate(epc_mid)
interpolate_linear
method descriptor
¶
Interpolate state at a given epoch using linear interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: Linearly interpolated state vector |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state1 = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state1)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 12, 2, 0.0, 0.0, bh.TimeSystem.UTC)
state2 = np.array([bh.R_EARTH + 510e3, 0.0, 0.0, 0.0, 7650.0, 0.0])
traj.add(epc2, state2)
epc_mid = bh.Epoch.from_datetime(2024, 1, 1, 12, 1, 0.0, 0.0, bh.TimeSystem.UTC)
state_interp = traj.interpolate_linear(epc_mid)
is_empty
method descriptor
¶
is_empty() -> bool
Check if trajectory is empty.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if trajectory contains no states, False otherwise |
last
method descriptor
¶
last() -> Tuple
Get the last (epoch, state) tuple in the trajectory, if any exists.
Returns:
| Type | Description |
|---|---|
Tuple
|
tuple or None: Tuple of (Epoch, numpy.ndarray) for last state, or None if empty |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc, state)
last_epc, last_state = traj.last()
len
method descriptor
¶
len() -> int
Get the number of states in the trajectory (alias for length).
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of states in the trajectory |
nearest_state
method descriptor
¶
Get the nearest state to a given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple
|
Tuple of (Epoch, numpy.ndarray) containing the nearest state |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 30.0, 0.0, bh.TimeSystem.UTC)
nearest_epc, nearest_state = traj.nearest_state(epc2)
remove
method descriptor
¶
Remove a state at a specific index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the state to remove |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple
|
Tuple of (Epoch, numpy.ndarray) for the removed epoch and state |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc, state)
removed_epc, removed_state = traj.remove(0)
remove_epoch
method descriptor
¶
Remove a state at a specific epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Epoch of the state to remove |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: The removed state vector |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc, state)
removed_state = traj.remove_epoch(epc)
set_eviction_policy_max_age
method descriptor
¶
Set maximum age for trajectory states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age
|
float
|
Maximum age in seconds relative to most recent state |
required |
set_eviction_policy_max_size
method descriptor
¶
Set maximum trajectory size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum number of states to retain |
required |
set_interpolation_method
method descriptor
¶
set_interpolation_method(method: InterpolationMethod) -> Any
Set interpolation method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
InterpolationMethod
|
New interpolation method |
required |
state_after_epoch
method descriptor
¶
Get the state at or after the given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple
|
Tuple of (Epoch, numpy.ndarray) containing state at or after the target epoch |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 11, 59, 0.0, 0.0, bh.TimeSystem.UTC)
ret_epc, ret_state = traj.state_after_epoch(epc2)
state_at_idx
method descriptor
¶
state_before_epoch
method descriptor
¶
Get the state at or before the given epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
Epoch
|
Target epoch |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple
|
Tuple of (Epoch, numpy.ndarray) containing state at or before the target epoch |
Example
import brahe as bh
import numpy as np
traj = bh.OrbitTrajectory(bh.OrbitFrame.ECI, bh.OrbitRepresentation.CARTESIAN, None)
epc1 = bh.Epoch.from_datetime(2024, 1, 1, 12, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([bh.R_EARTH + 500e3, 0.0, 0.0, 0.0, 7600.0, 0.0])
traj.add(epc1, state)
epc2 = bh.Epoch.from_datetime(2024, 1, 1, 12, 1, 0.0, 0.0, bh.TimeSystem.UTC)
ret_epc, ret_state = traj.state_before_epoch(epc2)
with_eviction_policy_max_age
method descriptor
¶
with_eviction_policy_max_age(max_age: float) -> DTrajectory
Set eviction policy to keep states within maximum age using builder pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age
|
float
|
Maximum age of states in seconds |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DTrajectory |
DTrajectory
|
Self with updated eviction policy |
with_eviction_policy_max_size
method descriptor
¶
with_eviction_policy_max_size(max_size: int) -> DTrajectory
Set eviction policy to keep maximum number of states using builder pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum number of states to retain |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DTrajectory |
DTrajectory
|
Self with updated eviction policy |
with_interpolation_method
method descriptor
¶
with_interpolation_method(interpolation_method: InterpolationMethod) -> DTrajectory
Set interpolation method using builder pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interpolation_method
|
InterpolationMethod
|
Interpolation method to use |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DTrajectory |
DTrajectory
|
Self with updated interpolation method |
Overview¶
DTrajectory is a flexible trajectory container where the state dimension is determined at runtime. Use this when you need to store state vectors of varying sizes or when the dimension isn't known at compile time.
Module: brahe.trajectories
Key Features: - Runtime dimension specification - Automatic time-ordering of states - Configurable interpolation (Linear or Lagrange) - Eviction policies for memory management - Efficient state queries and interpolation
Creating a Trajectory¶
import brahe as bh
# Create with specified dimension
traj = bh.DTrajectory(dimension=6) # For orbital states
# Create with specific interpolation method
traj = bh.DTrajectory(
dimension=3,
interpolation_method=bh.InterpolationMethod.LAGRANGE
)
# Create from existing data
import numpy as np
epochs = [epoch1, epoch2, epoch3]
states = np.array([[x1,y1,z1], [x2,y2,z2], [x3,y3,z3]])
traj = bh.DTrajectory.from_data(
epochs,
states,
interpolation_method=bh.InterpolationMethod.LINEAR
)
Adding States¶
import brahe as bh
import numpy as np
traj = bh.DTrajectory(dimension=6)
# Add single state
epoch = bh.Epoch.from_datetime(2024, 1, 1, 0, 0, 0.0, 0.0, bh.TimeSystem.UTC)
state = np.array([7e6, 0, 0, 0, 7.5e3, 0])
traj.add(epoch, state)
# Add multiple states from propagation
prop = bh.KeplerianPropagator(...)
for i in range(100):
future_epoch = epoch + i * 60.0 # Every minute
state = prop.propagate(future_epoch)
traj.add(future_epoch, state)
Querying States¶
# Interpolate at specific epoch
query_epoch = epoch + 1800.0 # 30 minutes later
interp_state = traj.interpolate(query_epoch)
# Get state at specific index
state_10 = traj.state(10)
epoch_10 = traj.epoch(10)
# Get first and last states
first_epoch, first_state = traj.first()
last_epoch, last_state = traj.last()
# Get state before/after epoch
before_epoch, before_state = traj.state_before_epoch(query_epoch)
after_epoch, after_state = traj.state_after_epoch(query_epoch)
# Get all data
all_states = traj.to_matrix() # Returns numpy array (n_states, dimension)
all_epochs = traj.to_epochs() # Returns list of Epochs
Eviction Policies¶
Control memory usage by automatically removing old states:
# Maximum age: keep only states within 1 hour of newest
traj.set_eviction_policy_max_age(3600.0)
# Maximum size: keep only last 1000 states
traj.set_eviction_policy_max_size(1000)
# No eviction (default)
traj.set_eviction_policy_no_eviction()
# Builder pattern (method chaining)
traj = bh.DTrajectory(dimension=6) \
.with_eviction_policy_max_age(3600.0) \
.with_interpolation_method(bh.InterpolationMethod.LAGRANGE)
Interpolation Methods¶
# Linear interpolation (faster, less accurate)
traj.set_interpolation_method(bh.InterpolationMethod.LINEAR)
# Lagrange interpolation (slower, more accurate)
traj.set_interpolation_method(bh.InterpolationMethod.LAGRANGE)
Trajectory Information¶
# Get dimension
dim = traj.dimension() # Returns dimension of state vectors
# Get size
n_states = traj.len() # Number of states stored
# Check if empty
is_empty = traj.is_empty()
# Get time span
span = traj.timespan() # Duration in seconds from first to last
# Get start/end epochs
start = traj.start_epoch()
end = traj.end_epoch()
Clearing and Removing States¶
# Clear all states
traj.clear()
# Remove state at specific epoch
removed_state = traj.remove_epoch(epoch)
# Remove state at index
removed_epoch, removed_state = traj.remove_at(index)
Complete Example¶
import brahe as bh
import numpy as np
import matplotlib.pyplot as plt
# Set up propagator
epoch_start = bh.Epoch.from_datetime(2024, 1, 1, 0, 0, 0.0, 0.0, bh.TimeSystem.UTC)
elements = np.array([7000e3, 0.001, 98*bh.DEG2RAD, 0, 0, 0])
prop = bh.KeplerianPropagator(
epoch=epoch_start,
elements=elements,
element_type=bh.OrbitRepresentation.MEAN_ELEMENTS,
frame=bh.OrbitFrame.ECI
)
# Create trajectory with eviction policy
traj = bh.DTrajectory(dimension=6) \
.with_interpolation_method(bh.InterpolationMethod.LINEAR) \
.with_eviction_policy_max_size(1000)
# Propagate and store states
times = np.linspace(0, 86400, 1440) # 1 day, 1-minute steps
for dt in times:
epoch = epoch_start + dt
state = prop.propagate(epoch)
traj.add(epoch, state)
print(f"Stored {traj.len()} states")
print(f"Time span: {traj.timespan()/3600:.1f} hours")
# Interpolate at arbitrary times
query_times = np.linspace(0, 86400, 100)
altitudes = []
for dt in query_times:
query_epoch = epoch_start + dt
state = traj.interpolate(query_epoch)
altitude = (np.linalg.norm(state[:3]) - bh.R_EARTH) / 1000 # km
altitudes.append(altitude)
# Plot altitude profile
plt.plot(query_times/3600, altitudes)
plt.xlabel('Time (hours)')
plt.ylabel('Altitude (km)')
plt.title('Orbit Altitude Over 1 Day')
plt.grid(True)
plt.show()
See Also¶
- STrajectory6 - Fixed 6D trajectory (faster)
- OrbitTrajectory - Frame-aware orbital trajectory
- InterpolationMethod