Skip to content

Instantly share code, notes, and snippets.

@njvack
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njvack/9674025 to your computer and use it in GitHub Desktop.
Save njvack/9674025 to your computer and use it in GitHub Desktop.
A wrapper to allow Freesurfer's mri_convert to work on a tarball of dicoms.
#!/bin/bash
in_file="$1"
out_file="$2"
out_base=$(dirname "${out_file}")
tmp_dir=$(mktemp -d -p "${out_base}")
if [ $? -ne 0 ]; then
echo "Can't make a temporary directory in ${out_base}" >&2
exit 1
fi
echo -n "Untarring ${in_file} to ${tmp_dir}"
tar -C "${tmp_dir}" -xf "${in_file}" --checkpoint=.100
if [ $? -ne 0 ]; then
echo
echo "Error untarring ${in_file}" >&2
rmdir "${tmp_dir}"
exit 2
fi
echo
echo "Converting..."
# This will find you the first file...
first_file=$(find "${tmp_dir}" -type f | sed 1q)
mri_convert "${first_file}" "${out_file}"
'rm' -r "${tmp_dir}"
@satra
Copy link

satra commented Mar 20, 2014

@njvack: would tarfile + pydicom + dcmstack let you do this with untarring?

@njvack
Copy link
Author

njvack commented Mar 21, 2014

I tried dcmstack; it was too slow for my purposes. mri_convert handles an 8400-slice EPI series in less than a minute; I gave up after waiting an hour for dcmstack to do its tricks.

It's a pity, because I really would like to annotate the niftis with the dicom headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment