#!/usr/bin/env python

# This script reads Multivac ouput file(s) and generates
# an image in "png" format that shows all fronts.

# Usage:
#
# 1) multivac_png [distance] [scale] [output file]
# 2) multivac_png [curves] [distance] [scale] [output file]
# 3) multivac_png [curves] [curves lenghts] [distance] [scale] [output file]
#
# [distance]: characteristic distance over which two contiguous
#   points are assumed to be on two different fronts. If the
#   simulation was performed on an orthogonal mesh, the distance
#   should be: \sqrt{delta x^2 + delta y^2}.
# [scale]: number that determines the size of the image.
#    Usually, 1 is sufficient.
# [output file]: image in "png" format.
# [curves]: Multivac output (file) that stores front points.
#    Default: "Curves".
# [curve lengths]: file containing the number of points that
#   define a front or a set of fronts. This helps splitting
#   points in file [curves]. Default: "CurveLengths".

import fileinput, glob, os, sys
# Number of arguments.
argc = len(sys.argv) - 1;

# Checks the number of arguments.
if ( (argc<3) or (argc>5) ):
    usage = "Usage:\n\n1) multivac_png [distance] [scale] [output file]\n2) multivac_png [curves] [distance] [scale] [output file]\n3) multivac_png [curves] [curves lenghts] [distance] [scale] [output file]\n\n[distance]: characteristic distance over which two contiguous points are assumed to be on two different fronts. If the simulation was performed on an orthogonal mesh, the distance should be: \sqrt{delta x^2 + delta y^2}.\n[scale]: number that determines the size of the image. Usually, 1 is sufficient.\n[output file]: image in \"png\" format.\n[curves]: Multivac output (file) that stores front points. Default: \"Curves\".\n[curve lengths]: file containing the number of points that define a front or a set of fronts. This helps splitting points in file [curves]. Default: \"CurveLengths\".\n"
    print "\nScript \"", sys.argv[0], "\" reads Multivac ouput file(s) and generates an image in \"png\" format that shows all fronts.\n\n", usage
    sys.exit(0)

# Scale of the image.
scale = sys.argv[argc-1]
# Output file.
output = sys.argv[argc]

# Temporary file storing output of "multivac_format".
i = 0
while (os.path.exists("/tmp/png_" + str(i))):
    i = i + 1
temp = "/tmp/_get_png_" + str(i)

# Generates curves, i.e. splits points.
# It uses program "multivac_format".

if (argc==3):
    os.system(" ".join(["../Display/multivac_format Curves", sys.argv[1], temp]))
elif (argc==4):
    os.system(" ".join(["../Display/multivac_format", sys.argv[1], sys.argv[2], temp]))
elif (argc==5):
    os.system(" ".join(["../Display/multivac_format", sys.argv[1], sys.argv[2], sys.argv[3], temp]))

# Temporary file for Gnuplot commands.
i = 0
while (os.path.exists("/tmp/_gnuplot_file_" + str(i))):
    i = i + 1
gnuplot_file_name = "/tmp/_gnuplot_file_" + str(i)

# Gnuplot commands.
gnuplot_file = open(gnuplot_file_name, 'w')
#gnuplot_file.write("set size square\nset size " + str(float(scale)/640.*480.) + ", " + scale + "\nset size ratio 1\nset nokey\nset terminal png medium color \nset output \"" + output + "\"\nplot \"" + temp + "\" with lines")

gnuplot_file.write("set size square\nset size " + str((float(scale)/640*480.)*512./480.) + ", " + str(float(scale)*512./480.) + "\nset size ratio 1\nset nokey\nset noborder\nset terminal png\nset bmargin 0\nset tmargin 0\nset lmargin 0\nset rmargin 0\nset output \"" + output + "\"\nplot [0:3][0:3] \"" + temp + "\" with lines")
gnuplot_file.close()

# Launches Gnuplot.
os.system("gnuplot /tmp/_gnuplot_file_" + str(i))

# Removes temporary files.
os.system(" ".join(["\\rm -rf", temp, gnuplot_file_name]))
