Mercurial > hg > orthanc
annotate Resources/Samples/Python/ArchiveStudiesInTimeRange.py @ 2102:f0544ab9396b
fix line endings for msvc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Oct 2016 19:35:31 +0200 |
parents | 7e6afa0beaf6 |
children | a3a65de1840f |
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 | 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 |
2070
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
52 def GetTag(tags, key): |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
53 if key in tags: |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
54 return tags[key] |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
55 else: |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
56 return 'No%s' % key |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
57 |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 # Loop over the studies |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 for studyId in RestToolbox.DoGet('%s/studies' % URL): |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 # Retrieve the DICOM tags of the current study |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 study = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId))['MainDicomTags'] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 # Retrieve the DICOM tags of the parent patient of this study |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 patient = RestToolbox.DoGet('%s/studies/%s/patient' % (URL, studyId))['MainDicomTags'] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 # Check that the StudyDate tag lies within the given range |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 studyDate = study['StudyDate'][:8] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 if studyDate >= START and studyDate <= END: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 # Create a filename |
2070
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
70 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
|
71 GetTag(patient, 'PatientID'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
72 GetTag(patient, 'PatientName'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
73 GetTag(study, 'StudyDescription')) |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 # 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
|
76 filename = filename.encode('ascii', errors = 'replace').translate(None, r"'\/:*?\"<>|!=").strip() |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 # Download the ZIP archive of the study |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 print('Downloading %s' % filename) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 zipContent = RestToolbox.DoGet('%s/studies/%s/archive' % (URL, studyId)) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 # Write the ZIP archive at the proper location |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 with open(os.path.join(TARGET, filename), 'wb') as f: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 f.write(zipContent) |