annotate Resources/Samples/Python/ArchiveStudiesInTimeRange.py @ 2408:26a0cc24d48d

New URI: "/instances/.../frames/.../raw.gz" to compress raw frames using gzip
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 25 Sep 2017 14:50:13 +0200
parents a3a65de1840f
children 878b59270859
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
2244
a3a65de1840f shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2070
diff changeset
7 # Copyright (C) 2017 Osimis, Belgium
1348
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 #
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # This program is free software: you can redistribute it and/or
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # 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
11 # published by the Free Software Foundation, either version 3 of the
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # License, or (at your option) any later version.
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 #
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # 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
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # General Public License for more details.
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 #
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # 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
20 # 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
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
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import os
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import os.path
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import sys
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 import RestToolbox
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 def PrintHelp():
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 print('Download ZIP archives for all the studies generated '
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 'during a given time range (according to the StudyDate tag)\n')
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 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
33 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
34 exit(-1)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 def CheckIsDate(date):
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 if len(date) != 8 or not date.isdigit():
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 print '"%s" is not a valid date!\n' % date
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 exit(-1)
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
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if len(sys.argv) != 5:
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 PrintHelp()
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 URL = sys.argv[1]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 START = sys.argv[2]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 END = sys.argv[3]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 TARGET = sys.argv[4]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 CheckIsDate(START)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 CheckIsDate(END)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
2070
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
53 def GetTag(tags, key):
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
54 if key in tags:
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
55 return tags[key]
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
56 else:
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
57 return 'No%s' % key
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
58
1348
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 # Loop over the studies
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 for studyId in RestToolbox.DoGet('%s/studies' % URL):
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 # Retrieve the DICOM tags of the current study
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 study = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId))['MainDicomTags']
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 # Retrieve the DICOM tags of the parent patient of this study
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 patient = RestToolbox.DoGet('%s/studies/%s/patient' % (URL, studyId))['MainDicomTags']
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 # Check that the StudyDate tag lies within the given range
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 studyDate = study['StudyDate'][:8]
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if studyDate >= START and studyDate <= END:
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 # Create a filename
2070
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
71 filename = '%s - %s %s - %s.zip' % (GetTag(study, 'StudyDate'),
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
72 GetTag(patient, 'PatientID'),
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
73 GetTag(patient, 'PatientName'),
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
74 GetTag(study, 'StudyDescription'))
1348
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 # Remove any non-ASCII character in the filename
2070
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
77 filename = filename.encode('ascii', errors = 'replace').translate(None, r"'\/:*?\"<>|!=").strip()
1348
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 # Download the ZIP archive of the study
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 print('Downloading %s' % filename)
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 zipContent = RestToolbox.DoGet('%s/studies/%s/archive' % (URL, studyId))
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 # Write the ZIP archive at the proper location
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 with open(os.path.join(TARGET, filename), 'wb') as f:
cff17da28916 ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 f.write(zipContent)