xsa_script.py 1.92 KB
Newer Older
1
#!/usr/bin/env python
2
import xsa.queries as queries
Franziska Koehn's avatar
Franziska Koehn committed
3
4
5
6

def main(host, user, passw):
    import distutils.core

Franziska Koehn's avatar
Franziska Koehn committed
7
8
    #TODO Read query from json-file

Franziska Koehn's avatar
Franziska Koehn committed
9
10
    root_element = 'xnat:mrScanData'
    constraints = [
Franziska Koehn's avatar
Franziska Koehn committed
11
        ('xnat:mrScanData/TYPE', 'LIKE', '%t2%'),
Franziska Koehn's avatar
Franziska Koehn committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        ('xnat:mrScanData/PARAMETERS_FLIP', '>=', '10'),
        'AND',
        [('xnat:mrScanData/PARAMETERS_TE', '>', '2.0'),
         ('xnat:mrScanData/PARAMETERS_TE', '<', '2.0'),
         'OR'
         ]
        ]

    search_fields=[ 'xnat:mrScanData/TYPE',
                    'xnat:mrSessionData/PROJECT',
                    'xnat:mrSessionData/SUBJECT_ID',
                    'xnat:mrSessionData/SESSION_ID',
                    'xnat:mrScanData/ID'
                    ]
Franziska Koehn's avatar
Franziska Koehn committed
26
    rest="/data/archive/projects/{xnat_mrsessiondata_project}/subjects/{xnat_mrsessiondata_subject_id}/experiments/{xnat_mrsessiondata_session_id}/scans/{id}/resources/DICOM/files?format=zip"
Franziska Koehn's avatar
Franziska Koehn committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


    results = queries.search_for(host, root_element, constraints,search_fields, user, passw)

    print "Search results (%s):" % len(results)
    print results

    while True:
        try:
            is_downl = distutils.util.strtobool(raw_input("download? "))
            break
        except ValueError:
            print "invalid character. Please use y or n."

    if is_downl:
        print "downloading files..."
Franziska Koehn's avatar
Franziska Koehn committed
43
44
45
        for result in results:
            print result
            queries.download_async(result, host, (user, passw), rest, dest_folder='')
Franziska Koehn's avatar
Franziska Koehn committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        print "finished"
    else:
        print "download canceled"

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='search and download')
    parser.add_argument('--host', type=str, help='hostname or ip-address (including port).', default="localhost:8080")
    parser.add_argument('--user', type=str, help='user:passw', default=":")

    args = parser.parse_args()

    u,p = args.user.split(':')
    main(args.host, u, p)