comparison OrthancServer/Resources/Samples/Lua/ClassifyUsingAet.lua @ 4248:38b24aa98ed3

new lua sample: ClassifyUsingAet.lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 13 Oct 2020 15:36:48 +0200
parents
children 485f9be9ba92
comparison
equal deleted inserted replaced
4247:112951171852 4248:38b24aa98ed3
1 -- Write the received DICOM instances to an external directory, and
2 -- group the patients by the source AET that has sent the DICOM
3 -- files. This sample is similar to "WriteToDisk.lua".
4
5
6 TARGET = '/tmp/lua'
7
8
9 function ToAscii(s)
10 -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub
11 -- https://groups.google.com/d/msg/orthanc-users/qMLgkEmwwPI/6jRpCrlgBwAJ
12 return s:gsub('[^a-zA-Z0-9-/-: ]', '_')
13 end
14
15
16 function GetFromTable(t, key, defaultValue)
17 local result = t[key]
18 if result == nil or result == '' then
19 return defaultValue
20 else
21 return result
22 end
23 end
24
25
26 function OnStoredInstance(instanceId, tags, metadata, origin)
27 local dicom = RestApiGet('/instances/' .. instanceId .. '/file')
28 local tags = ParseJson(RestApiGet('/instances/' .. instanceId .. '/tags?simplify'))
29 local metadata = ParseJson(RestApiGet('/instances/' .. instanceId .. '/metadata?expand'))
30
31 local path = ToAscii(
32 TARGET .. '/' ..
33 GetFromTable(metadata, 'RemoteAET', 'None') .. '/'..
34 GetFromTable(tags, 'PatientID', '') .. ' - ' .. GetFromTable(tags, 'PatientName', '') .. '/' ..
35 GetFromTable(tags, 'StudyDate', '') .. ' - ' .. GetFromTable(tags, 'StudyDescription', '') .. '/' ..
36 GetFromTable(tags, 'SeriesDescription', ''))
37
38 -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
39 -- http://stackoverflow.com/a/16029744/881731
40 os.execute('mkdir -p "' .. path .. '"')
41
42 -- Write to the file
43 local target = assert(io.open(path .. '/' .. GetFromTable(tags, 'SOPInstanceUID', 'none') .. '.dcm', 'wb'))
44 target:write(dicom)
45 target:close()
46
47 -- Optional step: Remove the source file
48 RestApiDelete('/instances/' .. instanceId)
49 end