Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/ArchiveStudiesInTimeRange.py @ 4437:d9473bd5ed43
upgrade to year 2021
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 06 Jan 2021 17:27:28 +0100 |
parents | 160ec8417874 |
children | 7053502fbf97 |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4090
diff
changeset
|
7 # Copyright (C) 2017-2021 Osimis S.A., 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 |
4089
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
65 |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
66 # Case 1: Baseline version |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 patient = RestToolbox.DoGet('%s/studies/%s/patient' % (URL, studyId))['MainDicomTags'] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 |
4089
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
69 # Case 2: Tweaked version that can be used if several patients |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
70 # share the same "Patient ID", but have different "Patient Name" |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
71 # (which is invalid according to the DICOM standard). |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
72 # https://groups.google.com/d/msg/orthanc-users/58AxIkxFbZs/N6Knub8MAgAJ |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
73 # patient = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId)) ['PatientMainDicomTags'] |
a2060a76ed6a
note in ArchiveStudiesInTimeRange.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
74 |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 # Check that the StudyDate tag lies within the given range |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 studyDate = study['StudyDate'][:8] |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 if studyDate >= START and studyDate <= END: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 # Create a filename |
2070
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
79 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
|
80 GetTag(patient, 'PatientID'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
81 GetTag(patient, 'PatientName'), |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
82 GetTag(study, 'StudyDescription')) |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 # 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
|
85 filename = filename.encode('ascii', errors = 'replace').translate(None, r"'\/:*?\"<>|!=").strip() |
1348
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 # Download the ZIP archive of the study |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 print('Downloading %s' % filename) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 zipContent = RestToolbox.DoGet('%s/studies/%s/archive' % (URL, studyId)) |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 # Write the ZIP archive at the proper location |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 with open(os.path.join(TARGET, filename), 'wb') as f: |
cff17da28916
ArchiveStudiesInTimeRange.py sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 f.write(zipContent) |