# Plotting the Light Curves
####################################
# theTab = Vxx
# Argument is name & P=period
# Vgraph is "gif" "ps" or "txt"
####################################

set Star = "OJ 287"
set Fall = "c=purple3;e=blue3;i=green4;m=red;o=red4;all"
set Fcol = "Filt"
set has_err = 1
set theFile = ( table[2-6].dat* )
set photosys = "BATC"
source $Vroot/lib/graph/lc-mag.csh
exit $status


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

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

#################################################################
# 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*{\object{$Star} in BATC filters "
    echo1 "{\fg{purple3}c} {\fg{blue3}e} {\fg{green2}i} {\fg{red2}m} {\fg{red4}o} (P=$P)} "
    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 verbose
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'

asu -data -n--- <<====EOQ | gawk -v P="$P" 'BEGIN{FS="\t"}\
  /^ *$/{print; next}\
  /---/{next}\
  {\
    jd = $1-2450000; \
    if (P > 0) phase = (jd /P) - int(jd/P) ; \
    else phase = jd; \
    printf("%.4f %s %s\n", phase, $2,$3) \
  }' \
| graph -T $Vgraph -C -I e -L "$Star" \
  --pen-colors 1=purple3:2=blue3:3=green2:4=red2:5=red4 \
  $plotarg -X $X:q -Y $Y:q $argv:q
-source=$theCat/oj287
-out=JD, mag, e_mag
Filt=c
-go
-source=$theCat/oj287
-out=JD, mag, e_mag
Filt=e
-go
-source=$theCat/oj287
-out=JD, mag, e_mag
Filt=i
-go
-source=$theCat/oj287
-out=JD, mag, e_mag
Filt=m
-go
-source=$theCat/oj287
-out=JD, mag, e_mag
Filt=o
-go
====EOQ
exit 0
