
try: paraview.simple
except: from paraview.simple import *

def RequestDataDescription(datadescription):
    "Callback to populate the request for current timestep"
    if datadescription.GetForceOutput() == True:
        for i in range(datadescription.GetNumberOfInputDescriptions()):
            datadescription.GetInputDescription(i).AllFieldsOn()
            datadescription.GetInputDescription(i).GenerateMeshOn()
        return

    timestep = datadescription.GetTimeStep()

    input_name = 'input'
    if (timestep % 3 == 0) :
        datadescription.GetInputDescriptionByName(input_name).AllFieldsOn()
        datadescription.GetInputDescriptionByName(input_name).GenerateMeshOn()
    else:
        datadescription.GetInputDescriptionByName(input_name).AllFieldsOff()
        datadescription.GetInputDescriptionByName(input_name).GenerateMeshOff()


def DoCoProcessing(datadescription):
    "Callback to do co-processing for current timestep"
    cp_writers = []
    timestep = datadescription.GetTimeStep()

    RenderView1 = CreateRenderView()
    RenderView1.LightSpecularColor = [1.0, 1.0, 1.0]
    RenderView1.KeyLightAzimuth = 10.0
    RenderView1.UseTexturedBackground = 0
    RenderView1.UseLight = 1
    RenderView1.CameraPosition = [75.0, 0.0, 755.8782057326417]
    RenderView1.FillLightKFRatio = 3.0
    RenderView1.Background2 = [0.0, 0.0, 0.165]
    RenderView1.FillLightAzimuth = -10.0
    RenderView1.LODResolution = 50.0
    RenderView1.BackgroundTexture = []
    RenderView1.InteractionMode = '3D'
    RenderView1.StencilCapable = 1
    RenderView1.LightIntensity = 1.0
    RenderView1.CameraFocalPoint = [75.0, 0.0, 33.5]
    RenderView1.ImageReductionFactor = 2
    RenderView1.CameraViewAngle = 30.0
    RenderView1.CameraParallelScale = 186.9652374105946
    RenderView1.EyeAngle = 2.0
    RenderView1.HeadLightKHRatio = 3.0
    RenderView1.StereoRender = 0
    RenderView1.KeyLightIntensity = 0.75
    RenderView1.BackLightAzimuth = 110.0
    RenderView1.OrientationAxesInteractivity = 0
    RenderView1.UseInteractiveRenderingForSceenshots = 0
    RenderView1.UseOffscreenRendering = 0
    RenderView1.Background = [0.31999694819562063, 0.3400015259021897, 0.4299992370489052]
    RenderView1.UseOffscreenRenderingForScreenshots = 0
    RenderView1.NonInteractiveRenderDelay = 2
    RenderView1.CenterOfRotation = [75.0, 0.0, 33.5]
    RenderView1.CameraParallelProjection = 0
    RenderView1.CompressorConfig = 'vtkSquirtCompressor 0 3'
    RenderView1.HeadLightWarmth = 0.5
    RenderView1.MaximumNumberOfPeels = 4
    RenderView1.LightDiffuseColor = [1.0, 1.0, 1.0]
    RenderView1.StereoType = 'Red-Blue'
    RenderView1.DepthPeeling = 1
    RenderView1.BackLightKBRatio = 3.5
    RenderView1.StereoCapableWindow = 1
    RenderView1.CameraViewUp = [0.0, 1.0, 0.0]
    RenderView1.LightType = 'HeadLight'
    RenderView1.LightAmbientColor = [1.0, 1.0, 1.0]
    RenderView1.RemoteRenderThreshold = 3.0
    RenderView1.CacheKey = 0.0
    RenderView1.UseCache = 0
    RenderView1.KeyLightElevation = 50.0
    RenderView1.CenterAxesVisibility = 0
    RenderView1.MaintainLuminance = 0
    RenderView1.StillRenderImageReductionFactor = 1
    RenderView1.BackLightWarmth = 0.5
    RenderView1.FillLightElevation = -75.0
    RenderView1.MultiSamples = 0
    RenderView1.FillLightWarmth = 0.4
    RenderView1.AlphaBitPlanes = 1
    RenderView1.LightSwitch = 0
    RenderView1.OrientationAxesVisibility = 1
    RenderView1.CameraClippingRange = [647.4944236753153, 818.3838788186314]
    RenderView1.BackLightElevation = 0.0
    RenderView1.ViewTime = 0.0
    RenderView1.OrientationAxesOutlineColor = [1.0, 1.0, 1.0]
    RenderView1.LODThreshold = 0.0
    RenderView1.CollectGeometryThreshold = 100.0
    RenderView1.UseGradientBackground = 0
    RenderView1.KeyLightWarmth = 0.6
    RenderView1.OrientationAxesLabelColor = [1.0, 1.0, 1.0]
    
    p_0_pvti = CreateProducer( datadescription, "input" )
    
    a1_p_PiecewiseFunction = CreatePiecewiseFunction( Points=[0.0, 0.0, 0.5, 0.0, 1.0, 1.0, 0.5, 0.0] )
    
    a1_p_PVLookupTable = GetLookupTableForArray( "p", 1, Discretize=1, RGBPoints=[0.0, 0.23, 0.299, 0.754, 0.993888557636377, 0.706, 0.016, 0.15], UseLogScale=0, VectorComponent=0, NanColor=[0.25, 0.0, 0.0], NumberOfTableValues=256, ColorSpace='Diverging', VectorMode='Magnitude', HSVWrap=0, ScalarRangeInitialized=1.0, LockScalarRange=0 )
    
    DataRepresentation1 = Show()
    DataRepresentation1.CubeAxesZAxisVisibility = 1
    DataRepresentation1.SelectionPointLabelColor = [0.5, 0.5, 0.5]
    DataRepresentation1.SelectionPointFieldDataArrayName = 'vtkOriginalPointIds'
    DataRepresentation1.SuppressLOD = 0
    DataRepresentation1.CubeAxesXGridLines = 0
    DataRepresentation1.CubeAxesYAxisTickVisibility = 1
    DataRepresentation1.Position = [0.0, 0.0, 0.0]
    DataRepresentation1.BackfaceRepresentation = 'Follow Frontface'
    DataRepresentation1.SelectionOpacity = 1.0
    DataRepresentation1.SelectionPointLabelShadow = 0
    DataRepresentation1.CubeAxesYGridLines = 0
    DataRepresentation1.OrientationMode = 'Direction'
    DataRepresentation1.Source.TipResolution = 6
    DataRepresentation1.ScaleMode = 'No Data Scaling Off'
    DataRepresentation1.Diffuse = 1.0
    DataRepresentation1.SelectionUseOutline = 0
    DataRepresentation1.CubeAxesZTitle = 'Z-Axis'
    DataRepresentation1.Specular = 0.1
    DataRepresentation1.SelectionVisibility = 1
    DataRepresentation1.InterpolateScalarsBeforeMapping = 1
    DataRepresentation1.CubeAxesZAxisTickVisibility = 1
    DataRepresentation1.Origin = [0.0, 0.0, 0.0]
    DataRepresentation1.CubeAxesVisibility = 0
    DataRepresentation1.Scale = [1.0, 1.0, 1.0]
    DataRepresentation1.SelectionCellLabelJustification = 'Left'
    DataRepresentation1.DiffuseColor = [1.0, 1.0, 1.0]
    DataRepresentation1.Shade = 0
    DataRepresentation1.SelectionCellLabelOpacity = 1.0
    DataRepresentation1.CubeAxesInertia = 1
    DataRepresentation1.Source = "Arrow"
    DataRepresentation1.Source.Invert = 0
    DataRepresentation1.Masking = 0
    DataRepresentation1.Opacity = 1.0
    DataRepresentation1.LineWidth = 1.0
    DataRepresentation1.MeshVisibility = 0
    DataRepresentation1.Visibility = 1
    DataRepresentation1.SelectionCellLabelFontSize = 18
    DataRepresentation1.CubeAxesCornerOffset = 0.0
    DataRepresentation1.SelectionPointLabelJustification = 'Left'
    DataRepresentation1.SelectionPointLabelVisibility = 0
    DataRepresentation1.SelectOrientationVectors = ''
    DataRepresentation1.CubeAxesTickLocation = 'Inside'
    DataRepresentation1.BackfaceDiffuseColor = [1.0, 1.0, 1.0]
    DataRepresentation1.CubeAxesYAxisVisibility = 1
    DataRepresentation1.SelectionPointLabelFontFamily = 'Arial'
    DataRepresentation1.Source.ShaftResolution = 6
    DataRepresentation1.CubeAxesFlyMode = 'Closest Triad'
    DataRepresentation1.SelectScaleArray = ''
    DataRepresentation1.CubeAxesYTitle = 'Y-Axis'
    DataRepresentation1.ColorAttributeType = 'CELL_DATA'
    DataRepresentation1.SpecularPower = 100.0
    DataRepresentation1.Texture = []
    DataRepresentation1.SelectionCellLabelShadow = 0
    DataRepresentation1.AmbientColor = [1.0, 1.0, 1.0]
    DataRepresentation1.MapScalars = 1
    DataRepresentation1.PointSize = 2.0
    DataRepresentation1.Source.TipLength = 0.35
    DataRepresentation1.SelectionCellLabelFormat = ''
    DataRepresentation1.Scaling = 0
    DataRepresentation1.StaticMode = 0
    DataRepresentation1.SelectionCellLabelColor = [0.0, 1.0, 0.0]
    DataRepresentation1.SliceMode = 'XY Plane'
    DataRepresentation1.Source.TipRadius = 0.1
    DataRepresentation1.EdgeColor = [0.0, 0.0, 0.5000076295109483]
    DataRepresentation1.CubeAxesXAxisTickVisibility = 1
    DataRepresentation1.SelectionCellLabelVisibility = 0
    DataRepresentation1.NonlinearSubdivisionLevel = 1
    DataRepresentation1.CubeAxesColor = [1.0, 1.0, 1.0]
    DataRepresentation1.Representation = 'Surface'
    DataRepresentation1.CustomBounds = [0.0, 1.0, 0.0, 1.0, 0.0, 1.0]
    DataRepresentation1.CubeAxesXAxisMinorTickVisibility = 1
    DataRepresentation1.Orientation = [0.0, 0.0, 0.0]
    DataRepresentation1.CubeAxesXTitle = 'X-Axis'
    DataRepresentation1.ScalarOpacityUnitDistance = 1.1244556452205368
    DataRepresentation1.BackfaceOpacity = 1.0
    DataRepresentation1.SelectionCellFieldDataArrayName = 'CellVisibility'
    DataRepresentation1.SelectionColor = [1.0, 0.0, 1.0]
    DataRepresentation1.Ambient = 0.0
    DataRepresentation1.VolumeRenderingMode = 'Smart'
    DataRepresentation1.SelectionPointLabelFontSize = 18
    DataRepresentation1.ScaleFactor = 26.0
    DataRepresentation1.BackfaceAmbientColor = [1.0, 1.0, 1.0]
    DataRepresentation1.Slice = 0
    DataRepresentation1.Source.ShaftRadius = 0.03
    DataRepresentation1.ScalarOpacityFunction = a1_p_PiecewiseFunction
    DataRepresentation1.SelectMaskArray = ''
    DataRepresentation1.SelectionLineWidth = 2.0
    DataRepresentation1.CubeAxesZAxisMinorTickVisibility = 1
    DataRepresentation1.CubeAxesXAxisVisibility = 1
    DataRepresentation1.Interpolation = 'Gouraud'
    DataRepresentation1.SelectionCellLabelFontFamily = 'Arial'
    DataRepresentation1.SelectionCellLabelItalic = 0
    DataRepresentation1.CubeAxesYAxisMinorTickVisibility = 1
    DataRepresentation1.CubeAxesZGridLines = 0
    DataRepresentation1.SelectionPointLabelFormat = ''
    DataRepresentation1.SelectionPointLabelOpacity = 1.0
    DataRepresentation1.Pickable = 1
    DataRepresentation1.CustomBoundsActive = [0, 0, 0]
    DataRepresentation1.SelectionRepresentation = 'Wireframe'
    DataRepresentation1.SelectionPointLabelBold = 0
    DataRepresentation1.ColorArrayName = 'p'
    DataRepresentation1.SelectionPointLabelItalic = 0
    DataRepresentation1.AllowSpecularHighlightingWithScalarColoring = 0
    DataRepresentation1.SpecularColor = [1.0, 1.0, 1.0]
    DataRepresentation1.LookupTable = a1_p_PVLookupTable
    DataRepresentation1.SelectionPointSize = 5.0
    DataRepresentation1.SelectionCellLabelBold = 0
    DataRepresentation1.Orient = 0
    

    for writer in cp_writers:
        if timestep % writer.cpFrequency == 0 or datadescription.GetForceOutput() == True:
            writer.FileName = writer.cpFileName.replace("%t", str(timestep))
            writer.UpdatePipeline()

    if timestep % 3 == 0 or datadescription.GetForceOutput() == True:
        views = servermanager.GetRenderViews()
        views += servermanager.GetContextViews()
        imagefilename = "image_%t.png"
        for view in range(len(views)):
            fname = imagefilename.replace("%v", str(view))
            fname = fname.replace("%t", str(timestep))
            WriteImage(fname, views[view])

    # explicitly delete the proxies -- we do it this way to avoid problems with prototypes
    tobedeleted = GetNextProxyToDelete()
    while tobedeleted != None:
        Delete(tobedeleted)
        tobedeleted = GetNextProxyToDelete()

def GetNextProxyToDelete():
    proxyiterator = servermanager.ProxyIterator()
    for proxy in proxyiterator:
        group = proxyiterator.GetGroup()
        if group.find("prototypes") != -1:
            continue
        if group != 'timekeeper' and group.find("pq_helper_proxies") == -1 :
            return proxy
    return None

def CreateProducer(datadescription, gridname):
    "Creates a producer proxy for the grid"
    if not datadescription.GetInputDescriptionByName(gridname):
        raise RuntimeError, "Simulation input name '%s' does not exist" % gridname
    grid = datadescription.GetInputDescriptionByName(gridname).GetGrid()
    producer = PVTrivialProducer()
    producer.GetClientSideObject().SetOutput(grid)
    if grid.IsA("vtkImageData") == True or grid.IsA("vtkStructuredGrid") == True or grid.IsA("vtkRectilinearGrid") == True:
        extent = datadescription.GetInputDescriptionByName(gridname).GetWholeExtent()
        producer.WholeExtent= [ extent[0], extent[1], extent[2], extent[3], extent[4], extent[5] ]

    producer.UpdatePipeline()
    return producer


def CreateWriter(proxy_ctor, filename, freq, cp_writers):
    writer = proxy_ctor()
    writer.FileName = filename
    writer.add_attribute("cpFrequency", freq)
    writer.add_attribute("cpFileName", filename)
    cp_writers.append(writer)
    return writer
