#!/usr/bin/env pvpython
from paraview.simple import *
import os, sys

case_dir = sys.argv[1]
out_root = sys.argv[2]

case_name = os.path.basename(case_dir)
foam = os.path.join(case_dir, case_name + ".foam")

os.makedirs(os.path.join(out_root, "contour_span"), exist_ok=True)

reader = OpenDataFile(foam)

try:
    reader.MeshRegions = ['internalMesh']
except:
    pass

try:
    reader.CellArrays = ['T','p','Qdot','kappa','U']
except:
    pass

tspan = 0.00062000
z = 0.013

sl = Slice(Input=reader)
sl.SliceType = 'Plane'
sl.SliceType.Origin = [0.06, 0.01, z]
sl.SliceType.Normal = [0, 0, 1]

c2p = CellDatatoPointData(Input=sl)

ct = Contour(Input=c2p)
ct.ContourBy = ['POINTS', 'T']
ct.Isosurfaces = [1200.0]

UpdatePipeline(time=tspan, proxy=reader)
UpdatePipeline(time=tspan, proxy=sl)
UpdatePipeline(time=tspan, proxy=c2p)
UpdatePipeline(time=tspan, proxy=ct)

out_csv = os.path.join(out_root, "contour_span", "T1200_z0.013_t0.00062000.csv")
SaveData(out_csv, proxy=ct)

print(out_csv)
