import matplotlib.pyplot as plt
import numpy as np
import sys

if len(sys.argv) != 4:
	print "usage:plotTimeSeries.py inputFile_statistics inputFile_clusterResult outputFile "
	exit(-1)

lineCount = 0 
clusterCount = 1 
clusterResult = {}
cluster = {}
fstatistics=open(sys.argv[1],'r')
fcluster=open(sys.argv[2],'r')
for line in fcluster.readlines():
	a = line.split("\t")
	if len(a[0]) < 7 :
		pass
	else:
		clusterResult[a[0]]=(int)(a[1])
	if (int)(a[1]) in cluster:
		pass
	else:
		if (int)(a[0]) in cluster:
			pass
		else:
			cluster[(int)(a[1])]= clusterCount
			clusterCount = clusterCount +1
#print "clusterCount:",clusterCount
#print "length 0f cluster:",len(cluster)
#print "length of clusterResult:",len(clusterResult)

#for key in cluster:
#	#print "clusterResult[23]",cluster[23]
#	print "cluster key is :",key,"value is :",cluster[key]

clusterCount = 1 
for key in cluster:
	plt.figure(cluster[key])
	plt.title("cluster Number:"+str(key))
	plt.xlabel("time")
	plt.ylabel("value")
	plt.xlim(-1.0,13.0)
	plt.ylim(-1.0,2.0)
	plt.grid(True)
	clusterCount = clusterCount + 1
#plt.show()

countList = []
for i in range(1,clusterCount):
	countList.append(0)

for line in fstatistics.readlines():
	a = line.split("\t")
	clusterCount = cluster[clusterResult[a[0]]]
	#print a[0] 
	#print clusterCount
	countList[clusterCount-1] = countList[clusterCount-1] + 1
	plt.figure(clusterCount)
	x = []
	y = []
	for i in range(1,len(a)):
		x.append(i)
		y.append(float(a[i]))
	plt.plot(x,y)
	plt.xlim(-1.0,13.0)
	plt.ylim(-0.1,1.1)
#print countList

for clusterCount in range(1,len(cluster)+1):
	plt.figure(clusterCount)
	outputFile = sys.argv[3]+"/"+"clusterResult"+str(clusterCount)+".png"
	plt.savefig(outputFile)
	clusterCount = clusterCount + 1
#plt.clf()

