Skip to content

Instantly share code, notes, and snippets.

@dmwelch
Last active August 29, 2015 13:59
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 dmwelch/10901731 to your computer and use it in GitHub Desktop.
Save dmwelch/10901731 to your computer and use it in GitHub Desktop.
Bash script to push zip file to XNAT archive
#!/bin/bash
############################################################################
# #
# Script: postXNAT.sh #
# #
# Task: Upload entire experiment in ZIP format via XNAT REST client #
# Overwrites previously uploaded experiment, if it exists #
# #
# Author: David Welch #
# Contact: david dot m period welch at gmail dot com #
# Date: 16 Apr, 2014 #
# Licence: Copyright (C) 2014 David Welch #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
############################################################################
PROJECT=$1
SUBJECT=$2
SESSION=$3
USER=$4
ZFILE=$5
echo -n "Password for ${USER}: "
read -s PASS
echo ""
XNAT_SITE=???
COOKIE=$(date +'%Y-%m-%d').cookie
if [[ ! -f ${COOKIE} ]]; then
echo "Creating cookie..."
$(curl -v -X PUT -u ${USER}:${PASS} -D ${COOKIE} "${XNAT_SITE}/xnat/data/JSESSION")
fi
LOGFILE=deleteme_$(date +'%Y-%m-%d_%H:%M').log
# To prevent duplicate scans clobering each other, delete the old experiment
$(curl -u ${USER}:${PASS} \
--cookie ${COOKIE} \
-X DELETE \
-D ${LOGFILE} \
"${XNAT_SITE}/xnat/data/archive/projects/${PROJECT}/subjects/${SUBJECT}/experiments/${SESSION}")
# Now post the entire zipped session to XNAT in it's place. Note: you will lose all auxillary files (scan reviews, etc.)
CODE=$(\
curl -u ${USER}:${PASS} \
--cookie ${COOKIE} \
-X POST \
--data-binary @${ZFILE} \
-H "Content-Type: application/zip" \
-D ${LOGFILE} \
"${XNAT_SITE}/xnat/data/services/import?project=${PROJECT}&subject=${SUBJECT}&session=${SESSION}&overwrite=delete&prearchive=false&inbody=true")
OUT=$?
echo ""
echo ${CODE}
echo ""
if [[ -f ${LOGFILE} ]]; then
# cat "===== UPLOAD =====" >> ${LOGFILE}
# cat .temp >> ${LOGFILE}
echo "Contents of log file:"
cat ${LOGFILE}
echo "========="
fi
exit ${OUT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment