#date sorts studies from orthanc dataset into target study dataset

import os
import json
import re
import sys
import datetime
import re

fhome=os.path.expanduser('~')
fsetup=os.path.join(fhome,'.labkey','setup.json')
with open(fsetup,'r') as f:
    setup=json.load(f)

sys.path.insert(0,setup['paths']['labkeyInterface'])
import labkeyInterface
import labkeyDatabaseBrowser

fconfig=os.path.join(fhome,'.labkey','network.json')

net=labkeyInterface.labkeyInterface()
net.init(fconfig)
db=labkeyDatabaseBrowser.labkeyDB(net)


i=0
#from orthancDatabase/Imaging dataset
projectOrthanc='Orthanc/Database'
inputDataset='Imaging'
#to target project dataset
projectStudy='iPNUMMretro/Study'
#for prospective, set
projectStudy='IPNUMMprospektiva/Study'
outputDataset='Imaging1'
#select patientId that are contained in the demographics dataset
demographicDataset='ClinicalData'
#for prospective
demographicDataset='demographicData'


orthancParticipantField='PatientId'
participantField='PatientId'
#for prospective
participantField='ParticipantId'

#make a list of patients
dsDemo=db.selectRows(projectStudy,'study',demographicDataset,[])
patients=[row[participantField] for row in dsDemo['rows']]
patients=list(set(patients))

patientListStr=""
for p in patients:
    if len(patientListStr)>0:
        patientListStr+=";"
    patientListStr+=p


patientFilter={'variable':orthancParticipantField,
        'value':patientListStr,'oper':'in'}

#takes orthanc as the baseline, selects from patient list
ds=db.selectRows(projectOrthanc,'study',inputDataset,[patientFilter])


#single entry for the patientId/dicomStudy pair
selectVars={participantField:orthancParticipantField,\
        'dicomStudy':'dicomStudy'}

dates=[datetime.datetime.strptime(row['studyDate'],'%Y/%m/%d %H:%M:%S') \
        for row in ds['rows']]

#date sorted entries
idx=sorted(range(len(dates)),key=lambda k:dates[k])


#historical traverse of all studies from inputDataset
for j in range(len(dates)):
    
    row=ds['rows'][idx[j]]

    #skip series which don't match selected filters
    outvar='NONE'
    sd=row['seriesDescription']
    if sd=='PET WB':
        outvar='PETWB_orthancId'
    if sd.find('CT WB')==0:
        if sd.find('fov')<0:
            outvar='CT_orthancId'

    #skip irrelevant series
    if outvar=='NONE':
        continue

    filters=[]
    for v in selectVars:
        filters.append({'variable':v,\
                'value':row[selectVars[v]],'oper':'eq'})

    #ds2 are all studies by patient from sorted dataset
    ds2=db.selectRows(projectStudy,'study',outputDataset,
            [{'variable':participantField,\
                    'value':row[orthancParticipantField],'oper':'eq'}])
    
    #ds1 is the matching row from output dataset 
    ds1=db.selectRows(projectStudy,'study',outputDataset,filters)
    if len(ds1['rows'])>1:
        print('ERROR: too many matches for {}/{}'.\
                format(row[orthancParticipantField],row['dicomStudy']))
        continue

    mode='update'
    outRow={}
    if len(ds1['rows'])==0:
        mode='insert'
        outRow[participantField]=row[orthancParticipantField]
        
        #setting sequence number to length of already included studies
        #sorted by date makes it historically incremental
        outRow['SequenceNum']=len(ds2['rows'])

        outRow['dicomStudy']=row['dicomStudy']
    else:
        outRow=ds1['rows'][0]
        
    outRow[outvar]=row['orthancSeries']
    outRow['studyDate']=row['studyDate']

    status=db.modifyRows(mode,projectStudy,'study',outputDataset,[outRow])
    print('{}'.format(status))
    if j==50:
        break
        
print("Done")
