Reading/Writing

opencosmo.open(*files, **open_kwargs)

Open a dataset or data collection from one or more opencosmo files.

If you open a file with this function, you should generally close it when you’re done

import opencosmo as oc
ds = oc.open("path/to/file.hdf5")
# do work
ds.close()

Alternatively you can use a context manager, which will close the file automatically when you are done with it.

import opencosmo as oc
with oc.open("path/to/file.hdf5") as ds:
    # do work

When you have multiple files that can be combined into a collection, you can use the following.

import opencosmo as oc
ds = oc.open("haloproperties.hdf5", "haloparticles.hdf5")
Parameters:
  • *files (str or pathlib.Path) – The path(s) to the file(s) to open.

  • **open_kwargs (bool) – True/False flags that can be used to only load certain datasets from the files. Check the documentation for the data type you are working with for available flags. Will be ignored if only one file is passed and the file only contains a single dataset.

Returns:

dataset – The dataset or collection opened from the file.

Return type:

oc.Dataset or oc.Collection

opencosmo.write(path, dataset, overwrite=False, **schema_kwargs)

Write a dataset or collection to the file at the sepecified path.

Parameters:
  • file (str or pathlib.Path) – The path to the file to write to.

  • dataset (oc.Dataset) – The dataset to write.

  • overwrite (bool, default = False) – If the file already exists, overwrite it

  • path (Path)

Raises:
  • FileExistsError – If the file at the specified path already exists and overwrite is False

  • FileNotFoundError – If the parent folder of the ouput file does not exist

Return type:

None

opencosmo.io.write_parquet(path, to_write, overwrite=False, **kwargs)

Write a dataset or collection to a parquet file at the given path. If you are writing a opencosmo.Dataset, or opencosmo.Lightcone the data will be written as a single file at the given path. If you are writing a opencosmo.StructureCollection the data will be written to several files, one for each type of particle.

Parameters: