import shutil

mp3du_rootname = 'UG_PT_Fall_AddlSept'
bkupCopyName = '_OrigGWV'
ThreadCount = 10

#make copies both as a backup and as templates
shutil.copyfile(mp3du_rootname + '.json', mp3du_rootname + bkupCopyName + '.json')
shutil.copyfile(mp3du_rootname + '-output.json', mp3du_rootname + '-output' + bkupCopyName + '.json')

#first, rewrite main .json adding threads to actual tracking
outF = open(mp3du_rootname + '.json','w')
with open(mp3du_rootname + bkupCopyName + '.json','r') as inF:
  for line in inF:
    outF.write(line)
    
    if 'CAPTURE_RADIUS' in line:
	    outF.write('        "THREAD_COUNT" : {},\n'.format(ThreadCount))

outF.close()

#second, rewrite output processing .json to also write shapefile
outF = open(mp3du_rootname + '-output.json','w')
with open(mp3du_rootname + '-output' + bkupCopyName + '.json','r') as inF:
  for line in inF:
    if ' } }' in line:  #this would be too simplistic if GWV starts writing more
      outF.write(line.replace('\n', ',\n')) #add a comma, then write the 
      outF.write('    { "PATHLINE_PARTS": {\n')
      outF.write('      "FILE_NAME": "' + mp3du_rootname + '_PathlineParts.shp",\n')
      outF.write('      "TIME_CONVERSION" : 1.0\n')
      outF.write('      } }\n')
    else:
      outF.write(line)
    