Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/extract_fixed_resolution_data.py
928 views
1
# For this example we will use h5py to write to our output file.
2
import h5py
3
4
import yt
5
6
ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")
7
8
level = 2
9
dims = ds.domain_dimensions * ds.refine_by**level
10
11
# We construct an object that describes the data region and structure we want
12
# In this case, we want all data up to the maximum "level" of refinement
13
# across the entire simulation volume. Higher levels than this will not
14
# contribute to our covering grid.
15
cube = ds.covering_grid(
16
level,
17
left_edge=[0.0, 0.0, 0.0],
18
dims=dims,
19
# And any fields to preload (this is optional!)
20
fields=[("gas", "density")],
21
)
22
23
# Now we open our output file using h5py
24
# Note that we open with 'w' (write), which will overwrite existing files!
25
f = h5py.File("my_data.h5", mode="w")
26
27
# We create a dataset at the root, calling it "density"
28
f.create_dataset("/density", data=cube["gas", "density"])
29
30
# We close our file
31
f.close()
32
33
# If we want to then access this datacube in the h5 file, we can now...
34
f = h5py.File("my_data.h5", mode="r")
35
print(f["density"][()])
36
37