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)

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.open_linked_files(*files, **load_kwargs)

WARNING: THIS METHOD IS DEPCREATED AND WILL BE REMOVED IN A FUTURE VERSION. PLEASE USE opencosmo.open()

Open a collection of files that are linked together, such as a properties file and a particle file.

Parameters:
  • files (Path)

  • load_kwargs (bool)

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:
opencosmo.read(file, datasets=None)

WARNING: THIS METHOD IS DEPRECATED AND WILL BE REMOVED IN A FUTURE VERSION. USE opencosmo.open()

Read a dataset from a file into memory.

You should use this function if the data are small enough that having a copy of it (or a few copies of it) in memory is not a problem. For larger datasets, use opencosmo.open().

Note that some dataset types cannot be read, due to complexities with how the data is handled. Using opencosmo.open() is recommended for most use cases.

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

  • datasets (str or list[str], optional) – If the file has multiple datasets, the name of the dataset(s) to read. All other datasets will be ignored. If not provided, will read all datasets

Returns:

dataset – The dataset or collection read from the file.

Return type:

oc.Dataset or oc.Collection