From 9b5d777a5b15d848eb43e53276ea2e436260d654 Mon Sep 17 00:00:00 2001 From: Florez Ospina Juan Felipe Date: Wed, 2 Oct 2024 14:38:35 +0200 Subject: [PATCH] Added .update_file() method, which enables complementary data structure updates to existing file with same name as append_dir's head. --- src/hdf5_ops.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/hdf5_ops.py b/src/hdf5_ops.py index 4943cde..4f0aabf 100644 --- a/src/hdf5_ops.py +++ b/src/hdf5_ops.py @@ -8,6 +8,7 @@ import pandas as pd import numpy as np import utils.g5505_utils as utils +import src.hdf5_lib as hdf5_lib import logging import datetime @@ -354,6 +355,34 @@ class HDF5DataOpsManager(): #return np.array(timestamps) return dt_column_data.to_numpy() + + def update_file(self, path_to_append_dir): + # Split the reference file path and the append directory path into directories and filenames + ref_tail, ref_head = os.path.split(self.file_path) + tail, head = os.path.split(path_to_append_dir) + head_filename, head_ext = os.path.splitext(head) + + # Ensure the append directory is in the same directory as the reference file and has the same name (without extension) + if not (ref_tail == tail and ref_head == head_filename): + raise ValueError("The append directory must be in the same directory as the reference HDF5 file and have the same name without the extension.") + + # Close the file if it's already open + if self.file_obj is not None: + self.close_file() + + # Attempt to open the file in 'r+' mode for appending + try: + hdf5_lib.create_hdf5_file_from_filesystem_path(path_to_append_dir, mode='r+') + except FileNotFoundError: + raise FileNotFoundError(f"Reference HDF5 file '{self.file_path}' not found.") + except OSError as e: + raise OSError(f"Error opening HDF5 file: {e}") + + + + + +