]> Creatis software - clitk.git/blobdiff - scripts/midp_common.sh
small bug when opening .mat files in VV
[clitk.git] / scripts / midp_common.sh
index 73d17873c7bc30135074e130c1767b027d22e4bc..16535a11850714b461fb71fac42a402c07bc8644 100755 (executable)
@@ -1,15 +1,80 @@
-#! /bin/sh +x
+#! /bin/sh -x
 
 ###############################################################################
 #
-# FILE: common.sh
+# FILE: midp_common.sh
 # AUTHOR: RĂ´mulo Pinho 05/08/2011
 #
 # Helper file with many functions used in the midP scripts.
 #
 ###############################################################################
 
+#
+# check return value passed and abort if it represents an error (ie, ret != 0)
+# optionally, a function can be passed as a 3rd parameter, to be called just
+# before exiting. this is useful for cleaning up, for example.
+#
+abort_on_error()
+{
+  if [ $2 != 0 ]; then
+    echo Aborted at $1 with code $2
+    if [ $# = 3 ]; then
+      eval $3
+    fi
+
+    exit $2
+  fi
+}
+
+#
+# wait for all processes in the list and return their exit codes
+# in the ret_codes variable.
+#
+# OBS: this function must always be called in the same shell
+# that launched the processes.
+#
+wait_pids()
+{
+  local pids=$*
+  local ret=
+  local rets=
+#   echo PIDS: $pids
+  for p in $pids; do
+#     echo waiting $p
+    wait $p > /dev/null 2> /dev/null
+    ret=$?
+    if [ ret != 127 ]; then
+      rets=$rets" "$ret
+    else
+      rets=$rets" "0
+    fi
+      
+  done
 
+  ret_codes=$rets
+}
+
+#
+# clean-up functions for maks, registration, and midp
+#
+clean_up_masks()
+{
+  rm -fr $mask_dir_tmp
+}
+
+clean_up_midp()
+{
+  rm -fr $midp_dir
+}
+
+clean_up_registration()
+{
+  rm -fr $vf_dir
+  rm -fr $output_dir
+}
+
+
+#
 # block execution untill the number of threads (jobs) launched by the
 # current process is below the given number of threads. 
 MAX_THREADS=2
@@ -108,3 +173,37 @@ extract_4d_phases_ref()
     fi
   done
 }
+
+#
+# replacement for clitkCombineImage
+combine_image()
+{
+#  eg: -i $result_in -j $result_out -o $out_result -m $motion_mask
+
+  clitkSetBackground -i $1 -o temp1.mhd -m $4
+  clitkSetBackground -i $2 -o temp2.mhd -m $4 --fg
+
+  clitkImageArithm -i temp1.mhd -j temp2.mhd -o $3
+  rm temp?.*
+}
+
+# 
+# replacement for clitkAverageTemporalDimension
+average_temporal_dimension()
+{
+  # eg: -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd
+
+  local tot=tot.mhd
+
+  local dir=`dirname $1` 
+  local first=`grep raw $1 | sed 's/raw/mhd/g' | head -n 1`
+  clitkImageArithm -i $dir/$first -o $tot -t 1 -s 0
+
+  local nbphases=`grep raw $1 | sed 's/raw/mhd/g' | wc -l`
+  for i in $(grep raw $1 | sed 's/raw/mhd/g'); do
+    clitkImageArithm -i $dir/$i -j $tot -o $tot
+  done
+
+  clitkImageArithm -i $tot -o $2 -t 11 -s $nbphases
+  rm tot.*
+}