from paraview.simple import *
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

paraview.simple._DisableFirstRenderCameraReset()
#------------------------------------------------------------
# ----- Get active view
renderView1 = GetActiveViewOrCreate('RenderView')
renderView1.ViewSize = [2560,1440]
renderView1.OrientationAxesVisibility = 0
renderView1.CenterAxesVisibility = 0
renderView1.UseLight = 1
renderView1.InteractionMode = '2D'
renderView1.Update()
SetActiveView(renderView1)
layout1 = GetLayout()
Render()

# Declare data directory
dataDir = r'path/to/data'

#
# ============================================================================= #
# ----- Load surface data to get normal vectors information in a slice
# ============================================================================= #
#
# Declare path to file to load
file2load = os.path.join(dataDir,'V2C_3D_DDES_snapshot_surf.tec')

# Read data
raw_surf_data = TecplotReader(registrationName='V2C DDES Snapshot', FileNames=[file2load])

# Load ALL available surface variables
available_vars = raw_surf_data.GetProperty('DataArrayInfo')
raw_surf_data.DataArrayStatus = [available_vars[i] for i in range(0, len(available_vars), 2)]
Show(raw_surf_data, renderView1)
Render()

# Specific post process for this case
mergeBlocks = MergeBlocks(registrationName='MergeBlocks', Input=raw_surf_data)
wingSurface = ExtractSurface(registrationName='ExtractSurface', Input=mergeBlocks)

# Compute normal vectors
normal_vectors = SurfaceNormals(registrationName='GenerateSurfaceNormals', Input=wingSurface)

# Slice the wing to get an airfoil
wingSlice = Slice(registrationName='Wing Slice',Input=normal_vectors)
wingSlice.SliceType = 'Plane'
wingSlice.SliceOffsetValues = [0.0]
wingSlice.SliceType.Origin = [0.0, 0.0, 0.0]
wingSlice.SliceType.Normal = [0.0, 1.0, 0.0]

#
# ============================================================================= #
# ----- Load flow data to get normal vectors information
# ============================================================================= #
#
# Declare path to file to load
file2load = os.path.join(dataDir,'V2C_2D_RANS_KEps.plt')

# Read data
flowfield_raw = VisItTecplotBinaryReader(registrationName='Raw Flowfield Data',FileName=file2load)

# Load ALL available flow variables
available_vars = flowfield_raw.GetProperty('PointArrayInfo')
flowfield_raw.PointArrayStatus = [available_vars[i] for i in range(0, len(available_vars), 2)]

# Transformation to strict 2D data
rot_angle = 45 # degrees
flowfield_raw_transformed = Transform(registrationName='Transformed Raw Flowfield Data',Input=flowfield_raw)
flowfield_raw_transformed.Transform = 'Transform'
flowfield_raw_transformed.Transform.Rotate = [rot_angle, 0.0, 0.0]
flowfield_raw_transformed.Transform.Scale  = [1.0, np.cos(np.deg2rad(rot_angle)), np.cos(np.deg2rad(rot_angle))]

# Make sure that the airfoil is in the 2D plane
coord_calc = Calculator(registrationName='Calculator: Coordinates', Input=flowfield_raw_transformed) 
coord_calc.ResultArrayName = 'NewCoord'
coord_calc.CoordinateResults = 1
coord_calc.Function = 'coordsX*iHat+0*jHat+coordsZ*kHat'

# Merge blocks
flowfield = MergeBlocks(registrationName='Complete Flowfield',Input=coord_calc)












