Skip to content

Instantly share code, notes, and snippets.

@mattsouth
Created November 9, 2017 16:22
Show Gist options
  • Save mattsouth/3819c27278e389a6ac2918f6fb2993f9 to your computer and use it in GitHub Desktop.
Save mattsouth/3819c27278e389a6ac2918f6fb2993f9 to your computer and use it in GitHub Desktop.
A bash script for creating a subject / experiment in xnat
#!/bin/bash
# creates subject / experiment ids if they dont already exist
# 1. url - xnat url
# 2. session - tomcat session_id (instead of username / password)
# 3. project - project id
# 4. subject - subject_id
# 5. experiment - experiment id
# 6. date - experiment_date
# 7. verbosity - 0 = surpress, 1 = log (default), 2 = debug
verbose_default=1
verbose=${7:-$verbose_default}
function log {
if [[ $verbose -gt 0 ]]; then
echo "createexperiment: $1"
fi
}
function debug {
if [[ $verbose -eq 2 ]]; then
echo "createexperiment (debug): $1"
fi
}
url="$1/data/archive/projects"
# check for correct number of arguments
if [[ "$#" -lt 6 && "$#" -gt 7 ]]; then
echo -e "Incorrect number of arguments - expected 6/7 - found $#"
echo "Usage: createexperiment.sh xnat_url tomcat_session_id project_id subject_id session_id session_date (verbosity)"
else
debug "project_id: $3, subject_id: $4, experiment_id: $5, experiment_date: $6"
project_exists=$(./XnatDataClient -c -s $2 -r $url/$3 | tail -n 1)
if [[ $project_exists == HTTP\ status\:\ 200 ]]; then
debug "project found: $3"
subject_exists=$(./XnatDataClient -c -s $2 -r $url/$3/subjects/$4 | tail -n 1)
if [[ $subject_exists == HTTP\ status\:\ 200 ]]; then
debug "subject found: $4"
else
log "creating subject: $4"
subject_created=$(./XnatDataClient -m PUT -s $2 -r $url/$3/subjects/$4)
debug "$subject_created"
fi
experiment_exists=$(./XnatDataClient -c -s $2 -r $url/$3/subjects/$4/experiments/$5 | tail -n 1)
if [[ $experiment_exists == HTTP\ status\:\ 200 ]]; then
debug "experiment found: $5"
else
log "creating MR session experiment: $5 $6"
experiment_created=$(./XnatDataClient -m PUT -s $2 -r $url/$3/subjects/$4/experiments/$5?xsiType=xnat\:mrSessionData\&xnat\:mrSessionData/date=$6)
debug "$experiment_created"
fi
elif [[ $project_exists == HTTP\ status\:\ 404 ]]; then
log "Failed: unknown project"
else
log "Failed: reason unknown ($project_exists)"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment