#!/usr/local/bin/perl

$selfdoc=
'Usage:  < data mma2sep > data.H
 
  Pearl script which translates Mathematica output into seplib 
  file format. 
 
  Your mathematica script computes a plot "g" (not density plot).
  You need the command 
  
        InputForm[Show[g]]  >> ./data 
 
  in your Mathematica script. It writes the list of point coordinates of
  plot g into the unix file "data".
   
  The filter mma2sep converts then the list of point coordinates from 
  Mathematica internal representation to seplib header and data file
  format. 

  kewords: Mathematica mma2sep

';
  
if ($#ARGV<0) { print "$selfdoc"; exit(1) ;}

open(ATOF,"|atoF >junktemp.strip") || die "could not open atoF\n";
#open(ATOF,"|more") || die "could not open atoF\n";
$ncurves=0;

line:
while (<STDIN>) {
    if (/Line\[\{\{(.*)/) {$line = "$line"."$1" ;
     while(<STDIN>) {
	  if (/(.*)\}\}\]/) {  $line = "$line"."$1";      
	      $_ = $line ; s/[\{\}\,\n]//g ; s/\s+/ /g;  
			   s/\*10\^/e/g ; print ATOF "$_  "; 
                 $npoints=split(/ /,$_); $npoints=$npoints/2; 
                 $line=""; push(@n1array,$npoints);
                 $ncurves++; next line;}
          else {  $line = "$line"."$_" ;}
     }
    }
} ;


close(ATOF);

open(TEMPIN,"<junktemp.strip") || die "lost tempfile\n";
while (<TEMPIN>) { print ; }
close(TEMPIN);


$n1 = `Get <junktemp.strip n1` ; $_ = $n1; s/n1=//g; $n1=$_; $n1=$n1/2/$ncurves ;

print "n1=@n1array[0]";
for $i (1..$#n1array) {print ",@n1array[$i]" ; }  print "\n";

print "n2=$ncurves\n";
print "esize=8\n";

system("/bin/rm junktemp.strip");


