#!/bin/csh -f
# invokes mathematica or gnuplot  to plot data files
### the major problem at UMD was that 'if(' should be 'if ('
### ie., with a blank in between!....
#
# 8/99 addition: it does a linear fit using robust methods and chopping
#                the flat parts of a box-count plot
#

# the -slope flag causes the corresponding regression line to be printed

set pgname = `basename $0 `
set errstr = "USAGE: $pgname [-g] [-yl] [-xl] [-xlabel xlabel] [-ylabel ylabel] [-l label] [-s style] [-slope] [-o outfile] datafile "
# set errstr = "USAGE: $pgname [-g] [-yl] [-xl] [-slope] datafile "
# set errstr = "USAGE: \[ -yl \] "
set logy = 0
set logx = 0
set grid = 0
set xlabel = ""
set ylabel = ""
set style  = linespoints
set withinflags = 1
set slope = 0
set outfile = "junk.eps"
set label = " "
set tmp = _tmp.$$

while ( ($#argv > 0) && ( $withinflags > 0 ) )
    # strip flags
    echo "processing |$1| ..." ##########TESTING
    # exit
    if ( "$argv[1]" =~ -* ) then
	echo "$1 is a flag"

	switch( $1 )
	case "-g": # set grid on
	    set grid = 1
	    shift
	breaksw
	case "-slope": # print regression line
	    set slope = 1
	    shift
	breaksw
	case "-yl": # log y plot
	    set logy = 1
	    shift
	breaksw
	case "-xl":
	    set logx = 1 # log log plot
	    shift
	breaksw
	case "-s":
	    echo new TESTING $*
	    shift
	    echo new TESTING $*
	    if ( $#argv < 1 ) then
		echo " $errstr "
	 	exit(-1)
	    else
	 	set style = $1
		echo style is $style
	 	shift
	    endif
	breaksw
	case "-xlabel":
	    shift
	    if ( $#argv < 1 ) then
		echo " $errstr "
	 	exit(-1)
	    else
	 	set xlabel = "$1"
	 	shift
	    endif
	breaksw
	case "-ylabel":
	    shift
	    if ( $#argv < 1 ) then
		echo " $errstr "
		exit(-1)
	    else
		set ylabel = "$1"
		shift
	     endif
	breaksw
	case "-l":
	    shift
	    if ( $#argv < 1 ) then
		echo " $errstr "
		exit(-1)
	     else
		set label = "$1"
		shift
	     endif
	breaksw
	case "-o":
	    shift
	    if ( $#argv < 1 ) then
		echo " $errstr "
		exit(-1)
	     else
		set outfile = $1
		shift
	     endif
	breaksw
	###################

	default:
	    echo "*** ${pgname}:  illegal flag (${1}) - ignored" > /dev/tty
	    shift
	breaksw
	endsw
    else 
	echo "$1 is NOT a flag"
	echo "    should stop stripping flags now"
	set withinflags = 0
    endif
end

echo "TESTING: args left: |$*|" #######testing

if ( $#argv == 0 ) then
    echo TESTING $* #######testing
    echo " ${errstr} "
    echo "*** needs datafiles "
    exit(-1)
else
    # echo -n "xl=" $logx "yl=" $logy "outfile=" $outfile 
    # echo -n " label= " $label
    # echo " data=" "$*"
    echo "$pgname building $outfile "
    \rm -f $tmp ; touch $tmp
    echo "set data style  $style " >> $tmp
	# all data files plotted with lines+points
    echo "set xlabel " \"$xlabel\" >> $tmp
    echo "set ylabel " \"$ylabel\" >> $tmp
    echo "set output " \"$outfile\" >> $tmp
    # echo "set terminal postscript 22 " >> $tmp
    echo "set terminal postscript eps 22 " >> $tmp
    # echo "set terminal postscript portrait" >> $tmp
    # echo "set size .7, .7 " >> $tmp
    # use the default size!
    # echo "set size 1.4, 1.4 " >> $tmp
    echo "set title " \'$label\' >> $tmp
    # echo "set terminal postscript portrait" >> $tmp
    # echo "set terminal latex" >> $tmp
    if ( $grid ) then
	echo "set grid" >> $tmp
    endif
    if ( $logx ) then
	echo "set logscale x" >> $tmp
    endif
    if ( $logy ) then
	echo "set logscale y" >> $tmp
    endif
    echo -n "plot " >> $tmp
    echo  -n \"$1\" >> $tmp
    if ( $slope ) then
    	set regeq = ` killFlats.pl $1 | fit.pl `
    	# echo -n "replot "  >> $tmp
    	echo -n "," >> $tmp
	# echo -n $regeq | slope2eq >> $tmp
	echo -n "$regeq[4]" " + x * ( " $regeq[2] ")"  >> $tmp
    endif
    shift
    foreach i ( $* )
	echo -n "," \"$i\" >> $tmp
    	if ( $slope ) then
    	    set regeq = ` killFlats.pl $i | fit.pl `
    	    # echo -n "replot "  >> $tmp
	    echo -n "," >> $tmp
	    echo -n "$regeq[4]" " + x * ( " $regeq[2] ")"  >> $tmp
    	    # echo -n $regeq | slope2eq >> $tmp
	endif
    end
    # if ( $slope ) then
	# echo "replot $regeq " >> $tmp
    # endif
    echo "" >> $tmp
    echo "quit" >> $tmp
    # exit ##

    gnuplot < $tmp

    \rm -f $tmp

endif
