Last active
August 29, 2015 14:26
-
-
Save mattsouth/c7410315ff0db6516b98 to your computer and use it in GitHub Desktop.
Uploads dicom images for a particular subject session to the tng xnat instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# uploads dicom images for a particular subject session to the tng xnat instance | |
# takes five arguments: | |
# 1: username - xnat username | |
# 2: password - xnat password | |
# 3: project - xnat project id | |
# 4: directory - the subdirectory that contains the session scan sud-directories | |
# 5: subject_id - the subject id associated with the session scan | |
# | |
# Assumptions! | |
# 1. There is a set of sub-directories under the specified directory each | |
# corresponding to a separate scan and that the dicom image files are all in | |
# those scan sub-directories. | |
# 2. The script is located in the same directory as the session directory | |
url="https://tng.psych.ox.ac.uk/xnat/data/archive/projects/$3" | |
# check for correct number of arguments | |
if [ "$#" -ne 5 ]; then | |
echo -e "Incorrect number of arguments - expected 5" | |
echo "Usage: uploaddcmtk.sh user password project directory subject" | |
else | |
# check directory is an existing directory | |
if [ -d $4 ]; then | |
total_size=$(du -sh $4 | awk '{print$1}') | |
cd $4 | |
# check for project | |
project_exists=$(curl -silent -I -u $1:$2 $url) | |
if [[ $project_exists == HTTP/1.1\ 200* ]]; then | |
subject_id=$5 | |
# check for subject and create if it doesnt exist | |
subject_exists=$(curl -silent -I -u $1:$2 $url/subjects/$subject_id) | |
if [[ $subject_exists == HTTP/1.1\ 200* ]]; then | |
echo "Using existing subject: $subject_id" | |
else | |
echo -n "Creating subject: $subject_id (" | |
curl -X PUT -u $1:$2 $url/subjects/$subject_id | |
echo " - created)" | |
fi | |
# check for existing session and bail if found | |
session_exists=$(curl -silent -I -u $1:$2 $url/subjects/$subject_id/experiments/$4) | |
if [[ $session_exists == HTTP/1.1\ 200* ]]; then | |
echo "Found existing session: $4 - no files will be uploaded" | |
else | |
# create temp directory for dicoms which will be minorly altered before sending | |
mkdir ../$4_temp | |
# show progress bar | |
total_dirs=$(find ./* -maxdepth 0 -type d | wc -l) | |
echo -ne 'Sending dcm files: [' | |
for i in $(seq 1 $total_dirs); do echo -n ' '; done; | |
echo -ne ']\r' | |
((total_dirs--)) | |
num_dirs=0 | |
start_time="$(date +%s)" | |
# iterate through scan sub-directories | |
for d in */ ; do | |
scan_size=$(du -sh $d | awk '{print$1}') | |
scan_start_time="$(date +%s)" | |
((num_dirs++)) | |
# copy local dcm files | |
cp $d* ../$4_temp/. | |
# add patient comments dcm header field to indicate project, subject and session | |
dcmodify -i "(0010,4000)=Project: $3; Subject: $subject_id; Session: $4" ../$4_temp/* | |
# push dicom files to xnat | |
storescu -aec XNAT tng.psych.ox.ac.uk 8104 ../$4_temp/* | |
# remove local dcm files | |
rm ../$4_temp/* | |
# update progress bar | |
scan_finish_time="$(date +%s)" | |
scan_total_time=$((scan_finish_time-scan_start_time)) | |
echo "$4/$d - Sent $scan_size in $scan_total_time seconds" | |
echo -n 'Sending dcm files: [' | |
for i in $(seq 1 $num_dirs); do echo -n '#'; done | |
for i in $(seq $num_dirs $total_dirs); do echo -n ' '; done | |
echo -ne ']\r' | |
done | |
# remove temp directory | |
rmdir ../$4_temp | |
# conclude progress | |
finish_time="$(date +%s)" | |
total_time=$((finish_time-start_time)) | |
echo "$4 - Sent $total_size in $total_time seconds - Finished $(date)" | |
fi | |
else | |
echo "project $3 not found - do the passed credentials have access to it?" | |
fi | |
cd .. | |
else | |
echo "unable to find directory $4" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment