]> Creatis software - clitk.git/blob - cluster_tools/gate_power_merge.sh
gate_power_merge support interfile sinogramme
[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 warning_count=0
11 function warning {
12 let "warning_count++"
13 echo "MERGE_WARNING: $1"
14 }
15
16 function start_bar {
17 count_max="${1:?"provide count max"}"
18 }
19
20 function update_bar {
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}"
26 }
27
28 function end_bar {
29 unset count_max
30 #echo -ne '\n'
31 }
32
33 function check_interfile {
34 local input_interfile="${1:?"provide input interfile"}"
35
36 grep -qs '!INTERFILE :=' "${input_interfile}" || return 1
37
38 local header_byte_size=$(awk -F' ' '
39 BEGIN { zsize = 0; }
40 /matrix size/ && $3 == "[1]" { xsize = $5; }
41 /matrix size/ && $3 == "[2]" { ysize = $5; }
42 /number of projections/ { zsize += $5; }
43 /number of bytes per pixel/ { byte_per_pixel = $7; }
44 END { print xsize * ysize * zsize * byte_per_pixel; }' "${input_interfile}")
45
46 local raw_interfile="$(dirname "${input_interfile}")/$(awk -F' := ' '/name of data file/ { print $2; }' "${input_interfile}")"
47
48 test -f "${raw_interfile}" || return 1
49 test $(stat -c%s "${raw_interfile}") -eq ${header_byte_size} || return 1
50 }
51
52 function write_mhd_header {
53 local input_interfile="${1:?"provide input interfile"}"
54 local output_mhd="$(dirname "${input_interfile}")/$(basename "${input_interfile}" ".hdr").mhd"
55
56 check_interfile "${input_interfile}" || error "${input_interfile} isn't an interfile image"
57
58 local header_start='ObjectType = Image
59 NDims = 3
60 AcquisitionDate = none
61 BinaryData = True
62 BinaryDataByteOrderMSB = False
63 CompressedData = False
64 TransformMatrix = 1 0 0 0 1 0 0 0 1
65 Offset = 0 0 0
66 CenterOfRotation = 0 0 0
67 DistanceUnits = mm
68 AnatomicalOrientation = RIP'
69
70 echo "${header_start}" > "${output_mhd}"
71
72 awk -F' ' '
73 /scaling factor/ && $4 == "[1]" { xspacing = $6; }
74 /scaling factor/ && $4 == "[2]" { yspacing = $6; }
75 END { print "ElementSpacing = " xspacing " " yspacing " 1"; }' "${input_interfile}" >> "${output_mhd}"
76
77 awk -F' ' '
78 BEGIN { zsize = 0; }
79 /matrix size/ && $3 == "[1]" { xsize = $5; }
80 /matrix size/ && $3 == "[2]" { ysize = $5; }
81 /number of projections/ { zsize += $5; }
82 END { print "DimSize = " xsize " " ysize " " zsize; }' "${input_interfile}" >> "${output_mhd}"
83
84 awk -F' := ' '
85 /number format/ { format = $2; }
86 /number of bytes per pixel/ { byte_per_pixel = $2 }
87 END {
88 if (format == "unsigned integer" && byte_per_pixel == 8) { print "ElementType = MET_ULONG"; exit };
89 if (format == "unsigned integer" && byte_per_pixel == 4) { print "ElementType = MET_UINT"; exit };
90 if (format == "unsigned integer" && byte_per_pixel == 2) { print "ElementType = MET_USHORT"; exit };
91 if (format == "unsigned integer" && byte_per_pixel == 1) { print "ElementType = MET_UCHAR"; exit };
92 if (format == "integer" && byte_per_pixel == 8) { print "ElementType = MET_LONG"; exit };
93 if (format == "integer" && byte_per_pixel == 4) { print "ElementType = MET_INT"; exit };
94 if (format == "integer" && byte_per_pixel == 2) { print "ElementType = MET_SHORT"; exit };
95 if (format == "integer" && byte_per_pixel == 1) { print "ElementType = MET_CHAR"; exit };
96 if (format == "float" && byte_per_pixel == 8) { print "ElementType = MET_FLOAT"; exit };
97 if (format == "float" && byte_per_pixel == 4) { print "ElementType = MET_DOUBLE"; exit };
98 print "ElementType = MET_INT";
99 }' "${input_interfile}" >> "${output_mhd}"
100
101 awk -F' := ' '
102 /name of data file/ { print "ElementDataFile = " $2; }' "${input_interfile}" >> "${output_mhd}"
103 }
104
105 rootMerger="clitkMergeRootFiles"
106 test -x "./clitkMergeRootFiles" && rootMerger="./clitkMergeRootFiles"
107
108 function merge_root {
109 local merged="$1"
110 shift
111 echo "  ${indent}entering root merger"
112 echo "  ${indent}merger is ${rootMerger}"
113 echo "  ${indent}creating ${merged}"
114 #echo "######## $#"
115 #echo "######## $*"
116
117 if test $# -eq 1
118 then
119     echo "  ${indent}just one partial file => just copy it"
120     cp "$1" "${merged}"
121     return
122 fi
123
124 local count=0
125 local arguments=" -o ${merged}"
126 while test $# -gt 0
127 do
128     local partial="$1"
129     shift
130     let count++
131     local arguments=" -i ${partial} ${arguments}"
132 done
133 ${rootMerger} ${arguments} > /dev/null || warning "error while calling ${rootMerger}"
134 echo "  ${indent}merged ${count} files"
135 }
136
137 statMerger="mergeStatFile.py"
138 test -x "./mergeStatFile.sh" && statMerger="./mergeStatFile.sh"
139
140 function merge_stat {
141 local merged="$1"
142 shift
143 echo "  ${indent}entering stat merger"
144 echo "  ${indent}merger is ${statMerger}"
145 echo "  ${indent}creating ${merged}"
146 local count=0
147 start_bar $#
148 while test $# -gt 0
149 do
150     local partial="$1"
151     shift
152     let count++
153
154     if test ! -f "${merged}"
155     then
156         update_bar ${count} "copying first partial result ${partial}"
157         cp "${partial}" "${merged}"
158         continue
159     fi
160
161     update_bar ${count} "adding ${partial}"
162     ${statMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${statMerger}"
163 done
164 end_bar
165 echo "  ${indent}merged ${count} files"
166 }
167
168 txtImageMerger="clitkMergeAsciiDoseActor"
169 test -f "./clitkMergeAsciiDoseActor" && txtImageMerger="./clitkMergeAsciiDoseActor"
170
171 function merge_txt_image {
172 local merged="$1"
173 shift
174 echo "  ${indent}entering text image merger"
175 echo "  ${indent}merger is ${txtImageMerger}"
176 echo "  ${indent}creating ${merged}"
177 local count=0
178 start_bar $#
179 while test $# -gt 0
180 do
181     local partial="$1"
182     shift
183     let count++
184
185     if test ! -f "${merged}"
186     then
187         update_bar ${count} "copying first partial result ${partial}"
188         cp "${partial}" "${merged}"
189         continue
190     fi
191
192     update_bar ${count} "adding ${partial}"
193     local header="$(cat "${merged}" | head -n 6)"
194     local tmp="$(mktemp)"
195     ${txtImageMerger} -i "${partial}" -j "${merged}" -o "${tmp}" 2> /dev/null > /dev/null || warning "error while calling ${txtImageMerger}"
196     echo "${header}" > "${merged}"
197     grep -v '## Merge' "${tmp}" >> "${merged}"
198     rm "${tmp}"
199 done
200 end_bar
201 echo "  ${indent}merged ${count} files"
202 }
203
204 hdrImageMerger="clitkImageArithm"
205 test -x "./clitkImageArithm" && hdrImageMerger="./clitkImageArithm"
206
207 function merge_hdr_image {
208 local merged="$1"
209 local merged_bin="${merged%.*}.img"
210 shift
211 echo "  ${indent}entering hdr image merger"
212 echo "  ${indent}merger is ${hdrImageMerger}"
213 echo "  ${indent}creating ${merged}"
214 local count=0
215 start_bar $#
216 while test $# -gt 0
217 do
218     local partial="$1"
219     local partial_bin="${partial%.*}.img"
220     shift
221     let count++
222
223     if test ! -f "${merged}"
224     then
225         update_bar ${count} "copying first partial result ${partial}"
226         cp "${partial}" "${merged}"
227         cp "${partial_bin}" "${merged_bin}"
228         continue
229     fi
230
231     update_bar ${count} "adding ${partial}"
232     ${hdrImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${hdrImageMerger}"
233 done
234 end_bar
235 echo "  ${indent}merged ${count} files"
236 }
237
238 mhdImageMerger="clitkImageArithm"
239 test -x "./clitkImageArithm" && mhdImageMerger="./clitkImageArithm"
240
241 function merge_mhd_image {
242 local merged="$1"
243 local merged_bin="${merged%.*}.raw"
244 shift
245 echo "  ${indent}entering mhd image merger"
246 echo "  ${indent}merger is ${mhdImageMerger}"
247 echo "  ${indent}creating ${merged}"
248 local count=0
249 start_bar $#
250 while test $# -gt 0
251 do
252     local partial="$1"
253     local partial_bin="$(dirname "${partial}")/$(awk -F' = ' '/ElementDataFile/ { print $2; }' "${partial}")"
254     shift
255     let count++
256
257     if test ! -f "${merged}"
258     then
259         update_bar ${count} "copying first partial result ${partial}"
260         cp "${partial}" "${merged}"
261         cp "${partial_bin}" "${merged_bin%.*}.${partial_bin##*.}"
262         continue
263     fi
264
265     update_bar ${count} "adding ${partial}"
266     ${mhdImageMerger} -t 0 -i "${partial}" -j "${merged}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${mhdImageMerger}"
267     mv "${merged_bin}" "${merged_bin%.*}.${partial_bin##*.}"
268     sed -i "s/$(basename "${merged_bin}")/$(basename "${merged_bin%.*}.${partial_bin##*.}")/" "${merged}"
269 done
270 end_bar
271 echo "  ${indent}merged ${count} files"
272 }
273
274 function merge_dispatcher {
275     local indent="  ** "
276     local outputfile="${1:?"provide output filename"}"
277     echo "merging ${outputfile}"
278
279     local partialoutputfiles="$(find "${rundir}" -type f -name "${outputfile}")"
280     local nboutputfiles="$(echo "${partialoutputfiles}" | wc -l)"
281     if test ${nboutputdirs} -ne ${nboutputfiles}
282     then
283         warning "missing files"
284         return
285     fi
286
287     local firstpartialoutputfile="$(echo "${partialoutputfiles}" | head -n 1)"
288     local firstpartialoutputextension="${firstpartialoutputfile##*.}"
289     echo "${indent}testing file type on ${firstpartialoutputfile}"
290
291     if test "${firstpartialoutputextension}" == "hdr" && grep -qs 'INTERFILE' "${firstpartialoutputfile}"
292     then
293         echo "${indent}this is a interfile image"
294         echo "${indent}creating mhd headers"
295         for partialoutputfile in $partialoutputfiles; do write_mhd_header "${partialoutputfile}"; done
296         local mhd_partialoutputfiles="$(for partialoutputfile in $partialoutputfiles; do echo "${partialoutputfile%.*}.mhd"; done)"
297         local mhd_mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}" ".hdr").mhd"
298         merge_mhd_image "${mhd_mergedfile}" ${mhd_partialoutputfiles} || error "error while merging"
299         echo "${indent}cleaning mhd headers"
300         for mhd_partialoutputfile in $mhd_partialoutputfiles; do rm "${mhd_partialoutputfile}"; done
301         rm "${mhd_mergedfile}"
302         echo "${indent}copy interfile header"
303         cp "${firstpartialoutputfile}" "${outputdir}"
304         return
305     fi
306
307     if test "${firstpartialoutputextension}" == "hdr"
308     then
309         echo "${indent}this is a analyse image"
310         local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
311         merge_hdr_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
312         return
313     fi
314
315     if test "${firstpartialoutputextension}" == "mhd"
316     then
317         echo "${indent}this is a mhd image"
318         local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
319         merge_mhd_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
320         return
321     fi
322
323     if test "${firstpartialoutputextension}" == "root"
324     then
325         echo "${indent}this is a root file"
326         local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
327         merge_root "${mergedfile}" ${partialoutputfiles} || error "error while merging"
328         return
329     fi
330
331     if test "${firstpartialoutputextension}" == "txt" && grep -qs 'NumberOfEvent' "${firstpartialoutputfile}"
332     then
333         echo "${indent}this is a stat file"
334         local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
335         merge_stat "${mergedfile}" ${partialoutputfiles} || error "error while merging"
336         return
337     fi
338
339     if test "${firstpartialoutputextension}" == "txt" && grep -qs 'Resol' "${firstpartialoutputfile}"
340     then
341         local resol="$(sed -nr '/Resol/s/^.*=\s+\((.+)\)\s*$/\1/p' "${firstpartialoutputfile}")"
342         local resolx="$(echo "${resol}" | cut -d',' -f1)"
343         local resoly="$(echo "${resol}" | cut -d',' -f2)"
344         local resolz="$(echo "${resol}" | cut -d',' -f3)"
345         if test "${resol}" == "1,1,1"
346         then
347             echo "${indent}this is a txt integral value"
348             local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
349             merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
350             return
351         fi
352         if test \( "${resolx}" == "1" -a "${resoly}" == "1" \) -o \( "${resoly}" == "1" -a "${resolz}" == "1" \) -o \( "${resolz}" == "1" -a "${resolx}" == "1" \)
353         then
354             echo "${indent}this is a txt profile"
355             local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
356             merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
357             return
358         fi
359     fi
360
361     if test "${firstpartialoutputextension}" == "txt"
362     then
363         echo "${indent}this is a non specific txt output"
364         local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
365         local nbdifferent="$(md5sum ${partialoutputfiles} | awk '{ print $1; }' | sort | uniq | wc -l)"
366         echo "  ${indent}${nbdifferent} different files"
367         if test ${nbdifferent} -gt 1
368         then
369             echo "  ${indent}catting to ${mergedfile}"
370             cat ${partialoutputfiles} > "${mergedfile}" || error "error while merging"
371             return
372         else
373             echo "  ${indent}moving to ${mergedfile}"
374             cp "${firstpartialoutputfile}" "${mergedfile}" || error "error while merging"
375             return
376         fi
377     fi
378
379     error "unknown file type"
380 }
381
382 echo "!!!! this is $0 v0.3j !!!!"
383
384 rundir="${1?"provide run dir"}"
385 rundir="$(echo "${rundir}" | sed 's|/*$||')"
386 nboutputdirs="$(find "${rundir}" -mindepth 1 -type d -name 'output*' | wc -l)"
387
388 test ${nboutputdirs} -gt 0 || error "no output dir found"
389 echo "found ${nboutputdirs} partial output dirs"
390
391 outputdir="results"
392 if [ "${rundir}" != "." -a "${rundir##*.}" != "${rundir}" ]
393 then
394     outputdir="results.${rundir##*.}"
395 fi
396 outputdir="$(basename "${outputdir}")"
397 echo "output dir is ${outputdir}"
398 test -d "${outputdir}" && rm -r "${outputdir}"
399 mkdir "${outputdir}"
400
401 for outputfile in $(find "${rundir}" -regextype 'posix-extended' -type f -regex "${rundir}/output.*\.(hdr|mhd|root|txt)" | awk -F '/' '{ print $NF; }' | sort | uniq)
402 do
403     merge_dispatcher "${outputfile}"
404 done
405
406 if [ -f "${rundir}/params.txt" ]
407 then
408         echo "copying params file"
409         cp "${rundir}/params.txt" "${outputdir}/params.txt"
410 fi
411
412 echo "these was ${warning_count} warning(s)"