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

import os

from subprocess import *

in_filename = "./track3p_results/OUTPUT/resonantparticles"
#in_filename = "./track3p_3T/OUTPUT/resonantparticles"
#in_filename = "./track3p_MagField/OUTPUT/resonantparticles"
print("in_filename: ", in_filename)


########################################

def get_resonant_data(filename):
  try:
    ifile = open(filename, 'r')
  except:
    print ("error: unable to open file", filename, "for reading.")
    return

  resonant_out = CSVReader(FileName=filename,
                          FieldDelimiterCharacters = ' ',
                          MergeConsecutiveDelimiters = 1,
                          HaveHeaders = 1)

  # this is necessary!  blank objects otherwise...
  resonant_out.UpdatePipeline()

  return (resonant_out)


def show_impact_energy(resonant_out):
  XYChartView1 = CreateXYPlotView()
  XYChartView1.ViewTime=0.0
  XYChartView1.ShowLegend = 0
  XYChartView1.ChartTitle = "Impact Energy vs. Field Gradient"

########  XYChartView1.AxisTitle = ['Field Gradient', 'Impact Energy [eV]', '', '']
  XYChartView1.BottomAxisTitle = ['Field Gradient']
  XYChartView1.LeftAxisTitle = ['Impact Energy [eV]']

  ######################################

  animscene = GetAnimationScene()
  animscene.ViewModules = [ XYChartView1 ]

  #######################################

  pd1 = PlotData(resonant_out)
  pd1.UpdatePipeline()

  #######################################

  pr1 = GetDisplayProperties(pd1)
  pr1.XArrayName = 'Field_Level'
  pr1.UseIndexForXAxis = 0
  pr1.AttributeType = 'Row Data'
  pr1.SeriesVisibility = ['Energy', '1']
  pr1.SeriesLineStyle = ['Energy', '0']
  pr1.SeriesMarkerStyle = ['Energy', '1']

  pr1.SeriesColor = ['Field_Level', '0', '0', '0',
                     'ID', '0.894118', '0.101961', '0.109804',
                     'Resonant_X', '0.215686', '0.494118', '0.721569',
                     'Resonant_Y', '0.301961', '0.686275', '0.290196',
                     'Resonant_Z', '0.596078', '0.305882', '0.639216',
                     'Initial_X', '0.976471', '0.513725', '0.141176',
                     'Initial_Y', '1', '0.898039', '0.0235294',
                     'Initial_Z', '1', '0.705882', '0',
                     'Total_time', '0.231373', '0.407843', '0.670588',
                     'Total_Num', '0.603922', '0.407843', '1',
                     '', '0', '0', '0',
                     'Energy', '0.666667', '0', '0',
                     'vtkOriginalIndices', '0', '0', '0']

  pr1.UpdatePipelineInformation()
  pr1.UpdatePipeline()
  pr1.Visibility = 1

  Render()

def show_impact_locations(resonant_out):
  TableToPoints1 = TableToPoints(resonant_out)
  TableToPoints1.XColumn = 'Resonant_X'
  TableToPoints1.YColumn = 'Resonant_Y'
  TableToPoints1.ZColumn = 'Resonant_Z'
  TableToPoints1.UpdatePipeline()

  DataRepresentation2 = Show(TableToPoints1)

  Render()


################################################################################

paraview.simple._DisableFirstRenderCameraReset()

SetActiveView(None)

#filename = get_filename()
filename = in_filename 

if filename == "":
  print ("Resonant particles macro cancelled.")
else:
  print("filename:", filename)

resonant_out = get_resonant_data(filename)

# ----------------------------------------------------------------
# setup view layouts
# ----------------------------------------------------------------

show_impact_locations(resonant_out)
#lineChartView2 = show_impact_energy(resonant_out)
#layout1 = CreateLayout(name='wakeplot')
#layout1.SplitVertical(0, 0.500000)
#layout1.AssignView(1, lineChartView1)
#layout1.AssignView(2, lineChartView2)

# ----------------------------------------------------------------
# restore active view
#SetActiveView(lineChartView2)

#os.remove(tmp_filename)
Render()
