
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 % 1 == 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()

    DataRepresentation4 = GetDisplayProperties(PlotOverLine1)
    XYChartView1 = CreateXYPlotView()
    XYChartView1.ShowAxis = [1, 1, 0, 0]
    XYChartView1.ShowAxisGrid = [1, 1, 0, 0]
    XYChartView1.TooltipPrecision = 6
    XYChartView1.AxisLabelsBottom = []
    XYChartView1.LegendLocation = 1
    XYChartView1.AxisLabelsLeft = []
    XYChartView1.ViewTime = 0.0
    XYChartView1.ShowLegend = 1
    XYChartView1.AxisRange = [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]
    XYChartView1.AxisTitleFont = ['Arial', '12', '1', '0', 'Arial', '12', '1', '0', 'Arial', '12', '1', '0', 'Arial', '12', '1', '0']
    XYChartView1.AxisLabelColor = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    XYChartView1.AxisTitleColor = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5]
    XYChartView1.ChartTitleColor = [0.0, 0.0, 0.0]
    XYChartView1.ChartTitleAlignment = 1
    XYChartView1.AxisColor = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    XYChartView1.TooltipNotation = 0
    XYChartView1.AxisLabelsTop = []
    XYChartView1.AxisLabelFont = ['Arial', '12', '0', '0', 'Arial', '12', '0', '0', 'Arial', '12', '0', '0', 'Arial', '12', '0', '0']
    XYChartView1.ShowAxisLabels = [1, 1, 1, 1]
    XYChartView1.AxisLabelNotation = [0, 0, 0, 0]
    XYChartView1.AxisLabelPrecision = [2, 2, 2, 2]
    XYChartView1.AxisGridColor = [0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996, 0.94999999999999996]
    XYChartView1.ChartTitle = ''
    XYChartView1.AxisLabelsRight = []
    XYChartView1.AxisBehavior = [0, 0, 0, 0]
    XYChartView1.AxisTitle = ['', '', '', '']
    XYChartView1.ChartTitleFont = ['Arial', '14', '0', '0']
    XYChartView1.AxisLogScale = [0, 0, 0, 0]
    
    input = CreateProducer( datadescription, "input" )
    
    PlotOverLine1 = PlotOverLine( guiName="PlotOverLine1", PassPartialArrays=1, Source="High Resolution Line Source" )
    PlotOverLine1.Source.Point1 = [-1.75, -1.25, 0.0]
    PlotOverLine1.Source.Point2 = [0.75, 1.25, 0.0]
    PlotOverLine1.Source.Resolution = 100
    
    a1_Iterations_PiecewiseFunction = CreatePiecewiseFunction( Points=[0.0, 0.0, 1.0, 1.0] )
    
    a1_Iterations_PVLookupTable = GetLookupTableForArray( "Iterations", 1, Discretize=1, RGBPoints=[1.8648648262023926, 0.23000000000000001, 0.29899999999999999, 0.754, 100.0, 0.70599999999999996, 0.016, 0.14999999999999999], UseLogScale=0, VectorComponent=0, NanColor=[0.25, 0.0, 0.0], NumberOfTableValues=256, ColorSpace='Diverging', VectorMode='Magnitude', HSVWrap=0, ScalarRangeInitialized=1.0, LockScalarRange=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 % 1 == 0 or datadescription.GetForceOutput() == True:
        renderviews = servermanager.GetRenderViews()
        imagefilename = "image_%t.png"
        for view in range(len(renderviews)):
            fname = imagefilename.replace("%v", str(view))
            fname = fname.replace("%t", str(timestep))
            WriteImage(fname, renderviews[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():
    iter = servermanager.vtkSMProxyIterator()
    iter.Begin()
    while not iter.IsAtEnd():
        if iter.GetGroup().find("prototypes") != -1:
            iter.Next()
            continue
        proxy = servermanager._getPyProxy(iter.GetProxy())
        proxygroup = iter.GetGroup()
        if proxygroup != 'timekeeper' and proxy != None and proxygroup.find("pq_helper_proxies") == -1 :
            return proxy
        iter.Next()

    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
