A Python wrapper for the NAIF CSPICE Toolkit providing essential tools for spacecraft navigation and planetary science calculations
Low-level functions for direct access to SPICE data files using DAF (Double Precision Array Files) and DAS (Direct Access, Segregated) architectures. These functions provide advanced file manipulation capabilities for users who need direct control over SPICE file structures.
Direct access to double precision array files like SPK and CK kernels.
def dafopr(fname: str) -> int:
"""
Open DAF for reading.
Parameters:
- fname: str, DAF file name
Returns:
int: file handle
"""
def dafopw(fname: str) -> int:
"""
Open DAF for writing.
Parameters:
- fname: str, DAF file name
Returns:
int: file handle
"""
def dafcls(handle: int) -> None:
"""
Close DAF file.
Parameters:
- handle: int, file handle
Returns:
None
"""Search for segments and arrays within DAF files.
def dafbfs(handle: int) -> None:
"""
Begin forward search in DAF.
Parameters:
- handle: int, DAF file handle
Returns:
None
"""
def dafbbs(handle: int) -> None:
"""
Begin backward search in DAF.
Parameters:
- handle: int, DAF file handle
Returns:
None
"""
def daffna() -> bool:
"""
Find next array in DAF.
Returns:
bool: True if array found
"""
def daffpa() -> bool:
"""
Find previous array in DAF.
Returns:
bool: True if array found
"""
def dafgn() -> str:
"""
Get name of current DAF array.
Returns:
str: array name
"""
def dafgs() -> ndarray:
"""
Get summary of current DAF array.
Returns:
ndarray: array summary (double precision)
"""Read and write data arrays within DAF files.
def dafgda(handle: int, begin: int, end: int) -> ndarray:
"""
Get data from DAF array.
Parameters:
- handle: int, DAF file handle
- begin: int, starting address
- end: int, ending address
Returns:
ndarray: data array
"""
def dafrda(handle: int, begin: int, end: int) -> ndarray:
"""
Read data from DAF file.
Parameters:
- handle: int, DAF file handle
- begin: int, starting address
- end: int, ending address
Returns:
ndarray: data array
"""
def dafada(handle: int, data: ndarray) -> None:
"""
Add data to DAF array.
Parameters:
- handle: int, DAF file handle
- data: ndarray, data to add
Returns:
None
"""Manage DAF file structure and metadata.
def dafhsf(handle: int, nd: int, ni: int) -> bool:
"""
Does DAF handle have a specific structure.
Parameters:
- handle: int, DAF file handle
- nd: int, number of double precision components
- ni: int, number of integer components
Returns:
bool: True if structure matches
"""
def dafps(nd: int, ni: int, dc: ndarray, ic: ndarray) -> ndarray:
"""
Pack DAF summary.
Parameters:
- nd: int, number of double precision components
- ni: int, number of integer components
- dc: ndarray, double precision components
- ic: ndarray, integer components
Returns:
ndarray: packed summary
"""
def dafus(sum: ndarray, nd: int, ni: int) -> Tuple[ndarray, ndarray]:
"""
Unpack DAF summary.
Parameters:
- sum: ndarray, packed summary
- nd: int, number of double precision components
- ni: int, number of integer components
Returns:
Tuple[ndarray, ndarray]: (dc, ic) components
"""Direct access to segregated data files with support for multiple data types.
def dasopn(fname: str, ftype: str, ncomch: int) -> int:
"""
Open new DAS file for writing.
Parameters:
- fname: str, DAS file name
- ftype: str, file type
- ncomch: int, number of comment characters
Returns:
int: file handle
"""
def dasopr(fname: str) -> int:
"""
Open DAS file for reading.
Parameters:
- fname: str, DAS file name
Returns:
int: file handle
"""
def dascls(handle: int) -> None:
"""
Close DAS file.
Parameters:
- handle: int, file handle
Returns:
None
"""Read and write different data types to DAS files.
def dasadd(handle: int, data: ndarray) -> None:
"""
Add double precision data to DAS file.
Parameters:
- handle: int, DAS file handle
- data: ndarray, double precision data
Returns:
None
"""
def dasadi(handle: int, data: ndarray) -> None:
"""
Add integer data to DAS file.
Parameters:
- handle: int, DAS file handle
- data: ndarray, integer data
Returns:
None
"""
def dasadc(handle: int, data: List[str]) -> None:
"""
Add character data to DAS file.
Parameters:
- handle: int, DAS file handle
- data: List[str], character data
Returns:
None
"""
def dasrdd(handle: int, first: int, last: int) -> ndarray:
"""
Read double precision data from DAS file.
Parameters:
- handle: int, DAS file handle
- first: int, first address
- last: int, last address
Returns:
ndarray: double precision data
"""
def dasrdi(handle: int, first: int, last: int) -> ndarray:
"""
Read integer data from DAS file.
Parameters:
- handle: int, DAS file handle
- first: int, first address
- last: int, last address
Returns:
ndarray: integer data
"""
def dasrdc(handle: int, first: int, last: int, lenout: int) -> List[str]:
"""
Read character data from DAS file.
Parameters:
- handle: int, DAS file handle
- first: int, first address
- last: int, last address
- lenout: int, maximum string length
Returns:
List[str]: character data
"""Query DAS file properties and metadata.
def dashfs(handle: int) -> Tuple[int, int, int, int, int, int, int, int, int]:
"""
Get DAS file summary.
Parameters:
- handle: int, DAS file handle
Returns:
Tuple[int, ...]: (nresvr, nresvc, ncomr, ncomc, free, lastla, lastrc, lastwd, lastc)
"""
def daslla(handle: int, dtype: int) -> int:
"""
Get last logical address for data type in DAS file.
Parameters:
- handle: int, DAS file handle
- dtype: int, data type (1=char, 2=double, 3=int)
Returns:
int: last logical address
"""
def dasluh(handle: int) -> int:
"""
Get last used handle in DAS file system.
Parameters:
- handle: int, DAS file handle
Returns:
int: highest handle in use
"""import spiceypy as spice
import numpy as np
# Open DAF file for reading
handle = spice.dafopr("ephemeris.bsp")
try:
# Begin forward search
spice.dafbfs(handle)
# Search through all arrays
found = spice.daffna()
array_count = 0
while found:
# Get array information
name = spice.dafgn()
summary = spice.dafgs()
print(f"Array {array_count}: {name}")
print(f" Summary: {summary[:8]}") # First 8 elements
# Unpack summary (assuming SPK structure: 2 doubles, 6 integers)
dc, ic = spice.dafus(summary, 2, 6)
print(f" Time range: {dc[0]} to {dc[1]}")
print(f" Body ID: {ic[0]}, Center: {ic[1]}")
# Find next array
found = spice.daffna()
array_count += 1
print(f"Total arrays found: {array_count}")
finally:
# Always close the file
spice.dafcls(handle)import spiceypy as spice
# Open SPK file
handle = spice.dafopr("ephemeris.bsp")
try:
# Search for specific body
target_body = 399 # Earth
spice.dafbfs(handle)
found = spice.daffna()
while found:
summary = spice.dafgs()
dc, ic = spice.dafus(summary, 2, 6)
if ic[0] == target_body: # Found target body
print(f"Found data for body {target_body}")
# Calculate data addresses (simplified example)
# Note: Real SPK parsing is much more complex
begin_addr = int(ic[4]) # Starting address
end_addr = int(ic[5]) # Ending address
if end_addr > begin_addr:
# Extract some data (first 10 values as example)
data = spice.dafgda(handle, begin_addr, min(begin_addr + 9, end_addr))
print(f"First 10 data values: {data}")
break
found = spice.daffna()
finally:
spice.dafcls(handle)import spiceypy as spice
import numpy as np
# Create new DAS file
handle = spice.dasopn("test_data.das", "TEST", 0)
try:
# Add different types of data
double_data = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
integer_data = np.array([10, 20, 30, 40, 50], dtype=int)
char_data = ["Hello", "World", "DAS", "File"]
# Write data to file
spice.dasadd(handle, double_data)
spice.dasadi(handle, integer_data)
spice.dasadc(handle, char_data)
print("Data written to DAS file")
# Get file summary
summary = spice.dashfs(handle)
print(f"File summary: {summary}")
finally:
# Close file
spice.dascls(handle)
# Read data back
handle = spice.dasopr("test_data.das")
try:
# Get last addresses for each data type
last_double = spice.daslla(handle, 2) # Double precision
last_int = spice.daslla(handle, 3) # Integer
last_char = spice.daslla(handle, 1) # Character
print(f"Last addresses - Double: {last_double}, Int: {last_int}, Char: {last_char}")
if last_double > 0:
# Read double precision data
doubles = spice.dasrdd(handle, 1, last_double)
print(f"Double data: {doubles}")
if last_int > 0:
# Read integer data
integers = spice.dasrdi(handle, 1, last_int)
print(f"Integer data: {integers}")
if last_char > 0:
# Read character data
chars = spice.dasrdc(handle, 1, last_char, 10)
print(f"Character data: {chars}")
finally:
spice.dascls(handle)import spiceypy as spice
# Analyze DAF file structure
def analyze_daf_structure(filename):
handle = spice.dafopr(filename)
try:
print(f"Analyzing DAF file: {filename}")
# Check file structure for different kernel types
structures = [
(2, 6, "SPK"), # SPK: 2 doubles, 6 integers
(1, 5, "CK"), # CK: 1 double, 5 integers
(2, 5, "PCK"), # PCK: 2 doubles, 5 integers
]
detected_type = None
for nd, ni, ktype in structures:
if spice.dafhsf(handle, nd, ni):
detected_type = ktype
break
if detected_type:
print(f"Detected kernel type: {detected_type}")
else:
print("Unknown kernel type")
# Count arrays
spice.dafbfs(handle)
count = 0
found = spice.daffna()
while found:
count += 1
found = spice.daffna()
print(f"Number of data arrays: {count}")
finally:
spice.dafcls(handle)
# Example usage
analyze_daf_structure("ephemeris.bsp")Note: These low-level functions require deep understanding of SPICE file formats and are primarily intended for advanced users developing custom tools or debugging SPICE data files. Most users should prefer the higher-level functions in other capability areas.
Install with Tessl CLI
npx tessl i tessl/pypi-spiceypydocs