]> Creatis software - clitk.git/blobdiff - cluster_tools/gate_power_merge.sh
bugfix
[clitk.git] / cluster_tools / gate_power_merge.sh
index c1c475576f35cea17ad1a682de68f55ac863510d..caec94a252cd829af0fc575a04c4bcc5fe9041cc 100755 (executable)
@@ -7,8 +7,10 @@ echo "ERROR: $1"
 exit 1
 }
 
+warning_count=0
 function warning {
-echo "WARNING: $1"
+let "warning_count++"
+echo "MERGE_WARNING: $1"
 }
 
 function start_bar {
@@ -19,12 +21,13 @@ function update_bar {
 local count="${1:?"provide count"}"
 local message="${2:?"provide message"}"
 local percent=$(echo "100*${count}/${count_max}" | bc)
-printf "[%03d/%03d] %3d%% %-80.80s\r" ${count} ${count_max} ${percent} "${message}"
+#printf "[%03d/%03d] %3d%% %-80.80s\r" ${count} ${count_max} ${percent} "${message}"
+printf "[%03d/%03d] %3d%% %-80.80s\n" ${count} ${count_max} ${percent} "${message}"
 }
 
 function end_bar {
 unset count_max
-echo -ne '\n'
+#echo -ne '\n'
 }
 
 rootMerger="clitkMergeRootFiles"
@@ -36,6 +39,16 @@ shift
 echo "  ${indent}entering root merger"
 echo "  ${indent}merger is ${rootMerger}"
 echo "  ${indent}creating ${merged}"
+#echo "######## $#"
+#echo "######## $*"
+
+if test $# -eq 1
+then
+    echo "  ${indent}just one partial file => just copy it"
+    cp "$1" "${merged}"
+    return
+fi
+
 local count=0
 local arguments=" -o ${merged}"
 while test $# -gt 0
@@ -150,86 +163,109 @@ end_bar
 echo "  ${indent}merged ${count} files"
 }
 
-rundir="${1?"provide run dir"}"
-nboutputdirs="$(find "${rundir}" -mindepth 1 -type d -name 'output*' | wc -l)"
-
-test ${nboutputdirs} -gt 0 || error "no output dir found"
-echo "found ${nboutputdirs} partial output dirs"
-
-outputdir="results"
-if [ "${rundir}" != "." -a "${rundir##*.}" != "${rundir}" ]
-then
-    outputdir="results.${rundir##*.}"
-fi
-outputdir="$(basename "${outputdir}")"
-echo "output dir is ${outputdir}"
-test -d "${outputdir}" && rm -r "${outputdir}"
-mkdir "${outputdir}"
-
-for outputfile in $(find "${rundir}" -regextype 'posix-extended' -type f -regex '.*\.(hdr|root|txt)' | awk -F '/' '{ print $NF }' | sort | uniq)
-do
-    indent="  ** "
+function merge_dispatcher {
+    local indent="  ** "
+    local outputfile="${1:?"provide output filename"}"
     echo "merging ${outputfile}"
 
-    partialoutputfiles="$(find "${rundir}" -type f -name "${outputfile}")"
-    nboutputfiles="$(echo "${partialoutputfiles}" | wc -l)"
+    local partialoutputfiles="$(find "${rundir}" -type f -name "${outputfile}")"
+    local nboutputfiles="$(echo "${partialoutputfiles}" | wc -l)"
     if test ${nboutputdirs} -ne ${nboutputfiles}
     then
         warning "missing files"
-        continue
+        return
     fi
 
-    firstpartialoutputfile="$(echo "${partialoutputfiles}" | head -n 1)"
-    firstpartialoutputextension="${firstpartialoutputfile##*.}"
+    local firstpartialoutputfile="$(echo "${partialoutputfiles}" | head -n 1)"
+    local firstpartialoutputextension="${firstpartialoutputfile##*.}"
     echo "${indent}testing file type on ${firstpartialoutputfile}"
 
     if test "${firstpartialoutputextension}" == "hdr"
     then
         echo "${indent}this is a analyse image"
-        mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+        local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
         merge_hdr_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
-        continue
+        return
     fi
 
     if test "${firstpartialoutputextension}" == "root"
     then
         echo "${indent}this is a root file"
-        mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+        local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
         merge_root "${mergedfile}" ${partialoutputfiles} || error "error while merging"
-        continue
+        return
     fi
 
     if test "${firstpartialoutputextension}" == "txt" && grep 'NumberOfEvent' "${firstpartialoutputfile}" > /dev/null
     then
         echo "${indent}this is a stat file"
-        mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+        local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
         merge_stat "${mergedfile}" ${partialoutputfiles} || error "error while merging"
-        continue
+        return
     fi
 
     if test "${firstpartialoutputextension}" == "txt" && grep 'Resol' "${firstpartialoutputfile}" > /dev/null
     then
-        resol="$(sed -nr '/Resol/s/^.*=\s+\((.+)\)\s*$/\1/p' "${firstpartialoutputfile}")"
-        resolx="$(echo "${resol}" | cut -d',' -f1)"
-        resoly="$(echo "${resol}" | cut -d',' -f2)"
-        resolz="$(echo "${resol}" | cut -d',' -f3)"
+        local resol="$(sed -nr '/Resol/s/^.*=\s+\((.+)\)\s*$/\1/p' "${firstpartialoutputfile}")"
+        local resolx="$(echo "${resol}" | cut -d',' -f1)"
+        local resoly="$(echo "${resol}" | cut -d',' -f2)"
+        local resolz="$(echo "${resol}" | cut -d',' -f3)"
         if test "${resol}" == "1,1,1"
         then
             echo "${indent}this is a txt integral value"
-            mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+            local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
             merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
-            continue
+            return
         fi
         if test \( "${resolx}" == "1" -a "${resoly}" == "1" \) -o \( "${resoly}" == "1" -a "${resolz}" == "1" \) -o \( "${resolz}" == "1" -a "${resolx}" == "1" \)
         then
             echo "${indent}this is a txt profile"
-            mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+            local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
             merge_txt_image "${mergedfile}" ${partialoutputfiles} || error "error while merging"
-            continue
+            return
         fi
     fi
 
+    if test "${firstpartialoutputextension}" == "txt"
+    then
+        echo "${indent}this is a non specific txt output"
+        local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")"
+        echo "  ${indent}catting ${mergedfile}"
+        cat ${partialoutputfiles} > "${mergedfile}" || error "error while merging"
+        return
+    fi
 
     warning "unknown file type"
+}
+
+echo "!!!! this is $0 v0.3g !!!!"
+
+rundir="${1?"provide run dir"}"
+rundir="$(echo "${rundir}" | sed 's|/*$||')"
+nboutputdirs="$(find "${rundir}" -mindepth 1 -type d -name 'output*' | wc -l)"
+
+test ${nboutputdirs} -gt 0 || error "no output dir found"
+echo "found ${nboutputdirs} partial output dirs"
+
+outputdir="results"
+if [ "${rundir}" != "." -a "${rundir##*.}" != "${rundir}" ]
+then
+    outputdir="results.${rundir##*.}"
+fi
+outputdir="$(basename "${outputdir}")"
+echo "output dir is ${outputdir}"
+test -d "${outputdir}" && rm -r "${outputdir}"
+mkdir "${outputdir}"
+
+for outputfile in $(find "${rundir}" -regextype 'posix-extended' -type f -regex "${rundir}/output.*\.(hdr|root|txt)" | awk -F '/' '{ print $NF; }' | sort | uniq)
+do
+    merge_dispatcher "${outputfile}"
 done
 
+if [ -f "${rundir}/params.txt" ]
+then
+       echo "copying params file"
+       cp "${rundir}/params.txt" "${outputdir}/params.txt"
+fi
+
+echo "these was ${warning_count} warning(s)"