import glob

try: paraview.simple
except: from paraview.simple import *
paraview.simple._DisableFirstRenderCameraReset()

# TODO:  
#  1.  Set min/max value for every frame in AnimationScene
#      See http://www.paraview.org/pipermail/paraview/2011-September/022682.html
#


LoadPlugin('/Users/schmitt/paraview/paraview/trunk/plugins/vtkLFMReader/build/libvtkLFMReader.dylib',ns=globals())

# Load LFM file(s)
#vtkLfmReaderObject = vtkLFMReader( FileNames=['/Users/schmitt/paraview/testData/LRs_mhd_1995-03-21T04-20-00Z.hdf'] )
#vtkLfmReaderObject = vtkLFMReader( FileNames=['/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_004.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_005.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_006.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_007.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_008.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_009.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_010.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_011.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_012.hdf',
#                                              '/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_013.hdf'] )

#files = glob.glob('/Users/schmitt/paraview/testData/doctoredAnimation/LMs_mhd_*.hdf')
files = glob.glob('/Users/schmitt/paraview/testData/doctoredAnimation/orig/LMs_mhd_*.hdf')
files.sort()
vtkLfmReaderObject = vtkLFMReader(FileNames = files)


vtkLfmReaderObject.PointArrayStatus = []
vtkLfmReaderObject.CellArrayStatus = []
vtkLfmReaderObject.GridScaleFactor = 'Earth Radius: 6.5e8 cm'
vtkLfmReaderObject.CellArrayStatus = ['Plasma Density', 'Sound Speed', 'Velocity Vector','Magnetic Field Vector']
Show().Visibility = 0

##################
# Top-left panel #
########################################################################
# Orient the camera
TopLeftRenderView = GetRenderView()
TopLeftRenderView.CameraPosition = [-7.0, -70.0, 0.0]
TopLeftRenderView.CameraFocalPoint = [-7, 0.0, 0.0]
TopLeftRenderView.CameraViewUp = [0.0, 0.0, 1.0]
TopLeftRenderView.CameraClippingRange = [122.35967717295773, 129.70814347061219]
TopLeftRenderView.CameraParallelScale = 218.48459610631258
TopLeftRenderView.CenterOfRotation = [0.0, 0.0, 0.0]

# Add plane and map it to the dataset
XZVectorPlane = Plane()
XZVectorPlane.Origin = [-60.0, 0.0, -30.0]
XZVectorPlane.Point1 = [30.0, 0.0, -30.0]
XZVectorPlane.Point2 = [-60.0, 0.0, 30.0]
XZVectorPlane.XResolution = 20
XZVectorPlane.YResolution = 15
SetActiveSource(XZVectorPlane)
Show().Visibility = 0

# ResampleWithDataset
SetActiveSource(vtkLfmReaderObject)
XZField = ResampleWithDataset( Source=XZVectorPlane )
Show().Visibility = 0

# Render vector field
XZVectors = Glyph( GlyphType="Arrow", GlyphTransform="Transform2" )
XZVectors.SetScaleFactor = 9.0
XZVectors.Vectors = ['POINTS', 'Velocity Vector']
XZVectors.GlyphTransform = "Transform2"
XZVectors.GlyphType = "Arrow"
XZVectors.GlyphType.TipRadius = 0.04
XZVectors.GlyphType.TipLength = 0.15
XZVectors.GlyphType.ShaftRadius = 0.015
XZVectors.SetScaleFactor = 2.14564239898506e-07
DataRepresentation16 = Show()
DataRepresentation16.EdgeColor = [0.0, 0.0, 0.5019607843137255]
DataRepresentation16.ColorArrayName = ''

# XY cutplane for colormap
SetActiveSource(vtkLfmReaderObject)
XZSlice = Slice( SliceType="Plane" )
XZSlice.SliceOffsetValues = [0.0]
XZSlice.SliceType.Origin = [-151.826509475708, 0.0, 0.0]
XZSlice.SliceType = "Plane"
XZSlice.SliceType.Normal = [0.0, 1.0, 0.0]

# Calculator for pressure
Pressure = Calculator()
Pressure.AttributeMode = 'point_data'
Pressure.Function = 'Plasma Density*4.7619e23*Sound Speed*Sound Speed*3.75e8'
Pressure.ResultArrayName = 'Pressure'

PressureRepresentation = Show()
PressureRepresentation.EdgeColor = [0.0, 0.0, 0.5000076295109483]
PressureRepresentation.ColorArrayName = 'Pressure'
a1_Pressure_PVLookupTable = GetLookupTableForArray( "Pressure", 1, NanColor=[0.498039, 0.498039, 0.498039], RGBPoints=[7.232339585875363e+19, 0.0, 0.0, 1.0, 3.964840999531023e+24, 1.0, 0.0, 0.0], VectorMode='Magnitude', ColorSpace='HSV', ScalarRangeInitialized=1.0 )
a1_Pressure_PiecewiseFunction = CreatePiecewiseFunction()
PressureRepresentation.LookupTable = a1_Pressure_PVLookupTable

ScalarBarWidgetLog10Pressure = CreateScalarBar( Orientation='Horizontal', Title='Pressure', Position2=[0.5, 0.15], LabelFontSize=12, Enabled=1, TitleFontSize=12, Position=[0.25,0.85] )
TopLeftRenderView.Representations.append(ScalarBarWidgetLog10Pressure)
a1_Pressure_PVLookupTable = GetLookupTableForArray( "Pressure", 1, UseLogScale=1,  RGBPoints=[1e+22, 0.0, 0.0, 1.0, 3.96484e+24, 1.0, 0.0, 0.0], LockScalarRange=1 )
TopLeftRenderView.CameraClippingRange = [119.96970760320372, 132.85099018726737]
ScalarBarWidgetLog10Pressure.LookupTable = a1_Pressure_PVLookupTable

# Describe the view
minValue = a1_Pressure_PVLookupTable.RGBPoints[0]
maxValue = a1_Pressure_PVLookupTable.RGBPoints[4]
TopLeftText = Text()
TopLeftText.Text = 'XZ (min=%e  max=%e)' % (minValue, maxValue)
TextRep = GetDisplayProperties(TopLeftText)
TextRep.Visibility = 1


###################
# Top-Right panel #
########################################################################
TopRightRenderView = CreateRenderView()
#TopRightRenderView.CameraPosition = [-9.54128751659703, -1.5694684006493071, 150.56293391130203]
TopRightRenderView.CameraPosition = [-7, 0.0, 70]
#TopRightRenderView.CameraFocalPoint = [-9.54128751659703, -1.5694684006493071, 0.0]
TopRightRenderView.CameraFocalPoint = [-7, 0.0, 0.0]
TopRightRenderView.CameraViewUp = [0.0, 1.0, 0.0]
TopRightRenderView.CompressorConfig = 'vtkSquirtCompressor 0 3'
TopRightRenderView.UseLight = 1
TopRightRenderView.LightSwitch = 0
TopRightRenderView.RemoteRenderThreshold = 3.0
TopRightRenderView.ViewTime = 0.0
TopRightRenderView.Background = [0.31999694819562063, 0.3400015259021897, 0.4299992370489052]
TopRightRenderView.CenterAxesVisibility = 0
TopRightRenderView.CenterOfRotation = [0.0, 0.0, 0.0]
TopRightRenderView.CameraParallelScale = 317.214749894812
TopRightRenderView.CameraClippingRange = [149.05730362328296, 152.8213791144487]

# XY Cutplane
SetActiveSource(vtkLfmReaderObject)

# Subtract Dipole
BzMinusDipole = Calculator()
BzMinusDipole.AttributeMode = 'point_data'
BzMinusDipole.Function = '(Magnetic Field Vector_Z*1e5)+(3.05e4*((coordsX^2+coordsY^2+coordsZ^2)^(-1.5))*(2-(3*(coordsX^2+coordsY^2))/(coordsX^2+coordsY^2+coordsZ^2)))'
BzMinusDipole.ResultArrayName = 'Bz-Dipole'

BzNoDipole = Slice( SliceType="Plane" )
BzNoDipole.SliceOffsetValues = [0.0]
BzNoDipole.SliceType.Origin = [-151.826509475708, 0.0, 0.0]
BzNoDipole.SliceType = "Plane"
BzNoDipole.SliceType.Normal = [0.0, 0.0, 1.0]
DataRepresentation22 = Show()
DataRepresentation22.EdgeColor = [0.0, 0.0, 0.5000076295109483]
DataRepresentation22.EdgeColor = [0.0, 0.0, 0.5019607843137255]

a1_BzDipole_PVLookupTable = GetLookupTableForArray( "Bz-Dipole", 1, RGBPoints=[-20.0, 0.0, 0.0, 1.0, 20.0, 1.0, 0.0, 0.0], VectorMode='Magnitude', NanColor=[0.498039, 0.498039, 0.498039], ColorSpace='HSV', ScalarRangeInitialized=1.0, LockScalarRange=1 )
a1_BzDipole_PiecewiseFunction = CreatePiecewiseFunction()
DataRepresentation22.ColorArrayName = 'Bz-Dipole'
DataRepresentation22.LookupTable = a1_BzDipole_PVLookupTable

ScalarBarWidgetBzNoDipole = CreateScalarBar( Orientation='Horizontal',Title='Bz-Dipole', LabelFontSize=12,Position2=[0.5, 0.15], Enabled=1, TitleFontSize=12,Position=[0.25,0.85] )
TopRightRenderView.Representations.append(ScalarBarWidgetBzNoDipole)
a1_BzNoDip_PVLookupTable = GetLookupTableForArray( "Bz-Dipole", 1, UseLogScale=1 )
ScalarBarWidgetBzNoDipole.LookupTable = a1_BzNoDip_PVLookupTable


# Describe the view
minValue = a1_BzNoDip_PVLookupTable.RGBPoints[0]