import os
from math import atan2,atan

def phaseDifference(a,b):
    return atan2( a[0]*b[1]- b[0]*a[1], a[0]*b[0] + a[1]*b[1]);


pointID = 187
datafile = "point%d.csv" % pointID
rawstats=open(datafile,"r")
data = [line.split(',') for line in rawstats]
rawstats.close()

# get info
info = data.pop(0)  # remove header info from data
tmapFactor = -17.1263
temperature = 35.9
magnitizationPhase = 0.0
print info 

# initialize
nzero = 3

gnudat=open( "%s.dat" % datafile.split(".")[0] ,"w")

for i in range(nzero+1):
  currData = [float(data[i][1]) , float(data[i][2])]
  gnudat.write( "%d,%f,%f \n" %  (i, atan2(currData[1],currData[0]),temperature) )

baseData = [float(data[nzero][1]) , float(data[nzero][2])]
# loop through timestep
for i in range(nzero+1,len(data)):
  currData = [float(data[i][1]) , float(data[i][2])]
  #print imagData,realData, atan2(imagData,realData) ,atan(imagData/realData) , data[i][0]
  deltaTemp = tmapFactor * phaseDifference(baseData,currData) 
  magnitizationPhase = magnitizationPhase + phaseDifference(baseData,currData) 
  temperature = temperature + deltaTemp
  gnudat.write( "%d,%f,%f,%f,%f \n" %  (i, atan2(currData[1],currData[0]),temperature,phaseDifference(baseData,currData),magnitizationPhase ) )
  # update for next time
  baseData = currData

gnudat.close; gnudat.flush()


gnuscript=open( "%s.plt" % datafile.split(".")[0] ,"w")
gnuscript.write("""
# Gnuplot script file
#set term pslatex color size 7.5,5.25                                           
#set   output "history.tex"
set   autoscale    # scale axes automatically                                   
unset log          # remove any log-scaling
unset label        # remove any previous labels                                 
set xtic auto      # set xtics automatically
set ytic auto nomirror  # set ytics automatically    
set y2tic auto      # set ytics automatically
#set xr [80:220]                                                                
#set xtics   ("0.0" 85.0, "$0.5\\Delta t$" 117.5, "$1.0\\Delta t$" 150.0, "$1.5\\Delta t$" 182.5, "$2.0\\Delta t$" 215.0)                                       
set y2r [-200:200]                                                              
#set yr [-pi:pi]                                                              
set key top Left                                                                
set grid
pluspidiv2 = pi/2.0                                                             
minuspidiv2 = -pi/2.0

#set title "temperature history"                                                
set y2label "temperature [C]"
set xlabel "time [s]"                                                           
set arrow  from 95,-5.0 to 95,5.0 nohead lw 1 lc 0 lt 0
set arrow  from 14,-5.0 to 14,5.0 nohead lw 1 lc 0 lt 0
set arrow  from 27,-5.0 to 27,5.0 nohead lw 1 lc 0 lt 0
""")

def getPointData(tmpID):
  prevData=[float(data[tmpID-1][1]),float(data[tmpID-1][2])]
  tempData =[float(data[tmpID][1]),float(data[tmpID][2])]
  tmpList=[tempData[0],tempData[1],tmpID,atan2(tempData[1],tempData[0]),
                               phaseDifference(prevData,tempData) ]
  print prevData, tmpList,  \
        tempData[0]*prevData[1] - prevData[0]*tempData[1], \
        prevData[0]*tempData[0] + prevData[1]*tempData[1]
  return tmpList

pointList = [13,14,15,26,27,28,94,95,96,97]
for timeID in pointList:
   rawdata = getPointData(timeID)
   gnuscript.write('set label "(%f,%f)" at %f,%f \n' %  \
               (rawdata[0],rawdata[1],rawdata[2],rawdata[3]))

#"point%d.csv" using 8:1 title "atan( %d)"           w l lw 1  ,\\
gnuscript.write("""
#set multiplot
# upper left                                                                    
#set origin 0.0,0.5
#set size   0.5,0.50
set datafile separator ","
set term x11 %d title "point %d"
plot \\
"point%d.csv" using 8:5 title "temp (%d)" axis x1y2 w lp lw 1 lc 0 ,\\
"point%d.dat" using 1:2 title "atan2(%d)"           w lp lw 1 lc 1 ,\\
"point%d.dat" using 1:3 title "temp (%d)" axis x1y2 w lp lw 1 lc 2 ,\\
"point%d.dat" using 1:4 title "deltaphi (%d)"       w lp lw 1 lc 3 ,\\
"point%d.dat" using 1:5 title "phi      (%d)"       w lp lw 1 lc 4 ,\\
pluspidiv2  notitle w l lw 1 lc 0 ,\\
minuspidiv2 notitle w l lw 1 lc 0
"""  % ( pointID,pointID,pointID,pointID,pointID,pointID,pointID,pointID,pointID,pointID,pointID,pointID) )

gnuscript.close; gnuscript.flush()


os.system("gnuplot -persist %s.plt" % datafile.split(".")[0] )
