diff --git a/sdafile/sda_file.py b/sdafile/sda_file.py index f686f63..849ee56 100644 --- a/sdafile/sda_file.py +++ b/sdafile/sda_file.py @@ -296,12 +296,11 @@ def _copy_visitor(path, source, destination, labels): # Copy everything else source_obj = source[path] - destination.attrs.update(source.attrs) if isinstance(source_obj, h5py.Group): - destination.create_group(path) + dest_obj = destination.create_group(path) else: ds = source_obj - destination.create_dataset( + dest_obj = destination.create_dataset( path, data=source_obj[()], chunks=ds.chunks, @@ -314,6 +313,8 @@ def _copy_visitor(path, source, destination, labels): fillvalue=ds.fillvalue, ) + dest_obj.attrs.update(source_obj.attrs) + pid, destination_path = tempfile.mkstemp() os.close(pid) with h5py.File(destination_path, 'w') as destination: diff --git a/sdafile/tests/test_sda_file.py b/sdafile/tests/test_sda_file.py index e636d24..aa95d21 100644 --- a/sdafile/tests/test_sda_file.py +++ b/sdafile/tests/test_sda_file.py @@ -705,9 +705,20 @@ def test_remove(self): sda_file.remove(*removed) self.assertEqual(sorted(sda_file.labels()), sorted(kept)) + # Make sure metadata is preserved and data can be extracted + with sda_file._h5file('r') as h5file: + for label in kept: + attrs = h5file[label].attrs + self.assertIn('Deflate', attrs) + self.assertIn('Description', attrs) + self.assertIn('RecordType', attrs) + self.assertIn('Empty', attrs) + sda_file.extract(label) + sda_file.remove(*kept) self.assertEqual(sda_file.labels(), []) + self.assertEqual(sda_file.FormatVersion, '1.1') self.assertNotEqual(sda_file.Updated, 'Unmodified') def test_probe(self):