]> Creatis software - clitk.git/commitdiff
improvement of 4D mhd creation script
authorRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Thu, 24 May 2012 14:12:40 +0000 (16:12 +0200)
committerRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Thu, 24 May 2012 14:12:40 +0000 (16:12 +0200)
scripts/create_mhd_4D.sh

index 70e21bbd61be22e96b6ea4ef027380fed5e468f8..24d3fd1d5110d80c60e2d57676e1eca183dd21f5 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/sh +x
 
 
 write_mhd_4D()
@@ -12,11 +12,12 @@ write_mhd_4D()
                  /DimSize/ s/.*/& $nbph/
                  s/ElementDataFile = .*/ElementDataFile = LIST/" > "$1/$file_name_4D"
   
-  for ph in $listph
+  for ph in ${listph[@]}
   do
     ph=`basename $ph`
     echo "$ph" >> "$1/$file_name_4D"
   done
+  echo $1/$file_name_4D
 }
 
 #################################################
@@ -40,25 +41,41 @@ list_prefix=""
 list_phase_file=`find $1 -maxdepth 1 -iname "*[0-9]*.mhd"`
 for phase_file in $list_phase_file
 do
-  phase_file_name=`basename $phase_file`
-  if [[ ! -z `echo "$phase_file_name" | grep "__[0-9]"` ]]
+  if grep -q "NDims = 4" $phase_file 2> /dev/null
   then
-    prefix=`echo $phase_file_name | sed "s/__[0-9].*/__/"`
-  else
-    if [[ ! -z `echo "$phase_file_name" | grep "[0-9]-.*\]"` ]]
-    then
-      if [[ ! -z `echo "$phase_file_name" | grep "[0-9][0-9]-.*\]"` ]]
-      then
-       prefix=`echo $phase_file_name | sed "s/[0-9][0-9]-.*//"`
-      else
-       prefix=`echo $phase_file_name | sed "s/[0-9]-.*//"`
-      fi
-    else
-      prefix="NONE"
-    fi
+    #echo $phase_file is 4D
+    continue;
+  fi;
+
+  phase_file_name=`basename $phase_file .mhd`
+#   if [[ ! -z `echo "$phase_file_name" | grep "__[0-9]"` ]]
+#   then
+#     prefix=`echo $phase_file_name | sed "s/__[0-9].*/__/"`
+#   else
+#     if [[ ! -z `echo "$phase_file_name" | grep "[0-9]-.*\]"` ]] 
+#     then
+#       if [[ ! -z `echo "$phase_file_name" | grep "[0-9][0-9]-.*\]"` ]]
+#       then
+#      prefix=`echo $phase_file_name | sed "s/[0-9][0-9]-.*//"`
+#       else
+#      prefix=`echo $phase_file_name | sed "s/[0-9]-.*//"`
+#       fi
+#     else
+#       prefix="NONE"
+#     fi
+#   fi
+
+  # preffix: grep sequence of characters followed by sequence of numbers and remove sequence of numbers
+  # prefix=`echo $phase_file_name | grep -o "\(^[[:alpha:][:punct:]]*\)\([[:digit:]\.\_\-]\+\)" | sed 's/\(^[[:alpha:][:punct:]]*\)\([[:digit:]\.\_\-]\+\)/\1/'`
+  prefix=`echo $phase_file_name | grep -o "\(^[[:alpha:][:punct:]]*\)\([[:digit:]]\+\)\([\.\_\-]*\)"`
+  prefix=`echo $prefix | sed 's/^\-\+//' | sed 's/\(^[[:alpha:][:punct:]]*\)\([[:digit:]]\+\)\([\.\_\-]*\)/\1/'`
+  if [[ -z $prefix ]]
+  then 
+    prefix="NONE"
   fi
 
-  if [[ -z `echo "$list_prefix" | grep "$prefix"` ]]
+  # register new preffix if not yet done
+  if [[ -z `echo "$list_prefix" | grep -w -- "$prefix"` ]]
   then
     list_prefix="$list_prefix $prefix"
   fi
@@ -76,38 +93,60 @@ do
   list_phase_file_prefix=`find $1 -maxdepth 1 -iname "${prefix}[0-9]*.mhd"`
   for phase_file_prefix in $list_phase_file_prefix
   do
-    phase_file_prefix_name=`basename $phase_file_prefix`
-    if [[ ! -z `echo "$phase_file_prefix_name" | grep "__[0-9]"` ]]
+    if grep -q "NDims = 4" $phase_file_prefix  2> /dev/null
     then
+      #echo $phase_file_prefix is 4D
+      continue;
+    fi;
+    phase_file_prefix_name=`basename $phase_file_prefix .mhd`
+#     if [[ ! -z `echo "$phase_file_prefix_name" | grep "__[0-9]"` ]]
+#     then
+#       suffix="NONE"
+#     else
+#       if [[ ! -z `echo "$phase_file_prefix_name" | grep "[0-9]-.*\]"` ]]
+#       then
+#      suffix=`echo $phase_file_prefix_name | sed "s/.*[0-9]-//;s/_\.mhd//;s/\.mhd//"`
+#       else
+#      suffix="NONE"
+#       fi
+#     fi
+# 
+
+    # suffix: grep sequence of numbers followed by sequence of characters and remove sequence of numbers
+    # suffix=`echo $phase_file_prefix_name | grep -o "\([[:digit:]\.\_\-]\+\)\([[:alpha:][:punct:]]*$\)" | sed 's/\([[:digit:]\.\_\-]\+\)\([[:alpha:][:punct:]]*$\)/\2/'`
+    suffix=`echo $phase_file_prefix_name | grep -o "\([\.\_\-]*\)\([[:digit:]]\+\)\([[:alpha:][:punct:]]*$\)"`
+    if ! echo $suffix | grep -qo "^\-\+"; then
+      suffix=`echo $suffix | sed 's/\([\.\_\-]*\)\([[:digit:]]\+\)\([[:alpha:][:punct:]]*$\)/\3/'`
+    fi
+    suffix=`echo $suffix | sed 's/[\.\_\-]\+$//'`
+    if [[ -z $suffix ]]
+    then 
       suffix="NONE"
-    else
-      if [[ ! -z `echo "$phase_file_prefix_name" | grep "[0-9]-.*\]"` ]]
-      then
-       suffix=`echo $phase_file_prefix_name | sed "s/.*[0-9]-//;s/_\.mhd//;s/\.mhd//"`
-      else
-       suffix="NONE"
-      fi
     fi
 
-    if [[ -z `echo "$list_suffix" | grep "$suffix"` ]]
+    # register new suffix if not yet done
+    if [[ -z `echo "$list_suffix" | grep -w -- "$suffix"` ]]
     then
       list_suffix="$list_suffix $suffix"
     fi
   done
 
+  
   for suffix in $list_suffix
   do
     if [ "$suffix" = "NONE" ]
     then
       suffix=""
     fi
-    nbph=`find $1 -maxdepth 1 -iname "*${prefix}*[0-9]*${suffix}*.mhd" | wc -l`
-    orig=`find $1 -maxdepth 1 -iname "*${prefix}*[0-9]*${suffix}*.mhd" | sort | head -n 1`
-    listph=`find $1 -maxdepth 1 -iname ".*${prefix}.*[0-9].*${suffix}.*.*raw" -iregex ".*.z*raw" | sort`
-
-    file_name_4D="${prefix}4D${suffix}.mhd"
-    
-    write_mhd_4D $1
+    orig=`find $1 -maxdepth 1 -iname "${prefix}[0-9]*${suffix}*.mhd" | grep "${prefix}[[:digit:][:punct:]]\+${suffix}[\.\_\-]*.mhd" | sort | head -n 1`
+    listph=( `find $1 -maxdepth 1 -iname "${prefix}[0-9]*${suffix}*.*raw" | grep "${prefix}[[:digit:][:punct:]]\+${suffix}[\.\_\-]*.z*raw" | sort` )
+    nbph=${#listph[@]}
+
+    # only create 4D file if potential number of phases is > 1
+    if [ $nbph -gt 1 ]; then
+      file_name_4D="${prefix}4D${suffix}.mhd"
+      write_mhd_4D $1
+    fi
 
   done