comparison Resources/Samples/Lua/WriteToDisk.lua @ 1465:905842836ad4

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