15 count_max="${1:?"provide count max"}"
19 local count="${1:?"provide count"}"
20 local message="${2:?"provide message"}"
21 local percent=$(echo "100*${count}/${count_max}" | bc)
22 printf "[%03d/%03d] %3d%% %-80s\r" ${count} ${count_max} ${percent} "${message}"
33 echo " ${indent}entering root merger"
34 echo " ${indent}creating ${merged}"
36 local arguments=" -o ${merged}"
42 local arguments=" -i ${partial} ${arguments}"
44 clitkMergeRootFiles ${arguments} 2> /dev/null > /dev/null || error "error while calling clitkMergeRootFiles"
45 echo " ${indent}merged ${count} files"
51 echo " ${indent}entering stat merger"
52 echo " ${indent}creating ${merged}"
61 if test ! -f "${merged}"
63 update_bar ${count} "copying first partial result ${partial}"
64 cp "${partial}" "${merged}"
68 update_bar ${count} "adding ${partial}"
69 mergeStatFile.py -i "${merged}" -j "${partial}" -o "${merged}" > /dev/null || error "error while calling mergeStatFile"
72 echo " ${indent}merged ${count} files"
75 function merge_txt_image {
78 echo " ${indent}entering text image merger"
79 echo " ${indent}creating ${merged}"
88 if test ! -f "${merged}"
90 update_bar ${count} "copying first partial result ${partial}"
91 cp "${partial}" "${merged}"
95 update_bar ${count} "adding ${partial}"
96 local header="$(cat "${merged}" | head -n 6)"
98 clitkMergeAsciiDoseActor -i "${partial}" -j "${merged}" -o "${tmp}" > /dev/null || error "error while calling clitkMergeAsciiDoseActor"
99 echo "${header}" > "${merged}"
100 grep -v '## Merge' "${tmp}" >> "${merged}"
104 echo " ${indent}merged ${count} files"
107 function merge_hdr_image {
109 local merged_bin="${merged%.*}.img"
111 echo " ${indent}entering hdr image merger"
112 echo " ${indent}creating ${merged}"
118 local partial_bin="${partial%.*}.img"
122 if test ! -f "${merged}"
124 update_bar ${count} "copying first partial result ${partial}"
125 cp "${partial}" "${merged}"
126 cp "${partial_bin}" "${merged_bin}"
130 update_bar ${count} "adding ${partial}"
131 clitkImageArithm -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || error "error while calling clitkImageArithm"
134 echo " ${indent}merged ${count} files"
137 rundir="${1?"provide run dir"}"
138 nboutputdirs="$(find "${rundir}" -mindepth 1 -type d | wc -l)"
140 test ${nboutputdirs} -gt 0 || error "no output dir found"
141 echo "found ${nboutputdirs} partial output dirs"
143 outputdir="merged.${rundir##*.}"
144 outputdir="$(basename "${outputdir}")"
145 echo "output dir is ${outputdir}"
146 test -d "${outputdir}" && rm -r "${outputdir}"
149 for outputfile in $(find "${rundir}" -regextype 'posix-extended' -type f -regex '.*\.(hdr|root|txt)' | awk -F '/' '{ print $NF }' | sort | uniq)
152 echo "merging ${outputfile}"
154 partialoutputfiles="$(find "${rundir}" -type f -name "${outputfile}")"
155 nboutputfiles="$(echo "${partialoutputfiles}" | wc -l)"
156 if test ${nboutputdirs} -ne ${nboutputfiles}
158 warning "missing files"
162 firstpartialoutputfile="$(echo "${partialoutputfiles}" | head -n 1)"
163 firstpartialoutputextension="${firstpartialoutputfile##*.}"
164 echo "${indent}testing file type on ${firstpartialoutputfile}"
166 if test "${firstpartialoutputextension}" == "hdr"
168 echo "${indent}this is a analyse image"
169 mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
170 merge_hdr_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
174 if test "${firstpartialoutputextension}" == "root"
176 echo "${indent}this is a root file"
177 mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
178 merge_root "${mergedfile}" ${partialoutputfiles} || error "error while merging"
182 if test "${firstpartialoutputextension}" == "txt" && grep 'NumberOfEvent' "${firstpartialoutputfile}" > /dev/null
184 echo "${indent}this is a stat file"
185 mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
186 merge_stat "${mergedfile}" ${partialoutputfiles} || error "error while merging"
190 if test "${firstpartialoutputextension}" == "txt" && grep 'Resol' "${firstpartialoutputfile}" > /dev/null
192 resol="$(sed -nr '/Resol/s/^.*=\s+\((.+)\)\s*$/\1/p' "${firstpartialoutputfile}")"
193 resolx="$(echo "${resol}" | cut -d',' -f1)"
194 resoly="$(echo "${resol}" | cut -d',' -f2)"
195 resolz="$(echo "${resol}" | cut -d',' -f3)"
196 if test "${resol}" == "1,1,1"
198 echo "${indent}this is a txt integral value"
199 mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
200 merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
203 if test \( "${resolx}" == "1" -a "${resoly}" == "1" \) -o \( "${resoly}" == "1" -a "${resolz}" == "1" \) -o \( "${resolz}" == "1" -a "${resolx}" == "1" \)
205 echo "${indent}this is a txt profile"
206 mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
207 merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
213 warning "unknown file type"