]> Creatis software - clitk.git/blob - scripts/midp_common.sh
using get_dicom_field_value in midp scripts
[clitk.git] / scripts / midp_common.sh
1 #! /bin/sh -x
2
3 ###############################################################################
4 #
5 # FILE: midp_common.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
7 #
8 # Helper file with many functions used in the midP scripts.
9 #
10 ###############################################################################
11
12 bin=`dirname $0`
13 source $bin/tools.sh
14
15 #
16 # check return value passed and abort if it represents an error (ie, ret != 0)
17 # optionally, a function can be passed as a 3rd parameter, to be called just
18 # before exiting. this is useful for cleaning up, for example.
19 #
20 abort_on_error()
21 {
22   if [ $2 != 0 ]; then
23     echo Aborted at $1 with code $2
24     #if [ $# = 3 ]; then
25     #  eval $3
26     #fi
27
28     exit $2
29   fi
30 }
31
32 #
33 # wait for all processes in the list and return their exit codes
34 # in the ret_codes variable.
35 #
36 # OBS: this function must always be called in the same shell
37 # that launched the processes.
38 #
39 wait_pids()
40 {
41   local pids=$*
42   local ret=
43   local rets=
44 #   echo PIDS: $pids
45   for p in $pids; do
46 #     echo waiting $p
47     wait $p > /dev/null 2> /dev/null
48     ret=$?
49     if [ ret != 127 ]; then
50       rets=$rets" "$ret
51     else
52       rets=$rets" "0
53     fi
54       
55   done
56
57   ret_codes=$rets
58 }
59
60 #
61 # clean-up functions for maks, registration, and midp
62 #
63 clean_up_masks()
64 {
65   rm -fr $mask_dir_tmp
66 }
67
68 clean_up_midp()
69 {
70   rm -fr $midp_dir
71 }
72
73 clean_up_registration()
74 {
75   rm -fr $vf_dir
76   rm -fr $output_dir
77 }
78
79
80 #
81 # block execution untill the number of threads (jobs) launched by the
82 # current process is below the given number of threads. 
83 MAX_THREADS=2
84 check_threads()
85 {
86   nbth=$1
87   while [[ $(jobs -p | wc -l) -ge $nbth ]]; do
88       jobs
89       sleep 10
90   done
91 }
92
93 #
94 # receive a 4D file and and extract the corresponding phase numbers
95 # export the variables containing each of the extracted data
96 #
97 extract_4d_phase_numbers()
98 {
99   mhd4d=$1
100
101   nb_phases=${#phase_files[@]}
102
103   # get everything except numbers and punctuation
104   cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[^0-9[:punct:]]*" | sort -u > /tmp/patterns.txt
105
106   # find which patterns have the phases connected to it
107   patterns=`cat /tmp/patterns.txt`
108   if [ -z "$patterns" ]; then
109     phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep "[0-9]\+"` )
110   else
111     for i in $patterns; do 
112
113       # check if the pattern appears before the phase number
114       nb_phases_found=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | sort -u | wc -l`
115       if [ $nb_phases_found == $nb_phases ]; then
116         # keep only what identifies the phase number
117         phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` ) 
118         break
119       fi
120     
121       # check if the pattern appears after the phase number
122       nb_phases_found=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | sort -u | wc -l`
123       if [ $nb_phases_found == $nb_phases ]; then
124         # keep only what identifies the phase number
125         phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` ) 
126         break
127       fi
128
129     done
130   fi
131
132   echo "Phase numbers are ${phase_nbs[@]}"
133   rm /tmp/patterns.txt
134 }
135
136 #
137 # receive a 4D file and extract the corresponding phase files, 
138 # and phase numbers.
139 # export the variables containing each of the extracted data
140 #
141 extract_4d_phases()
142 {
143   mhd4d=$1
144
145   echo "4D file is $mhd4d"
146
147   # array of phase files
148   phase_files=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:'` )
149   echo "Phase files are ${phase_files[@]}"
150
151   extract_4d_phase_numbers $mhd4d 
152 }
153
154
155 #
156 # receive a 4D file and the reference phase number as input 
157 # and extract the corresponding phase files, phase numbers, 
158 # and reference phase file. 
159 #
160 # export the variables containing each of the extracted data
161 #
162 extract_4d_phases_ref()
163 {
164   extract_4d_phases $1
165
166   # reference phase file
167   ref_phase_file=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | grep $2`
168   echo "Reference phase is $ref_phase_file"
169
170   # reference phase number
171   for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
172     ref_phase_nb=`echo ${phase_nbs[$i]} | grep $2`
173     if [ -n "$ref_phase_nb" ]; then
174       echo "Reference phase number is $ref_phase_nb"
175       break
176     fi
177   done
178 }
179
180 #
181 # replacement for clitkCombineImage
182 combine_image()
183 {
184 #  eg: -i $result_in -j $result_out -o $out_result -m $motion_mask
185   local tmp1=$RANDOM
186   local tmp2=$RANDOM
187
188   clitkSetBackground -i $1 -o $tmp1.mhd -m $4
189   clitkSetBackground -i $2 -o $tmp2.mhd -m $4 --fg
190
191   clitkImageArithm -i $tmp1.mhd -j $tmp2.mhd -o $3
192   rm $tmp1.* $tmp2.*
193 }
194
195
196 # replacement for clitkAverageTemporalDimension
197 average_temporal_dimension()
198 {
199   # eg: -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd
200   local tmp=$RANDOM
201   local tot=$tmp.mhd
202
203   local dir=`dirname $1` 
204   local first=`grep z*raw $1 | sed 's/z*raw/mhd/g' | head -n 1`
205   clitkImageArithm -i $dir/$first -o $tot -t 1 -s 0
206
207   local nbphases=`grep z*raw $1 | sed 's/z*raw/mhd/g' | wc -l`
208   for i in $(grep z*raw $1 | sed 's/z*raw/mhd/g'); do
209     clitkImageArithm -i $dir/$i -j $tot -o $tot
210   done
211
212   clitkImageArithm -i $tot -o $2 -t 11 -s $nbphases
213   rm $tmp.*
214 }
215
216 select_contour_gui()
217 {
218   local roi_list=$@
219   roi=`zenity --list --title="Available Contours" --column="Please choose a contour:" $roi_list`
220   case $? in
221     0)
222     if [ -z $roi ]
223     then
224       zenity --warning --text="You must choose one contour."
225       select_contour_gui $roi_list
226     else
227       rtstruct_roi_name=$roi
228     fi;;
229     1)
230     if zenity --question --text="Do you really wish to quit?"
231     then
232       exit
233     else
234       select_contour_gui $roi_list
235     fi;;
236     -1)
237       zenity --error --text="Unexpected error. Please relaunch the application."
238       exit;;
239   esac
240 }
241
242 select_contour()
243 {
244   local roi_list=$@
245   echo "Available Contours:" 
246   for r in $roi_list; do
247     echo $r
248   done
249
250   echo "Please choose a contour number:"
251   read rtstruct_roi_index
252   let i=0
253   for r in $roi_list; do
254     if [ $rtstruct_roi_index = `echo $r | cut -d ':' -f 1` ]; then
255       rtstruct_roi_index=$i
256       break;
257     fi
258     let i=i+1
259   done
260   
261 }
262
263 select_roi()
264 {
265   rtstruct_roi_name_list=( `get_dicom_field_value "3006|0026" ${rtstruct_file} all | sed 's/ /_/g'` )
266   rtstruct_roi_number_list=( `get_dicom_field_value "3006|0022" ${rtstruct_file} all | sed 's/ //g'` )
267   rtstruct_roi_list=( )
268   for i in $(seq 0 1 $(( ${#rtstruct_roi_name_list[@]} - 1 ))); do
269     rtstruct_roi_list[$i]=${rtstruct_roi_number_list[$i]}:${rtstruct_roi_name_list[$i]}
270   done
271
272   if [ $gui_mode = 1 ]; then
273     select_contour_gui ${rtstruct_roi_list[@]}
274     rtstruct_roi=`echo ${rtstruct_roi_name} | cut -d ':' -f 1`
275     rtstruct_roi_name=`echo ${rtstruct_roi_name} | cut -d ':' -f 2`
276   else
277     select_contour ${rtstruct_roi_list[@]}
278     rtstruct_roi=${rtstruct_roi_number_list[$(( $rtstruct_roi_index ))]}
279     rtstruct_roi_name=${rtstruct_roi_name_list[$(( $rtstruct_roi_index ))]}
280   fi
281 }