Storage area by study date pyton

Hello, I’m trying to develop a plugin pyton that sorts the storage area by study date (example: …/YYYY/MM/DD/uuidmd5.dcm), but I’m not able to retrieve the tags from the dicom file received to create the directory, here is a sample of the current code:

import orthanc
import os

def GetPath(uuid, contentType, data):
return ‘attachment-%d-%s-data-’ % (contentType, uuid)

def OnCreate(uuid, contentType, data):
with open(GetPath(uuid, contentType, data), ‘wb’) as f:
f.write(data)

def OnRead(uuid, contentType):
with open(GetPath(uuid, contentType), ‘rb’) as f:
return f.read()

def OnRemove(uuid, contentType):
os.remove(GetPath(uuid, contentType))

orthanc.RegisterStorageArea(OnCreate, OnRead, OnRemove)

Hi,

That is not that easy to implement :frowning:

When you wish to create an attachment, you can use pydicom to parse the data and extract the dicom tags to generate a path.

Then, you must store the mapping between the uuid and the path because when reading the attachment, you’ll only get the uuid and you’ll need this mapping to retrieve the path.
Right now, this mapping must be stored in an external DB, it can not be stored in Orthanc DB.

HTH

Alain.

It was exactly where I went, I analyzed it using pydicom, and I managed to store it the way I wanted, but to recover the data it was complicated, I even tried to recover it through the API-SELF…

from io import BytesIO

from pydicom import dcmread, dcmwrite
from pydicom.filebase import DicomFileLike

import orthanc
import os
import os.path
import sys

def GetPath(uuid, contentType):

series = orthanc.RestApiGet(‘/instances/%s/series’ % (uuid))
study = orthanc.RestApiGet(‘/instances/%s/study’ % (uuid))
ano = series[‘MainDicomTags’][‘SeriesDate’][:4]
mes = series[‘MainDicomTags’][‘SeriesDate’][4:6]
dia = series[‘MainDicomTags’][‘SeriesDate’][6:8]
modalidade = series[‘MainDicomTags’][‘Modality’]
idPaciente = study[‘PatientMainDicomTags’][‘PatientID’]
nomePaciente = study[‘PatientMainDicomTags’][‘PatientName’]
path = ‘C:\storage%s%s%s%s%s - %s’ % (modalidade, ano, mes, dia, idPaciente, nomePaciente)

file = os.path.join(path, uuid)

return file

def OnCreate(uuid, contentType, data):
tags = dcmread(BytesIO(data))
ano = tags.StudyDate[:4]
mes = tags.StudyDate[4:6]
dia = tags.StudyDate[6:8]
modalidade = tags.Modality
idPaciente = tags.PatientID
nomePaciente = tags.PatientName
path = ‘C:\storage%s%s%s%s%s - %s’ % (modalidade, ano, mes, dia, idPaciente, nomePaciente)

try:
os.makedirs(path)
except:

Already existing directory, ignore the error

pass

file = os.path.join(path, ‘%s-%s’ % (contentType,uuid) )

with open(file, ‘wb’) as f:
f.write(data)

def OnRead(uuid, contentType):
with open(GetPath(uuid, contentType), ‘rb’) as f:
return f.read()

def OnRemove(uuid, contentType):
os.remove(GetPath(uuid, contentType))

orthanc.RegisterStorageArea(OnCreate, OnRead, OnRemove)

Hello,

BTW, are you aware of the WebDAV support that is built in Orthanc, and that provides access by study date?

https://book.orthanc-server.com/users/webdav.html

Regards,

Sébastien-