ucftools/bash/ucftar_pack

139 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
display_help() {
(>&2 echo "Usage: $(basename "$0") [-hv] [-f outfile] filename")
(>&2 echo "UCF archive packer v1.0")
(>&2 echo)
(>&2 echo " filename path to arbitrary file with correct sequence number")
(>&2 echo " -f, --file output file (default: archive_XXXX.ucf)")
(>&2 echo " -h, --help display this help message")
(>&2 echo " -v, --verbose verbose output")
}
# Parse command line arguments
if [ $# -eq 0 ]; then
display_help
exit -1
fi
fout=""
verbose=0
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
display_help
exit 0
shift # past argument
;;
-f|--file)
fout="$2"
shift # past argument
shift # past value
;;
-v|--verbose)
verbose=1
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}"
# Parse input filename
din=$(dirname $1)
fin=$(basename $1)
seqnum=$(echo $fin | grep -o '_[0-9]\+')
if [ -z "$seqnum" ]; then
(>&2 echo "[Error] No sequence number found in input filename.")
exit 1;
fi
# Construct output filename if not specified
if [ -z "$fout" ]; then
fout="archive${seqnum}.ucf"
fi
# Display verbose info
if [ $verbose -eq 1 ]; then
(>&2 echo "Creating archive: $fout")
fi
# Check input files
fparam="parameters${seqnum}.asc"
fgrid="grid${seqnum}.bin"
fproc="proc${seqnum}.bin"
fpart="particles${seqnum}.bin"
fbuvwp="uvwp"
fbscal="scal"
# Check if obligatory files are present
if [ ! -s ${din}/${fparam} ]; then
(>&2 echo "[Error] File not found or empty: ${din}/${fparam}")
exit 1
fi
if [ ! -s ${din}/${fgrid} ]; then
(>&2 echo "[Error] File not found or empty: ${din}/${fgrid}")
exit 1
fi
if [ ! -s ${din}/${fproc} ]; then
(>&2 echo "[Error] File not found or empty: ${din}/${fproc}")
exit 1
fi
if [ ! -s ${din}/${fpart} ]; then
(>&2 echo "[Error] File not found or empty: ${din}/${fpart}")
exit 1
fi
# Check if all velocity fields exist and are not empty
nproc=$(cat ${din}/${fparam} | grep "nprocs" | awk '{print $NF}')
if [ $verbose -eq 1 ]; then
(>&2 echo "Number of processors: $nproc")
fi
fuvwp=()
for (( ii=0; ii<$nproc; ii++ )); do
ftmp=$(printf uvwp${seqnum}.%05d $ii)
if [ ! -s ${din}/${ftmp} ]; then
(>&2 echo "[Error] File not found or empty: ${din}/${ftmp}")
exit 1
fi
fuvwp+=($ftmp)
done
# Check if all scalar fields exist (if any) and are not empty
if grep -q "scalar" "${din}/${fparam}"; then
fscal=()
for (( ii=0; ii<$nproc; ii++ )); do
ftmp=$(printf scal${seqnum}.%05d $ii)
if [ ! -s ${din}/${ftmp} ]; then
(>&2 echo "[Error] File not found or empty: ${din}/${ftmp}")
exit 1
fi
fscal+=($ftmp)
done
fi
# Verbose info
if [ $verbose -eq 1 ]; then
(>&2 echo "[x] parameters")
(>&2 echo "[x] grid")
(>&2 echo "[x] processor grid")
(>&2 echo "[x] particles")
(>&2 echo "[x] fluid fields")
if [ ${#fscal[@]} -ne 0 ]; then
(>&2 echo "[x] scalar fields")
else
(>&2 echo "[ ] scalar fields")
fi
fi
# Now tar them (remove seqence number from file names)
if [ $verbose -eq 1 ]; then
flagtar="-v"
fi
tar $flagtar --format ustar --transform="flags=r;s|$seqnum||" --directory=${din} -cf ${fout} ${fparam} ${fgrid} ${fproc} ${fpart} ${fuvwp[@]} ${fscal[@]}