Skip to content

Commit

Permalink
rmf_cat: check mismatched structure/static frame
Browse files Browse the repository at this point in the history
Check all the input files passed to rmf_cat to make
sure that they have the same structure and static frame,
and exit with an error if they don't. Add a --force
option to allow the user to override this check if desired.
Closes #128.
  • Loading branch information
benmwebb committed Feb 16, 2024
1 parent c3afe83 commit dd34301
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
25 changes: 24 additions & 1 deletion bin/rmf_cat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,42 @@ std::string output;
}
int main(int argc, char** argv) {
try {
options.add_options()("force,f",
"Combine files even if they have different "
"structure or static frames.");
positional_options.add_options()(
"input-files,i",
boost::program_options::value<std::vector<std::string> >(&inputs),
"input rmf file");
positional_names.emplace_back("input_1.rmf input_2.rmf ... output.rmf");
positional_options_description.add("input-files", -1);
process_options(argc, argv);
boost::program_options::variables_map vm(process_options(argc, argv));

if (inputs.size() < 3) {
print_help_and_exit(argv);
}

output = inputs.back();
inputs.pop_back();
bool force = vm.count("force");
if (!force && inputs.size() > 1) {
RMF::FileConstHandle rh1 = RMF::open_rmf_file_read_only(inputs[0]);
for (unsigned int i = 1; i < inputs.size(); ++i) {
RMF::FileConstHandle rh2 = RMF::open_rmf_file_read_only(inputs[i]);
if (!RMF::get_equal_structure(rh1, rh2, true)) {
std::cerr << inputs[0] << " and " << inputs[i]
<< " have different structure, cannot concatenate "
<< "without --force" << std::endl;
exit(1);
}
if (!RMF::get_equal_static_values(rh1, rh2)) {
std::cerr << inputs[0] << " and " << inputs[i]
<< " have different static frames, cannot concatenate "
<< "without --force" << std::endl;
exit(1);
}
}
}
RMF::FileHandle orh = RMF::create_rmf_file(output);
orh.set_producer("rmf_cat");
for (unsigned int i = 0; i < inputs.size(); ++i) {
Expand Down
1 change: 1 addition & 0 deletions doc/Executables.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ and how much they are used.

Combine two or more rmf files.
Usage: ./bin/rmf_cat input_1.rmf input_2.rmf ... output.rmf
-f [ --force ] Combine files even if they have different structure or static frame.
-h [ --help ] Get help on command line arguments.
-v [ --verbose ] Produce more output.
--hdf5-errors Show hdf5 errors.
Expand Down
59 changes: 59 additions & 0 deletions test/test_rmf_cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import unittest
import os
import RMF
import subprocess


class Tests(unittest.TestCase):

def test_help(self):
"""Test rmf_cat --help"""
p = subprocess.Popen(['rmf_cat', '--help'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
out, err = p.communicate()
self.assertEqual(out, "")
self.assertIn("Combine two or more rmf files", err)
self.assertEqual(p.returncode, 1)

def test_version(self):
"""Test rmf_cat --version"""
p = subprocess.Popen(['rmf_cat', '--version'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
out, err = p.communicate()
self.assertEqual(err, "")
self.assertIn("RMF version", out)
self.assertEqual(p.returncode, 0)

def test_cat_mismatch(self):
"""Test rmf_cat of mismatched files"""
p = subprocess.Popen(
['rmf_cat', RMF._get_test_input_file_path("simple.rmf"),
RMF._get_test_input_file_path("rep_and_geom.rmf"),
'test_cat_mismatch.rmf'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
out, err = p.communicate()
self.assertIn("have different structure", err)
self.assertEqual(p.returncode, 1)

def test_cat_ok(self):
"""Test rmf_cat of similar files"""
p = subprocess.Popen(
['rmf_cat', RMF._get_test_input_file_path("simple.rmf"),
RMF._get_test_input_file_path("simple-new.rmf"),
'test_cat_ok.rmf'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
out, err = p.communicate()
self.assertEqual(out, "")
self.assertEqual(err, "")
self.assertEqual(p.returncode, 0)
os.unlink('test_cat_ok.rmf')


if __name__ == '__main__':
unittest.main()

0 comments on commit dd34301

Please sign in to comment.