import gzip
from pathlib import Path
from typing import Generator
import pytest
from ..h5_to_spec import convert_h5
from ..utils import spec
from .data import resource_path
[docs]
@pytest.fixture
def raw_hdf5_file_path() -> Generator[Path, None, None]:
with resource_path("sical_0001.h5") as file_path:
yield file_path
[docs]
@pytest.fixture
def raw_spec_file_path() -> Generator[Path, None, None]:
with resource_path("blc16786_sical_0001.dat") as file_path:
yield file_path
[docs]
@pytest.fixture
def rebin_hdf5_file_path() -> Generator[Path, None, None]:
with resource_path("blc16786_sical_0001_w0003.h5") as file_path:
yield file_path
[docs]
@pytest.fixture
def rebin_spec_file_path() -> Generator[Path, None, None]:
with resource_path("blc16786_sical_0001_w0003.adv.gz") as file_path:
yield file_path
[docs]
def test_convert_h5(raw_hdf5_file_path: Path, raw_spec_file_path: Path, tmp_path: Path):
outfile, converted = convert_h5(
str(raw_hdf5_file_path), outdirs={"primary": str(tmp_path)}
)
initial_mtime = (tmp_path / outfile).stat().st_mtime
assert converted == ["16.1", "16.2", "17.1", "17.2", "18.1", "18.2"]
with open(raw_spec_file_path) as f:
expected = f.readlines()
def _assert_content(outfile):
with open(tmp_path / outfile) as f:
actual = f.readlines()
# First line has the original file name which is different but still starts with #F
assert actual[0][:2] == expected[0][:2] == "#F"
# All the other lines should be the same
assert actual[1:] == expected[1:]
_assert_content(outfile)
# Convert the same scans with overwrite=False
outfile2, converted = convert_h5(
str(raw_hdf5_file_path), outdirs={"primary": str(tmp_path)}, overwrite=False
)
new_mtime = (tmp_path / outfile2).stat().st_mtime
# All scans were already converted
assert outfile == outfile2
assert new_mtime == initial_mtime
assert converted == []
# Convert the same scans with overwrite=True
outfile3, converted = convert_h5(
str(raw_hdf5_file_path), outdirs={"primary": str(tmp_path)}, overwrite=True
)
new_mtime = (tmp_path / outfile3).stat().st_mtime
assert outfile3 == outfile
assert new_mtime != initial_mtime, "file is not re-created"
assert converted == ["16.1", "16.2", "17.1", "17.2", "18.1", "18.2"]
_assert_content(outfile3)
[docs]
def test_convert_rebin_h5(
raw_hdf5_file_path: Path,
rebin_hdf5_file_path: Path,
rebin_spec_file_path: Path,
tmp_path: Path,
):
outfile, converted = convert_h5(
str(raw_hdf5_file_path),
outdirs={"primary": str(tmp_path)},
rebin_filename=str(rebin_hdf5_file_path),
)
initial_mtime = (tmp_path / outfile).stat().st_mtime
assert converted == ["16.1", "16.2", "17.1", "17.2", "18.1", "18.2"]
with gzip.open(rebin_spec_file_path) as f:
expected = [s.decode() for s in f.readlines()]
def _assert_content(outfile):
with open(tmp_path / outfile) as f:
actual = f.readlines()
# First line has the original file name which is different but still starts with #F
assert actual[0][:2] == expected[0][:2] == "#F"
# All the other lines should be the same
assert actual[1:] == expected[1:]
_assert_content(outfile)
# Convert the same scans with overwrite=False
outfile2, converted = convert_h5(
str(raw_hdf5_file_path),
outdirs={"primary": str(tmp_path)},
rebin_filename=str(rebin_hdf5_file_path),
overwrite=False,
)
new_mtime = (tmp_path / outfile2).stat().st_mtime
# All scans were already converted
assert outfile == outfile2
assert new_mtime == initial_mtime
assert converted == []
# Convert the same scans with overwrite=True
outfile3, converted = convert_h5(
str(raw_hdf5_file_path),
outdirs={"primary": str(tmp_path)},
rebin_filename=str(rebin_hdf5_file_path),
overwrite=True,
)
new_mtime = (tmp_path / outfile3).stat().st_mtime
assert outfile3 == outfile
assert new_mtime != initial_mtime, "file is not re-created"
assert converted == ["16.1", "16.2", "17.1", "17.2", "18.1", "18.2"]
_assert_content(outfile3)
[docs]
def test_read_write_spec(raw_spec_file_path: Path, tmp_path: Path):
header, scans = spec.read_spec_file(raw_spec_file_path)
assert set(scans) == {16, 17, 18}
copy_spec_file_path = tmp_path / "copy.dat"
spec.write_spec_file(copy_spec_file_path, header, scans)
with open(raw_spec_file_path) as f:
expected = f.readlines()
with open(copy_spec_file_path) as f:
actual = f.readlines()
assert actual == expected