annotate 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
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
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