Skip to content

Commit 82a735b

Browse files
committed
Add support for LZMA compression of G3 files
1 parent bdc56ab commit 82a735b

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

core/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ if(BZIP2_FOUND)
5252
target_compile_definitions(core PRIVATE -DBZIP2_FOUND)
5353
endif()
5454

55+
if(NOT DEFINED WITH_LZMA)
56+
set(WITH_LZMA TRUE CACHE BOOL "Enable LZMA file compression")
57+
endif()
58+
if(WITH_LZMA)
59+
find_package(LibLZMA)
60+
endif()
61+
if(LIBLZMA_FOUND)
62+
target_compile_definitions(core PRIVATE -DLZMA_FOUND)
63+
endif()
64+
5565
link_python_dir()
5666

5767
add_spt3g_program(bin/spt3g-dump)
@@ -80,6 +90,9 @@ add_spt3g_test(compressedfileio)
8090
if(BZIP2_FOUND)
8191
add_spt3g_test(bz2fileio)
8292
endif()
93+
if(LIBLZMA_FOUND)
94+
add_spt3g_test(xzfileio)
95+
endif()
8396
add_spt3g_test(portability)
8497
add_spt3g_test(vecint)
8598
add_spt3g_test(ts_bufferprotocol)

core/src/dataio.cxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#ifdef BZIP2_FOUND
66
#include <boost/iostreams/filter/bzip2.hpp>
77
#endif
8+
#ifdef LZMA_FOUND
9+
#include <boost/iostreams/filter/lzma.hpp>
10+
#endif
811
#include <boost/iostreams/device/file.hpp>
912
#include <boost/iostreams/device/file_descriptor.hpp>
1013
#include <boost/iostreams/device/array.hpp>
@@ -33,6 +36,13 @@ g3_istream_from_path(g3_istream &stream, const std::string &path,
3336
log_fatal("Boost not compiled with bzip2 support.");
3437
#endif
3538
}
39+
if (path.size() > 3 && !path.compare(path.size() - 3, 3, ".xz")) {
40+
#ifdef LZMA_FOUND
41+
stream.push(boost::iostreams::lzma_decompressor());
42+
#else
43+
log_fatal("Boost not compiled with LZMA support.");
44+
#endif
45+
}
3646

3747
int fd = -1;
3848

@@ -189,6 +199,13 @@ g3_ostream_to_path(g3_ostream &stream, const std::string &path,
189199
log_fatal("Boost not compiled with bzip2 support.");
190200
#endif
191201
}
202+
if (path.size() > 3 && !path.compare(path.size() - 3, 3, ".xz") && !append) {
203+
#ifdef LZMA_FOUND
204+
stream.push(boost::iostreams::lzma_compressor());
205+
#else
206+
log_fatal("Boost not compiled with LZMA support.");
207+
#endif
208+
}
192209

193210
if (counter)
194211
stream.push(boost::iostreams::counter64());

core/tests/xzfileio.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
from spt3g import core
4+
import time
5+
6+
# File to disk
7+
pipe = core.G3Pipeline()
8+
pipe.Add(core.G3InfiniteSource, type=core.G3FrameType.Timepoint, n=10)
9+
n = 0
10+
11+
12+
def addinfo(fr):
13+
global n
14+
if fr.type != core.G3FrameType.Timepoint:
15+
return
16+
fr["time"] = core.G3Time(int(time.time() * core.G3Units.s))
17+
fr["count"] = n
18+
n += 1
19+
20+
21+
pipe.Add(addinfo)
22+
pipe.Add(core.Dump)
23+
pipe.Add(core.G3Writer, filename="test.g3.xz")
24+
pipe.Run()
25+
26+
# And back from disk
27+
print("Reading")
28+
pipe = core.G3Pipeline()
29+
pipe.Add(core.G3Reader, filename="test.g3.xz")
30+
pipe.Add(core.Dump)
31+
n = 0
32+
33+
34+
def checkinfo(fr):
35+
global n
36+
if fr.type != core.G3FrameType.Timepoint:
37+
return
38+
if "time" not in fr:
39+
raise KeyError("time")
40+
if fr["count"] != n:
41+
raise ValueError("Out of order frame")
42+
n += 1
43+
44+
45+
pipe.Add(checkinfo)
46+
pipe.Run()
47+
48+
if n != 10:
49+
raise ValueError("Wrong number of frames (%d should be %d)" % (n, 10))

0 commit comments

Comments
 (0)