Data import using CURL/REST
Import existing DICOM/PDF/XXX data into XNat
Assumptions
- project exists in XNat
XNat 1.5.4 (virtual machine image with default user and password)
- DICOM data is sorted into sub-directories
- using curl and bash
Upload DICOM from a directory structure
The following code first defines some variables like the project and visit id (session) and afterwards uses 'curl' to send a single DICOM file to the pre-archive. From there in a second step it needs to be moved to the archive to be visible.
USER=admin
PASSORD=admin
PROJECT=ADCSFlor
subject=P0001
session=P0001_01
u=somedicom.dcm
erg=`curl -H 'Content-Type: application/dicom' \
-X POST -u $USER:$PASSWORD \
"http://localhost:8080/xnat/data/services/import \
?inbody=true&PROJECT_ID=$PROJECT&SUBJECT_ID=$subject&EXPT_LABEL=$session" \
--data-binary @$u | tr -d [:cntrl:]`
The last part of the command (tr) will remove any control characters from the returned information that we have to use for the next step. 'erg' is used by the following steps as a reference to the uploaded data. The '\' character at the end of each line needs to be the last character and indicates that the command continues.
Mark data as ready
Data in the pre-archive needs to be moved into the archive to be visible for the user. In a first step after all the DICOM files have been moved using the call above we call build/commit to mark the data in the pre-archive as 'READY'.
args="-X POST -u $USER:$PASSWORD"
url=http://localhost:8080/xnat${erg}?action=build
echo "CALLING URL: $url ($args)"
/usr/bin/curl $args $url
url=http://localhost:8080/xnat${erg}?action=commit
/usr/bin/curl $args $url
Move data from the pre-archive to the archive
Data which is marked 'ready' in the pre-archive can be moved to the archive with:
args="-X POST -u $USER:$PASSWORD"
url="http://localhost:8080/xnat/data/services/archive?overwrite=delete&src=${erg}&dest=/archive/projects/$PROJECT/subjects/$subject"
/usr/bin/curl $args $url
Add PDF data to the session
PDF (and csv) data can be uploaded and is available in the "Manage Files' tab of the session. This could be a report that was created outside of XNat. Binary files do not need to be copied first to the pre-archive.
args="-X PUT -u $USER:$PASSWORD"
url="http://localhost:8080/xnat/data/archive/projects/$PROJECT/subjects/$subject/experiments/$session/files/$u?inbody=true&format=PDF"
curl $args "$url" --data-binary @$u
Update for XNat 1.6.1
I have updated the script to work with a larger number of DICOM files. Before it was only possible to send at most 1,000 DICOM files before getting an error message. The error message was apparently caused by the large number of sessions created each time the authentication '-u x:y' was used in curl. Waiting for 10 minutes closed the sessions again and the script could be run again. The new version of the script creates now a single session that is used throughout the script.
import2XNat.sh for version 1.6.1 with single session sign on
Add demographic information
Right after a subject has been defined I can send demographic information to the system. Somehow this does not work anymore after the data has been moved from the pre-archive to the archive.
args="-X PUT -u $USER:$PASSWORD"
# or, if one uses cookies to store connection information
# args="--cookie JSESSIONID=$cookie -k -X PUT"
# define $PROJECT, $subject, and $gender (has to be "male" or "female")
url="http://localhost:8080/xnat/data/archive/projects/$PROJECT/subjects/$subject?xnat:subjectData/demographics\\[@xsi:type%3dxnat:demographicData\\]/gender=$gender"
curl $args "$url"