#!/bin/csh
####################################
# theTab = table name
# Argument is P=period
# Vgraph is "gif" "ps" or "txt"
####################################
set debug = 0

if ($debug) then
    touch /tmp/Vplot
    (echo ""; echo "----`date`") >> /tmp/Vplot
    printenv | sort >> /tmp/Vplot
    @ i = 0
    while ($i < $#argv)
        @ i += 1
        echo "....Arg#$i => '$argv[$i]'" >> /tmp/Vplot
    end
endif

#################################################################
# Get the Arguments: tab and Period
#################################################################

if ("$theTab" =~ table11*) then
   set Star = "SN 1994ae"
else ("$theTab" =~ table12*) then
   set Star = "SN 1998aq"
else
   set Star = `echo "$theTab" | tr '_' ' '| trim "
endif


if ("$1" =~ P=*) then
    set P = `echo "$1" | cut -c3-` ; shift
    if (("$P" == 0) && ("$1" == "-x" )) then	### REMOVE Limits 0/1
	if (("$2" == "0") && ("$3" == "1" )) then 
	    shift; shift; shift
	endif
    endif
else
    set P = ""
endif
if ("$P" == "") set P = 0 


#################################################################
# Vgraph not defined ==> just the plot introduction
#################################################################
if (! $?Vgraph) then
    if ($P != 0) then
        set plotx = "-x 0 1"
    else
        set plotx = ""
    endif
    set ploty = "-y - - -"       # Reverse Y axis
    # These values are superseded by what's contained in $argv
    if ("$theTab" =~ table1*) then
    echo1 "\section*{\object{$Star}  {\fg{purple3}U} {\fg{blue2}B} {\fg{green3}V} {\fg{red}Rc} {\fg{red4}Ic}}"
    else
    echo1 "\section*{\object{[RLS2005] $Star} {\fg{purple3}F555W} {\fg{blue2}F810W} {\fg{green3}F814W} (P=$Per)}"
    endif
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
	P=$P -graph $plotx $ploty $argv:q
    exit 0
endif

################################################################
# Actual gif or ps plot (Vgraph contains gif or plot)
################################################################

set  plotarg = \
  "-f 0.0325 -h 0.75 -w 0.75 -u 0.125 -r 0.15 --title-font-size 0.05 -m -1 -S 3"

if ($P != 0) then
    set X = "Phase"
else
    set X = "MJD"
endif
set Y = 'mag'

if ("$theTab" =~ table1*) then
asu -data -n- <<====EOQ1 | gawk -v P=$P 'BEGIN{FS="\t"}\
  /^$/{print; next}\
  /---/ {next}\
  {\
    jd = $1; mag=$2+0;\
    if (P > 0) phase = (jd /P) - int(jd/P) ; \
    else phase = jd;\
    printf("%.2f %6.3f %5.3f\n", phase, mag, $3) \
  }' \
| graph -T $Vgraph -C -L "$Star" --pen-colors 1=purple3:2=blue2:3=green3:4=red:5=red4 \
  $plotarg -X $X:q -Y $Y:q $argv:q
-source=$theCat/$theTab
-out=MJD, Umag, e_Umag
-go
-source=$theCat/$theTab
-out=MJD, Bmag. e_Bmag
-go
-source=$theCat/$theTab
-out=MJD, Vmag, e_Vmag
-go
-source=$theCat/$theTab
-out=MJD, Rcmag, e_Rcmag
-go
-source=$theCat/$theTab
-out=MJD, Icmag, e_Icmag
-go
====EOQ1
else
asu -data -n- <<====EOQ2 | gawk -v P=$P 'BEGIN{FS="\t"}\
  /^$/{print; next}\
  /---/ {next}\
  {\
    jd = $1; mag=$2+0;\
    if (P > 0) phase = (jd /P) - int(jd/P) ; \
    else phase = jd;\
    printf("%.2f %5.2f %4.2f\n", phase, mag, $3) \
  }' \
| graph -T $Vgraph -C -L "$Star" --pen-colors 1=purple3:2=blue2:3=green3 \
  $plotarg -X $X:q -Y $Y:q $argv:q
-source=$theCat/table5  
-out=MJD, mag, e_mag
Filt=F555W
[RLS2005]=$Star
-go
-source=$theCat/table5  
-out=MJD, mag, e_mag
Filt=F810W
[RLS2005]=$Star
-go
-source=$theCat/table5  
-out=MJD, mag, e_mag
Filt=F814W
[RLS2005]=$Star
-go
-go
====EOQ2
endif

exit 0
