diff --git a/src/auxiliary/TemplateFile.cpp b/src/auxiliary/TemplateFile.cpp index 0963364ff0..bb66c69744 100644 --- a/src/auxiliary/TemplateFile.cpp +++ b/src/auxiliary/TemplateFile.cpp @@ -67,6 +67,19 @@ namespace copyAttributes(initializeMe, fromTemplate); } + void initializeFromTemplate( + RecordComponent &initializeMe, RecordComponent const &fromTemplate) + { + if (fromTemplate.getDatatype() != Datatype::UNDEFINED) + { + initializeMe.resetDataset( + Dataset{fromTemplate.getDatatype(), fromTemplate.getExtent()}); + } + initializeFromTemplate( + static_cast(initializeMe), + static_cast(fromTemplate)); + } + void initializeFromTemplate( ParticleSpecies &initializeMe, ParticleSpecies const &fromTemplate) { diff --git a/test/SerialIOTest.cpp b/test/SerialIOTest.cpp index 8b337a60be..e0c0c2b670 100644 --- a/test/SerialIOTest.cpp +++ b/test/SerialIOTest.cpp @@ -2721,21 +2721,21 @@ TEST_CASE("git_hdf5_sample_structure_test", "[serial][hdf5]") TEST_CASE("git_hdf5_sample_attribute_test", "[serial][hdf5]") { - try - { - Series o = Series("../samples/git-sample/data%T.h5", Access::READ_ONLY); - + auto verifySeries = [](Series o, bool this_is_the_original_file) { REQUIRE(o.openPMD() == "1.1.0"); REQUIRE(o.openPMDextension() == 1); REQUIRE(o.basePath() == "/data/%T/"); REQUIRE(o.meshesPath() == "fields/"); REQUIRE(o.particlesPath() == "particles/"); - REQUIRE(o.iterationEncoding() == IterationEncoding::fileBased); - REQUIRE(o.iterationFormat() == "data%T.h5"); - REQUIRE(o.name() == "data%T"); + if (this_is_the_original_file) + { + REQUIRE(o.iterationEncoding() == IterationEncoding::fileBased); + REQUIRE(o.iterationFormat() == "data%T.h5"); + REQUIRE(o.name() == "data%T"); - REQUIRE(o.iterations.size() == 5); - REQUIRE(o.iterations.count(100) == 1); + REQUIRE(o.iterations.size() == 5); + REQUIRE(o.iterations.count(100) == 1); + } Iteration &iteration_100 = o.iterations[100]; REQUIRE(iteration_100.time() == 3.2847121452090077e-14); @@ -2965,6 +2965,30 @@ TEST_CASE("git_hdf5_sample_attribute_test", "[serial][hdf5]") REQUIRE(weighting_scalar.getDatatype() == Datatype::DOUBLE); REQUIRE(weighting_scalar.getDimensionality() == 1); REQUIRE(weighting_scalar.getExtent() == e); + }; + + try + { + { + Series o = + Series("../samples/git-sample/data%T.h5", Access::READ_ONLY); + verifySeries(o, true); + + Series fromTemplate( + "../samples/initialized_from_git_sample.json", + Access::CREATE, + R"(json.mode = "template")"); + auxiliary::initializeFromTemplate(fromTemplate, o, 100); + fromTemplate.flush(); + } + + { + Series o( + "../samples/initialized_from_git_sample.json", + Access::READ_ONLY, + R"(json.mode = "template")"); + verifySeries(o, false); + } } catch (no_such_file_error &e) {