WriteToDisk.lua script

238 views
Skip to first unread message

Andrew

unread,
May 27, 2021, 4:01:16 AM5/27/21
to Orthanc Users
Dear Orthanc community,
I'm trying to customize the script for my needs. I need to zip the folder after writing to disk and delete the folder.

TARGET = '/tmp/lua'

function ToAscii(s)
   return s:gsub('[^a-zA-Z0-9-/-:]', '_')
end

function OnStableSeries(seriesId, tags, metadata)
   local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances']
   local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags']
   local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['ID']
   local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags']

   print('This series is now stable, writing its instances on the disk: ' .. ToAscii(patient['PatientName'] .. '-' .. seriesId))

   for i, instance in pairs(instances) do
      local path = ToAscii(TARGET .. '/' .. patient['PatientName'] .. '/IMAGES')

      -- Retrieve the DICOM file from Orthanc
      local dicom = RestApiGet('/instances/' .. instance .. '/file')

      -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
      os.execute('mkdir -p "' .. path .. '"')
      ptname = ToAscii(patient['PatientName'])

      -- Write to the file
      local target = assert(io.open(path .. '/' .. instance .. '.dcm', 'wb'))
      target:write(dicom)
      target:close()
      RestApiDelete('/instances/' .. instance)
   end
   os.execute('/tmp/lua/zip.sh ' .. ToAscii(patient['PatientName']))
end
os.execute('rm -rf' .. ToAscii(TARGET .. '/' .. ToAscii(patient['PatientName'])))

How can i use variable patient after last "end"? It returns a nil value

Andrew

unread,
May 27, 2021, 4:09:11 AM5/27/21
to Orthanc Users
I also tried  ('rm -rf' .. TARGET .. '/' .. ptname)) 
ptname also returns a nil value

четверг, 27 мая 2021 г. в 11:01:16 UTC+3, Andrew:

Guilherme Tavares

unread,
May 27, 2021, 6:23:31 AM5/27/21
to Andrew, Orthanc Users
Also had that doubt, gave up after a while

Att.

Guilherme A. C. Tavares
037 98833-6086


--
You received this message because you are subscribed to the Google Groups "Orthanc Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orthanc-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orthanc-users/0419d125-ced5-4874-9ed0-d7b1da00355an%40googlegroups.com.

Sébastien Jodogne

unread,
May 27, 2021, 10:17:23 AM5/27/21
to Orthanc Users
As far as I'm concerned, your script runs correctly on my computer.

Pay attention to the fact that the call to "mkdir" only correctly runs on GNU/Linux (as indicated in the comments).

You might be trying to handle a DICOM file that has no "PatientName" tag. You should modify the sample script to deal with such a situation.

Sébastien-

Андрей Валентинович

unread,
May 27, 2021, 10:25:35 AM5/27/21
to Sébastien Jodogne, Orthanc Users
Thank you for reply. 
Patients name exists. If I move rm command inside the function - it works. But if there two or more instances - rm command runs two or more times. :(
If I move rm command at the end of script - Error “Can’t concatenate nil value”

Чт, 27 мая 2021 г. в 17:17, Sébastien Jodogne <s.jo...@gmail.com>:
--
You received this message because you are subscribed to a topic in the Google Groups "Orthanc Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orthanc-users/VNO3SDx_sOM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orthanc-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orthanc-users/dd8478cb-f388-4b13-8fe5-2eee124e67f1n%40googlegroups.com.

Sébastien Jodogne

unread,
May 27, 2021, 10:31:28 AM5/27/21
to Orthanc Users
You obviously have to put the last "os.execute()" inside the function, otherwise variable "patient" is not defined (because "patient" is a local variable of the function).

Андрей Валентинович

unread,
May 27, 2021, 10:40:36 AM5/27/21
to Sébastien Jodogne, Orthanc Users
I thought this should work:
Set global var ptname inside the function

ptname = ToAscii(patient['PatientName'])

And then os.execute outside the function

os.execute('rm -rf ' .. ToAscii(TARGET .. '/' .. ptname))

But it doesn’t work :-(
Again ptname returns nil value


Чт, 27 мая 2021 г. в 17:17, Sébastien Jodogne <s.jo...@gmail.com>:
As far as I'm concerned, your script runs correctly on my computer.
--
You received this message because you are subscribed to a topic in the Google Groups "Orthanc Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orthanc-users/VNO3SDx_sOM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orthanc-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orthanc-users/dd8478cb-f388-4b13-8fe5-2eee124e67f1n%40googlegroups.com.

Sébastien Jodogne

unread,
May 27, 2021, 10:43:11 AM5/27/21
to Orthanc Users
The "os.remove()" outside the function is only executed once, when Orthanc starts. If you want it to be executed for each received series (which is visibly what you expect), it must be located *within* the callback function "OnStableSeries".

Андрей Валентинович

unread,
May 27, 2021, 10:45:35 AM5/27/21
to Sébastien Jodogne, Orthanc Users
I actually need to run rm once after receiving all series.  

Чт, 27 мая 2021 г. в 17:43, Sébastien Jodogne <s.jo...@gmail.com>:
--
You received this message because you are subscribed to a topic in the Google Groups "Orthanc Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orthanc-users/VNO3SDx_sOM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orthanc-user...@googlegroups.com.

Sébastien Jodogne

unread,
May 28, 2021, 4:50:15 AM5/28/21
to Orthanc Users
Instead of discussing your code, you'll find attached a complete working solution.

Note that using "os.execute()" is inherently insecure, and should be avoided in any production setup.
fixed.lua
Reply all
Reply to author
Forward
0 new messages