DAMIT Asteroid Models¶
DAMIT asteroid shape model download, parsing, spin computation, and mesh export.
See the DAMIT User Guide for concepts and usage.
Download¶
Download and extract the DAMIT complete export archive.
Provides helpers to stream-download the large (~1.3 GB) DAMIT complete export tar.gz file and extract it to a directory for fast per-file access. Uses streaming with 1 MB chunks to avoid holding the entire file in memory, and writes to a temporary file with atomic rename on completion.
DAMIT_URL = 'https://damit.cuni.cz/projects/damit/exports/complete/latest'
module-attribute
¶
Default URL for the DAMIT complete export (tar.gz).
download_damit_file(filepath, *, url=DAMIT_URL, timeout=_DEFAULT_TIMEOUT)
¶
Stream-download the DAMIT complete export to filepath.
Creates parent directories if they do not exist. The file is first
written to a .tmp sibling and atomically renamed on successful
completion, preventing partial downloads from corrupting the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Destination path for the downloaded archive. |
required |
url
|
str
|
URL to fetch. Defaults to :data: |
DAMIT_URL
|
timeout
|
float
|
HTTP timeout in seconds. Defaults to 600 (10 minutes). |
_DEFAULT_TIMEOUT
|
Returns:
| Name | Type | Description |
|---|---|---|
Resolved |
Path
|
class: |
Raises:
| Type | Description |
|---|---|
HTTPStatusError
|
If the server returns a non-2xx status. |
TransportError
|
On network-level failures (DNS, timeout, etc.). |
extract_damit_archive(tar_path, extract_dir)
¶
Extract the DAMIT tar.gz archive to extract_dir.
Uses tarfile.extractall with the data filter on Python 3.12+
for safety, falling back to manual member filtering on older versions.
A .extracted marker file is written on success so callers can
detect whether the extraction is up-to-date relative to the archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tar_path
|
str | Path
|
Path to the DAMIT |
required |
extract_dir
|
str | Path
|
Directory to extract into. Created if it does not exist. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Resolved |
Path
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If tar_path does not exist. |
TarError
|
On archive corruption or read errors. |
Parsers¶
Parsing utilities for the DAMIT asteroid shape model dataset.
Extracts CSV tables and shape files from the DAMIT complete export
tar.gz archive. The archive contains tables/ with CSV metadata
and files/ with per-asteroid shape models.
The canonical data source is asteroid_models.csv which contains
all spin parameters (lambda, beta, period, etc.) alongside physical
properties, eliminating the need to parse individual spin.txt files.
build_shape_index(files_dir)
¶
Build a mapping from model ID to relative shape file path.
Walks the files/ directory tree looking for directories matching
model_<N> that contain a shape.txt file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files_dir
|
Path
|
The |
required |
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
Dictionary mapping model ID (int) to the relative path from |
dict[int, str]
|
files_dir to the |
dict[int, str]
|
|
load_shape_for_model(filepath, model_id, *, extracted_dir=None)
¶
Load the shape mesh for a specific DAMIT model.
When extracted_dir is provided, looks for the shape file directly on disk (fast O(1) read). Otherwise falls back to scanning the tar.gz archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to the DAMIT |
required |
model_id
|
int
|
DAMIT model ID (from the |
required |
extracted_dir
|
str | Path | None
|
Optional path to the extracted archive directory. When provided, the shape file is read directly from disk. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Array, Array]
|
Tuple of |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath does not exist. |
KeyError
|
If no shape file is found for the given model_id. |
parse_damit_asteroids_table(filepath, *, extracted_dir=None)
¶
Parse the asteroids.csv table from a DAMIT tar.gz archive.
Reads tables/asteroids.csv from the compressed archive and
returns a Polars DataFrame with asteroid identity information.
The created and modified timestamp columns are dropped.
When extracted_dir is provided and contains the expected CSV file, it is read directly from disk (fast path) instead of decompressing the tar.gz archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to the DAMIT |
required |
extracted_dir
|
str | Path | None
|
Optional path to the extracted archive directory. When provided, the CSV is read directly from disk. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with columns: |
DataFrame
|
(Int64), |
DataFrame
|
(Utf8). |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath does not exist. |
ValueError
|
If the archive does not contain |
parse_damit_models_table(filepath, *, extracted_dir=None)
¶
Parse the asteroid_models.csv table from a DAMIT tar.gz archive.
This table is the canonical source for spin parameters (lambda, beta, period, yorp, jd0, phi0) as well as physical properties (diameter, albedo, thermal inertia) and model quality flags.
When extracted_dir is provided and contains the expected CSV file, it is read directly from disk (fast path) instead of decompressing the tar.gz archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to the DAMIT |
required |
extracted_dir
|
str | Path | None
|
Optional path to the extracted archive directory. When provided, the CSV is read directly from disk. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with spin and physical property columns. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If filepath does not exist. |
ValueError
|
If the archive does not contain |
parse_shape_file(shape_text)
¶
Parse a DAMIT shape.txt file into vertices and facets.
The file format is:
- Line 1:
N_vertices N_facets - Next N_vertices lines:
x y zwhitespace-separated coordinates - Next N_facets lines:
v1 v2 v3whitespace-separated 1-indexed vertex indices
Facet indices are converted from 1-indexed to 0-indexed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape_text
|
str
|
Raw text content of a |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Tuple of |
Array
|
|
tuple[Array, Array]
|
int32 JAX array with 0-indexed vertex references. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file format is invalid. |
write_shape_index(extracted_dir)
¶
Build and persist the shape index JSON file.
Locates the files/ directory inside extracted_dir, builds
the model-ID-to-path mapping, and writes it as
extracted_dir/shape_index.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extracted_dir
|
Path
|
Root of the extracted archive. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the written |
Providers¶
Factory functions for loading the DAMIT asteroid shape model dataset.
Provides convenience functions for loading DAMIT data as Polars DataFrames and JAX arrays:
- :func:
load_damit_asteroids: Load asteroid identity table from cache. - :func:
load_damit_models: Load model/spin parameter table from cache. - :func:
get_damit_spin: Look up spin parameters for a specific asteroid. - :func:
get_damit_shape: Load shape mesh for a specific model.
get_damit_shape(filepath=None, *, model_id, max_age_days=_DEFAULT_MAX_AGE_DAYS)
¶
Load the shape mesh for a specific DAMIT model.
Downloads the DAMIT archive if needed, extracts it, then reads
model_{model_id}/shape.txt directly from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path | None
|
Path to the cached tar.gz file. When |
None
|
model_id
|
int
|
DAMIT model ID (from the |
required |
max_age_days
|
float
|
Maximum acceptable age of the cached file in days. Defaults to 30. |
_DEFAULT_MAX_AGE_DAYS
|
Returns:
| Type | Description |
|---|---|
Array
|
Tuple of |
Array
|
|
tuple[Array, Array]
|
int32 JAX array with 0-indexed vertex references. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the download fails and no cached file exists. |
KeyError
|
If no shape file exists for model_id. |
Examples:
get_damit_spin(models_df, asteroid_id, *, model_index=0)
¶
Look up spin parameters for an asteroid from a pre-loaded models DataFrame.
Filters the models table by asteroid_id and returns the row at
model_index as a dictionary. Multiple models may exist for a
single asteroid; use model_index to select among them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models_df
|
DataFrame
|
Polars DataFrame as returned by :func: |
required |
asteroid_id
|
int
|
DAMIT asteroid ID (from the |
required |
model_index
|
int
|
Zero-based index among models for this asteroid. Defaults to 0 (first/best model). |
0
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with all columns from the matched row, including |
dict
|
spin parameters |
dict
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If no models exist for asteroid_id or model_index is out of range. |
Examples:
load_damit_asteroids(filepath=None, *, max_age_days=_DEFAULT_MAX_AGE_DAYS)
¶
Load the DAMIT asteroids table from a local cache, downloading when stale.
Checks whether the cached tar.gz at filepath exists and is younger than max_age_days. If the file is missing or stale, a fresh copy is downloaded from the DAMIT server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path | None
|
Path to the cached tar.gz file. When |
None
|
max_age_days
|
float
|
Maximum acceptable age of the cached file in days. Defaults to 30. |
_DEFAULT_MAX_AGE_DAYS
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with columns: |
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the download fails and no cached file exists. |
Examples:
load_damit_models(filepath=None, *, max_age_days=_DEFAULT_MAX_AGE_DAYS)
¶
Load the DAMIT asteroid models table from a local cache, downloading when stale.
The models table contains spin parameters (lambda, beta, period, yorp, jd0, phi0) and physical properties (diameter, albedo, thermal inertia) for all principal-axis rotator models in DAMIT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path | None
|
Path to the cached tar.gz file. When |
None
|
max_age_days
|
float
|
Maximum acceptable age of the cached file in days. Defaults to 30. |
_DEFAULT_MAX_AGE_DAYS
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with spin parameter and physical property |
DataFrame
|
columns for all DAMIT models. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the download fails and no cached file exists. |
Examples:
Spin Computation¶
JAX rotation and shape operations for DAMIT spin parameters.
Computes body-fixed-to-ecliptic rotation matrices from DAMIT spin
parameters, and provides utilities for scaling and rotating asteroid
shape vertices. All functions use JAX primitives and are compatible
with jax.jit, jax.vmap, and jax.grad.
The rotation model for principal-axis rotators is:
.. math::
R = R_z(\lambda) \cdot R_y(90^\circ - \beta) \cdot R_z(\phi_0 + \frac{360}{P} (t - t_0) \cdot 24 + \frac{1}{2} \dot{\omega} (t - t_0)^2)
where :math:(t - t_0) is in days, :math:P is in hours, and
:math:\dot{\omega} (YORP) is in deg/day².
damit_spin_to_rotation(spin_params, target_jd)
¶
Compute the body-to-ecliptic rotation matrix from DAMIT spin parameters.
Implements the principal-axis rotation model used by DAMIT:
R = Rz(lambda) @ Ry(90 - beta) @ Rz(phi0 + 360/P * (t-t0) * 24 + 0.5 * yorp * (t-t0)^2)
where time differences are in days, period P is in hours, and YORP acceleration is in deg/day^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spin_params
|
ArrayLike
|
6-element array |
required |
target_jd
|
ArrayLike
|
Julian date at which to evaluate the rotation. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
3x3 rotation matrix (body-fixed to ecliptic). |
Examples:
rotate_shape_points(spin_params, target_jd, vertices)
¶
Rotate shape vertices from body-fixed to ecliptic frame.
Computes the rotation matrix via :func:damit_spin_to_rotation
and applies it to all vertex positions: (R @ vertices.T).T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spin_params
|
ArrayLike
|
6-element spin parameter array (see
:func: |
required |
target_jd
|
ArrayLike
|
Julian date at which to evaluate the rotation. |
required |
vertices
|
ArrayLike
|
|
required |
Returns:
| Type | Description |
|---|---|
Array
|
|
Examples:
scale_shape_vertices(vertices, max_extent_m)
¶
Rescale shape vertices so the furthest vertex is at a given distance.
Normalizes the vertex cloud by its current maximum extent (distance from origin to furthest vertex), then scales to max_extent_m.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ArrayLike
|
|
required |
max_extent_m
|
ArrayLike
|
Desired maximum extent in meters (distance from origin to the furthest vertex after scaling). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
|
Examples:
Mesh Export¶
Mesh export utilities for DAMIT asteroid shape models.
Provides functions to convert DAMIT shape data (vertices and facets)
into standard 3D mesh formats (GLB, STL) via the optional trimesh
library. Functions raise :class:ImportError with install instructions
if trimesh is not available.
compute_spherical_uvs(vertices)
¶
Compute spherical-projection UV coordinates for mesh vertices.
Projects each vertex onto a unit sphere centred at the mesh centroid
and maps the resulting spherical coordinates to the [0, 1] UV range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ArrayLike
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
ndarray
|
(azimuth) and |
Examples:
export_shape_glb(vertices, facets, filepath)
¶
Export DAMIT shape data to a GLB (binary glTF) file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ArrayLike
|
|
required |
facets
|
ArrayLike
|
|
required |
filepath
|
str | Path
|
Destination path for the GLB file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Resolved |
Path
|
class: |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Examples:
export_shape_glb_textured(vertices, facets, filepath, texture_image)
¶
Export DAMIT shape data to a textured GLB (binary glTF) file.
Applies a texture to the mesh using spherical UV projection. The texture is embedded as a PBR base-color map in the exported GLB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ArrayLike
|
|
required |
facets
|
ArrayLike
|
|
required |
filepath
|
str | Path
|
Destination path for the GLB file. |
required |
texture_image
|
Image
|
PIL Image to use as the base-color texture. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Resolved |
Path
|
class: |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Examples:
export_shape_stl(vertices, facets, filepath)
¶
Export DAMIT shape data to an STL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ArrayLike
|
|
required |
facets
|
ArrayLike
|
|
required |
filepath
|
str | Path
|
Destination path for the STL file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Resolved |
Path
|
class: |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Examples:
shape_to_trimesh(vertices, facets)
¶
Convert DAMIT shape arrays to a trimesh Trimesh object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ArrayLike
|
|
required |
facets
|
ArrayLike
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
Trimesh
|
class: |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Examples: