# pylint: skip-file
# execute in command line with pvpython
# script has to be located in a subfolder in the case directory
# tested with paraview version 5.6.3

#### import the simple module from the paraview
from paraview.simple import *
import os

#### some helper functions
def executeInBash( command ):
    print( " Execute in bash: " + command )
    os.system( command )

def show_points_from_csv( filename ):
    print("Read 3D points: " + filename )
    if os.path.isfile( filename ):
        csvdata = CSVReader(FileName=[filename])
        tableToPoints1 = TableToPoints(Input=csvdata)
        tableToPoints1.XColumn = '# x' # CSV header line starts with '#'
        tableToPoints1.YColumn = 'y'
        tableToPoints1.ZColumn = 'z'
        tableToPoints1Display_1 = Show(tableToPoints1, renderView1)
        tableToPoints1Display_1.SetRepresentationType('Points')
        tableToPoints1Display_1.PointSize = 8.0
        tableToPoints1Display_1.AmbientColor = [0.0, 0.0, 0.0]
    else:
        print( " ... not found. Skipped." )

def create_latestTimeVTK_ifNotAlreadyThere(): # create VTK/latestTime.vtk if not already there
    fname = "../VTK/latestTime.vtk"
    if os.path.isfile(fname):
        print("Found "+fname)
    else:
        executeInBash('cd .. && foamToVTK -latestTime') # convert
        executeInBash('cd ../VTK &&  mv $(ls -1 -t *.vtk | head -1) latestTime.vtk') # rename newest VTK to latestTime.vtk

def saveCurrentViewAsPNG( filename ):
    renderView1 = GetActiveViewOrCreate('RenderView')
    SetActiveSource(thisFile) # just focus something different to hide the red frame
    SaveScreenshot(filename, renderView1, ImageResolution=[800, 600], OverrideColorPalette='WhiteBackground')
    print("Written: " + filename)


## init
paraview.simple._DisableFirstRenderCameraReset()
renderView1 = GetActiveViewOrCreate('RenderView')

## specify VTK file
create_latestTimeVTK_ifNotAlreadyThere()
FileName='../VTK/latestTime.vtk'
thisFile = LegacyVTKReader(FileNames=[FileName])

## read 3d points from CSV file and show them as points
csvfile_points='./2014_Adelsberger_et_al_case2/C2_shape_OpenFOAM_t3.5_xyz.csv'
show_points_from_csv( csvfile_points  )


## SLICE

# create a new slice
slice1 = Slice(Input=thisFile)
slice1.SliceType = 'Plane'
slice1.SliceType.Origin = [0.5, 1.0, 0.5]
slice1.SliceType.Normal = [0.0, 0.0, 1.0]
slice1Display = Show(slice1, renderView1)

# set scalar coloring
ColorBy(slice1Display, ('CELLS', 'alpha.air'))
slice1Display.RescaleTransferFunctionToDataRange(True, False)
slice1Display.SetScalarBarVisibility(renderView1, True)

# set projection and scaling
renderView1.ResetCamera() # zoom to visible data
renderView1.CameraPosition = [0.5,1.0,999.0]
renderView1.CameraParallelProjection = 1
renderView1.CameraParallelScale = 1.0
renderView1.Update()

# save screenshot
saveCurrentViewAsPNG( 'latestTime_slice.png' )


## ZOOM OF THE SLICE

# set projection and scaling
renderView1.CameraParallelProjection = 1
renderView1.CameraParallelScale = 0.3 # the smaller the larger

renderView1.CameraFocalPoint = [0.5, 1.60, 0.5] # case 2
renderView1.CameraPosition   = [0.5, 1.60, 999]

renderView1.OrientationAxesVisibility = 0
slice1Display.SetScalarBarVisibility(renderView1, False)

# save screenshot
saveCurrentViewAsPNG( 'latestTime_slice_zoom.png' )


## CLIPPED CONTOUR SHAPE
contour1 = Contour(Input=thisFile)
contour1.ContourBy = ['POINTS', 'alpha.air']
contour1.Isosurfaces = [0.5]
contour1.PointMergeMethod = 'Uniform Binning'
contour1Display = Show(contour1, renderView1)

# create a new clip
Hide(slice1, renderView1)
Hide(contour1, renderView1)
clip1 = Clip(Input=contour1)
clip1.ClipType = 'Plane'
clip1.Scalars = ['POINTS', 'p']
clip1.ClipType.Origin = [0.5, 1.0, 0.5]
clip1.ClipType.Normal = [0.0, 0.0, 1.0]

clip1Display = Show(clip1, renderView1)

# save screenshot
saveCurrentViewAsPNG( 'latestTime_isoClip.png' )


## show pictures
print('Show images:')
executeInBash('feh *.png')
