#------------------------------------------------------------------------------------------------------------------------------------------
'''This is a python script that takes as input a Praat Textgrid file containing interval tiers and writes the annotations into another text file in the following format(Audacity label track format):
Start-time    End-time   Label
	0         1.4       "a"
	.          .         .
	.          .         .

Author: M A Rohit
Updated: 22/1/2019
'''
#------------------------------------------------------------------------------------------------------------------------------------------
import sys; import numpy as np

TGFileName = sys.argv[1]	# Provide TG file name as input (Input argument)
OutFileName = sys.argv[2]       # Output text file to contain the annotations, also to be provided as cmdline argument

fidTG = open(TGFileName,'r')
#fidOut = open(OutFileName, 'w')

data = fidTG.readlines()
onsetpts = np.array(['Start', 'End', 'Label'])

for indline in range(len(data)-2):
  item=data[indline]
  if(("xmin" in item) & ("xmax" in data[indline+1]) & ("text" in data[indline+2])):
  	xmin=item.split()[-1]; xmax=data[indline+1].split()[-1]; label=data[indline+2].split()[-1]
  	onsetpts=np.vstack((onsetpts,[xmin,xmax,label]))
  	indline+=3

np.savetxt(OutFileName,onsetpts,fmt='%s')
