annotate Resources/Samples/Python/ArchiveStudiesInTimeRange.py @ 2043:35ccba7adae9

Toolbox::UriEncode
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 23 Jun 2016 10:08:27 +0200
parents 65b1ce7cb84f
children 7e6afa0beaf6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1348
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 # Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1348
diff changeset
5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1348
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 # Department, University Hospital of Liege, Belgium
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 #
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # modify it under the terms of the GNU General Public License as
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # published by the Free Software Foundation, either version 3 of the
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # License, or (at your option) any later version.
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 #
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful, but
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # General Public License for more details.
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 #
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 # You should have received a copy of the GNU General Public License
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import os
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import os.path
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import sys
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import RestToolbox
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 def PrintHelp():
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 print('Download ZIP archives for all the studies generated '
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 'during a given time range (according to the StudyDate tag)\n')
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 print('Usage: %s <URL> <StartDate> <EndDate> <TargetFolder>\n' % sys.argv[0])
2032
65b1ce7cb84f Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
32 print('Example: %s http://127.0.0.1:8042/ 20150101 20151231 /tmp/\n' % sys.argv[0])
1348
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 exit(-1)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 def CheckIsDate(date):
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 if len(date) != 8 or not date.isdigit():
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 print '"%s" is not a valid date!\n' % date
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 exit(-1)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 if len(sys.argv) != 5:
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 PrintHelp()
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 URL = sys.argv[1]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 START = sys.argv[2]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 END = sys.argv[3]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 TARGET = sys.argv[4]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 CheckIsDate(START)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 CheckIsDate(END)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 # Loop over the studies
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 for studyId in RestToolbox.DoGet('%s/studies' % URL):
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 # Retrieve the DICOM tags of the current study
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 study = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId))['MainDicomTags']
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 # Retrieve the DICOM tags of the parent patient of this study
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 patient = RestToolbox.DoGet('%s/studies/%s/patient' % (URL, studyId))['MainDicomTags']
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 # Check that the StudyDate tag lies within the given range
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 studyDate = study['StudyDate'][:8]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 if studyDate >= START and studyDate <= END:
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 # Create a filename
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 filename = '%s - %s %s - %s.zip' % (study['StudyDate'],
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 patient['PatientID'],
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 patient['PatientName'],
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 study['StudyDescription'])
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 # Remove any non-ASCII character in the filename
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 filename = filename.encode('ascii', errors = 'replace')
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 # Download the ZIP archive of the study
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 print('Downloading %s' % filename)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 zipContent = RestToolbox.DoGet('%s/studies/%s/archive' % (URL, studyId))
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 # Write the ZIP archive at the proper location
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 with open(os.path.join(TARGET, filename), 'wb') as f:
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 f.write(zipContent)