#Example of python/Visit plotting for Gamera
#Assuming visGam is in python path

#Ring-Avg MHD BW

import sys
import os
import numpy as np
from visit import *
from visit_utils import *
from visit_utils.common import lsearch #lsearch(dir(),"blah")
import visGam as vg
import pyVisit as pyv #Note, this is from LFMTP

Quiet = True
doVid = True

db = "/Users/soratka1/Work/Gamera/viz/Data/raBW_H.xmf"
Nd = vg.getSize(db)
Nx = Nd[0]
Ny = Nd[1]
print(Nx,Ny)
cMap = "viridis"
vId = "P"
vLab = "Pressure"
vB = [0.1,0.5]

outVid = "mhdBW_RA" + ".mp4"


if (Quiet):
	LaunchNowin()
else:
	Launch()



OpenDatabase(db)
md0 = GetMetaData(db)
vg.gamExprs()

#Aesthetics
pyv.setAtts(xLab="X-Axis",yLab="Y-Axis")

DefineScalarExpression("j","mod(zID,%d)"%(Nx))
DefineScalarExpression("i","floor(zID/%d)"%(Nx))
DefineVectorExpression("B","{Bx,By}")

#Make figures
#Main PC
pyv.lfmPCol(db,vId,cMap=cMap,Log=True,vBds=vB)


#Mesh stuff
AddPlot("Mesh","gMesh")
mOp = GetPlotOptions()
mOp.opaqueMode = 1
mOp.opacity = 0.25
SetPlotOptions(mOp)

AddPlot("Contour","i")
cOp = GetPlotOptions()
cOp.colorType = 0
cOp.singleColor = (0, 0, 0, 255)
cOp.legendFlag = 0
cOp.contourNLevels = 16
SetPlotOptions(cOp)

AddPlot("Contour","j")
SetPlotOptions(cOp)

#Field lines
AddPlot("Pseudocolor","operators/IntegralCurve/B2")
pcOp = GetPlotOptions()
pcOp.legendFlag = 0
pcOp.colorTableName = "Autumn"
pcOp.lineType = 0 #Line
pcOp.lineWidth = 1
SetPlotOptions(pcOp)

icOp = GetOperatorOptions(0)
icOp.sourceType = 3 #Circle
icOp.radius = 5
icOp.dataValue = 0 #Solid
icOp.integrationDirection = 2
icOp.relTol = 1.0e-6
icOp.randomSamples = 1
icOp.numberOfRandomSamples = 35
SetOperatorOptions(icOp)

#Make Video
plX = [0.0125]; plY = [0.3]
pyv.cleanLegends(plX,plY,[vLab],plHt=0.01,dy=0.01)

#v2d = GetView2D()
#v2d.viewportCoords = ()
DrawPlots()

T = md0.GetTimes()
dt = T[1]-T[0]
if (doVid):
	#Now do time loop to make video
	pyv.doTimeLoop(T0=0.0,dt=dt,Save=True,tLabPos=(0.45,0.05),Trim=True,bLen=40)
	
	
	pyv.makeVid(Clean=True,outVid=outVid,tScl=2)

	DeleteAllPlots()
	CloseDatabase(db)
