Learn the fundamentals of opening HDF5 files and walking the file structure
- Opening HDF5 files
- Reading superblock information
- Walking through file structure using
Walk() - Identifying groups and datasets
- Automatic test file generation with Python (optional)
# From this directory
go run main.go
# Or from repository root
go run examples/01-basic/main.goThe example will:
- Generate test HDF5 files (if Python/h5py available)
- Open each test file
- Display superblock version
- Walk through all objects (groups and datasets)
- Show object hierarchy
Example Output:
Creating test HDF5 files...
Created: testdata/v0.h5
Created: testdata/v2.h5
Created: testdata/v3.h5
Created: testdata/with_groups.h5
===== Testing file: testdata/v0.h5 =====
File opened successfully. Superblock version: 0
File structure:
[Group] / (1 children)
[Dataset] /test
===== Testing file: testdata/v2.h5 =====
File opened successfully. Superblock version: 2
File structure:
[Group] / (1 children)
[Dataset] /data
===== Testing file: testdata/with_groups.h5 =====
File opened successfully. Superblock version: 2
File structure:
[Group] / (2 children)
[Dataset] /dataset1
[Group] /subgroup/ (2 children)
[Dataset] /subgroup/dataset2
[Group] /subgroup/nested_group/ (1 children)
[Dataset] /subgroup/nested_group/nested_data
file, err := hdf5.Open("data.h5")
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer file.Close() // Always close!// Superblock version (0, 2, or 3)
version := file.SuperblockVersion()
fmt.Println("Superblock version:", version)file.Walk(func(path string, obj hdf5.Object) {
switch v := obj.(type) {
case *hdf5.Group:
fmt.Printf("[Group] %s (%d children)\n",
path, len(v.Children()))
case *hdf5.Dataset:
fmt.Printf("[Dataset] %s\n", path)
default:
fmt.Printf("[Unknown] %s\n", path)
}
})The library provides two main object types:
| Type | Description | Example Path |
|---|---|---|
*hdf5.Group |
Container for other objects | /, /experiments/ |
*hdf5.Dataset |
Array data storage | /temperature, /data |
HDF5 files are organized hierarchically:
/ (root group)
├── dataset1
├── subgroup/
│ ├── dataset2
│ └── nested_group/
│ └── nested_data
└── dataset3
- Version 0: Original HDF5 format (1998-2006)
- Version 2: Modern format (HDF5 1.8+, 2008+)
- Version 3: Latest format with SWMR support (HDF5 1.10+, 2016+)
The example includes automatic test file generation:
func createTestFiles() error {
// Checks for Python with h5py
if !checkPythonDependencies() {
log.Printf("Python dependencies missing...")
return nil
}
// Generates Python script
// Creates test HDF5 files
// ...
}Requirements:
- Python 3
- h5py:
pip install h5py - numpy:
pip install numpy
If Python is not available, you can provide your own HDF5 files.
file, _ := hdf5.Open("unknown.h5")
defer file.Close()
var groupCount, datasetCount int
file.Walk(func(path string, obj hdf5.Object) {
switch obj.(type) {
case *hdf5.Group:
groupCount++
case *hdf5.Dataset:
datasetCount++
}
})
fmt.Printf("File contains: %d groups, %d datasets\n",
groupCount, datasetCount)func findDataset(file *hdf5.File, name string) *hdf5.Dataset {
var result *hdf5.Dataset
file.Walk(func(path string, obj hdf5.Object) {
if ds, ok := obj.(*hdf5.Dataset); ok {
if ds.Name() == name {
result = ds
}
}
})
return result
}func printTree(file *hdf5.File) {
file.Walk(func(path string, obj hdf5.Object) {
// Calculate indent based on depth
depth := strings.Count(path, "/") - 1
indent := strings.Repeat(" ", depth)
switch v := obj.(type) {
case *hdf5.Group:
if path != "/" {
fmt.Printf("%s📁 %s/\n", indent, v.Name())
}
case *hdf5.Dataset:
fmt.Printf("%s📄 %s\n", indent, v.Name())
}
})
}Cause: File doesn't exist or path is incorrect.
Solution:
import "os"
// Check if file exists
if _, err := os.Stat("data.h5"); os.IsNotExist(err) {
fmt.Println("File does not exist")
}Cause: File is not in HDF5 format.
Solution: Verify with h5dump -H file.h5 (if HDF5 tools installed).
Cause: Python or h5py not installed.
Solution:
# Install Python dependencies
pip install h5py numpy
# Or skip test generation and use your own HDF5 filesAfter understanding basic usage:
- Example 02 - Advanced file navigation
- Example 03 - Reading dataset values
- Reading Data Guide - Comprehensive guide
- Installation Guide - Setup instructions
- Quick Start Guide - 5-minute intro
- API Reference - GoDoc
Part of the HDF5 Go Library v0.10.0-beta