from paraview.simple import *
from paraview import coprocessing


#--------------------------------------------------------------
# Code generated from cpstate.py to create the CoProcessor.
# ParaView 4.2.0-15-g46ac001 64 bits


# ----------------------- CoProcessor definition -----------------------

def CreateCoProcessor():
  def _CreatePipeline(coprocessor, datadescription):
    class Pipeline:
      # state file generated using paraview version 4.2.0-15-g46ac001

      # ----------------------------------------------------------------
      # setup the data processing pipelines
      # ----------------------------------------------------------------

      #### disable automatic camera reset on 'Show'
      paraview.simple._DisableFirstRenderCameraReset()

      # writer for 2D rectilinear grid
      h02D = coprocessor.CreateProducer(datadescription, 'input')
      h02DWriter = servermanager.writers.XMLPUnstructuredGridWriter(Input=h02D)
      h02DWriter.FileName = 'h02D_{time}.pvtu'
      coprocessor.RegisterWriter(h02DWriter, filename=h02DWriter.FileName, freq=48)

      # writer for 3D rectilinear grid
      h03D = coprocessor.CreateProducer(datadescription, 'input3D')
      h03DWriter = servermanager.writers.XMLPUnstructuredGridWriter(Input=h03D)
      h03DWriter.FileName = 'h03D_{time}.pvtu'
      coprocessor.RegisterWriter(h03DWriter, filename=h03DWriter.FileName, freq=48)

      # writer for 2D spherical grid
      h0s2D = coprocessor.CreateProducer(datadescription, 'sinput')
      h0s2DWriter = servermanager.writers.XMLPUnstructuredGridWriter(Input=h0s2D)
      h0s2DWriter.FileName = 'h0s2D_{time}.pvtu'
      coprocessor.RegisterWriter(h0s2DWriter,
                                 filename=h0s2DWriter.FileName, freq=48)

      # writer for 3D spherical grid
      h0s3D = coprocessor.CreateProducer(datadescription, 'sinput3D')
      h0s3DWriter = servermanager.writers.XMLPUnstructuredGridWriter(Input=h0s3D)
      h0s3DWriter.FileName = 'h0s3D_{time}.pvtu'
      coprocessor.RegisterWriter(h0s3DWriter,
                                 filename=h0s3DWriter.FileName, freq=48)

    return Pipeline()

  class CoProcessor(coprocessing.CoProcessor):
    def CreatePipeline(self, datadescription):
      self.Pipeline = _CreatePipeline(self, datadescription)

  coprocessor = CoProcessor()
  # these are the frequencies at which the coprocessor updates.
  freqs = {'input': [24],
           'input3D': [24],
           'sinput': [24],
           'sinput3D': [24]}
  coprocessor.SetUpdateFrequencies(freqs)
  return coprocessor

#--------------------------------------------------------------
# Global variables that will hold the pipeline for each timestep
# Creating the CoProcessor object, doesn't actually create the ParaView pipeline.
# It will be automatically setup when coprocessor.UpdateProducers() is called the
# first time.
coprocessor = CreateCoProcessor()

#--------------------------------------------------------------
# Enable Live-Visualizaton with ParaView
coprocessor.EnableLiveVisualization(True, 1)


# ---------------------- Data Selection method ----------------------

def RequestDataDescription(datadescription):
    "Callback to populate the request for current timestep"
    global coprocessor
    if datadescription.GetForceOutput() == True:
        # We are just going to request all fields and meshes from the simulation
        # code/adaptor.
        for i in range(datadescription.GetNumberOfInputDescriptions()):
            print('input dname %s' % datadescription.GetInputDescriptionName(i))
            datadescription.GetInputDescription(i).AllFieldsOn()
            datadescription.GetInputDescription(i).GenerateMeshOn()
        return

    # setup requests for all inputs based on the requirements of the
    # pipeline.
    coprocessor.LoadRequestedData(datadescription)

# ------------------------ Processing method ------------------------

def DoCoProcessing(datadescription):
    "Callback to do co-processing for current timestep"
    global coprocessor

    # Update the coprocessor by providing it the newly generated simulation data.
    # If the pipeline hasn't been setup yet, this will setup the pipeline.
    coprocessor.UpdateProducers(datadescription)

    # Write output data, if appropriate.
    coprocessor.WriteData(datadescription);

    # Write image capture (Last arg: rescale lookup table), if appropriate.
    coprocessor.WriteImages(datadescription, rescale_lookuptable=False)

    # Live Visualization, if enabled.
    coprocessor.DoLiveVisualization(datadescription, "localhost", 22222)
