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

set Star = "SN 2004dj"
#set theFile = 
set Fall = "UBVRI"
set has_err = 1
set JD0     = 2450000
source $Vroot/lib/graph/lc-mag.csh
exit $status

#################################################################
# Get the Arguments: tab and Period
#################################################################
set Star = "SN 2004dj"
set Fall = "UBVRI"

# Accept a menu
if ("$1" =~ "LIST_F="*) then
    set Flist = "$1"; shift
else
    set Flist = "LIST_F=`echo $Fall|sed 's/\(.\)/\1;/g'`;$Fall"
endif

if ("$1" =~ "F="*) then
    set F = `echo "$1" | acut -d= -f2-`; shift
else
    set F = "$Fall"
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
    echo1 "\subsection*{Light curve of \object{$Star} in "
    echo1 "`graph_title $Fall` (P=$P)}"
    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.04 -h 0.85 -w 0.75 -u 0.125 -r 0.175 -m -1 -S 16 0.02"

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

tabmap -par -data '*' ./$theTab.dat | gawk -v P=$P -v F="$F" -v Fall="$Fall" '\
/^%JD/   { x = substr($2,3)+0; if(P>0) x = (x/P) - int(x/P) } \
/^%.mag/ { if (NF!=3) next; c=substr($1,2,1); m=index(Fall,c); \
   if(index(F,c)==0) next; \
   if(m!=M) { M=m; printf "# m = -%d\n", m } \
   printf "%.3f %6s %6s\n", x, $2, $3 }' \
| graph -T $Vgraph -C -I e --pen-colors `graph_color $Fall` \
  $plotarg -X $X:q -Y $Y:q $argv:q
exit 0


#asu -data -n- <<====EOQ | 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-2450000;\
#    printf("%.2f %5.2f %4.2f\n", phase, mag, $3) \
#  }' \
#| graph -T $Vgraph -C -I e -L "$Star" --pen-colors 1=purple2:2=blue2:3=green3:4=red2:5=red4 \
#  $plotarg -X $X:q -Y $Y:q $argv:q
#-source=$theCat/$theTab
#-out=JD, Umag, e_Umag
#-go
#-source=$theCat/$theTab
#-out=JD, Bmag, e_Bmag
#-go
#-source=$theCat/$theTab
#-out=JD, Vmag, e_Vmag
#-go
#-source=$theCat/$theTab
#-out=JD, Rmag, e_Rmag
#-go
#-source=$theCat/$theTab
#-out=JD, Imag, e_Imag
#-go
#====EOQ
#exit 0
