# Plotting the Light Curves
####################################
# theTab = table name
# Argument is name & 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" =~ table5) then
    set Cluster = "NGC 3021"
else
    set Cluster = "NGC 1309"
endif
    
if ("$1" =~ ID=*) then
    set Star = `echo $1 | tr -d '"' | acut -c4-`
else
    set Star = $1
endif
shift

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

    echo1 "\section*{Star {\fg{red3} [RML2009] $Cluster $Star}, P=$P \quad "
    echo1 " {\bf\fg{green2}V} \quad {\bf\fg{red4}I} light curves}"
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
	ID="$Star" 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 = "JD-2450000"
endif
set Y = 'mag'


#fcat catalog.dat | gawk -v P=$P "/^$Star /"'{\
asu -data -n- <<====EOQ | gawk -v P=$P 'BEGIN{FS="\t"}\
  /^$/{print; next}\
  /---/ {next}\
  {\
    jd = $1+0; mag=$2+0;\
    if (P > 0) phase = (jd /P) - int(jd/P) ; \
    else phase = jd-2450000;\
    printf("%.4f %6.3f\n", phase, $2, $3) \
  }' \
| graph -T $Vgraph -C -I e -L "$Star" --pen-colors 1=green2:2=red2:3=red4 \
  $plotarg -X $X:q -Y $Y:q $argv:q
-source=$theCat/$theTab
[RML2009]==$Star
-out=MJDV, F555Wmag, e_F555Wmag
-go
-source=$theCat/$theTab
[RML2009]==$Star
-out=MJDI, F814Wmag, e_F814Wmag
-go
====EOQ
exit 0
