#basic python
import os
import subprocess
import re
import datetime
import sys


fhome=os.path.expanduser('~')

sys.path.insert(1,fhome+'/software/src/labkeyInterface')
import labkeyInterface
import labkeyDatabaseBrowser


def getValue(filename,tags):
    #extract value of tags (list of tags) from dcmdump-ed file
    #supply tags without braces ie. 0002,0008
    #rows is a list of lines from file
    with open(filename,"r") as f:
        out=f.read()
        lst=out.split('\n')
        for tag in tags.keys():
            tags[tag]=loadDicom.getTagValue(lst,tag)
    return True


net=labkeyInterface.labkeyInterface()
net.init(fhome+'/.labkey/network.json')

db=labkeyDatabaseBrowser.labkeyDB(net)

#by default uses .labkey/Remote.json configuration


dicomBase='/data/dicom/database/dicom'
ds=db.selectRows('IPNUMMretro/Study','lists','imageList',{})


storescu='storescu'
peer='onko-nix.onko-i.si'#this is actually localhost
port='11112'#this is for orthanc

i=0
for row in ds['rows']:
    print("ID: {}".format(row['patientid']))
    f=os.path.join(dicomBase,row['study'],row['series'],row['file'])

    print("Instance: {} ".format(f))
    #add files to orthanc via PACS
    outText=subprocess.check_output([storescu,peer,port,f])
    if i==0:
        break
    i=i+1

   
print("Done")
quit()

