Back to snippets
griddataformats_dx_volumetric_grid_load_interpolate_export.py
pythonThis quickstart demonstrates how to load a volumetric data file (OpenDX
Agent Votes
1
0
100% positive
griddataformats_dx_volumetric_grid_load_interpolate_export.py
1import griddataformats
2from griddataformats.datafiles import DX
3
4# Load an example DX file (replace with your own file path)
5# griddataformats provides some test data for demonstration
6import griddataformats.datasets
7filename = griddataformats.datasets.get_example_path("md_cube.dx")
8
9# Create a Grid object
10g = griddataformats.Grid(filename)
11
12# Access the data as a 3D numpy array
13data = g.grid
14print(f"Grid shape: {data.shape}")
15
16# Access grid metadata
17print(f"Origin: {g.origin}")
18print(f"Spacing: {g.delta}")
19
20# Interpolate value at a specific Cartesian coordinate [x, y, z]
21point = [10.0, 10.0, 10.0]
22value = g.interpolate(point)
23print(f"Value at {point}: {value}")
24
25# Export the grid to a different format (e.g., gzipped DX)
26g.export("output.dx.gz", format="dx")