Skip to content

Input and output

Reading and writing time series.

read_hdf5(path)

Read every dataset in path written in the layout :func:write_hdf5 writes.

Parameters:

Name Type Description Default
path str | Path

Input file.

required

Returns:

Type Description
dict[str, TimeSeries]

Mapping of dataset name to :class:~gwmock_data.timeseries.TimeSeries, in file order.

Raises:

Type Description
TypeError

If a member is a group rather than a dataset.

ValueError

If a dataset lacks the x0 or dx attribute, or its time unit is not seconds.

Source code in src/gwmock_data/io/hdf5.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def read_hdf5(path: str | Path) -> dict[str, TimeSeries]:
    """Read every dataset in ``path`` written in the layout :func:`write_hdf5` writes.

    Args:
        path: Input file.

    Returns:
        Mapping of dataset name to :class:`~gwmock_data.timeseries.TimeSeries`, in file order.

    Raises:
        TypeError: If a member is a group rather than a dataset.
        ValueError: If a dataset lacks the ``x0`` or ``dx`` attribute, or its time unit is not seconds.
    """
    result = {}
    for key, dataset in hdf5.read(Path(path)).members.items():
        if isinstance(dataset, hdf5.Group):
            raise TypeError(f"{key!r} is a group, not a time series dataset.")
        attrs = dataset.attrs
        if "x0" not in attrs or "dx" not in attrs:
            raise ValueError(f"dataset {key!r} has no x0 and dx attributes, so it is not a time series.")
        xunit = _text(attrs.get("xunit", "s"))
        if xunit not in ("s", "second"):
            raise ValueError(f"dataset {key!r} has a time axis in {xunit!r}; only seconds are supported.")
        name = _text(attrs["name"]) if "name" in attrs else None
        result[key] = TimeSeries(
            np.asarray(dataset.data),
            t0=float(attrs["x0"]),
            sample_rate=1.0 / float(attrs["dx"]),
            name=name,
            unit=_text(attrs.get("unit", "")) or None,
        )
    return result

write_hdf5(series, path)

Write each series to path as one HDF5 dataset named by its key, replacing the file.

Parameters:

Name Type Description Default
series Mapping[str, TimeSeries]

Mapping of dataset name to a one-dimensional time series (this package's or GWpy's).

required
path str | Path

Output file.

required

Raises:

Type Description
ValueError

If a series is not one-dimensional.

Source code in src/gwmock_data/io/hdf5.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def write_hdf5(series: Mapping[str, TimeSeries], path: str | Path) -> None:
    """Write each series to ``path`` as one HDF5 dataset named by its key, replacing the file.

    Args:
        series: Mapping of dataset name to a one-dimensional time series (this package's or
            GWpy's).
        path: Output file.

    Raises:
        ValueError: If a series is not one-dimensional.
    """
    channels = {key: as_timeseries(value) for key, value in series.items()}
    for key, channel in channels.items():
        if len(channel.data.shape) != 1:
            raise ValueError(f"{key!r} must be one-dimensional to write; it has shape {tuple(channel.data.shape)}.")
    members: dict[str, hdf5.Group | hdf5.Dataset] = {}
    for key, channel in channels.items():
        attrs = {"x0": channel.t0, "dx": channel.dt, "xunit": "s", "unit": channel.unit or ""}
        if channel.name is not None:
            attrs["name"] = channel.name
        members[key] = hdf5.Dataset(np.asarray(channel.data), attrs)
    hdf5.write(hdf5.Group(members), Path(path), compression="gzip")