#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

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

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

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

parameterFile=sys.argv[1]
runid=sys.argv[2];

ana=analysisInterface.analysisInterface(db,fb,runid)
ana.updateStatus(2)
pars=ana.getParameters(parameterFile)

if pars==None:
    sys.exit()



i=0
#from orthancDatabase/Imaging dataset
projectOrthanc=pars['Orthanc']['project']
inputQuery=pars['Orthanc']['queryName']
inputSchema=pars['Orthanc']['schemaName']
inputParticipantField=pars['Orthanc']['participantField']

#to target project dataset
projectStudy=pars['Database']['project']
#'iPNUMMretro/Study'
#for prospective, set
#projectStudy='IPNUMMprospektiva/Study'
outputQuery=pars['Database']['queryName']
outputSchema=pars['Database']['schemaName']
#select patientId that are contained in the demographics dataset
listQuery=pars['Database']['listQuery']
dbParticipantField=pars['Database']['participantField']


#make a list of patients
dsDemo=db.selectRows(projectStudy,outputSchema,listQuery,[])
patients=[row[dbParticipantField] for row in dsDemo['rows']]
patients=list(set(patients))

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


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

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


#single entry for the patientId/dicomStudy pair
selectVars={dbParticipantField:inputParticipantField,\
        '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,outputSchema,outputQuery,
            [{'variable':dbParticipantField,\
                    'value':row[inputParticipantField],'oper':'eq'}])
    
    #ds1 is the matching row from output dataset 
    ds1=db.selectRows(projectStudy,outputSchema,outputQuery,filters)
    if len(ds1['rows'])>1:
        print('ERROR: too many matches for {}/{}'.\
                format(row[inputParticipantField],row['dicomStudy']))
        continue

    mode='update'
    outRow={}
    if len(ds1['rows'])==0:
        mode='insert'
        outRow[dbParticipantField]=row[inputParticipantField]
        
        #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,outputSchema,outputQuery,[outRow])
    print('{}'.format(status))
    if j==50:
        break
        
print("Done")
