Mercurial > hg > orthanc
annotate Resources/Samples/Lua/WriteToDisk.lua @ 2130:72cb107a7346
option ORTHANC_DISABLE_PATCH
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 10:06:16 +0100 |
parents | 905842836ad4 |
children | 4555a8ef2e88 |
rev | line source |
---|---|
1465
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 TARGET = '/tmp/lua' |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 function ToAscii(s) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 return s:gsub('[^a-zA-Z0-9-/ ]', '_') |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 end |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 function OnStableSeries(seriesId, tags, metadata) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 print('This series is now stable, writing its instances on the disk: ' .. seriesId) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances'] |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags'] |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags'] |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags'] |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 for i, instance in pairs(instances) do |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 local path = ToAscii(TARGET .. '/' .. |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 patient['PatientID'] .. ' - ' .. patient['PatientName'] .. '/' .. |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '/' .. |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 series['SeriesDescription']) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 -- Retrieve the DICOM file from Orthanc |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 local dicom = RestApiGet('/instances/' .. instance .. '/file') |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 -- http://stackoverflow.com/a/16029744/881731 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 os.execute('mkdir -p "' .. path .. '"') |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 -- Write to the file |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 local target = assert(io.open(path .. '/' .. instance .. '.dcm', 'wb')) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 target:write(dicom) |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 target:close() |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 end |
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 end |