'''
Format time series partitions file from R strcchange package.
Process file in multiple stages.
'''

fIn = 'partitions_strucchange.txt'
fOut = 'partitions_strucchange_formatted.txt'
tLastSample = '2017' #Time of last sample. Give as a string

fdIn = open(fIn, 'r')
fdOut = open('tmp.txt', 'w')
tmpStr = ''
for line in fdIn:
    tmpStr += line[:-1]
fdIn.close()

tmpStr = tmpStr.replace('list(', '')
tmpStr = tmpStr.replace('))', ')')
tmpStr = tmpStr.replace('c(', '\nc(')
tmpStr = tmpStr.replace(')', ')\n')
tmpStr = tmpStr.replace('NA,', 'NA\n')
fdOut.write(tmpStr) #Dump to tmp file
fdOut.close()

fdIn = open('tmp.txt', 'r')
fdOut = open(fOut, 'w')
for line in fdIn:
    line = line[:-1].replace(' ', '')
    if len(line) == 0: continue
    if 'c' in line:
        line = line.replace('c(', '')
        line = line.replace(')', '')
        tmpLine = line.split(',')
        tmpStr = ''
        for i in tmpLine:
            if len(i) == 0: continue
            if i is not tLastSample: tmpStr += i + '\t'
        fdOut.write(tmpStr + tLastSample + '\n')
    else:
        tmpLine = line.split(',')
        for i in tmpLine:
            if len(i) == 0: continue
            if 'NA' in i: fdOut.write(tLastSample + '\n')
            elif i is not tLastSample: 
                fdOut.write(i + '\t' + tLastSample + '\n') #Not last sample
            else: fdOut.write(tLastSample + '\n') #Last sample

fdIn.close()
fdOut.close()
