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