listOrthanc.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. def getValue(filename,tags):
  12. #extract value of tags (list of tags) from dcmdump-ed file
  13. #supply tags without braces ie. 0002,0008
  14. #rows is a list of lines from file
  15. with open(filename,"r") as f:
  16. out=f.read()
  17. lst=out.split('\n')
  18. for tag in tags.keys():
  19. tags[tag]=loadDicom.getTagValue(lst,tag)
  20. return True
  21. net=labkeyInterface.labkeyInterface()
  22. net.init(fhome+'/.labkey/network.json')
  23. db=labkeyDatabaseBrowser.labkeyDB(net)
  24. #by default uses .labkey/Remote.json configuration
  25. dicomBase='/data/dicom/database/dicom'
  26. project='IPNUMMretro/Study'
  27. schema='study'
  28. outputList='imageList'
  29. ds=db.selectRows(project,schema,'Imaging',{})
  30. dicomProject='Test/Transfer'
  31. dicomDataset='Imaging'
  32. for row in ds['rows']:
  33. print("ID: {}".format(row['PatientId']))
  34. nfilter=[{'variable':'PatientId','value':row['PatientId'],'oper':'eq'}]
  35. ds1=db.selectRows(dicomProject,'study',dicomDataset,nfilter)
  36. for fi in ds1['rows']:
  37. fdir=os.path.join(dicomBase,fi['Study'],fi['Series'])
  38. #print("Instance: {} ".format(fdir))
  39. files=os.listdir(fdir)
  40. for f in files:
  41. tfile=os.path.join(fdir,f)
  42. print("{}".format(tfile))
  43. #add files to orthanc via PACS
  44. #outText=subprocess.check_output([storescu,peer,port,tfile])
  45. oRow={'PatientId':row['PatientId'],'Series':fi['Series'],'Study':fi['Study'],'file':f,'statusFlag':'AVAILABLE'}
  46. ds2=db.selectRows(project,'lists',outputList,[{'variable':'file','value':f,'oper':'eq'}])
  47. if len(ds2['rows']):
  48. continue
  49. db.modifyRows('insert',project,'lists',outputList,[oRow])
  50. print("Done")
  51. quit()