#!/bin/csh
####################################
# theTab = File Name e.g. v2051oph
# Argument is P=period
# Vgraph is "gif" "ps" or "txt"
####################################

#################################################################
# Get the Arguments: Star (in $theTab) and Period
#################################################################

set Star = $theTab

# Accept a menu
if ("$1" =~ "LIST_F="*) then
    set Flist = "$1"; shift
else
    set Flist = "LIST_F=b;v;r1;r3;all"
endif
if ("$1" =~ "F="*) then
    set F = `echo "$1" | acut -d= -f2-`; shift
else
    set F = "all"
endif


if ("$1" =~ P=*) then
    set P = `echo "$1" | acut -d= -f2-` ; shift
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 "\subsection*{Light curve of {\fg{blue3}QVS2 $theTab} in \qquad"
    echo1 '{\fg{blue3}b} {\fg{green3}v} {\fg{red2}r1} {\fg{red4}r3}}'
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
	"$Flist" F="$F" P=$P -graph $plotx $ploty $argv:q
    exit 0
endif

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

set  plotarg = \
  "-f 0.0375 -h 0.85 -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-2451224.0"
endif
set Y = '[mag]'
#set ploty = "-y - - -"
#echo2 "Star='$Star'"

################################################################
# lc_get returns JDx, magx, e_magx 
################################################################

#echo2 "....Getting: ./lc_get $theTab"
./lc_get $theTab $F | gawk -v P=$P '\
  BEGIN{nc=split("bm,vm,r1,r3", color, ","); \
       for(i=1;i<=nc;i++) c[color[i]]=i}\
  /^#.*maglc/{printf "\n# m = -%d, S = %d\n", c[substr($NF,1,2)], 3; next}\
  /^#/{print; next}\
  { x = $1+0; if (P>0) x = (x/P) - int(x/P); \
    printf("%9.4f %6s %5s\n", x, $2, $3) \
  }' \
| tee /tmp/4Plot.txt \
| graph -T $Vgraph -C -I e \
  --pen-colors 1=blue3:2=green3:3=red2:4=red4 \
  $plotarg -X $X:q -Y $Y:q $argv:q
exit 0
