# Question about Lua Script to Edit / Add Tag OnStoredInstance, MWL Plug-in, misc.

**URL:** https://discourse.orthanc-server.org/t/question-about-lua-script-to-edit-add-tag-onstoredinstance-mwl-plug-in-misc/2422
**Category:** Google Groups archive
**Created:** [May 6, 2021, 11:25pm UTC](https://discourse.orthanc-server.org/t/question-about-lua-script-to-edit-add-tag-onstoredinstance-mwl-plug-in-misc/2422 "2021-05-06T23:25:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sdscotti](https://discourse.orthanc-server.org/user_avatar/discourse.orthanc-server.org/sdscotti/32/2563_2.png) [@sdscotti](https://discourse.orthanc-server.org/u/sdscotti)
#### Post date: [May 6, 2021, 11:25pm UTC](https://discourse.orthanc-server.org/t/question-about-lua-script-to-edit-add-tag-onstoredinstance-mwl-plug-in-misc/2422/1 "2021-05-06T23:25:14Z")

</div>

Question and Comment:

1. One of the acquisition devices we have does not read StudyDescription from the MWL and the operator normally has to enter that manually, although ‘tags[‘RequestAttributesSequence’][1][‘RequestedProcedureDescription’]’ is set via the MWL. I wrote a first attempt at a Lua script (below) based on an example that seems to work, but there might be a better way and it needs some error checking etc. Basically want to populate [‘0008,1030’] with [‘RequestAttributesSequence’][1][‘RequestedProcedureDescription’] if it is present in an instance when it gets sent to a from a particular AET via DICOM. Might want to add some other similar items down the road. It seems to work, but I only want to do that if:

a. [‘0008,1030’] is not already set.  
b. [‘RequestAttributesSequence’][1][‘RequestedProcedureDescription’] is set.

Probably simple to check for those conditions, but I an a novice with Lua scripting, and not clear to me that this is the best way to do that. Also, wondering about performance since it has to create a modified instance, store it, and then delete than old one, rather than just making the change before it is stored the first time. Is it possible to just edit / add a tag before storing it the first time ?

1. Also, discovered an MWL plug-in on Github after connecting with the author:

[WorklistFilePurger](https://github.com/alvinak/WorklistFilePurger)

Not sure if that is indexed on the Orthanc site somewhere. Since I am using Custom Docker images, I guess I’d have to compile that when I build the Docker Container, or rewrite it in Python with some modifiations, but could be useful because I’d like to implement MPPS also. Might actually be easier to write a Python script that as suggested here: [https://groups.google.com/g/orthanc-users/c/Tec0xgD4s2c](https://groups.google.com/g/orthanc-users/c/Tec0xgD4s2c)

**Lua Script**

function OnStoredInstance(instanceId, tags, metadata, origin)

– PrintRecursive(instanceId)  
– PrintRecursive(tags)  
– PrintRecursive(metadata)  
– PrintRecursive(origin)  
– Do not modify twice the same file  
if origin.RequestOrigin == ‘DicomProtocol’ and origin.RemoteAet == ‘SOME\_AET’ then  
– Need a check here for [‘0008,1030’] not set and tags[‘RequestAttributesSequence’][1][‘RequestedProcedureDescription’] is set.  
local replace = {}  
replace[‘0008,1030’] = tags[‘RequestAttributesSequence’][1][‘RequestedProcedureDescription’]  
local request = {}  
request[‘Replace’] = replace

– Create the modified instance  
local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’,  
DumpJson(request, true))

– Upload the modified instance to the Orthanc store  
RestApiPost(‘/instances/’, modified)

– Delete the original instance  
RestApiDelete(‘/instances/’ … instanceId)  
end  
end  
end

---

<div class="post-metadata">

### Author: ![sdscotti](https://discourse.orthanc-server.org/user_avatar/discourse.orthanc-server.org/sdscotti/32/2563_2.png) [@sdscotti](https://discourse.orthanc-server.org/u/sdscotti)
#### Post date: [May 7, 2021, 2:35am UTC](https://discourse.orthanc-server.org/t/question-about-lua-script-to-edit-add-tag-onstoredinstance-mwl-plug-in-misc/2422/2 "2021-05-07T02:35:03Z")

</div>

Did a little more testing. **This seems to work for the Instances, but not for the Study Level Study Description. Must be that Orthanc creates a Study Objects upon receipt of a new StudyInstanceUID and populates the Study Level StudyDescription at that point, so it isn’t setting that at the StudyLevel ?** This is the current version of the Lua Script. Added a function to check if a key in the table is set and not an empty string. Not sure how to handle that ? I basically want to add the StudyDescription to the Study, Series (if that applies) and to Instances.

Thanks.

function tableHasKey(table,key)  
return table[key] ~= nil and table[key] ~= ‘’  
end

function OnStoredInstance(instanceId, tags, metadata, origin)

– PrintRecursive(instanceId)  
– PrintRecursive(tags)  
– PrintRecursive(metadata)  
– PrintRecursive(origin)  
– Do not modify twice the same file  
– if origin.RequestOrigin == ‘DicomProtocol’ and origin.RemoteAet == ‘IMACLATE2013’ or NmrEsaote  
if origin[‘RequestOrigin’] ~= ‘Lua’ then  
if origin.RequestOrigin == ‘DicomProtocol’ and origin.RemoteAet == ‘IMACLATE2013’ and not tableHasKey(tags,‘0008,1030’) and tableHasKey(tags,‘RequestAttributesSequence’) then  
local replace = {}  
replace[‘0008,1030’] = tags[‘RequestAttributesSequence’][1][‘RequestedProcedureDescription’]  
local request = {}  
request[‘Replace’] = replace

– Create the modified instance  
local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’,  
DumpJson(request, true))

– Upload the modified instance to the Orthanc store  
RestApiPost(‘/instances/’, modified)

– Delete the original instance  
RestApiDelete(‘/instances/’ … instanceId)  
end  
end  
end

---

<div class="post-metadata">

### Author: ![sdscotti](https://discourse.orthanc-server.org/user_avatar/discourse.orthanc-server.org/sdscotti/32/2563_2.png) [@sdscotti](https://discourse.orthanc-server.org/u/sdscotti)
#### Post date: [May 8, 2021, 2:58am UTC](https://discourse.orthanc-server.org/t/question-about-lua-script-to-edit-add-tag-onstoredinstance-mwl-plug-in-misc/2422/3 "2021-05-08T02:58:54Z")

</div>

Seems to be somewhat related to this question: [Modification of DICOM tags without changing UUID](https://groups.google.com/g/orthanc-users/c/a8vy-CLNJl0/m/bsosZculAQAJ), but not completely.

---

<div class="post-metadata">

### Author: ![sdscotti](https://discourse.orthanc-server.org/user_avatar/discourse.orthanc-server.org/sdscotti/32/2563_2.png) [@sdscotti](https://discourse.orthanc-server.org/u/sdscotti)
#### Post date: [May 12, 2021, 5:07am UTC](https://discourse.orthanc-server.org/t/question-about-lua-script-to-edit-add-tag-onstoredinstance-mwl-plug-in-misc/2422/4 "2021-05-12T05:07:50Z")

</div>

Having a conversation with myself, but found what I was looking for, although now might modify that somewhat to integrate with RIS or MWL files so that tags not handled my the Modality can be updated / added by the RIS or python plug-in for Orthanc.

function OnStoredInstance(instanceId, tags, metadata)  
– Ignore the instances that result from a modification to avoid  
– infinite loops  
if (metadata[‘ModifiedFrom’] == nil and  
metadata[‘AnonymizedFrom’] == nil) then

– The tags to be replaced  
local replace = {}  
replace[‘0070,0084’] = ‘Technologist/Operator’  
replace[‘StudyDescription’] = ‘TEST STUDY’

– The tags to be removed  
– local remove = { ‘MilitaryRank’ }

– Modify the instance, send it, then delete the modified instance  
Delete(SendToModality(ModifyInstance(instanceId, replace, remove, true), ‘sample’))

– Delete the original instance  
Delete(instanceId)  
end  
end
