WriteToDisk.lua Paths Based on Modality or AET

Hi Orthanc Group

I haven’t yet found an answer to so far from all these excellent posts but I’m looking to better understand how to use a condition with the WriteToDisk.lua script. I have a feeling the auto-routing script might have clues to how this could be done but I’ve only seen fragments here and there and all the links to that script I’ve seen so far are dead.

I’d like to export a study using a specific path based on the modality type or the AET. Not all modalities being used are populating the SeriesDescription so the export script fails to build paths for those devices.

For example:

  • If Siemens MR & CT: patient[‘PatientID’] … ‘/’ … series[‘Modality]’ … ‘/’ … patient[‘PatientName’] … ‘/’ … study[‘StudyDescription’] … ‘/’ … series[‘SeriesDescription’])
  • If Ziehm RF: patient[‘PatientID’] … ‘/’ … series[‘Modality]’ … ‘/’ … patient[‘PatientName’] … ‘/’ … study[‘StudyDescription’])

Any pointers to an existing script that might hold some clues is appreciated or if there’s a dedicated page somewhere full of script examples I’d love to see it.

Kind regards,
Simon

Could the declaration of path structure be done earlier in the script and the conditions done later under the ‘write to file’ section?

local path1 = ToAscii(TARGET … ‘/’ … patient[‘PatientID’] …

local path2 = ToAscii(TARGET … ‘/’ … patient[‘PatientName’] …

local target = assert(io.open(path1 … ‘/’ … instance … ‘.dcm’, ‘wb’))

local target = assert(io.open(path2 … ‘/’ … instance … ‘.dcm’, ‘wb’))

FYI, there are a few sample lua files here: https://hg.orthanc-server.com/orthanc/file/tip/OrthancServer/Resources/Samples/Lua

and here: https://bitbucket.org/osimis/orthanc-setup-samples/src/master/lua-samples/

Hope you can find what you’re looking for over there.

Thanks again Alain, from those sample scripts I was able to get what I needed.
Of course, anyone who knows what they’re doing will likely look on in horror at what I’ve created but it works for now.

OK, so I should’ve READ THE F.ING MANUAL on Lua as Lua needs to be OR NOT for an IF/OR/THEN/ELSE statement to work.

Correct Script will look like:

TARGET = ‘/opt/dcmconf/datastore/Imaging_Data/’

function ToAscii(s)

return s:gsub(‘[^a-zA-Z0-9-/-:. ]’, ‘_’)
end

function OnStableSeries(seriesId, tags, metadata, origin)
print('This series is now stable, writing its instances on the disk: ’ … seriesId)
PrintRecursive(origin)

local modality = string.lower(tags[‘Modality’])
local instances = ParseJson(RestApiGet(‘/series/’ … seriesId)) [‘Instances’]
local patient = ParseJson(RestApiGet(‘/series/’ … seriesId … ‘/patient’)) [‘MainDicomTags’]
local study = ParseJson(RestApiGet(‘/series/’ … seriesId … ‘/study’)) [‘MainDicomTags’]
local series = ParseJson(RestApiGet(‘/series/’ … seriesId)) [‘MainDicomTags’]

if string.find(modality, ‘US’) or not string.find(modality, ‘RF’) then
for i, instance1 in pairs(instances) do
local path1 = ToAscii(TARGET … ‘/’ …
patient[‘PatientID’] … ‘/’ … series[‘Modality’] … ‘/’ … patient[‘PatientName’] … ‘/’ … study[‘StudyDescription’])

local dicom = RestApiGet(‘/instances/’ … instance1 … ‘/file’)
os.execute(‘mkdir -p "’ … path1 … ‘"’)

local target = assert(io.open(path1 … ‘/’ … instance1 … ‘.dcm’, ‘wb’))
target:write(dicom)
target:close()
end
else
for i, instance2 in pairs(instances) do
local path2 = ToAscii(TARGET … ‘/’ …
patient[‘PatientID’] … ‘/’ … series[‘Modality’] … ‘/’ … patient[‘PatientName’] … ‘/’ … study[‘StudyDescription’] … ‘/’ … series[‘SeriesDescription’])

local dicom = RestApiGet (‘/instances/’ … instance2 … ‘/file’)
os.execute(‘mkdir -p "’ … path2 … ‘"’)
local tartget = assert(io.open(path2 … ‘/’ … instance2 … ‘.dcm’, ‘wb’))
target:write(dicom)
end
end
end