Skip to content

Commit

Permalink
relative paths - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Nov 21, 2024
1 parent 3552a5d commit 87274ca
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions scripts/cp-dataset-histos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BASE_PATH> <DEST_DIR>
Expand Down Expand Up @@ -33,40 +33,52 @@ 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
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"' {} \;

#######################################################################################

Expand Down

0 comments on commit 87274ca

Please sign in to comment.