13 echo "MERGE_WARNING: $1"
17 count_max="${1:?"provide count max"}"
21 local count="${1:?"provide count"}"
22 local message="${2:?"provide message"}"
23 local percent=$(echo "100*${count}/${count_max}" | bc)
24 #printf "[%03d/%03d] %3d%% %-80.80s\r" ${count} ${count_max} ${percent} "${message}"
25 printf "[%03d/%03d] %3d%% %-80.80s\n" ${count} ${count_max} ${percent} "${message}"
33 rootMerger="clitkMergeRootFiles"
34 test -x "./clitkMergeRootFiles" && rootMerger="./clitkMergeRootFiles"
39 echo " ${indent}entering root merger"
40 echo " ${indent}merger is ${rootMerger}"
41 echo " ${indent}creating ${merged}"
47 echo " ${indent}just one partial file => just copy it"
53 local arguments=" -o ${merged}"
59 local arguments=" -i ${partial} ${arguments}"
61 ${rootMerger} ${arguments} > /dev/null || warning "error while calling ${rootMerger}"
62 echo " ${indent}merged ${count} files"
65 statMerger="mergeStatFile.py"
66 test -x "./mergeStatFile.sh" && statMerger="./mergeStatFile.sh"
71 echo " ${indent}entering stat merger"
72 echo " ${indent}merger is ${statMerger}"
73 echo " ${indent}creating ${merged}"
82 if test ! -f "${merged}"
84 update_bar ${count} "copying first partial result ${partial}"
85 cp "${partial}" "${merged}"
89 update_bar ${count} "adding ${partial}"
90 ${statMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${statMerger}"
93 echo " ${indent}merged ${count} files"
96 txtImageMerger="clitkMergeAsciiDoseActor"
97 test -f "./clitkMergeAsciiDoseActor" && txtImageMerger="./clitkMergeAsciiDoseActor"
99 function merge_txt_image {
102 echo " ${indent}entering text image merger"
103 echo " ${indent}merger is ${txtImageMerger}"
104 echo " ${indent}creating ${merged}"
113 if test ! -f "${merged}"
115 update_bar ${count} "copying first partial result ${partial}"
116 cp "${partial}" "${merged}"
120 update_bar ${count} "adding ${partial}"
121 local header="$(cat "${merged}" | head -n 6)"
122 local tmp="$(mktemp)"
123 ${txtImageMerger} -i "${partial}" -j "${merged}" -o "${tmp}" 2> /dev/null > /dev/null || warning "error while calling ${txtImageMerger}"
124 echo "${header}" > "${merged}"
125 grep -v '## Merge' "${tmp}" >> "${merged}"
129 echo " ${indent}merged ${count} files"
132 hdrImageMerger="clitkImageArithm"
133 test -x "./clitkImageArithm" && hdrImageMerger="./clitkImageArithm"
135 function merge_hdr_image {
137 local merged_bin="${merged%.*}.img"
139 echo " ${indent}entering hdr image merger"
140 echo " ${indent}merger is ${hdrImageMerger}"
141 echo " ${indent}creating ${merged}"
147 local partial_bin="${partial%.*}.img"
151 if test ! -f "${merged}"
153 update_bar ${count} "copying first partial result ${partial}"
154 cp "${partial}" "${merged}"
155 cp "${partial_bin}" "${merged_bin}"
159 update_bar ${count} "adding ${partial}"
160 ${hdrImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${hdrImageMerger}"
163 echo " ${indent}merged ${count} files"
166 function merge_dispatcher {
168 local outputfile="${1:?"provide output filename"}"
169 echo "merging ${outputfile}"
171 local partialoutputfiles="$(find "${rundir}" -type f -name "${outputfile}")"
172 local nboutputfiles="$(echo "${partialoutputfiles}" | wc -l)"
173 if test ${nboutputdirs} -ne ${nboutputfiles}
175 warning "missing files"
179 local firstpartialoutputfile="$(echo "${partialoutputfiles}" | head -n 1)"
180 local firstpartialoutputextension="${firstpartialoutputfile##*.}"
181 echo "${indent}testing file type on ${firstpartialoutputfile}"
183 if test "${firstpartialoutputextension}" == "hdr"
185 echo "${indent}this is a analyse image"
186 local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
187 merge_hdr_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
191 if test "${firstpartialoutputextension}" == "root"
193 echo "${indent}this is a root file"
194 local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
195 merge_root "${mergedfile}" ${partialoutputfiles} || error "error while merging"
199 if test "${firstpartialoutputextension}" == "txt" && grep 'NumberOfEvent' "${firstpartialoutputfile}" > /dev/null
201 echo "${indent}this is a stat file"
202 local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
203 merge_stat "${mergedfile}" ${partialoutputfiles} || error "error while merging"
207 if test "${firstpartialoutputextension}" == "txt" && grep 'Resol' "${firstpartialoutputfile}" > /dev/null
209 local resol="$(sed -nr '/Resol/s/^.*=\s+\((.+)\)\s*$/\1/p' "${firstpartialoutputfile}")"
210 local resolx="$(echo "${resol}" | cut -d',' -f1)"
211 local resoly="$(echo "${resol}" | cut -d',' -f2)"
212 local resolz="$(echo "${resol}" | cut -d',' -f3)"
213 if test "${resol}" == "1,1,1"
215 echo "${indent}this is a txt integral value"
216 local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
217 merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
220 if test \( "${resolx}" == "1" -a "${resoly}" == "1" \) -o \( "${resoly}" == "1" -a "${resolz}" == "1" \) -o \( "${resolz}" == "1" -a "${resolx}" == "1" \)
222 echo "${indent}this is a txt profile"
223 local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
224 merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
229 warning "unknown file type"
232 echo "!!!! this is $0 v0.3f !!!!"
234 rundir="${1?"provide run dir"}"
235 nboutputdirs="$(find "${rundir}" -mindepth 1 -type d -name 'output*' | wc -l)"
237 test ${nboutputdirs} -gt 0 || error "no output dir found"
238 echo "found ${nboutputdirs} partial output dirs"
241 if [ "${rundir}" != "." -a "${rundir##*.}" != "${rundir}" ]
243 outputdir="results.${rundir##*.}"
245 outputdir="$(basename "${outputdir}")"
246 echo "output dir is ${outputdir}"
247 test -d "${outputdir}" && rm -r "${outputdir}"
250 for outputfile in $(find "${rundir}" -regextype 'posix-extended' -type f -regex '.*\.(hdr|root|txt)' | awk -F '/' '{ print $NF }' | sort | uniq)
252 merge_dispatcher "${outputfile}"
255 if [ -f "${rundir}/params.txt" ]
257 echo "copying params file"
258 cp "${rundir}/params.txt" "${outputdir}/params.txt"
261 echo "these was ${warning_count} warning(s)"