#!/bin/csh
######################################
# Profile (various parameters + filters)
# $theTab = Model (eos2 eos3)
# NEED the Temperatture / Yp
# PA      = position angle
# Y       = Quantity plotted along Y-axis
######################################

#set verbose
set tt = /tmp/set$$ ; touch $tt

if ("$1" =~ LIST_Y=* ) then
    echo "$1" >> $tt; shift
else
    preadme -csv ./ReadMe | gawk 'BEGIN{FS="\t"}\
    /Hb2/ { next }\
    /^table3.dat/ {if(index("FHz", substr($3,1,1))==0) next; # bad column\
       if(n>0) {a = a ";"; u = u ";" } n++; a = a $3; u = u $4 }\
      END { print "LIST_Y=" a; print "units=" u }' >> $tt
endif
if ("$1" =~ Y*=*) then
    echo "Y=$1" | acut -d= -f1,3- >> $tt; shift
else 
    echo "Y=z" >> $tt
endif

if ("$1" =~ LIST_PA*=* ) then
    echo "$1" >> $tt; shift
else
    echo "LIST_PA=68=red3;131=green4;164=blue3;all" >> $tt
endif
if ("$1" =~ PA=*) then
    echo "$1" >> $tt; shift
else
    echo "PA=all" >> $tt
endif

# Convert to csh
acut -d= -i"set " -f1 -i" = '" -f2- -i"'" $tt > $tt.s
#if (! $?Vgraph) then
#    echo1 "\begin{verbatim}"
#    cat $tt ; echo "" ; cat $tt.s
#    echo1 "\end{verbatim}"
#endif
source $tt.s ; rm -f $tt $tt.s


#################################################################
# Vgraph not defined ==> just the text for the plot
#################################################################

if (! $?Vgraph) then
  echo1 "\section*{Profile of {\fg{red}$Y} for PA(\d)=`graph_title '$LIST_PA'`}"
  cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
    "@Y=$LIST_Y" Y="$Y" "@PA=$LIST_PA" "PA=$PA" -graph $argv:q
  exit 0
endif

################################################################
# Actual gif or ps plot (Vgraph contains gif or plot)  $1=m or c
# Several ordfers -- insert blank line
################################################################

set  plotarg = ( -f 0.05 -h 0.85 -w 0.75 -u 0.125 -r 0.22 \
                 --title-font-size 0.05 -S 16 0.02 )

# Find units
if ("$Y" == "Hb") then
    set u = " [mW/m\sp2\ep/\oA]"
else if ("$Y" =~ [FH]*) then
    set u = "/H\*b"	# Relative to Hbeta
else
    set u = ""
endif

#echo2 "#...asu -dat -source=$theCat -out.max=999 -out='$X $Y' 'Yp=$Yp' '$iso=$isoval'"
#| gawk '{ if($1!=M) { if(n>0) n=0; else n=1; M=$1; if(n==0) next; \
#          if(NR>1) print ""} if(n>0) print $2 "\t" $3}' \
tabmap -data -ascii "PA Dist $Y" ./table3.dat \
| tee /tmp/gAJ.143.98 \
| gawk -v pa="$PA" 'NF==3{\
  if($1 != PA) { m++; PA=$1; printf "# m = %d\n",m; } \
  if((pa!="all")&&(pa!=PA)) next; \
  printf "%7s %7s\n", $2, $3 }' \
| graph -T $Vgraph -C $plotarg \
   -X "r [arcsec]" -Y "$Y$u" --pen-colors "`graph_color '$LIST_PA'`" $argv:q
