Skip to content

Saving and Loading Particles

Dane Cross edited this page Feb 7, 2022 · 1 revision

This page goes through the example found in path/to/GROUPy/examples/io/saving_loading_halos.py. This will give a basic tutorial on how to save and load halo files. The particle saving/loading mechanism is almost identical, but instead of using load_halo, use load_particle.

This is a self-contained example, so you do not need to generate test data beforehand. To run the example script, type the following into your terminal:

cd path/to/GROUPy/examples/io
python saving_loading_halos.py

If everything runs correctly, there should be the following output:

True

Now for what the example actually does...

I/O procedure for ROCKSTAR halos

Import Statements

First import the Halo library, along with the os library, which is used to create the directory we save the halo into:

import os
from Halo import load_halo

Housekeeping

This section creates the output directory and specifies what we will call the file

save_dir = "save_output/"
if not os.path.exists(save_dir): os.mkdir(save_dir)
save_name = os.path.join(save_dir, "halo.npy")

Halo Object Creation

Here we instantiate the halo and add some dummy data:

h = Halo()

h.ids = [1, 2, 3, 4]
h.x = [1, 2, 3, 4]

Save the Halo

Now we save the halo to the desired file name.

h.save(save_name)

Load the Halo from the Saved File

h2 = load_halo(save_name)

For a sanity check, see if the loaded halo and the original halo have the same id list:

print(h2.ids == h.ids)

This should output True

Clone this wiki locally