From a34c5150ce39628621cd85ce5d8f56a28dd19a50 Mon Sep 17 00:00:00 2001
From: Gamelin Alexis <alexis.gamelin@synchrotron-soleil.fr>
Date: Wed, 17 Nov 2021 16:25:01 +0100
Subject: [PATCH] Add a new tool to copy a HDF5 file to a different HDF5
 version

---
 tracking/monitors/__init__.py |  2 +-
 tracking/monitors/tools.py    | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/tracking/monitors/__init__.py b/tracking/monitors/__init__.py
index 1bc0337..7f5ba51 100644
--- a/tracking/monitors/__init__.py
+++ b/tracking/monitors/__init__.py
@@ -25,4 +25,4 @@ from mbtrack2.tracking.monitors.plotting import (plot_bunchdata,
                                                  plot_beamspectrum,
                                                  streak_beamspectrum)
 
-from mbtrack2.tracking.monitors.tools import merge_files
\ No newline at end of file
+from mbtrack2.tracking.monitors.tools import (merge_files, copy_files)
\ No newline at end of file
diff --git a/tracking/monitors/tools.py b/tracking/monitors/tools.py
index 9735a6e..d79a6ff 100644
--- a/tracking/monitors/tools.py
+++ b/tracking/monitors/tools.py
@@ -71,5 +71,36 @@ def merge_files(files_prefix, files_number, file_name=None):
                     f[group][dataset_name][tuple(slice_list)] = fi[group][dataset_name]
         fi.close()
     f.close()
+    
+def copy_files(source, copy, version=None):
+    """
+    Copy a source hdf5 file into another hdf5 file using a different HDF5 
+    version.
+    
+    The function assumes that the source file has only a single group layer.
+
+    Parameters
+    ----------
+    source : str
+        Name of the source file.
+    copy : str
+        Name of the copy file.
+    version : str, optional
+        Version number of the copy file.
+
+    """
+    if version == None:
+        version = 'v108'
+    f = hp.File(source + ".hdf5", "r")
+    h = hp.File(copy + ".hdf5", "a", libver=('earliest', version))
+    
+    ## Copy file
+    for group in list(f):
+        h.require_group(group)
+        for dataset_name in list(f[group]):
+            h[group][dataset_name] = f[group][dataset_name][()]
+            
+    f.close()
+    h.close()
 
             
\ No newline at end of file
-- 
GitLab