#!/usr/bin/env python
"""
Viper: A simple mesh plotter and run--time visualization module for plotting
and saving simulation data. The class C{Viper} can visualize solutions given
as numpy arrays, and meshes that provide the two methods C{cells()} and
C{coordinates()}. These methods should return numpy arrays specifying the
node-element ordering and coordinates of the nodes, respectively.

To use the module as a script, the syntax is:
viper.py -i <infile.xml[.gz]> [-o <savefile>]

or,
viper.py --input <infile.xml[.gz]> [--output <savefile>]

If the optional command line argument -o is given, the mesh is stored in vtk
format.
"""

from viper import *
import sys
import getopt
import numpy
import os

def _dolfin_mesh_plotter(infile, outfile):
    from dolfin import Mesh, UnitSquare
    if infile:
        mesh = Mesh(infile)
    else:
        mesh = UnitSquare(10,10)
    n = mesh.numVertices()
    x = numpy.zeros((n,), dtype='d')
    p = Viper(mesh, x, 0, 1)
    p.update()
    if outfile: 
        p.init_writer("")
        p.write_vtk(os.path.splitext(os.path.splitext(outfile)[0])[0]+".vtk")
    p.interactive()
    return p

def _vtk_plotter(infile):
    p = Viper()
    p.init_from_file(infile)
    p.interactive()
    return p

def _main(infile, outfile):
    if infile:
        suffix = os.path.splitext(infile)[-1]
        if suffix == ".vtk":
            return _vtk_plotter(infile)

    return _dolfin_mesh_plotter(infile, outfile) 
        
def _help():
    print """
Usage:
viper -i file -o file
"""

if __name__ == '__main__':
    import sys
    (opts, args) = getopt.getopt(sys.argv[1:], "i:o:h", ["input=", "output=", "help"])
    infile = None
    outfile = None
    for (opt, val) in opts:
        if (opt == "-input" or opt == "-i"):
            infile = val
        elif (opt =="-output" or opt == "-o"):
            outfile = val
        elif (opt == "-help" or opt == "-h"):
            _help()
            sys.exit()
    if infile == outfile == None:
        if len(args) > 0:
            _main(args[-1], None)
            sys.exit()

    p = _main(infile, outfile)
