Reading/Writing

opencosmo.open(file, datasets=None)

Open a dataset or data collection from a file without reading the data into memory.

The object returned by this function will only read data from the file when it is actually needed. This is useful if the file is very large and you only need to access a small part of it.

If you open a file with this dataset, 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
Parameters:
  • file (str or pathlib.Path) – The path to the file to open.

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

Returns:

dataset – The dataset or collection opened from the file.

Return type:

oc.Dataset or oc.Collection

opencosmo.write(path, dataset)

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.

  • path (Path)

Raises:
  • FileExistsError – If the file at the specified path already exists

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

Return type:

None

opencosmo.read(file, datasets=None)

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