Skip to content

Instantly share code, notes, and snippets.

@mattsouth
Last active March 28, 2017 15:21
Show Gist options
  • Save mattsouth/962fe145896bb60fc95e58a6ae6ab86d to your computer and use it in GitHub Desktop.
Save mattsouth/962fe145896bb60fc95e58a6ae6ab86d to your computer and use it in GitHub Desktop.
#!/bin/bash
# upload pre-processed niftii images to xnat 1.6.5
# 1. xnat - xnat url, e.g. https://central.xnat.org
# 2. username - xnat username
# 3. password - xnat password
# 4. project - project id
# 5. subject - subject id
# 6. session - session id
# 7. scan - scan id
# 8. nifti - filepath of nifti image
verbose=1
function log {
if [[ $verbose == 1 ]]; then
echo "uploadnifti: $1"
fi
}
# check for correct number of arguments
if [ "$#" -ne 8 ]; then
echo -e "Incorrect number of arguments - expected 8"
echo "Usage: uploadnifti.sh xnat-url xnat-username xnat-password project_id subject_id session_id scan_id filepath"
else
url="$1/data/archive/projects"
log "project_id: $4, subject_id: $5, session_id: $6, scan_id: $7, filepath: $8"
resources_exist=$(curl -silent -I -u $2:$3 $url/$4/subjects/$5/experiments/$6/scans/$7/resources)
if [[ $resources_exist == HTTP/1.1\ 200* ]]; then
resources=$(curl -sb -H -u $2:$3 $url/$4/subjects/$5/experiments/$6/scans/$7/resources?format=csv)
# create NIFTI resource type if not already present
if [[ $(echo "$resources" | grep "NIFTI" | wc -l) == 0 ]]; then
curl -X PUT -u $2:$3 $url/$4/subjects/$5/experiments/$6/scans/$7/resources/NIFTI
log "NIFTI resource folder created for subject $5, session $6, scan $7"
fi
filename=$(basename $8)
nifti_exists=$(curl -silent -I -u $2:$3 $url/$4/subjects/$5/experiments/$6/scans/$7/resources/NIFTI/files/$filename)
if [[ $nifti_exists == HTTP/1.1\ 200* ]]; then
log "Nifti file already exists: IGNORED"
else
curl -u $2:$3 -X PUT --data-binary "@$8" "$url/$4/subjects/$5/experiments/$6/scans/$7/resources/NIFTI/files/$filename?format=NIFTI&inbody=true"
log "Nifti file uploaded: $filename"
fi
else
# something is amiss in the first seven parameters, lets debug...
project_exists=$(curl -silent -I -u $2:$3 $url/$4)
if [[ $project_exists == HTTP/1.1\ 200* ]]; then
subject_exists=$(curl -silent -I -u $2:$3 $url/$4/subjects/$5)
if [[ $subject_exists == HTTP/1.1\ 200* ]]; then
session_exists=$(curl -silent -I -u $2:$3 $url/$4/subjects/$5/experiments/$6)
if [[ $session_exists == HTTP/1.1\ 200* ]]; then
scan_exists=$(curl -silent -I -u $2:$3 $url/$4/subjects/$5/experiments/$6/scans/$7)
if [[ $scan_exists == HTTP/1.1\ 200* ]]; then
echo "Failed: Reason unknown! (project,subject,session and scan are all present)"
else
echo "Failed: unknown scan"
fi
else
echo "Failed: unknown session"
fi
else
echo "Failed: unknown subject"
fi
else
# todo: check that credentials work at all?
echo "Failed: unknown project $project_exists"
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment