3 ###############################################################################
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
8 # Helper file with many functions used in the midP scripts.
10 ###############################################################################
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.
20 echo Aborted at $1 with code $2
30 # wait for all processes in the list and return their exit codes
31 # in the ret_codes variable.
33 # OBS: this function must always be called in the same shell
34 # that launched the processes.
44 wait $p > /dev/null 2> /dev/null
46 if [ ret != 127 ]; then
58 # clean-up functions for maks, registration, and midp
70 clean_up_registration()
78 # block execution untill the number of threads (jobs) launched by the
79 # current process is below the given number of threads.
84 while [[ $(jobs -p | wc -l) -ge $nbth ]]; do
91 # receive a 4D file and and extract the corresponding phase numbers
92 # export the variables containing each of the extracted data
94 extract_4d_phase_numbers()
98 nb_phases=${#phase_files[@]}
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
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]\+"` )
108 for i in $patterns; do
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]\+"` )
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]\+"` )
129 echo "Phase numbers are ${phase_nbs[@]}"
134 # receive a 4D file and extract the corresponding phase files,
136 # export the variables containing each of the extracted data
142 echo "4D file is $mhd4d"
144 # array of phase files
145 phase_files=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:'` )
146 echo "Phase files are ${phase_files[@]}"
148 extract_4d_phase_numbers $mhd4d
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.
157 # export the variables containing each of the extracted data
159 extract_4d_phases_ref()
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"
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"