]> Creatis software - clitk.git/commitdiff
script to compress MHDs
authorRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Thu, 24 May 2012 14:24:50 +0000 (16:24 +0200)
committerRomulo Pinho <romulo.pinho@lyon.unicancer.fr>
Thu, 24 May 2012 14:24:50 +0000 (16:24 +0200)
- either one file or an entire directory
- directories processed recursively

scripts/compress_mhd.sh [new file with mode: 0755]

diff --git a/scripts/compress_mhd.sh b/scripts/compress_mhd.sh
new file mode 100755 (executable)
index 0000000..f2e8912
--- /dev/null
@@ -0,0 +1,69 @@
+#! /bin/bash 
+
+compress_file()
+{
+  local f=$1
+  if [ "$verbose" = "1" ]; then
+    echo "Compressing $f..."
+  fi
+
+  local msg=`clitkImageConvert -i $f -o $f -c`
+  if [ -z "$msg" ]; then
+    raw=`echo $f | sed 's/\.mhd/\.raw/'`
+    if test -e $raw; then
+      rm $raw
+    fi
+  fi
+}
+
+compress_directory()
+{
+  local input=$1
+
+  files=$(find $input -name "*.mhd" | sort)
+  for f in $files; do
+    if ! grep -iq 'LIST' $f; then
+      # process all except 4D files
+      compress_file $f
+    else
+      # process only 4D files (just update pointers to new zraw files)
+      # assumes each .RAW in the list has a corresponding .MHD, which
+      # must be compressed separately
+      sed -i 's/\.raw/\.zraw/g' $f
+      if grep -q "CompressedData" $f; then
+        sed -i 's/CompressedData.*/CompressedData = True/' $f
+      else
+        tmp=/tmp/$RANDOM.mhd
+        echo "CompressedData = True" > $tmp
+        cat $f >> $tmp
+        mv $tmp $f
+      fi 
+    fi
+  done
+}
+
+# main program
+
+if [ $# -lt 1 -o $# -gt 2 ]; then
+  echo "Invalid params. `basename $0` [-h | --help] for help" 1>&2
+  exit -1
+fi
+
+if [ -n "`echo $@ | grep '\-h\|--help'`" ]; then
+  echo "Usage: `basename $0` {FILE | DIRECTORY | -h | --help | -v}"
+  exit 0
+fi
+
+if [ -n "`echo $@ | grep '\-v'`" ]; then
+  echo Verbose mode : ON
+  verbose=1
+fi  
+
+input=$1
+if test -d $input; then
+  compress_directory $input
+elif test -f $input; then
+  compress_file $input
+else 
+  echo "Unknow input file type." 1>&2
+fi