try: paraview.simple
except: from paraview.simple import *
import numpy as np
import ncdfReader
import glob
import os

paraview.simple._DisableFirstRenderCameraReset()

def viewModel(meshname,mod):
    # Load the mesh
    mesh = SLACDataReader(MeshFileName=meshname)
    # Load the fields
    mesh.ModeFileName = [mod]


    # set up the camera
    view = GetRenderView()
    view.Background = [1, 1, 1] # white background
    view.CameraPosition = [-2.74, 0.0, -0.54]
    view.CameraFocalPoint = [0.102, 0.0, -0.54]
    view.CenterOfRotation = [0.102, 0.0, -0.54]
#    view.Angle = 30.0
#    view.CameraParallelScale = 0.66468238209486596
#    view.CameraClippingRange = [2.3883251083946053, 2.8006742718462574]
    view.CameraViewUp = [0.0, 1.0, 0.0]
#    view.ViewSize = [1600, 1200]
    view.CenterAxesVisibility = 0
    view.OrientationAxesVisibility = 0
    # Set up color scaling
    a3_Displacement_PiecewiseFunction = CreatePiecewiseFunction()
    a3_Displacement_PVLookupTable = GetLookupTableForArray( "Displacement", 3, RGBPoints=[0.00080916209469989299, 0.23000000000000001, 0.29899999999999999, 0.754, 0.22036426075011187, 0.86499999999999999, 0.86499999999999999, 0.86499999999999999, 0.43991935940552385, 0.00599999999999996, 0.016, 0.14999999999999999], VectorMode='Magnitude', NanColor=[0.25, 0.0, 0.0], ColorSpace='Diverging', ScalarRangeInitialized=1.0 )
    DR = Show()
    DR.Representation = 'Surface'
    DR.ColorArrayName = 'Displacement'
    DR.LookupTable = a3_Displacement_PVLookupTable
    DR.ScalarOpacityFunction = a3_Displacement_PiecewiseFunction
    #Render()
    return(mesh)

###script can be run using pypython animateMode.py, it assumes animation direction is created and in the same working directory
# CKN modify the following two lines for paths
mechanicalMesh='/Users/cho/SLAC/doc/cw23/animation/650MHz-beta92-Cavity-Mechanical-Model.ncdf'
modes=glob.glob('/Users/cho/SLAC/doc/cw23/animation/tem3p_results/Displacement_Mode*')
modes.sort(key=lambda s:int(s.split('Mode')[-1].split('_')[0]))
offset=1
###offset mode number for the first mode. In this case, the offset=6 to specifies the first mode from tem3p is labelled at the 6th mode
for j in xrange(len(modes)):
    f=float(modes[j].split('Freq')[-1].split('Hz')[0])
    time=np.linspace(0,1/(f),10)
    modeFile=modes[j]
    dis=ncdfReader.ncdfReader(modeFile)
    print('Processing Mode'+str(j+offset)+'_Freq_'+str(f)+'Hz') 
    for i in xrange(time.size):
        m=ncdfReader.ncdfReader(mechanicalMesh)
# CKN       m.coords+=(dis.Displacement*0.01/np.max(np.abs(dis.Displacement))*np.exp(1j*2*np.pi*f*time[i]))
        m.coords+=(dis.Displacement*0.01/np.max(np.abs(dis.Displacement))*np.sin(1*2*np.pi*f*time[i]))
        newName='animation/test'+str(i)+'.ncdf'
        m.write(newName)
        mesh=viewModel(newName,modeFile)
        outFilename = 'animation/Freq_'+str(f)+'Hz_%.3i.png' %i
        WriteImage(outFilename)
        Delete(mesh)

    os.system('/Users/cho/bin/cropwhite.pl animation/*.png')
    os.system('convert -delay 10 -loop 0 animation/Freq_'+str(f)+'Hz_*.png animation/Mode'+str(j+offset)+'_Freq_'+str(f)+'Hz.gif')
#    os.system('rm animation/*.png')
