|
| 1 | +from contextlib import ExitStack |
| 2 | +import gzip |
| 3 | +from pathlib import Path |
| 4 | + |
1 | 5 | import pytest |
2 | 6 | import numpy as np |
3 | 7 |
|
| 8 | +from zstandard import ZstdCompressor |
| 9 | + |
4 | 10 | from corsikaio.constants import BLOCK_SIZE_BYTES |
5 | 11 | from corsikaio.io import RECORD_MARKER |
6 | 12 |
|
@@ -167,3 +173,40 @@ def test_longitudinal_parameters(): |
167 | 173 | parameters = event.end["longitudinal_fit_parameters"] |
168 | 174 | np.testing.assert_array_equal(parameters != 0, True) |
169 | 175 | assert n_events == 5 |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.parametrize ( |
| 179 | + "test_path", |
| 180 | + [ |
| 181 | + 'tests/resources/mmcs65', |
| 182 | + 'tests/resources/corsika74100', |
| 183 | + ] |
| 184 | +) |
| 185 | +@pytest.mark.parametrize( "compression", ["gz", "zst"]) |
| 186 | +def test_compressed(test_path, compression, tmp_path): |
| 187 | + from corsikaio import CorsikaCherenkovFile |
| 188 | + |
| 189 | + test_path = Path(test_path) |
| 190 | + compressed = tmp_path / f"{test_path.name}.{compression}" |
| 191 | + |
| 192 | + ctx = ExitStack() |
| 193 | + |
| 194 | + with ctx: |
| 195 | + infile = ctx.enter_context(test_path.open("rb")) |
| 196 | + outfile = ctx.enter_context(compressed.open("wb")) |
| 197 | + |
| 198 | + if compression == "gz": |
| 199 | + outstream = ctx.enter_context(gzip.GzipFile(fileobj=outfile, mode="wb")) |
| 200 | + elif compression == "zst": |
| 201 | + compressor = ZstdCompressor(level=10) |
| 202 | + outstream = ctx.enter_context(compressor.stream_writer(outfile)) |
| 203 | + else: |
| 204 | + raise ValueError(f"Unknown compression: {compression}") |
| 205 | + |
| 206 | + for chunk in iter(lambda : infile.read(102400), b""): |
| 207 | + outstream.write(chunk) |
| 208 | + |
| 209 | + with CorsikaCherenkovFile(compressed) as cf, CorsikaCherenkovFile(test_path) as f: |
| 210 | + for event in f: |
| 211 | + compressed_event = next(cf) |
| 212 | + assert event.header["event_number"] == compressed_event.header["event_number"] |
0 commit comments