]> Creatis software - clitk.git/blob - scripts/midp_common.sh
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[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 #
13 # check return value passed and abort if it represents an error (ie, ret != 0)
14 # optionally, a function can be passed as a 3rd parameter, to be called just
15 # before exiting. this is useful for cleaning up, for example.
16 #
17 abort_on_error()
18 {
19   if [ $2 != 0 ]; then
20     echo Aborted at $1 with code $2
21     if [ $# = 3 ]; then
22       eval $3
23     fi
24
25     exit $2
26   fi
27 }
28
29 #
30 # wait for all processes in the list and return their exit codes
31 # in the ret_codes variable.
32 #
33 # OBS: this function must always be called in the same shell
34 # that launched the processes.
35 #
36 wait_pids()
37 {
38   local pids=$*
39   local ret=
40   local rets=
41 #   echo PIDS: $pids
42   for p in $pids; do
43 #     echo waiting $p
44     wait $p > /dev/null 2> /dev/null
45     ret=$?
46     if [ ret != 127 ]; then
47       rets=$rets" "$ret
48     else
49       rets=$rets" "0
50     fi
51       
52   done
53
54   ret_codes=$rets
55 }
56
57 #
58 # clean-up functions for maks, registration, and midp
59 #
60 clean_up_masks()
61 {
62   rm -fr $mask_dir_tmp
63 }
64
65 clean_up_midp()
66 {
67   rm -fr $midp_dir
68 }
69
70 clean_up_registration()
71 {
72   rm -fr $vf_dir
73   rm -fr $output_dir
74 }
75
76
77 #
78 # block execution untill the number of threads (jobs) launched by the
79 # current process is below the given number of threads. 
80 MAX_THREADS=2
81 check_threads()
82 {
83   nbth=$1
84   while [[ $(jobs -p | wc -l) -ge $nbth ]]; do
85       jobs
86       sleep 10
87   done
88 }
89
90 #
91 # receive a 4D file and and extract the corresponding phase numbers
92 # export the variables containing each of the extracted data
93 #
94 extract_4d_phase_numbers()
95 {
96   mhd4d=$1
97
98   nb_phases=${#phase_files[@]}
99
100   # get everything except numbers and punctuation
101   cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[^0-9[:punct:]]*" | sort -u > /tmp/patterns.txt
102
103   # find which patterns have the phases connected to it
104   patterns=`cat /tmp/patterns.txt`
105   if [ -z "$patterns" ]; then
106     phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep "[0-9]\+"` )
107   else
108     for i in $patterns; do 
109
110       # check if the pattern appears before the phase number
111       nb_phases_found=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | sort -u | wc -l`
112       if [ $nb_phases_found == $nb_phases ]; then
113         # keep only what identifies the phase number
114         phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.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]\+"` ) 
115         break
116       fi
117     
118       # check if the pattern appears after the phase number
119       nb_phases_found=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | sort -u | wc -l`
120       if [ $nb_phases_found == $nb_phases ]; then
121         # keep only what identifies the phase number
122         phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.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]\+"` ) 
123         break
124       fi
125
126     done
127   fi
128
129   echo "Phase numbers are ${phase_nbs[@]}"
130   rm /tmp/patterns.txt
131 }
132
133 #
134 # receive a 4D file and extract the corresponding phase files, 
135 # and phase numbers.
136 # export the variables containing each of the extracted data
137 #
138 extract_4d_phases()
139 {
140   mhd4d=$1
141
142   echo "4D file is $mhd4d"
143
144   # array of phase files
145   phase_files=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:'` )
146   echo "Phase files are ${phase_files[@]}"
147
148   extract_4d_phase_numbers $mhd4d 
149 }
150
151
152 #
153 # receive a 4D file and the reference phase number as input 
154 # and extract the corresponding phase files, phase numbers, 
155 # and reference phase file. 
156 #
157 # export the variables containing each of the extracted data
158 #
159 extract_4d_phases_ref()
160 {
161   extract_4d_phases $1
162
163   # reference phase file
164   ref_phase_file=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | grep $2`
165   echo "Reference phase is $ref_phase_file"
166
167   # reference phase number
168   for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
169     ref_phase_nb=`echo ${phase_nbs[$i]} | grep $2`
170     if [ -n "$ref_phase_nb" ]; then
171       echo "Reference phase number is $ref_phase_nb"
172       break
173     fi
174   done
175 }