Skip to content

Commit

Permalink
Add script to sensor dicoms -- for the error where dcm2niix might or …
Browse files Browse the repository at this point in the history
…might not fail but issues an Error
  • Loading branch information
yarikoptic committed Dec 7, 2023
1 parent 5374088 commit c1ed1c0
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions utils/sensor-dicoms
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

set -eu

# Function to show usage
show_usage() {
echo "Usage: $0 [--dry-run|-n] --move-to DIRNAME directory [directory2 ...]"
}

# Parsing options
TEMP=$(getopt -o 'n' --long move-to:,dry-run -n "$(basename "$0")" -- "$@")
# shellcheck disable=SC2181
if [ $? != 0 ]; then echo "Terminating..." >&2; exit 1; fi

# Note the quotes around `$TEMP`: they are essential!
eval set -- "$TEMP"

# Initialize variables
MOVE_TO_DIR=""
DRY_RUN=""

# Extract options and their arguments into variables
while true; do
case "$1" in
--move-to)
MOVE_TO_DIR="$2"
shift 2
;;
-n|--dry-run)
DRY_RUN=1
shift
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done

# Check for mandatory option
if [ -z "$MOVE_TO_DIR" ]; then
echo "Error: --move-to option is required."
show_usage
exit 1
fi

# Create MOVE_TO_DIR if it does not exist
if [ ! -d "$MOVE_TO_DIR" ]; then
mkdir -p "$MOVE_TO_DIR"
fi

TEMP=$(mktemp -d "${TMPDIR:-/tmp}/dl-XXXXXXX")

# Process the remaining arguments (directories)
for dir in "$@"; do
echo ""
echo "Processing directory: $dir"
rm -rf "${TEMP:?}/*"
failed=
dcm2niix -z y -b y -o "$TEMP/" "$dir" 2>"$TEMP/stderr" >"$TEMP/stdout" || {
echo " Exited with $?; We will proceed with the analysis. Standard error output was:"
sed -e 's,^, ,g' "$TEMP/stderr"
}

if grep "Error: Check sorted order: 4D dataset has" "$TEMP/stderr"; then
failed=1
fi
if [ -n "$failed" ]; then
if [ -n "$DRY_RUN" ]; then
echo mv "$dir" "$MOVE_TO_DIR"
else
echo " Moving $dir to $MOVE_TO_DIR"
mv "$dir" "$MOVE_TO_DIR"
fi
fi
done

0 comments on commit c1ed1c0

Please sign in to comment.