Arbitrary tags in write to file?

Hi,

I'm going round in circles trying to get arbitrary dicom tags when writing data to disk. I'm using the writetodisk.lua example which I've modified:

TARGET = 'C:\\Users\\ben\\Downloads\\orthancAndPluginsWin64.stable\\Data'

function ToAscii(s)
   -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub
   return s
end

function OnStoredInstance(instanceId, tags, metadata)
   print('Sending to ADQ: ' .. instanceId)
   SendToModality(instanceId, 'ADQ')
end

function OnStableSeries(seriesId, tag, metadata)
   print('This series is now stable, writing its instances on the disk: ' .. seriesId)

   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']
   
   for i, instance in pairs(instances) do
      local path = ToAscii(TARGET .. '\\' ..
                              patient['PatientID'] .. ' _ ' .. patient['PatientName'] .. '\\' ..
                              study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '\\' ..
                              series['SeriesDescription'])

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

      -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
      -- http://stackoverflow.com/a/16029744/881731
      os.execute('mkdir "' .. path .. '"')
    
      -- Write to the file
      local target = assert(io.open(path .. '\\' .. tag.Modality .. '_' .. patient['PatientID'] .. '-' .. instance .. '.dcm', 'wb'))
      target:write(dicom)
      target:close()
   end
end

How would I add for example the RTPlanLabel DICOM tag value into the file path? dicom.RTPlanLabel or tag.RTPlanLabel are nil values (I'm sending a test RTPlan which definitely has this element filled with a value).

Any advice?

Thanks,

Ben

I think I've got it now I have a better understanding of REST, I modified the section to:

for i, instance in pairs(instances) do
      local path = ToAscii(TARGET .. '\\' ..
                              patient['PatientID'] .. ' _ ' .. patient['PatientName'] .. '\\' ..
                              study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '\\' ..
                              series['SeriesDescription'])

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

      -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
      -- http://stackoverflow.com/a/16029744/881731
      os.execute('mkdir "' .. path .. '"')
      
      --Label = elements['RTPlanLabel']
      --print(Label)
      
      -- Write to the file
      local target = assert(io.open(path .. '\\' .. tag.Modality .. '_' .. patient['PatientID'] .. '-' .. elements['RTPlanLabel'] .. '-' .. instance .. '.dcm', 'wb+'))
      target:write(dicom)
      target:close()
   end

Hello,

I think I’ve got it now I have a better understanding of REST, I modified the section to:

[…]

– Retrieve the DICOM elements

local elements = ParseJson(RestApiGet(‘/instances/’ …instance … ‘/simplified-tags’))

[…]

Label = elements[‘RTPlanLabel’]
print(Label)

Yes, you’re perfectly right: Lua scripts have full access to the REST API, which allows you to extract the DICOM tags of the individual instances contained in the series.

Sébastien-