#last update 1/23/17
#prev update 3/23/16 1.03pm


#run it at command line with: 
#visit -assume_format Chombo -cli -s /Users/mhuartee/Box\ Sync/py/polarisation_xmdv_3.py
#meant to produce DATE.okc file to be opened with visit then.

SetActiveWindow(1)
SetActiveWindow(1)
DeleteActivePlots()

import math, datetime, sys, time, os
#import numpy as np
SuppressMessages(2)
start = time.time()#measure program time


#  *** params ***
DefineScalarExpression("angjet", "0.")
xcells=64#from data
pix=(40,20) #36min
#pix=(32,40)#11mins
ylim=(0.,32.)
zlim=(32.,40.)
dy=(ylim[1]-ylim[0])/float(pix[0])
dz=(zlim[1]-zlim[0])/float(pix[1])
where='/scratch/bliu17/Laurence/run_dir/out/'
###############################

#data
hoy=datetime.datetime.now().strftime("%m-%d")
#OpenDatabase(str(where+'chombo00008.hdf'), 0)


#   z=jet                                 
#   ^                                     
#   |                                     
#   |---> y    x axis is pointing out      
DefineScalarExpression("3dB_strength",
   "sqrt(Bx*Bx+By*By+Bz*Bz)")
DefineScalarExpression("fact",
   #"1")
   "(px*px+py*py+pz*pz)/rho/2.")#Ekinetic
DefineScalarExpression("Bproj",
   "Bz*cos(angjet)+Bx*sin(angjet)")
DefineScalarExpression("I",
   "fact*(By*By+Bproj*Bproj)")
DefineScalarExpression("Q",
   "fact*(By*By-Bproj*Bproj)")
DefineScalarExpression("U",
   "2.*fact*Bproj*By")

#get data extents
AddPlot("Pseudocolor", "rho", 1, 0)
DrawPlots()
SetQueryFloatFormat("%g")
Query("SpatialExtents")
xmax=float(GetQueryOutputValue()[1])
#ymax=GetQueryOutputValue()[3]
#zmax=GetQueryOutputValue()[5]
print xmax

intcells=int(
   float(xcells)*(ylim[1]-ylim[0])/float(xmax)
   )
DeleteActivePlots()

#lineout initialize
AddPlot("Curve", "operators/Lineout/I", 1, 0)
DrawPlots()
LineoutAtts = LineoutAttributes()
LineoutAtts.point1 = (0., 0., 32.)
LineoutAtts.point2 = (xmax, 0., 32.)
LineoutAtts.interactive = 0
LineoutAtts.ignoreGlobal = 0
LineoutAtts.samplingOn = 0
LineoutAtts.numberOfSamplePoints = 10
LineoutAtts.reflineLabels = 0
SetOperatorOptions(LineoutAtts, 0)


#initial IO
lines=pix[0]*pix[1]
f = open('/scratch/bliu17/Laurence/run_dir/polarMaps/'+hoy+'pol.okc', 'w+')
print f.write("%g %g %g\n" % (6,lines,6))
print f.write("x\ny\nz\npolx\npoly\npolz\n")
print f.write("%g %g %g \n" % (ylim[0],float(1+pix[0])*dy, 10))
print f.write("%g %g %g \n" % (zlim[0],float(1+pix[1])*dz, 10))
print f.write("%g %g %g \n" % (0.,0.00, 10))
print f.write("%g %g %g \n" % (0.,0.75, 10))
print f.write("%g %g %g \n" % (0.,0.75, 10))
print f.write("%g %g %g \n" % (0.,0.00, 10))

c=1
#compute
z=zlim[0]
while z<zlim[1]:
   print str(int(98.*z/(zlim[1]-1.)))+'%'

   y=ylim[0]
   while y<ylim[1]:
      psi=0.
      degree=0.

      LineoutAtts = LineoutAttributes()
      LineoutAtts.point1 = (ylim[0], y, z)
      LineoutAtts.point2 = (ylim[1], y, z)
      LineoutAtts.interactive = 1
      LineoutAtts.ignoreGlobal = 0
      LineoutAtts.samplingOn = 0
      LineoutAtts.numberOfSamplePoints = intcells
      LineoutAtts.reflineLabels = 0
      SetOperatorOptions(LineoutAtts, 0)

      if c==1:                                                                  
         #I                                                                     
         #ChangeActivePlotsVar("I")                                             
         Query("Integrate")                                                     
         I0 = GetQueryOutputValue()/float(intcells)                       
         #Q                                                                     
         ChangeActivePlotsVar("Q")                                              
         Query("Integrate")                                                     
         Q0 = 0.75*GetQueryOutputValue()/float(intcells)                       
         #U                                                                     
         ChangeActivePlotsVar("U")                                              
         Query("Integrate")                                                     
         U0 = 0.75*GetQueryOutputValue()/float(intcells)                                                                             
      elif c==2:                                                                  
         #U                                                                     
         #ChangeActivePlotsVar("U")                                             
         Query("Integrate")                                                     
         U0 = 0.75*GetQueryOutputValue()/float(intcells)                       
         #I                                                                     
         ChangeActivePlotsVar("I")                                              
         Query("Integrate")                                                     
         I0 = GetQueryOutputValue()/float(intcells)                       
         #Q                                                                     
         ChangeActivePlotsVar("Q")                                              
         Query("Integrate")                                                     
         Q0 = 0.75*GetQueryOutputValue()/float(intcells)                       
      elif c==3:                                                                  
         #Q                                                                     
         #ChangeActivePlotsVar("Q")                                             
         Query("Integrate")                                                     
         Q0 = 0.75*GetQueryOutputValue()/float(intcells)                       
         #U                                                                     
         ChangeActivePlotsVar("U")                                              
         Query("Integrate")                                                     
         U0 = 0.75*GetQueryOutputValue()/float(intcells)                       
         #I                                                                     
         ChangeActivePlotsVar("I")                                              
         Query("Integrate")                                                     
         I0 = GetQueryOutputValue()/float(intcells)                       
         c=0                                                         

      if I0 != 0.:
         degree=( math.sqrt(math.pow(U0,2.) +
            math.pow(Q0,2.))/I0 
            )

      if Q0 != 0.:
         psi=0.5*(math.atan(U0/Q0)+0.*math.pi)

      f.write("%g %g %g %g %g %g\n" % (y,z,0.,degree*math.cos(psi),
         degree*math.sin(psi),0.))
      
      c=c+1
      y=y+dy
   z=z+dz

f.close()
stop = time.time()
print '\n%3.1f min \n' % float((stop-start)/60.)
print 'Done!, the xmdv file is called local:/scratch/bliu17/Laurence/run_dir/polarMaps/x-ypol.okc\n'
#os.system('say "Your calculation is done"')

