mf4-rs
mf4-rs is a minimal Rust library for working with ASAM MDF 4 (Measurement Data Format) files. It supports parsing existing files as well as writing new ones through a safe API, implementing a subset of the MDF 4.1 specification sufficient for simple data logging and inspection tasks.
Architecture
High-Level Structure
The codebase is organized into distinct layers:
1. API Layer (src/api/)
- High-level user-facing API for working with MDF files
MDF- Main entry point for parsing files from diskChannelGroup- Wrapper providing ergonomic access to channel group metadataChannel- High-level channel representation with value decoding
2. Writer Module (src/writer/)
- MdfWriter - Core writer for creating MDF 4.1-compliant files
- Guarantees little-endian encoding, 8-byte alignment, and zero-padding
- Handles block linking and manages open data blocks during writing
- Supports both single record writing (
write_record) and batch operations (write_records)
3. Block Layer (src/blocks/)
- Low-level MDF block implementations matching the specification
- Each block type (HeaderBlock, ChannelBlock, ChannelGroupBlock, etc.) has parsing and serialization
- Conversion system supporting various data transformations (linear, formula, lookup tables)
- Common utilities for block headers and data type handling
4. Parsing Layer (src/parsing/)
- File parsing and memory management using memory-mapped files
- Raw block parsers that maintain references to memory-mapped data
- Channel value decoder supporting multiple data types
- Lazy evaluation - channels and values are decoded on demand
5. Utilities (src/)
cut.rs- Time-based file cutting functionalitymerge.rs- File merging utilitieserror.rs- Centralized error handlingindex.rs- MDF file indexing system for fast metadata-based access
Key Design Patterns
Memory-Mapped File Access: The parser uses memmap2 to avoid loading entire files into memory, enabling efficient handling of large measurement files.
Lazy Evaluation: Channel groups, channels, and values are created as lightweight wrappers that decode data only when accessed.
Builder Pattern: The writer uses closure-based configuration for channels and channel groups, allowing flexible setup while maintaining type safety.
Block Linking: The MDF format uses address-based linking between blocks. The writer maintains a position map to update links after blocks are written.
Usage
Building and Testing
# Build the project
# Run all tests
# Run specific test file
Examples
The project includes simplified examples in the examples/ directory:
write_file.rs- Comprehensive example of writing MDF files with multiple channelsread_file.rs- Demonstrates parsing and inspecting MDF filesindex_operations.rs- Shows advanced indexing, byte-range reading, and conversion resolutionmerge_files.rs- Merging multiple MF4 filescut_file.rs- Time-based file cuttingpython_equivalent.rs- Comparison with Python functionality
Run them with:
Working with MDF Files
Basic File Creation Pattern:
use MdfWriter;
use DataType;
use DecodedValue;
let mut writer = new?;
writer.init_mdf_file?;
let cg = writer.add_channel_group?;
// Create master channel (usually time)
let time_ch_id = writer.add_channel?;
writer.set_time_channel?; // Mark as master channel
// Add data channels with master as parent
writer.add_channel?;
writer.start_data_block_for_cg?;
writer.write_record?;
writer.finish_data_block?;
writer.finalize?;
Basic File Parsing Pattern:
use MDF;
let mdf = MDFfrom_file?;
for group in mdf.channel_groups
MDF Indexing System
The library includes a powerful indexing system that allows you to:
- Create lightweight JSON indexes of MDF files containing all metadata needed for data access
- Read channel data without full file parsing using only the index and targeted file I/O
- Serialize/deserialize indexes for persistent storage and sharing
- Support multiple data sources through the
ByteRangeReadertrait (local files, HTTP, S3, etc.)
Basic Indexing Workflow:
// Create an index from an MDF file
let index = from_file?;
// Save index to JSON for later use
index.save_to_file?;
// Later: load index and read specific channel data
let loaded_index = load_from_file?;
// Option 1: Use built-in file reader
let mut file_reader = new?;
let channel_values = loaded_index.read_channel_values?;
// Option 2: Use HTTP range reader (production)
let mut http_reader = new;
let channel_values = loaded_index.read_channel_values?;
Python Bindings
mf4-rs includes high-performance Python bindings generated using pyo3. This allows you to use the library's features directly from Python with minimal overhead.
Installation
You can install the package directly using pip or uv (requires a Rust compiler):
# or
For development, you can use maturin:
# Install maturin
# Build and install in current environment
Python Examples
Check the python_examples/ directory for complete scripts:
write_file.py- Creating MDF filesread_file.py- Reading and inspecting filesindex_operations.py- Using the indexing system
Basic Usage
# Writing a file
=
=
# Add channels
=
=
# Write data
# Reading a file
=
Performance
mf4-rs is designed for high performance:
- Use
write_recordsfor batch operations instead of multiplewrite_recordcalls - Data blocks automatically split when they exceed 4MB to maintain performance
- Memory-mapped file access minimizes memory usage for large files
- Channel values are decoded lazily only when accessed
- Use indexing for repeated access to the same files to avoid re-parsing overhead
Note: Previous benchmarks have been removed as they are being updated.
Dependencies
nom- Binary parsing combinatorsbyteorder- Endianness handlingmemmap2- Memory-mapped file I/Omeval- Mathematical expression evaluation for formula conversionsthiserror- Error handling derive macros