#!/bin/csh
####################################
# theTab = File Name table1 (UBV) / table2 (RV)
# Argument is P=period
# Vgraph is "gif" "ps" or "txt"
####################################

set Star = `echo $theTab | acut -i'WASP-' -c2-4 `

if ("$theTab" =~ *phot*) then
   set Case = "Phot"
else
   set Case = "RV"
endif

if ("$theTab" =~ w147phot*) then
   set Tit = "{\fg{blue2}BB} {\fg{green3}JV} {\fg{red4}r'} light curves"
else if ("$theTab" =~ w160phot&) then
   set Tit = "{\fg{gray30}IZ} {\fg{red4}r'} light curves"
else if  ("$theTab" =~ w164phot*) then
   set Tit = "{\fg{blue2}BB}  {\fg{gray30}IZ} {\fg{red2}R} {\fg{red4}r'} light curves"
else if  ("$theTab" =~ w165phot*) then
   set Tit = "{\fg{green3}NG} light curves"
else
   set Tit = "CORALIE radial velocity curve "
endif
#################################################################
# Get the Arguments: tab and Period
#################################################################

if ("$1" =~ P=*) then
    set P = `echo "$1" | cut -c3-` ; shift
else
    set P = ""
endif
if ("$P" == "") set P = 0 


#################################################################
# Vgraph not defined ==> just the plot introduction
#################################################################
if (! $?Vgraph) then
        # These values are superseded by what's contained in $argv
        echo1 "\section*{\object{$Star} $Tit (P=$P)}"
    cgigraph -img -ps:"Postscript Figure" -txt:"Data as a Table" -form -htx \
	P=$P -graph $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 16)

if ($P != 0) then
    set plotx = "-x 0 1"
    set X = "Phase"
else
    set plotx = ""
    set X = "HJD-2450000"
endif

if ($theTab =~ *rv) then
    set Y = 'RV [km/s]'
    set ploty = ""
    asu -data -n- <<====EOQ2 | gawk -v P=$P 'BEGIN{FS="\t"}\
      /^$/{color++; print; print "#Color " color; next}\
      { x = $1; if (P!=0) x = (x/P) - int(x/P);\
	printf( "%11.6f %s %s\n", x, $2, $3); \
      }' | graph -T $Vgraph -C -I e -L "$theTab" \
      --pen-colors 1=blue2:2=red2:3=purple2 \
      $plotarg $plotx $ploty -X $X:q -Y $Y:q $argv:q
-source=$theCat/$theTab
-out=HJD,RV, e_RV 
====EOQ2
else if ("$theTab" =~ w165phot*) then
    set ploty = ""
    set Y = 'Normalized flux'
    asu -data -n- <<====EOQ1 | gawk -v P=$P 'BEGIN{FS="\t"}\
      /^$/{color++; print; print "#Color " color; next}\
      { x = $1; if (P!=0) x = (x/P) - int(x/P);\
	printf( "%11.6f %s %s\n", x, $2, $3); \
      }' | graph -T $Vgraph -C -I e -L "$theTab" \
      --pen-colors 1=green3:2=blues:3=purple2 \
      $plotarg $plotx $ploty -X $X:q -Y $Y:q $argv:q
-source=$theCat/$theTab
-out=HJD,Flux,e_Flux
====EOQ1
else if ("$theTab" =~ *phot*) then
    set ploty = ""
    set Y = 'Normalized flux'
    asu -data -n- <<====EOQ3 | gawk -v P=$P 'BEGIN{FS="\t"}\
      /^$/{color++; print; print "#Color " color; next}\
      { x = $1; if (P!=0) x = (x/P) - int(x/P);\
        printf( "%11.6f %s %s\n", x, $2, $3); \
      }' | graph -T $Vgraph -C -I e -L "$theTab" \
      -rpen-colors 1=blue2:2=green3:3=red2:4=red4:5=gray30 \
      $plotarg $plotx $ploty -X $X:q -Y $Y:q $argv:q
-source=$theCat/$theTab
-out=HJD,Flux,e_Flux
Filter=BB
-go
-source=$theCat/$theTab
-out=HJD,Flux,e_Flux
Filter=JV
-go
-source=$theCat/$theTab
-out=HJD,Flux,e_Flux
Filter=R 
-go
-source=$theCat/$theTab
-out=HJD,Flux,e_Flux
Filter=r'
-go
-source=$theCat/$theTab
-out=HJD,Flux,e_Flux
Filter=IZ
-go
====EOQ3
endif

exit 0
