import analysis
import sys
import os
import getData
import json
import config


def main(configFile):
   with open(configFile,'r') as f:
      xsetup=json.load(f)
   db,fb=getData.connectDB(xsetup['network'])

   qfilter=config.getFilter(xsetup)

   #getData.downloadPatientFiles(db,fb,xsetup,qfilter)
   rows=getData.getPatients(db,xsetup,qfilter)
   try:
      calculateCenters=xsetup['calculateCenters']
   except KeyError:
      calculateCenters=True

   for r in rows:
      #download original files
      getData.downloadFiles(fb,r,xsetup)
      if calculateCenters:
         analysis.calculateRowCenters(r,xsetup)
      #upload centers
      getData.uploadCenters(fb,r,xsetup)
      getData.updateStatus(db,r,xsetup,'calculateCenters')

if __name__=="__main__":
   main(sys.argv[1])
