AUTOMATIC STUDY MERGE

Hello. I would like some help to create a LUA script that automatically joins studies with the same accession number, it would be very useful to attach “OT” mode, I’m not very good at programming so I leave the outline of my script.

function Initialize()
print “Auto-router initialising…”
end

function OnStoredInstance(instanceId, tags, metadata)
local acc = string.lower ( tags [ ’ AccessionNumber’ ])

– local consulta = string.lower(RestApiPost(‘tools/find’DumpJson(condicao)))
– if (consulta [ ’ AccessionNumber’ ] == tags [ ’ AccessionNumber’ ]) then
– SendToModality(instanceId, ‘gepacs’)
– end
end

Lua is not expressive enough to that end. Call the REST API of Orthanc using a programming language that you master:
https://book.orthanc-server.com/users/rest.html

https://book.orthanc-server.com/users/anonymization.html#split-merge

Thanks for the clarification, can I do with PHP?

Yes, by using the cURL library:
https://www.php.net/manual/en/book.curl.php

Contributed code written in PHP is available:
https://github.com/jodogne/OrthancContributed/tree/master/Applications/Php

Stumbled on the need to do the same thing occasionally, although setup a GUI to just allow selecting any study as a parent and one or more children, usually for the same scenario you mentioned with the same AccessionNumber. This is via a “Merge” and not on the initial save to PACS because these have separate StudyInstanceUID’s, same AccessionNumber.

I just started looking into using that, and one issue I’ve noticed is:

If the parent has series say 1-10, and a child or children have overlapping series numbers, then the merged study will have duplicate series numbers, e.g. multiple series with a number 1, although they stay group together. Not sure how it affects references lines, etc., and not sure that Orthanc can renumber automatically.

Have a UI that sort of will helps with that:

merge.png

The backend generates the CLI command and also can send to Orthanc using an API for PHP:

{
“cli”: “http://localhost:8042/studies/e3c3d16c-95d9604b-c563be4c-92f2c12e-a672c4a9/merge -d ‘{"Resources":["25faf75f-f69e214c-1d56e0d3-de130a58-dc6c40e4","5ccfcee7-c4e1f3cd-375c9087-e096dcb8-7b635b0c"]}’”
}

You can build a PHP library to make the CURL calls, e.g.

public function executeCURLPOSTJSON($JSONQuery, $url) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->OrthancURL . $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $JSONQuery);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_ENCODING , “gzip”);
$headers = self::$headers;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return $this->processCURLResults($ch);

}

Would be happy to go in further detail offline.