annotate Resources/Samples/Lua/WriteToDisk.lua @ 3200:1f4a2c58e7fa

set encoding to UTF-8, IBinaryFormatter
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 06 Feb 2019 13:51:20 +0100
parents 4555a8ef2e88
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2576
4555a8ef2e88 escape colons in path
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1465
diff changeset
5 -- https://groups.google.com/d/msg/orthanc-users/qMLgkEmwwPI/6jRpCrlgBwAJ
4555a8ef2e88 escape colons in path
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1465
diff changeset
6 return s:gsub('[^a-zA-Z0-9-/-: ]', '_')
1465
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 end
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 function OnStableSeries(seriesId, tags, metadata)
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 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
11
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 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
13 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
14 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
15 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
16
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 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
18 local path = ToAscii(TARGET .. '/' ..
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 patient['PatientID'] .. ' - ' .. patient['PatientName'] .. '/' ..
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '/' ..
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 series['SeriesDescription'])
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 -- 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
24 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
25
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 -- 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
27 -- 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
28 os.execute('mkdir -p "' .. path .. '"')
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 -- Write to the file
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 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
32 target:write(dicom)
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 target:close()
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 end
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 end