import datetime
import sys
begin = datetime.datetime.now()
import pyodbc
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

dataini = "'"+str(sys.argv[1])
horaini = str(sys.argv[2])+"'"
datafim = "'"+str(sys.argv[3])
horafim = str(sys.argv[4])+"'"




cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=.\SQLExpress;DATABASE=DTS_Teste;UID=sa;PWD=Elipse21')
cursor = cnxn.cursor()

string = ' '
for i in np.arange(501)[1:501]:
    if i == 500:
        string = string + 'T' + str(i)
    else:
        string = string + 'T' + str(i) + ','

querystring = 'SELECT'+string+' FROM Hist_SupportTest1 WHERE E3TimeStamp >= CAST('+str(dataini)+' '+str(horaini)+' AS datetime) AND E3TimeStamp <= CAST('+str(datafim)+' '+str(horafim)+' AS datetime) '
print querystring
cursor.execute(querystring)


rows = cursor.fetchall()

intensity = []
for item in rows:
    intensity.append(item)

#convert intensity (list of lists) to a numpy array for plotting
intensity = np.array(intensity)


x = np.arange(500)
y = np.arange(len(rows))

#setup the 2D grid with Numpy
x, y = np.meshgrid(x, y)

print np.shape(intensity)


#now just plug the data into pcolormesh, it's that easy!
plt.pcolormesh(x, y, intensity)
plt.colorbar() #need a colorbar to show the intensity scale
plt.savefig('heatmap.png')
end = datetime.datetime.now()
print 'tempo:', end - begin
plt.show()



