diff --git a/scripts/cp-dataset-histos.sh b/scripts/cp-dataset-histos.sh index 4e467fb..e912afa 100755 --- a/scripts/cp-dataset-histos.sh +++ b/scripts/cp-dataset-histos.sh @@ -4,7 +4,7 @@ set -euo pipefail ####################################################################################### # This script automates the copying of histogram files from dataset directories # into a specified destination directory, while preserving the original directory -# structure relative to the provided BASE_PATH. +# structure, like: # # Usage: # ./copy-histograms.sh @@ -33,32 +33,44 @@ mkdir -p "$DEST_DIR" depth_to_datasets=$(python -m simprod_histogram.calc_depth_to_dataset_dirs "$BASE_PATH" 2>&1) ####################################################################################### -# Define a function to copy the single histogram file while preserving the directory structure +# Copy! -copy_file() { +cp_histo() { + # Copies all histogram files (*.histo.hdf5) from a given dataset directory + # to a destination directory, preserving the directory structure relative + # to a specified base path (SIM). If multiple histogram files exist in the + # dataset directory, they are all copied. local src_dataset_dir="$1" - local histo_file="$src_dataset_dir"/*.histo.hdf5 - if [ ! -f "$histo_file" ]; then + # Check for histo files + local histo_files=("$src_dataset_dir"/*.histo.hdf5) + if [ ! -e "${histo_files[0]}" ]; then echo "No histogram file found in $src_dataset_dir" return fi # Compute the relative path from 'sim' - # cp the filetree like: + # cp the file tree like: # src_dataset_dir = '.../sim/IceCube/2023/generated/neutrino-generator/22645' # relative_path = 'IceCube/2023/generated/neutrino-generator/22645' # dest_dataset_dir = '$DEST_DIR/sim/IceCube/2023/generated/neutrino-generator/22645' local relative_path="${src_dataset_dir#"$SIM"/}" - local dest_dataset_dir="$DEST_DIR/"$SIM"/$relative_path" + local dest_dataset_dir="$DEST_DIR/$SIM/$relative_path" mkdir -p "$dest_dataset_dir" - # Copy the histogram file(s) - cp "$histo_file" "$dest_dataset_dir/" - echo "Copied $histo_file to $dest_dataset_dir" + # Loop through all matching files and copy them + local histo_file + for histo_file in "${histo_files[@]}"; do + if [ -f "$dest_dataset_dir/$(basename "$histo_file")" ]; then + echo "Histogram file already exists at $dest_dataset_dir (will not overwrite)" + else + cp "$histo_file" "$dest_dataset_dir/" + echo "Copied $histo_file to $dest_dataset_dir" + fi + done } -export -f copy_file +export -f copy_histo_file export BASE_PATH DEST_DIR SIM # Use find with -exec to copy files from each dataset @@ -66,7 +78,7 @@ find "$BASE_PATH" \ -mindepth "$depth_to_datasets" \ -maxdepth "$depth_to_datasets" \ -type d \ - -exec bash -c 'copy_file "$0"' {} \; + -exec bash -c 'cp_histo "$0"' {} \; #######################################################################################