#!/bin/sh
DATE="06/10/1998"
MYCOMMAND=/usr/local/share/vecfem3/vempfade
#**************************************************************************
#**                                                                     ***
#**        Copyrights University of Karlsruhe, 1996                     ***
#**        Program by L. Grosz                                          ***
#**                                                                     ***
#**************************************************************************
#**                                                                     ***
if [ $# -lt 1 ]
 then
   echo "illegal number of arguments:" >&2
   echo "call:  vempost <workpiece>" >&2
   exit 1
fi
echo "VECFEM: creates postprocessor script"
echo "Version: $DATE"
echo "Copyrights by University of Karlsruhe 1996"
#
#---------------------------------
#
#** find the vecfem variables
#
if [ "$MYCOMMAND" = "" ] ; then
  . vempfade
  echo vempfade from `dirname $(which vempfade)`
else
  VEMPATH=`dirname $MYCOMMAND`
  SC=`basename $MYCOMMAND`
  if test -r $VEMPATH/vempfade ; then
     . $VEMPATH/vempfade
     echo vempfade from $VEMPATH
  else
    HELP=`which $SC`
    VEMPATH=`dirname $HELP`
    if test -r $VEMPATH/vempfade ; then
      . $VEMPATH/vempfade
      echo vempfade from $VEMPATH
    else
      . vempfade
      echo vempfade from `dirname $(which vempfade)`
    fi
  fi
fi
#
#---------------------------------
EXT=$$
WORKPIECE=$1

test -r $WORKPIECE.lst
if [ $? -eq 1 ]
  then
  echo ">> input file "$WORKPIECE".lst was not found !" >&2
  echo ">> abend !" >&2
  exit 1
fi
#
#-------------------------------------------------------------------------
#
#  find important values of file list file
#
NHEAD=`$VECFEM_AWK 'BEGIN{getline;split($0,ARG," ");
		  nhead=ARG[1]+1
    	          c=0; while (c < ARG[1]) {getline;c++}
                  getline;split($0,ARG," ")
		  nhead=nhead+4+ARG[1]
		  print nhead
		  exit }' $WORKPIECE.lst`

PRE=`$VECFEM_AWK 'BEGIN{getline;split($0,ARG," ")
		 print ARG[7]
		 exit}' $WORKPIECE.lst`

POST=`$VECFEM_AWK 'BEGIN{getline;split($0,ARG," ");pre=ARG[7]
    	          c=0;while (c < ARG[1]) {getline;c++}
                  getline;n=split($0,ARG," ")
		  if (n>6) {print ARG[7]} else {print pre}
		  exit }' $WORKPIECE.lst`
case $POST in
#
#-------------------------------------------------------------------------
#
#  start ensight format:
#
ensight)  
touch /tmp/scalar.$EXT /tmp/vector.$EXT;
C0=`$VECFEM_AWK 'BEGIN{c=0;while (c < '$NHEAD') {getline;c++}
	       FS="|"
	       C=0;C1=0} {
         
         gsub(" ","",$1)
         gsub(" ","",$2)
         gsub(" ","",$6)
	 gsub(" ","",$5) 
         if (C != $1) { printf "%s ",$2 >> "/tmp/time.'$EXT'"
		        C=$1;C1++}
         if (C1 == "1") { 
           if ($4 == "1") 
	         { printf "%s %s\n",$6,$5 >> "/tmp/scalar.'$EXT'"}
           else  { printf "%s %s\n",$6,$5 >> "/tmp/vector.'$EXT'"}
         C0=$1}}
	 END{print C0
	     printf "\n" >> "/tmp/time.'$EXT'"}' $WORKPIECE.lst`;

NTIME=`wc -w < /tmp/time.$EXT`;
echo `wc -l < /tmp/scalar.$EXT`" "`wc -l < /tmp/vector.$EXT`" 0" > $WORKPIECE.result;
echo $NTIME >> $WORKPIECE.result;
cat /tmp/time.$EXT >> $WORKPIECE.result;
if [ $NTIME -eq 1 ]
 then
   cat /tmp/scalar.$EXT >> $WORKPIECE.result
   cat /tmp/vector.$EXT >> $WORKPIECE.result
else
   echo $C0" 1" >> $WORKPIECE.result
   cat /tmp/scalar.$EXT /tmp/vector.$EXT |
   $VECFEM_AWK 'BEGIN{FS=" "}
	{H=substr($1,1,length($1)-3)
         printf "%s*** %s\n",H,$2}' >> $WORKPIECE.result
fi;
echo "The EnSight result file is "$WORKPIECE.result"."
echo "The geometry file is "`$VECFEM_AWK 'BEGIN{getline;split($0,ARG," ");pre=ARG[7]
    	                            c=0;while (c < ARG[1]+2) {getline;c++}
                                    gsub(" ","",$0)
				    print $0; exit }' $WORKPIECE.lst`"."
rm -f /tmp/*.$EXT;
;;
#
#-------------------------------------------------------------------------
#
#  avs-ucd
#
AVS-UCD | avs-ucd )
touch /tmp/ucdlst.$EXT 
C0=`$VECFEM_AWK 'BEGIN{c=0;while (c < '$NHEAD') {getline;c++}
	       FS="|"
	       C=0;C0=0} {
         
         gsub(" ","",$1)
         gsub(" ","",$4)
	 gsub(" ","",$5) 
         gsub(" ","",$6)

	 if ( $1 != C0 ) { C++; C0=$1 } 
	 
	 if ( C == 1 ) { 
	   if ($1 == "-1") { 
	     if ($4 < 2 ) 
	       { printf "%s,%s,scalar,steady\n",$6,$5 >> "/tmp/ucdlst.'$EXT'"}
	     else { if ($4 < 4 )
	        { printf "%s,%s,vector,steady\n",$6,$5 >> "/tmp/ucdlst.'$EXT'"}}
           } else {
	     if ($4 < 2 ) 
	       { printf "%s,%s,scalar,series\n",$6,$5 >> "/tmp/ucdlst.'$EXT'"}
	     else  { if ($4 < 4 )
	       { printf "%s,%s,vector,series\n",$6,$5 >> "/tmp/ucdlst.'$EXT'"}}
	   } }

         }
	 END{print C0}' $WORKPIECE.lst`;

for i in `cat /tmp/ucdlst.$EXT`
do 
   FILE=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $1}'`
   DATASET=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $2}'`
   NET=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $3}'`
   SERIES=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $4}'`
   if  test -r $FILE ; then
     if [ $SERIES = "series" ]; then
        FILE=`echo $FILE  |  sed 's/\.[0-9]*$//'`
     fi
     NETFILE=$VECFEM_ROOT/share/vecfem3/avs$NET$SERIES.net
     INFILE=`echo $PWD/$FILE | sed 's/\//\\\\\//g'`
     sed 's/%UCDFILE_/'$INFILE'/g
  	  s/%DATASET_/'$DATASET'/g
	  s/%NSTEP_/'$C0'/g' $NETFILE > $WORKPIECE.$DATASET.net
     echo "net file for output set "$DATASET" is "$WORKPIECE"."$DATASET".net !"
   fi
done
rm -f /tmp/*.$EXT;
;;
#
#-------------------------------------------------------------------------
#
#  data explorer
#
dataexplorer )
DXFILE=`$VECFEM_AWK 'BEGIN{getline;split($0,ARG," ");pre=ARG[7]
    	           c=0;while (c < ARG[1]+2) {getline;c++}
                   gsub(" ","",$0)
	           print $0; exit }' $WORKPIECE.lst` ;
sed '/^end/d' $DXFILE > /tmp/dx.$EXT ;

touch /tmp/dxlst.$EXT 
C0=`$VECFEM_AWK 'BEGIN{c=0;while (c < '$NHEAD') {getline;c++}
	       FS="|"
	       C=0;C0=0} {
         
         gsub(" ","",$1)
         gsub(" ","",$4)
	 gsub(" ","",$5) 
         gsub(" ","",$6)

	 if ( $1 != C0 ) { C++; C0=$1 } 
	 
	 if ( C == 1 ) { 
	   if ($1 == "-1") { 
	     if ($4 < 2 ) 
	       { printf "%s,%s,scalar,steady\n",$6,$5 >> "/tmp/dxlst.'$EXT'"}
	     else { if ($4 < 4 )
	        { printf "%s,%s,vector,steady\n",$6,$5 >> "/tmp/dxlst.'$EXT'"}}
           } else {
	     if ($4 < 2 ) 
	       { printf "%s,%s,scalar,series\n",$6,$5 >> "/tmp/dxlst.'$EXT'"}
	     else  { if ($4 < 4 )
	       { printf "%s,%s,vector,series\n",$6,$5 >> "/tmp/dxlst.'$EXT'"}}
	   } }

         }
	 END{print C0-1}' $WORKPIECE.lst`;

for i in `cat /tmp/dxlst.$EXT`
do 
   FILE=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $1}'`
   DATASET=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $2}'`
   NET=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $3}'`
   SERIES=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $4}'`
   if  test -r $FILE ; then
     cat $FILE >> /tmp/dx.$EXT
     if [ $SERIES = "series" ]
      then
        echo "object \""$DATASET"\" class series" >>  /tmp/dx.$EXT
        $VECFEM_AWK 'BEGIN{C=0;FS=" "}{
  		   if ( ( $1 == "object") && ($4 == "field") )
                      {printf "  member %s position %s value %s\n",C,C+1,$2
		       C++}
		} ' $FILE >> /tmp/dx.$EXT
     fi
     echo $FILE" has been merged to "$DXFILE"."
     NETFILE=$VECFEM_ROOT/share/vecfem3/dx$NET$SERIES.net
     INFILE=`echo $PWD/$DXFILE | sed 's/\//\\\\\//g'`
     sed 's/%DXFILE_/'$INFILE'/g
  	  s/%DATASET_/'$DATASET'/g
	  s/%NSTEP_/'$C0'/g' $NETFILE > $WORKPIECE.$DATASET.net
     echo "net file for output set "$DATASET" is "$WORKPIECE"."$DATASET".net !"
     rm $FILE
   fi
done
echo "end" >> /tmp/dx.$EXT;
mv /tmp/dx.$EXT $DXFILE;
rm -f /tmp/*.$EXT;
;;
#
#-------------------------------------------------------------------------
#
#  start gnuplot format:
#
gnuplot)  
touch /tmp/plotlst.$EXT
$VECFEM_AWK 'BEGIN{c=0;while (c < '$NHEAD') {getline;c++}
	       FS="|"}{
         gsub(" ","",$1)
	 gsub(" ","",$5) 
         gsub(" ","",$6)
	 if ($1 == "-1") 
	    { printf "%s,%s,-1\n",$6,$5 >> "/tmp/plotlst.'$EXT'"}
         else 
	    { printf "%s,%s,%s\n",$6,$5,$1>> "/tmp/plotlst.'$EXT'"}
         }' $WORKPIECE.lst;
for i in `cat /tmp/plotlst.$EXT`
do 
   FILE=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $1}'`
   DATASET=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $2}'`
   SERIES=`echo $i | $VECFEM_AWK 'BEGIN{FS=","} {print $3}'`
   PLOTSCRIPT="$WORKPIECE"."$DATASET".gplot

   if  test -r $FILE ; then
     if [ $SERIES = "-1" ]; then
      echo "set parametric" > $PLOTSCRIPT
      echo "set xlabel \"x1\"" >> $PLOTSCRIPT
      echo "set ylabel \"x2\"" >> $PLOTSCRIPT
      echo "set zlabel \"$DATASET\"" >> $PLOTSCRIPT
      echo "set dgrid3d 5,5,4" >> $PLOTSCRIPT
      echo "set contour base" >> $PLOTSCRIPT
      echo "set title \"$DATASET\"" >> $PLOTSCRIPT
      echo "splot \"$PWD/$FILE\" using 1:2:4 with lines" >> $PLOTSCRIPT
      echo "pause -1 \"hit return to continue!\"" >> $PLOTSCRIPT
      echo "file for output set $DATASET is $PLOTSCRIPT !"
     else
      if [ $SERIES = "1" ]; then
        echo "file for output set $DATASET is $PLOTSCRIPT !"
        echo "set parametric" > $PLOTSCRIPT
        echo "set xlabel \"x1\"" >> $PLOTSCRIPT
        echo "set ylabel \"x2\"" >> $PLOTSCRIPT
        echo "set zlabel \"$DATASET\"" >> $PLOTSCRIPT
        echo "set dgrid3d 5,5,4" >> $PLOTSCRIPT
        echo "set contour base" >> $PLOTSCRIPT
      fi
      echo "set title \"$DATASET: time step $SERIES\"" >> $PLOTSCRIPT
      echo "splot '< awk \"/^ *$SERIES /\" $PWD/$FILE' using 3:4:6 with lines" >> $PLOTSCRIPT
      echo "pause -1 \"hit return to continue!\"" >> $PLOTSCRIPT
     fi
   fi
done
rm -f /tmp/*.$EXT;
;;
#
#-------------------------------------------------------------------------
#
#  start patran format:
#
patran)  
echo "Use the result template $VECFEM_ROOT/share/vecfem3/patran.vecfem.res_tmpl !"
rm -f /tmp/*.$EXT;
;;
#
#-------------------------------------------------------------------------
#
#  no work for other post processors:
#
*) echo " no work !" ;;
esac
