#!/bin/csh
######################################
# SN collapse model
# $theTab = iso/lf (isochrones / luminosity function)
# NEED the Temperature / Yp
# X       = Quantity plotted along X-axis
# Y       = Quantity plotted along Y-axis
######################################

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

set age = "7=U;8=B;9=cyan3;10=V;12=R;14=I"
if ($theTab =~ iso*) then	# iso-density
    set tit1 = "Isochrones"
    set M = "Mini"  		# Parameter required
else
    set tit1 = "LF"
    set M = ""
endif

if ("$1" =~ LIST_X*=* ) then
    echo "list=$1" | acut -d= -f1,3- >> $tt; shift
else	 # Get the list of parameters
    preadme -csv ./ReadMe | gawk -v T="$theTab" 'BEGIN{FS="\t"}\
    { if(index($1,T)!=1) next;		# Look for list of parameters\
      if($3=="t") next; if($3=="Z") next; \
      if(n>0) {a = a ";"; u = u ";" } n++; a = a $3; u = u $4 }\
      END { print "list=" a; print "units=" u }' >> $tt
endif

if ("$1" =~ LIST_Y=* ) then
    shift
endif

if ("$1" =~ X*=*) then
    echo "X=$1" | acut -d= -f1,3- >> $tt; shift
else if ("$theTab" =~ iso* ) then
    set X = "log(Te)"
else
    set X = "KMAG"
endif

if ("$1" =~ Y=*) then
    echo "$1" >> $tt; shift
else if ("$theTab" =~ iso* ) then
    set Y = "log(L)"
else
    set Y = "logN"
endif

# 1st condition
if ("$1" =~ LIST_Z=*) then
    echo "Zlist=$1" | acut -d= -f1,3- >> $tt; shift
else
    set Zlist = "0.0003;0.008;0.02;all"
endif
if( "$1" =~ "Z="* ) then
    echo "Z=$1" | acut -d= -f1,3- >> $tt; shift
else
    set Z = "all"
endif

# 2nd condition
#echo2 "#...argument '$1' with $iso=*"
if ("$1" =~ LIST_t=*) then
    echo "tlist=$1"  | acut -d= -f1,3- >> $tt; shift
else
    set tlist = "7;8;9;10;12;14;7,8,10,12,14"
endif
if ("$1" =~ t=*) then
    echo "t=$1" | acut -d= -f1,3- >> $tt; shift
else
    set t = "7,8,10,12,14"
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 "\subsection*{$tit1 for age `graph_title '$age'`Gyr,"
    echo1 " \quad Z=0.003, 0.08(dotted), 0.02(=\sun,dashed)}"
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
      "@X=$list" "@Y=$list" X="$X" Y="$Y" "@Z=$Zlist" Z="$Z" "@t=$tlist" t="$t"\
      -graph $argv:q
    #echo1 "\underline{Values of Z}: 66 values from 0.01 to 0.65 (step of 0.01)"
    #echo1 "\br"
    #echo1 "\underline{Values of $iso}:"
    #if ( -r $iso.lis) then
    #    cat $iso.lis
    #else
    #    echo "110 values from 5.1 to 16.0 (step of 0.1)"
    #endif
    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.2 \
                 --title-font-size 0.05 )  #-S 16 0.02 )

# Find units
(echo "$list"; echo "$units"; echo "$X"; echo "$Y") | gawk -F\; -v q="'" '\
   NR==1{for(i=1;i<=NF;i++){I[$i] = i; I[i]=$i}} # Names\
   NR==2{for(i=1;i<=NF;i++) L[I[i]] = sprintf("%s [%s]", I[i], $i)} # Units \
   NR==3{print "set Xlab = " q L[$1] q }\
   NR==4{print "set Ylab = " q L[$1] q }' > $tt
echo2 "#... Executing $tt" ; cat2 $tt
source $tt ; rm -f $tt

#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}' \
set Zcond = "Z=$Z"
if ("$Z" == "all") set Zcond = ""
set x = "";  set y = ""
if ("$Xlab" =~ *mag*) set x = (-x - - - )
if ("$Ylab" =~ *mag*) set y = (-y - - - )
set colors = "1=purple3:2=blue2:3=green3:4=red:5=red4"
if ("$t" =~ *9*) set colors = `echo $colors | sed s/blue2/cyan3/`
echo2 "#...asu -data -source=$theCat/$theTab -out='Z t $M $X $Y' $Zcond 't=$t'"
asu -data -source=$theCat/$theTab -out.max=9999 \
    -out="Z t $M $X $Y" $Zcond "t=$t" \
| sort -n \
| tee /tmp/aa549.102.gr \
| gawk 'BEGIN{FS="\t"; at[7]=1;at[8]=2;at[9]=2;at[10]=3;at[12]=4;at[14]=5} \
  { if($1!=Z) { Z=$1; z=Z+0; if(z<0.001) z=0; else if (z<0.01) z=5; else z=10;M=z+t}\
    if($2!=T) { T=$2; t=T+0; M=at[t]+z }\
    if(m!=M) { m=M; print "# m = " m }\
    print $(NF-1),$NF }' \
| tee /tmp/aa549.102.gr1 \
| graph -T $Vgraph -C $plotarg \
   -X "$Xlab:q" -Y "$Ylab:q" \
  --pen-colors "$colors" $x $y $argv:q
exit 0
