copyToOrthanc.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #basic python
  2. import os
  3. import subprocess
  4. import re
  5. import datetime
  6. import sys
  7. fhome=os.path.expanduser('~')
  8. sys.path.insert(1,fhome+'/software/src/labkeyInterface')
  9. import labkeyInterface
  10. import labkeyDatabaseBrowser
  11. sys.path.insert(1,fhome+'/software/src/orthancInterface')
  12. import orthancInterface
  13. import orthancDatabaseBrowser
  14. net=labkeyInterface.labkeyInterface()
  15. net.init(fhome+'/.labkey/network.json')
  16. db=labkeyDatabaseBrowser.labkeyDB(net)
  17. #by default uses .labkey/Remote.json configuration
  18. oNet=orthancInterface.orthancInterface()
  19. #orthanc only looks at orthanc part of the configuration, so it is the same file
  20. oNet.init(fhome+'/.labkey/network.json')
  21. oDb=orthancDatabaseBrowser.orthancDB(oNet)
  22. dicomBase='/data/dicom/database/dicom'
  23. project='IPNUMMretro/Study'
  24. ds=db.selectRows(project,'lists','imageList',{})
  25. storescu='storescu'
  26. peer='onko-nix.onko-i.si'#this is actually localhost
  27. port='11112'#this is for orthanc
  28. i=0
  29. for row in ds['rows']:
  30. if row['statusflag']=='ORTHANC':
  31. continue
  32. if row['file']=='dump.txt':
  33. row['statusflag']='DUMP'
  34. db.modifyRows('update',project,'lists','imageList',[row])
  35. continue
  36. print("ID: {}".format(row['patientid']))
  37. f=os.path.join(dicomBase,row['study'],row['series'],row['file'])
  38. print("Instance: {} ".format(f))
  39. #add files to orthanc via PACS
  40. try:
  41. outText=subprocess.check_output([storescu,peer,port,f])
  42. except subprocess.CalledProcessError:
  43. jsonStatus=oDb.upload(f)
  44. #check if the file made it to orthanc
  45. qfilter={}
  46. qfilter['SOPInstanceUID']=row['file']
  47. rsp=oDb.selectRows(qfilter)
  48. if len(rsp):
  49. print("OK")
  50. row['statusflag']='ORTHANC'
  51. else:
  52. print("Failed")
  53. #update labkey database
  54. db.modifyRows('update',project,'lists','imageList',[row])
  55. if i==-1:
  56. break
  57. i=i+1
  58. print("Done")
  59. quit()