#!/usr/bin/env bash
-set -u
+set -u
function error {
-echo "ERROR: $1"
-exit 1
+ echo "ERROR: $1"
+ exit 1
}
warning_count=0
function warning {
-let "warning_count++"
-echo "MERGE_WARNING: $1"
+ let "warning_count++"
+ echo "MERGE_WARNING: $1"
}
function start_bar {
-count_max="${1:?"provide count max"}"
+ count_max="${1:?"provide count max"}"
}
function update_bar {
-local count="${1:?"provide count"}"
-local message="${2:?"provide message"}"
-local percent=$(echo "100*${count}/${count_max}" | bc)
-#printf "[%03d/%03d] %3d%% %-80.80s\r" ${count} ${count_max} ${percent} "${message}"
-printf "[%03d/%03d] %3d%% %-80.80s\n" ${count} ${count_max} ${percent} "${message}"
+ local count="${1:?"provide count"}"
+ local message="${2:?"provide message"}"
+ local percent=$(echo "100*${count}/${count_max}" | bc)
+ #printf "[%03d/%03d] %3d%% %-80.80s\r" ${count} ${count_max} ${percent} "${message}"
+ printf "[%03d/%03d] %3d%% %-80.80s\n" ${count} ${count_max} ${percent} "${message}"
}
function end_bar {
-unset count_max
-#echo -ne '\n'
+ unset count_max
+ #echo -ne '\n'
}
function check_interfile {
-local input_interfile="${1:?"provide input interfile"}"
+ local input_interfile="${1:?"provide input interfile"}"
-grep -qs '!INTERFILE :=' "${input_interfile}" || return 1
+ grep -qs '!INTERFILE :=' "${input_interfile}" || return 1
-local header_byte_size=$(awk -F' ' '
+ local header_byte_size=$(awk -F' ' '
BEGIN { zsize = 0; }
/matrix size/ && $3 == "[1]" { xsize = $5; }
/matrix size/ && $3 == "[2]" { ysize = $5; }
/number of bytes per pixel/ { byte_per_pixel = $7; }
END { print xsize * ysize * zsize * byte_per_pixel; }' "${input_interfile}")
-local raw_interfile="$(dirname "${input_interfile}")/$(awk -F' := ' '/name of data file/ { print $2; }' "${input_interfile}")"
+ local raw_interfile="$(dirname "${input_interfile}")/$(awk -F' := ' '/name of data file/ { print $2; }' "${input_interfile}")"
-test -f "${raw_interfile}" || return 1
-test $(stat -c%s "${raw_interfile}") -eq ${header_byte_size} || return 1
+ test -f "${raw_interfile}" || return 1
+ test $(stat -c%s "${raw_interfile}") -eq ${header_byte_size} || return 1
}
function write_mhd_header {
-local input_interfile="${1:?"provide input interfile"}"
-local output_mhd="$(dirname "${input_interfile}")/$(basename "${input_interfile}" ".hdr").mhd"
+ local input_interfile="${1:?"provide input interfile"}"
+ local output_mhd="$(dirname "${input_interfile}")/$(basename "${input_interfile}" ".hdr").mhd"
-check_interfile "${input_interfile}" || error "${input_interfile} isn't an interfile image"
+ check_interfile "${input_interfile}" || error "${input_interfile} isn't an interfile image"
-local header_start='ObjectType = Image
+ local header_start='ObjectType = Image
NDims = 3
AcquisitionDate = none
BinaryData = True
DistanceUnits = mm
AnatomicalOrientation = RIP'
-echo "${header_start}" > "${output_mhd}"
+ echo "${header_start}" > "${output_mhd}"
-awk -F' ' '
+ awk -F' ' '
/scaling factor/ && $4 == "[1]" { xspacing = $6; }
/scaling factor/ && $4 == "[2]" { yspacing = $6; }
END { print "ElementSpacing = " xspacing " " yspacing " 1"; }' "${input_interfile}" >> "${output_mhd}"
-awk -F' ' '
+ awk -F' ' '
BEGIN { zsize = 0; }
/matrix size/ && $3 == "[1]" { xsize = $5; }
/matrix size/ && $3 == "[2]" { ysize = $5; }
/number of projections/ { zsize += $5; }
END { print "DimSize = " xsize " " ysize " " zsize; }' "${input_interfile}" >> "${output_mhd}"
-awk -F' := ' '
+ awk -F' := ' '
/number format/ { format = $2; }
/number of bytes per pixel/ { byte_per_pixel = $2 }
END {
print "ElementType = MET_INT";
}' "${input_interfile}" >> "${output_mhd}"
-awk -F' := ' '
+ awk -F' := ' '
/name of data file/ { print "ElementDataFile = " $2; }' "${input_interfile}" >> "${output_mhd}"
}
test -x "./clitkMergeRootFiles" && rootMerger="./clitkMergeRootFiles"
function merge_root {
-local merged="$1"
-shift
-echo " ${indent}entering root merger"
-echo " ${indent}merger is ${rootMerger}"
-echo " ${indent}creating ${merged}"
-#echo "######## $#"
-#echo "######## $*"
-
-if test $# -eq 1
-then
- echo " ${indent}just one partial file => just copy it"
- cp "$1" "${merged}"
- return
-fi
-
-local count=0
-local arguments=" -o ${merged}"
-while test $# -gt 0
-do
- local partial="$1"
+ local merged="$1"
shift
- let count++
- local arguments=" -i ${partial} ${arguments}"
-done
-${rootMerger} ${arguments} > /dev/null || error "error while calling ${rootMerger}"
-echo " ${indent}merged ${count} files"
+ echo " ${indent}entering root merger"
+ echo " ${indent}merger is ${rootMerger}"
+ echo " ${indent}creating ${merged}"
+ #echo "######## $#"
+ #echo "######## $*"
+
+ if test $# -eq 1
+ then
+ echo " ${indent}just one partial file => just copy it"
+ cp "$1" "${merged}"
+ return
+ fi
+
+ local count=0
+ local arguments=" -o ${merged}"
+ while test $# -gt 0
+ do
+ local partial="$1"
+ shift
+ let count++
+ local arguments=" -i ${partial} ${arguments}"
+ done
+ ${rootMerger} ${arguments} > /dev/null || error "error while calling ${rootMerger}"
+ echo " ${indent}merged ${count} files"
}
statMerger="mergeStatFile.py"
test -x "./mergeStatFile.sh" && statMerger="./mergeStatFile.sh"
function merge_stat {
-local merged="$1"
-shift
-echo " ${indent}entering stat merger"
-echo " ${indent}merger is ${statMerger}"
-echo " ${indent}creating ${merged}"
-local count=0
-start_bar $#
-while test $# -gt 0
-do
- local partial="$1"
+ local merged="$1"
shift
- let count++
-
- if test ! -f "${merged}"
- then
- update_bar ${count} "copying first partial result ${partial}"
- cp "${partial}" "${merged}"
- continue
- fi
+ echo " ${indent}entering stat merger"
+ echo " ${indent}merger is ${statMerger}"
+ echo " ${indent}creating ${merged}"
+ local count=0
+ start_bar $#
+ while test $# -gt 0
+ do
+ local partial="$1"
+ shift
+ let count++
+
+ if test ! -f "${merged}"
+ then
+ update_bar ${count} "copying first partial result ${partial}"
+ cp "${partial}" "${merged}"
+ continue
+ fi
- update_bar ${count} "adding ${partial}"
- ${statMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${statMerger}"
-done
-end_bar
-echo " ${indent}merged ${count} files"
+ update_bar ${count} "adding ${partial}"
+ ${statMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${statMerger}"
+ done
+ end_bar
+ echo " ${indent}merged ${count} files"
}
doseMerger="mergeDosePerEnegryFile.sh"
test -x "./mergeDosePerEnergyFile.sh" && doseMerger="./mergeDosePerEnergyFile.sh"
function merge_dose {
-local merged="$1"
-shift
-echo " ${indent}entering dose merger"
-echo " ${indent}merger is ${doseMerger}"
-echo " ${indent}creating ${merged}"
-local count=0
-start_bar $#
-while test $# -gt 0
-do
- local partial="$1"
+ local merged="$1"
shift
- let count++
-
- if test ! -f "${merged}"
- then
- update_bar ${count} "copying first partial result ${partial}"
- cp "${partial}" "${merged}"
- continue
- fi
+ echo " ${indent}entering dose merger"
+ echo " ${indent}merger is ${doseMerger}"
+ echo " ${indent}creating ${merged}"
+ local count=0
+ start_bar $#
+ while test $# -gt 0
+ do
+ local partial="$1"
+ shift
+ let count++
+
+ if test ! -f "${merged}"
+ then
+ update_bar ${count} "copying first partial result ${partial}"
+ cp "${partial}" "${merged}"
+ continue
+ fi
- update_bar ${count} "adding ${partial}"
- ${doseMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${doseMerger}"
-done
-end_bar
-echo " ${indent}merged ${count} files"
+ update_bar ${count} "adding ${partial}"
+ ${doseMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${doseMerger}"
+ done
+ end_bar
+ echo " ${indent}merged ${count} files"
}
txtImageMerger="clitkMergeAsciiDoseActor"
test -f "./clitkMergeAsciiDoseActor" && txtImageMerger="./clitkMergeAsciiDoseActor"
function merge_txt_image {
-local merged="$1"
-shift
-echo " ${indent}entering text image merger"
-echo " ${indent}merger is ${txtImageMerger}"
-echo " ${indent}creating ${merged}"
-local count=0
-start_bar $#
-while test $# -gt 0
-do
- local partial="$1"
+ local merged="$1"
shift
- let count++
-
- if test ! -f "${merged}"
- then
- update_bar ${count} "copying first partial result ${partial}"
- cp "${partial}" "${merged}"
- continue
- fi
+ echo " ${indent}entering text image merger"
+ echo " ${indent}merger is ${txtImageMerger}"
+ echo " ${indent}creating ${merged}"
+ local count=0
+ start_bar $#
+ while test $# -gt 0
+ do
+ local partial="$1"
+ shift
+ let count++
+
+ if test ! -f "${merged}"
+ then
+ update_bar ${count} "copying first partial result ${partial}"
+ cp "${partial}" "${merged}"
+ continue
+ fi
- update_bar ${count} "adding ${partial}"
- local header="$(cat "${merged}" | head -n 6)"
- local tmp="$(mktemp)"
- ${txtImageMerger} -i "${partial}" -j "${merged}" -o "${tmp}" 2> /dev/null > /dev/null || warning "error while calling ${txtImageMerger}"
- echo "${header}" > "${merged}"
- grep -v '## Merge' "${tmp}" >> "${merged}"
- rm "${tmp}"
-done
-end_bar
-echo " ${indent}merged ${count} files"
+ update_bar ${count} "adding ${partial}"
+ local header="$(cat "${merged}" | head -n 6)"
+ local tmp="$(mktemp)"
+ ${txtImageMerger} -i "${partial}" -j "${merged}" -o "${tmp}" 2> /dev/null > /dev/null || warning "error while calling ${txtImageMerger}"
+ echo "${header}" > "${merged}"
+ grep -v '## Merge' "${tmp}" >> "${merged}"
+ rm "${tmp}"
+ done
+ end_bar
+ echo " ${indent}merged ${count} files"
}
hdrImageMerger="clitkImageArithm"
test -x "./clitkImageArithm" && hdrImageMerger="./clitkImageArithm"
function merge_hdr_image {
-local merged="$1"
-local merged_bin="${merged%.*}.img"
-shift
-echo " ${indent}entering hdr image merger"
-echo " ${indent}merger is ${hdrImageMerger}"
-echo " ${indent}creating ${merged}"
-local count=0
-start_bar $#
-while test $# -gt 0
-do
- local partial="$1"
- local partial_bin="${partial%.*}.img"
+ local merged="$1"
+ local merged_bin="${merged%.*}.img"
shift
- let count++
-
- if test ! -f "${merged}"
- then
- update_bar ${count} "copying first partial result ${partial}"
- cp "${partial}" "${merged}"
- cp "${partial_bin}" "${merged_bin}"
- continue
- fi
+ echo " ${indent}entering hdr image merger"
+ echo " ${indent}merger is ${hdrImageMerger}"
+ echo " ${indent}creating ${merged}"
+ local count=0
+ start_bar $#
+ while test $# -gt 0
+ do
+ local partial="$1"
+ local partial_bin="${partial%.*}.img"
+ shift
+ let count++
+
+ if test ! -f "${merged}"
+ then
+ update_bar ${count} "copying first partial result ${partial}"
+ cp "${partial}" "${merged}"
+ cp "${partial_bin}" "${merged_bin}"
+ continue
+ fi
- update_bar ${count} "adding ${partial}"
- ${hdrImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${hdrImageMerger}"
-done
-end_bar
-echo " ${indent}merged ${count} files"
+ update_bar ${count} "adding ${partial}"
+ ${hdrImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${hdrImageMerger}"
+ done
+ end_bar
+ echo " ${indent}merged ${count} files"
}
mhdImageMerger="clitkImageArithm"
test -x "./clitkImageArithm" && mhdImageMerger="./clitkImageArithm"
function merge_mhd_image {
-local merged="$1"
-local merged_bin="${merged%.*}.raw"
-shift
-echo " ${indent}entering mhd image merger"
-echo " ${indent}merger is ${mhdImageMerger}"
-echo " ${indent}creating ${merged}"
-local count=0
-start_bar $#
-while test $# -gt 0
-do
- local partial="$1"
- local partial_bin="$(dirname "${partial}")/$(awk -F' = ' '/ElementDataFile/ { print $2; }' "${partial}")"
+ local merged="$1"
+ local merged_bin="${merged%.*}.raw"
shift
- let count++
-
- if test ! -f "${merged}"
- then
- update_bar ${count} "copying first partial result ${partial}"
- cp "${partial}" "${merged}"
- cp "${partial_bin}" "${merged_bin%.*}.${partial_bin##*.}"
- continue
- fi
+ echo " ${indent}entering mhd image merger"
+ echo " ${indent}merger is ${mhdImageMerger}"
+ echo " ${indent}creating ${merged}"
+ local count=0
+ start_bar $#
+ while test $# -gt 0
+ do
+ local partial="$1"
+ local partial_bin="$(dirname "${partial}")/$(awk -F' = ' '/ElementDataFile/ { print $2; }' "${partial}")"
+ shift
+ let count++
+
+ if test ! -f "${merged}"
+ then
+ update_bar ${count} "copying first partial result ${partial}"
+ cp "${partial}" "${merged}"
+ cp "${partial_bin}" "${merged_bin%.*}.${partial_bin##*.}"
+ continue
+ fi
- update_bar ${count} "adding ${partial}"
- ${mhdImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${mhdImageMerger}"
- mv "${merged_bin}" "${merged_bin%.*}.${partial_bin##*.}"
- sed -i "s/$(basename "${merged_bin}")/$(basename "${merged_bin%.*}.${partial_bin##*.}")/" "${merged}"
-done
-end_bar
-echo " ${indent}merged ${count} files"
+ update_bar ${count} "adding ${partial}"
+ ${mhdImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${mhdImageMerger}"
+ mv "${merged_bin}" "${merged_bin%.*}.${partial_bin##*.}"
+ sed -i "s/$(basename "${merged_bin}")/$(basename "${merged_bin%.*}.${partial_bin##*.}")/" "${merged}"
+ done
+ end_bar
+ echo " ${indent}merged ${count} files"
}
function merge_dispatcher {
if [ -f "${rundir}/params.txt" ]
then
- echo "copying params file"
- cp "${rundir}/params.txt" "${outputdir}/params.txt"
+ echo "copying params file"
+ cp "${rundir}/params.txt" "${outputdir}/params.txt"
fi
echo "these was ${warning_count} warning(s)"