+ if [[ "${firstpartialoutputfile}" == *Uncertainty* ]]
+ then
+ if test "${firstpartialoutputextension}" == "mhd" || test "${firstpartialoutputextension}" == "mha"
+ then
+ echo "${indent}Uncertainty file found: ${firstpartialoutputfile}"
+ ## search for sum
+ local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+ summed_merged_file=${mergedfile//-Uncertainty/}
+ if [ ! -f ${summed_merged_file} ];
+ then
+ warning "${summed_merged_file} does not exist. Error, no uncertainty computed"
+ return;
+ fi
+ echo "${indent}${summed_merged_file} found"
+ ## search for Squared
+ squared_merged_file=${mergedfile//-Uncertainty/-Squared}
+ if [ ! -f ${squared_merged_file} ];
+ then
+ warning "${squared_merged_file} does not exist. Error, no uncertainty computed"
+ return;
+ fi
+ echo "${indent}${squared_merged_file} found"
+ ## search for NumberOfEvent
+ totalEvents=0;
+ unamestr=`uname`
+ if [[ "$unamestr" == 'Darwin' ]]; then
+ files=$(find -L "${rundir}" -type f -regex ".*output.*[hdr|mhd|mha|root|txt]" | awk -F '/' '{ print $NF; }' | sort | uniq)
+ else
+ files=$(find -L "${rundir}" -regextype 'posix-extended' -type f -regex "${rundir}/output.*\.(hdr|mhd|mha|root|txt)" | awk -F '/' '{ print $NF; }' | sort | uniq)
+ fi
+ echo $files
+ for outputfile in ${files}
+ do
+ #echo $outputfile
+ if grep -q 'NumberOfEvent' "${outputdir}/${outputfile}"
+ then
+ totalEvents="$(grep "NumberOfEvents" "${outputdir}/${outputfile}" | cut -d' ' -f4)"
+ echo "${indent}Find the NumberOfEvent in $outputfile: ${totalEvents}"
+ fi
+ done
+
+ if test ${totalEvents} -gt 0
+ then
+ uncerImageMerger="clitkImageUncertainty"
+ test -x "./clitkImageUncertainty" && uncerImageMerger="./clitkImageUncertainty"
+ ${uncerImageMerger} -i ${summed_merged_file} -s ${squared_merged_file} -o ${mergedfile} -n ${totalEvents}
+ else
+ warning "${totalEvents} not positive. A at least one stat file (SimulationStatisticActor) must be provided. Error, no uncertainty computed"
+ return;
+ fi
+ else
+ error "merge_dispatcher_uncertainty does not handle ${firstpartialoutputfile} files"
+ fi
+ fi
+
+}
+
+echo "!!!! this is $0 v0.3k !!!!"
+echo "Usage: gate_power_merge.sh run.dir [--force]"
+echo " where --force allows to merge files even if the file is missing in some output directories"
+
+rundir="${1?"provide run dir"}"
+force=${2:-""}
+rundir="$(echo "${rundir}" | sed 's|/*$||')"
+nboutputdirs="$(find "${rundir}" -mindepth 1 -type d -name 'output*' -o -type l -name 'output*' | wc -l)"
+
+test ${nboutputdirs} -gt 0 || error "no output dir found"
+echo "found ${nboutputdirs} partial output dirs"
+
+outputdir="results"
+if [ "${rundir}" != "." -a "${rundir##*.}" != "${rundir}" ]
+then
+ outputdir="results.${rundir##*.}"
+fi
+outputdir="$(basename "${outputdir}")"
+echo "output dir is ${outputdir}"
+
+test -d "${outputdir}" && rm -r "${outputdir}"
+mkdir "${outputdir}"
+
+unamestr=`uname`
+if [[ "$unamestr" == 'Darwin' ]]; then
+ files=$(find -L "${rundir}" -type f -regex ".*output.*[hdr|mhd|mha|root|txt]" | awk -F '/' '{ print $NF; }' | sort | uniq)
+else
+ files=$(find -L "${rundir}" -regextype 'posix-extended' -type f -regex "${rundir}/output.*\.(hdr|mhd|mha|root|txt)" | awk -F '/' '{ print $NF; }' | sort | uniq)
+fi
+echo files $files
+for outputfile in ${files}
+do
+ merge_dispatcher "${outputfile}" "${force}"