#!/usr/bin/perl -w
#/*@@
#  @file      ascii1Dtoxvs.pl
#  @date      Sun 23 Nov 2003
#  @author    Thomas Radke
#  @desc
#             Reads datasets from a list of ASCII input files (as generated
#             by the Cactus IOASCII thorn) and sends them to an xvs server
#             for further processing and visualization.
#
#             The input files are assumed to contain one-dimensional datasets,
#             one dataset per timestep, each timestep prepended by a comment
#             line '(#|")Time = <time>' and terminated by a blank line.
#  @enddesc
#  @version   $Header$
#@@*/


# print usage message when called with no arguments
if ($#ARGV < 0)
{
  print STDERR "Usage: $0 <list of 1D ASCII files to be sent to xvs>\n\n";
  exit (0);
}

# check the XVSHOST environment setting
if (! $ENV{'XVSHOST'})
{
  print STDERR "Environment variable XVSHOST not set. Defaulting to " .
               "'localhost'...\n";
  $ENV{'XVSHOST'} = 'localhost';
}

# now loop over all input files
while ($infile = shift)
{
  # open the file, take its name as the default label for xvs
  open (INFILE, $infile) ||
    (print STDERR "Couldn't open input file '$infile'\n" && next);
  $label = $infile;

  # loop over the contents of the file
  while (<INFILE>)
  {
    # replace the label if a corresponding comment line is found
    $label = $2 if (/^("|#)y-label (.*)/);

    # continue to next line if the current one indicates the start of a dataset
    next if (! /^("|#)Time = (.*)/);
    $time = $2;

    # dump away the dataset piping it into xvs
    open (OUTFILE, "| xvs put '$label' '$time'") ||
      die "Failed to run xvs command\n\n";
    while (<INFILE>)
    {
      last if ($_ eq "\n");
      print OUTFILE;
    }
    close (OUTFILE);
  }

  close (INFILE);
}

1;
