]> Creatis software - clitk.git/blob - scripts/midp_common.sh
73d17873c7bc30135074e130c1767b027d22e4bc
[clitk.git] / scripts / midp_common.sh
1 #! /bin/sh +x
2
3 ###############################################################################
4 #
5 # FILE: 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 # block execution untill the number of threads (jobs) launched by the
14 # current process is below the given number of threads. 
15 MAX_THREADS=2
16 check_threads()
17 {
18   nbth=$1
19   while [[ $(jobs -p | wc -l) -ge $nbth ]]; do
20       jobs
21       sleep 10
22   done
23 }
24
25 #
26 # receive a 4D file and and extract the corresponding phase numbers
27 # export the variables containing each of the extracted data
28 #
29 extract_4d_phase_numbers()
30 {
31   mhd4d=$1
32
33   nb_phases=${#phase_files[@]}
34
35   # get everything except numbers and punctuation
36   cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[^0-9[:punct:]]*" | sort -u > /tmp/patterns.txt
37
38   # find which patterns have the phases connected to it
39   patterns=`cat /tmp/patterns.txt`
40   if [ -z "$patterns" ]; then
41     phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep "[0-9]\+"` )
42   else
43     for i in $patterns; do 
44
45       # check if the pattern appears before the phase number
46       nb_phases_found=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | sort -u | wc -l`
47       if [ $nb_phases_found == $nb_phases ]; then
48         # keep only what identifies the phase number
49         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]\+"` ) 
50         break
51       fi
52     
53       # check if the pattern appears after the phase number
54       nb_phases_found=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | sort -u | wc -l`
55       if [ $nb_phases_found == $nb_phases ]; then
56         # keep only what identifies the phase number
57         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]\+"` ) 
58         break
59       fi
60
61     done
62   fi
63
64   echo "Phase numbers are ${phase_nbs[@]}"
65   rm /tmp/patterns.txt
66 }
67
68 #
69 # receive a 4D file and extract the corresponding phase files, 
70 # and phase numbers.
71 # export the variables containing each of the extracted data
72 #
73 extract_4d_phases()
74 {
75   mhd4d=$1
76
77   echo "4D file is $mhd4d"
78
79   # array of phase files
80   phase_files=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:'` )
81   echo "Phase files are ${phase_files[@]}"
82
83   extract_4d_phase_numbers $mhd4d 
84 }
85
86
87 #
88 # receive a 4D file and the reference phase number as input 
89 # and extract the corresponding phase files, phase numbers, 
90 # and reference phase file. 
91 #
92 # export the variables containing each of the extracted data
93 #
94 extract_4d_phases_ref()
95 {
96   extract_4d_phases $1
97
98   # reference phase file
99   ref_phase_file=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | grep $2`
100   echo "Reference phase is $ref_phase_file"
101
102   # reference phase number
103   for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
104     ref_phase_nb=`echo ${phase_nbs[$i]} | grep $2`
105     if [ -n "$ref_phase_nb" ]; then
106       echo "Reference phase number is $ref_phase_nb"
107       break
108     fi
109   done
110 }