]> Creatis software - clitk.git/blob - scripts/create_mhd_4D.sh
24d3fd1d5110d80c60e2d57676e1eca183dd21f5
[clitk.git] / scripts / create_mhd_4D.sh
1 #!/bin/sh +x
2
3
4 write_mhd_4D()
5 {
6   cat $orig | sed "s/NDims = .*/NDims = 4/
7                   s/TransformMatrix = .*/TransformMatrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1/
8                   /Offset/ s/.*/& 0/
9                   /CenterOfRotation/ s/.*/& 0/
10                   s/AnatomicalOrientation = .*/AnatomicalOrientation = ????/
11                   /ElementSpacing/ s/.*/& 1/
12                   /DimSize/ s/.*/& $nbph/
13                   s/ElementDataFile = .*/ElementDataFile = LIST/" > "$1/$file_name_4D"
14   
15   for ph in ${listph[@]}
16   do
17     ph=`basename $ph`
18     echo "$ph" >> "$1/$file_name_4D"
19   done
20   echo $1/$file_name_4D
21 }
22
23 #################################################
24 # create_mhd_4D         argument : repertoire   #
25 #################################################
26 if [ $# -lt 1 ]
27 then
28   echo "Usage: create_mhd_4D.sh DIRECTORY"
29   exit 1
30 fi
31
32 nb_phase_file=`find $1 -maxdepth 1 -iname "*[0-9]*.mhd" | wc -l`
33 if [ $nb_phase_file = 0 ]
34 then
35   echo "No phase found in $1"
36   exit 1
37 fi
38
39
40 list_prefix=""
41 list_phase_file=`find $1 -maxdepth 1 -iname "*[0-9]*.mhd"`
42 for phase_file in $list_phase_file
43 do
44   if grep -q "NDims = 4" $phase_file 2> /dev/null
45   then
46     #echo $phase_file is 4D
47     continue;
48   fi;
49
50   phase_file_name=`basename $phase_file .mhd`
51 #   if [[ ! -z `echo "$phase_file_name" | grep "__[0-9]"` ]]
52 #   then
53 #     prefix=`echo $phase_file_name | sed "s/__[0-9].*/__/"`
54 #   else
55 #     if [[ ! -z `echo "$phase_file_name" | grep "[0-9]-.*\]"` ]] 
56 #     then
57 #       if [[ ! -z `echo "$phase_file_name" | grep "[0-9][0-9]-.*\]"` ]]
58 #       then
59 #       prefix=`echo $phase_file_name | sed "s/[0-9][0-9]-.*//"`
60 #       else
61 #       prefix=`echo $phase_file_name | sed "s/[0-9]-.*//"`
62 #       fi
63 #     else
64 #       prefix="NONE"
65 #     fi
66 #   fi
67
68   # preffix: grep sequence of characters followed by sequence of numbers and remove sequence of numbers
69   # prefix=`echo $phase_file_name | grep -o "\(^[[:alpha:][:punct:]]*\)\([[:digit:]\.\_\-]\+\)" | sed 's/\(^[[:alpha:][:punct:]]*\)\([[:digit:]\.\_\-]\+\)/\1/'`
70   prefix=`echo $phase_file_name | grep -o "\(^[[:alpha:][:punct:]]*\)\([[:digit:]]\+\)\([\.\_\-]*\)"`
71   prefix=`echo $prefix | sed 's/^\-\+//' | sed 's/\(^[[:alpha:][:punct:]]*\)\([[:digit:]]\+\)\([\.\_\-]*\)/\1/'`
72   if [[ -z $prefix ]]
73   then 
74     prefix="NONE"
75   fi
76
77   # register new preffix if not yet done
78   if [[ -z `echo "$list_prefix" | grep -w -- "$prefix"` ]]
79   then
80     list_prefix="$list_prefix $prefix"
81   fi
82 done
83
84
85 for prefix in $list_prefix
86 do
87   if [ "$prefix" = "NONE" ]
88   then
89     prefix=""
90   fi
91
92   list_suffix=""
93   list_phase_file_prefix=`find $1 -maxdepth 1 -iname "${prefix}[0-9]*.mhd"`
94   for phase_file_prefix in $list_phase_file_prefix
95   do
96     if grep -q "NDims = 4" $phase_file_prefix  2> /dev/null
97     then
98       #echo $phase_file_prefix is 4D
99       continue;
100     fi;
101     phase_file_prefix_name=`basename $phase_file_prefix .mhd`
102 #     if [[ ! -z `echo "$phase_file_prefix_name" | grep "__[0-9]"` ]]
103 #     then
104 #       suffix="NONE"
105 #     else
106 #       if [[ ! -z `echo "$phase_file_prefix_name" | grep "[0-9]-.*\]"` ]]
107 #       then
108 #       suffix=`echo $phase_file_prefix_name | sed "s/.*[0-9]-//;s/_\.mhd//;s/\.mhd//"`
109 #       else
110 #       suffix="NONE"
111 #       fi
112 #     fi
113
114
115     # suffix: grep sequence of numbers followed by sequence of characters and remove sequence of numbers
116     # suffix=`echo $phase_file_prefix_name | grep -o "\([[:digit:]\.\_\-]\+\)\([[:alpha:][:punct:]]*$\)" | sed 's/\([[:digit:]\.\_\-]\+\)\([[:alpha:][:punct:]]*$\)/\2/'`
117     suffix=`echo $phase_file_prefix_name | grep -o "\([\.\_\-]*\)\([[:digit:]]\+\)\([[:alpha:][:punct:]]*$\)"`
118     if ! echo $suffix | grep -qo "^\-\+"; then
119       suffix=`echo $suffix | sed 's/\([\.\_\-]*\)\([[:digit:]]\+\)\([[:alpha:][:punct:]]*$\)/\3/'`
120     fi
121     suffix=`echo $suffix | sed 's/[\.\_\-]\+$//'`
122     if [[ -z $suffix ]]
123     then 
124       suffix="NONE"
125     fi
126
127     # register new suffix if not yet done
128     if [[ -z `echo "$list_suffix" | grep -w -- "$suffix"` ]]
129     then
130       list_suffix="$list_suffix $suffix"
131     fi
132   done
133
134   
135   for suffix in $list_suffix
136   do
137     if [ "$suffix" = "NONE" ]
138     then
139       suffix=""
140     fi
141     orig=`find $1 -maxdepth 1 -iname "${prefix}[0-9]*${suffix}*.mhd" | grep "${prefix}[[:digit:][:punct:]]\+${suffix}[\.\_\-]*.mhd" | sort | head -n 1`
142     listph=( `find $1 -maxdepth 1 -iname "${prefix}[0-9]*${suffix}*.*raw" | grep "${prefix}[[:digit:][:punct:]]\+${suffix}[\.\_\-]*.z*raw" | sort` )
143     nbph=${#listph[@]}
144
145     # only create 4D file if potential number of phases is > 1
146     if [ $nbph -gt 1 ]; then
147       file_name_4D="${prefix}4D${suffix}.mhd"
148       write_mhd_4D $1
149     fi
150
151   done
152
153 done
154