Added a few lines to detect the existence of the file and change the file mode from 'w' to 'a' based on that information.

This commit is contained in:
2024-06-20 09:03:47 +02:00
parent 498a51cbc6
commit 106795ae59

View File

@ -482,7 +482,7 @@ import os
import src.g5505_utils as utils
import h5py
import src.metadata_review_lib as metadata_lib
def save_processed_dataframe_to_hdf5(df, annotator, src_hdf5_path, script_date, script_name):
def save_processed_dataframe_to_hdf5(df, annotator, output_filename): # src_hdf5_path, script_date, script_name):
"""
Save processed dataframe columns with annotations to an HDF5 file.
@ -505,9 +505,9 @@ def save_processed_dataframe_to_hdf5(df, annotator, src_hdf5_path, script_date,
# Get metadata
metadata_dict = annotator.get_metadata()
# Prepare project level attributes
# Prepare project level attributes to be added at the root level
root_level_attributes = metadata_dict['metadata']['project']
project_level_attributes = metadata_dict['metadata']['project']
# Prepare high-level attributes
high_level_attributes = {
@ -524,13 +524,10 @@ def save_processed_dataframe_to_hdf5(df, annotator, src_hdf5_path, script_date,
if isinstance(value,dict):
data_level_attributes[key] = metadata_lib.parse_attribute(value)
# Generate output filename
parent_file_name = os.path.split(src_hdf5_path)[1]
output_filename = f'data_products/processed/fig_{script_date}_{parent_file_name}'
# Prepare file dictionary
file_dict = {
'name': script_name,
'name': project_level_attributes['script_name'],
'attributes_dict': high_level_attributes,
'datasets': [{
'name': "data_table",
@ -540,9 +537,19 @@ def save_processed_dataframe_to_hdf5(df, annotator, src_hdf5_path, script_date,
}]
}
# Check if the file exists
if os.path.exists(output_filename):
mode = "a"
print(f"File {output_filename} exists. Opening in append mode.")
else:
mode = "w"
print(f"File {output_filename} does not exist. Creating a new file.")
# Write to HDF5
with h5py.File(output_filename, 'w') as h5file:
h5file.attrs.update(root_level_attributes)
with h5py.File(output_filename, mode) as h5file:
# Add project level attributes at the root/top level
h5file.attrs.update(project_level_attributes)
transfer_file_dict_to_hdf5(h5file, '/', file_dict)