]> Creatis software - clitk.git/blob - cluster_tools/gate_power_merge.sh
nice print and verbose
[clitk.git] / cluster_tools / gate_power_merge.sh
1 #!/usr/bin/env bash
2
3 set -u 
4
5 function error {
6 echo "ERROR: $1"
7 exit 1
8 }
9
10 function warning {
11 echo "WARNING: $1"
12 }
13
14 function start_bar {
15 count_max="${1:?"provide count max"}"
16 }
17
18 function update_bar {
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%% %-80.80s\r" ${count} ${count_max} ${percent} "${message}"
23 }
24
25 function end_bar {
26 unset count_max
27 echo -ne '\n'
28 }
29
30 rootMerger="clitkMergeRootFiles"
31 test -x "./clitkMergeRootFiles" && rootMerger="./clitkMergeRootFiles"
32
33 function merge_root {
34 local merged="$1"
35 shift
36 echo "  ${indent}entering root merger"
37 echo "  ${indent}merger is ${rootMerger}"
38 echo "  ${indent}creating ${merged}"
39 local count=0
40 local arguments=" -o ${merged}"
41 while test $# -gt 0
42 do
43     local partial="$1"
44     shift
45     let count++
46     local arguments=" -i ${partial} ${arguments}"
47 done
48 ${rootMerger} ${arguments} > /dev/null || warning "error while calling ${rootMerger}"
49 echo "  ${indent}merged ${count} files"
50 }
51
52 statMerger="mergeStatFile.py"
53 test -x "./mergeStatFile.sh" && statMerger="./mergeStatFile.sh"
54
55 function merge_stat {
56 local merged="$1"
57 shift
58 echo "  ${indent}entering stat merger"
59 echo "  ${indent}merger is ${statMerger}"
60 echo "  ${indent}creating ${merged}"
61 local count=0
62 start_bar $#
63 while test $# -gt 0
64 do
65     local partial="$1"
66     shift
67     let count++
68
69     if test ! -f "${merged}"
70     then
71         update_bar ${count} "copying first partial result ${partial}"
72         cp "${partial}" "${merged}"
73         continue
74     fi
75
76     update_bar ${count} "adding ${partial}"
77     ${statMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${statMerger}"
78 done
79 end_bar
80 echo "  ${indent}merged ${count} files"
81 }
82
83 txtImageMerger="clitkMergeAsciiDoseActor"
84 test -f "./clitkMergeAsciiDoseActor" && txtImageMerger="./clitkMergeAsciiDoseActor"
85
86 function merge_txt_image {
87 local merged="$1"
88 shift
89 echo "  ${indent}entering text image merger"
90 echo "  ${indent}merger is ${txtImageMerger}"
91 echo "  ${indent}creating ${merged}"
92 local count=0
93 start_bar $#
94 while test $# -gt 0
95 do
96     local partial="$1"
97     shift
98     let count++
99
100     if test ! -f "${merged}"
101     then
102         update_bar ${count} "copying first partial result ${partial}"
103         cp "${partial}" "${merged}"
104         continue
105     fi
106
107     update_bar ${count} "adding ${partial}"
108     local header="$(cat "${merged}" | head -n 6)"
109     local tmp="$(mktemp)"
110     ${txtImageMerger} -i "${partial}" -j "${merged}" -o "${tmp}" 2> /dev/null > /dev/null || warning "error while calling ${txtImageMerger}"
111     echo "${header}" > "${merged}"
112     grep -v '## Merge' "${tmp}" >> "${merged}"
113     rm "${tmp}"
114 done
115 end_bar
116 echo "  ${indent}merged ${count} files"
117 }
118
119 hdrImageMerger="clitkImageArithm"
120 test -x "./clitkImageArithm" && hdrImageMerger="./clitkImageArithm"
121
122 function merge_hdr_image {
123 local merged="$1"
124 local merged_bin="${merged%.*}.img"
125 shift
126 echo "  ${indent}entering hdr image merger"
127 echo "  ${indent}merger is ${hdrImageMerger}"
128 echo "  ${indent}creating ${merged}"
129 local count=0
130 start_bar $#
131 while test $# -gt 0
132 do
133     local partial="$1"
134     local partial_bin="${partial%.*}.img"
135     shift
136     let count++
137
138     if test ! -f "${merged}"
139     then
140         update_bar ${count} "copying first partial result ${partial}"
141         cp "${partial}" "${merged}"
142         cp "${partial_bin}" "${merged_bin}"
143         continue
144     fi
145
146     update_bar ${count} "adding ${partial}"
147     ${hdrImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${hdrImageMerger}"
148 done
149 end_bar
150 echo "  ${indent}merged ${count} files"
151 }
152
153 rundir="${1?"provide run dir"}"
154 nboutputdirs="$(find "${rundir}" -mindepth 1 -type d -name 'output*' | wc -l)"
155
156 test ${nboutputdirs} -gt 0 || error "no output dir found"
157 echo "found ${nboutputdirs} partial output dirs"
158
159 outputdir="results"
160 if [ "${rundir}" != "." -a "${rundir##*.}" != "${rundir}" ]
161 then
162     outputdir="results.${rundir##*.}"
163 fi
164 outputdir="$(basename "${outputdir}")"
165 echo "output dir is ${outputdir}"
166 test -d "${outputdir}" && rm -r "${outputdir}"
167 mkdir "${outputdir}"
168
169 for outputfile in $(find "${rundir}" -regextype 'posix-extended' -type f -regex '.*\.(hdr|root|txt)' | awk -F '/' '{ print $NF }' | sort | uniq)
170 do
171     indent="  ** "
172     echo "merging ${outputfile}"
173
174     partialoutputfiles="$(find "${rundir}" -type f -name "${outputfile}")"
175     nboutputfiles="$(echo "${partialoutputfiles}" | wc -l)"
176     if test ${nboutputdirs} -ne ${nboutputfiles}
177     then
178         warning "missing files"
179         continue
180     fi
181
182     firstpartialoutputfile="$(echo "${partialoutputfiles}" | head -n 1)"
183     firstpartialoutputextension="${firstpartialoutputfile##*.}"
184     echo "${indent}testing file type on ${firstpartialoutputfile}"
185
186     if test "${firstpartialoutputextension}" == "hdr"
187     then
188         echo "${indent}this is a analyse image"
189         mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
190         merge_hdr_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
191         continue
192     fi
193
194     if test "${firstpartialoutputextension}" == "root"
195     then
196         echo "${indent}this is a root file"
197         mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
198         merge_root "${mergedfile}" ${partialoutputfiles} || error "error while merging"
199         continue
200     fi
201
202     if test "${firstpartialoutputextension}" == "txt" && grep 'NumberOfEvent' "${firstpartialoutputfile}" > /dev/null
203     then
204         echo "${indent}this is a stat file"
205         mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
206         merge_stat "${mergedfile}" ${partialoutputfiles} || error "error while merging"
207         continue
208     fi
209
210     if test "${firstpartialoutputextension}" == "txt" && grep 'Resol' "${firstpartialoutputfile}" > /dev/null
211     then
212         resol="$(sed -nr '/Resol/s/^.*=\s+\((.+)\)\s*$/\1/p' "${firstpartialoutputfile}")"
213         resolx="$(echo "${resol}" | cut -d',' -f1)"
214         resoly="$(echo "${resol}" | cut -d',' -f2)"
215         resolz="$(echo "${resol}" | cut -d',' -f3)"
216         if test "${resol}" == "1,1,1"
217         then
218             echo "${indent}this is a txt integral value"
219             mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
220             merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
221             continue
222         fi
223         if test \( "${resolx}" == "1" -a "${resoly}" == "1" \) -o \( "${resoly}" == "1" -a "${resolz}" == "1" \) -o \( "${resolz}" == "1" -a "${resolx}" == "1" \)
224         then
225             echo "${indent}this is a txt profile"
226             mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
227             merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
228             continue
229         fi
230     fi
231
232
233     warning "unknown file type"
234 done
235