from pathlib import Path
import gzip


def _open_xdf(filename):
    """Open XDF file for reading."""
    filename = Path(filename)  # convert to pathlib object
    if filename.suffix == ".xdfz" or filename.suffixes == [".xdf", ".gz"]:
        f = gzip.open(filename, "rb")
    else:
        f = open(filename, "rb")
    if f.read(4) != b"XDF:":  # magic bytes
        raise IOError(f"Invalid XDF file {filename}")
    return f