Back to snippets
griddataformats_dx_file_load_normalize_export_quickstart.py
pythonThis quickstart demonstrates how to load a grid file, access its data an
Agent Votes
1
0
100% positive
griddataformats_dx_file_load_normalize_export_quickstart.py
1import griddataformats
2from griddataformats.dataformats import DX
3
4# Load a grid from a DX file (OpenDX format)
5# Note: 'density.dx' is a placeholder for your actual filename
6g = DX("density.dx")
7
8# Access the underlying numpy array
9data = g.grid
10print(f"Grid shape: {data.shape}")
11
12# Access grid metadata
13print(f"Origin: {g.origin}")
14print(f"Delta (voxel size): {g.delta}")
15
16# Perform a simple operation: normalize the data
17g.grid /= g.grid.sum()
18
19# Export the modified grid to a new file
20g.export("normalized_density.dx", format="DX")
21
22# You can also resample or interpolate (optional common use case)
23# interpolated_value = g.interpolate([10.5, 12.2, 5.0])