#!/usr/local/bin/perl -w
#++++++++++++++++
#.IDENTIFICATION plot
#.LANGUAGE       Perl
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT    VizieR
#.KEYWORDS       graph
#.VERSION  1.0   06-May-2001
#.PURPOSE        Display the Light Curves
#.COMMENTS       Called from HTTPD, with SCRIPT_NAME=vizExec or vizPlot
#	ARGV[0] = table number,record number  or  tno,D33-ID   or tno,D33-ID,P
#	ARGV[1] = '-ps'	  in vizPlot
#----------------

#### Applications
%content_type = (
   "gif" => "image/gif",
   "ps"  => "application/postscript",
   "txt" => "text/plain"
) ;
%color = (
    "V"  => 0,
    "I"  => 1,
    "B"  => 2
) ;
#### Get the table number + row number
@tr = split(/[,]/, $ARGV[0]);
if ($#tr > 1) { $Per = $tr[2]; $tt = "/tmp/plotP" }
else          { $Per = 0     ; $tt = "/tmp/plot"  }

if ($ENV{'SCRIPT_NAME'} =~ /vizPlot/) {
#############################################################
#   Plot the figure: directly call the graph function
#############################################################
    open(IN, "fcat table$tr[0].dat |") || die "Can't open table$tr[0].dat ";

    $D33 = "" ;
    $col = 0 ;
    $found = 0 ;
    $band = "" ;

    print "Content-type: $content_type{$ARGV[1]}\n\n"; 
    select(STDOUT); $| = 1 ;

    $i = 0 ;
    @lim = (99, -9) ;

    while(<IN>) {
        @P = split ;
	if ($P[0] ne $D33)  { 
	    #print "#D33='$D33', P[0]='$P[0]', look for '$tr[1]'\n";
	    if ($found) { last } 
	    $D33 = $P[0] ;
	    if ($D33 eq $tr[1]) { $found++ }
	}
	if ($found == 0) { next }
	if ($P[1] ne $band) { 
	    $band = $P[1] ;
	    while ($col < $color{$band}) { 
		$line[$i++]="\n#OtherColor\n"; 
		$col++; 
	    }
	    $line[$i++] = $Per ?
	      "#Phase        Mag     Err ===>Band: $band, P=$Per\n" :
	      "#JD-2400000   Mag     Err ===>Band: $band\n" ;
	}
	$x = $P[2] - 2400000. ;
	if ($P[3] < $lim[0]) { $lim[0] = $P[3] }
	if ($P[3] > $lim[1]) { $lim[1] = $P[3] }

	if ($Per) { $x = ($x/$Per) - int($x/$Per) }
	$line[$i++] = sprintf("%.4f   %6s %6s\n", $x,$P[3],$P[4]) ;
    }

    ### Effective Plot
    $lim[0] = int($lim[0] - 0.5) ;
    $lim[1] = int($lim[1] + 1.5) ;
    if ($ARGV[1] eq "txt") { 
	$cmd = "cat" ;
    }
    else {
        $ENV{'BITMAPSIZE'} = "640x500" ;
        $cmd = "graph -T $ARGV[1] -I e ";
	$cmd .= "--pen-colors 1=green2:2=red3:3=blue:4=black -C" ;
        $cmd .= " -m -1 -w 0.85 -r 0.10 -h 0.80 -u 0.075 ";
        $cmd .= " -f 0.035 --title-font-size 0.045" ;
	if ($Per) { $cmd .= " -X Phase -x 0 1 -L '$tr[1] (P=$Per d)'" }
	else      { $cmd .= " -X HJD-2400000 -L '$tr[1]'" }
        $cmd .=  " -Y mag -y $lim[1] $lim[0]" ;
    }
    open(OUT, "| tee $tt | $cmd") || die "Can't open $cmd" ;
    #open(OUT, "> $tt") || die "Can't open $cmd" ;
    select(OUT) ; $| = 1 ;
    print "#Look for '$tr[1]' from table$tr[0].dat\n" ;
    print "#-- $cmd\n";
    for ($j = 0; $j < $i; $j++) { print $line[$j] }

    exit 0 ;
}

#############################################################
#   Prepare the Text and Image. Arg = t,r
#############################################################
$t = $tr[0] + 4 ;		# Table with Photometry
$cat = `trcat .` ;
chomp($cat) ;
@P = split(/\//, $ENV{'SCRIPT_NAME'}) ;
$P[$#P] = "vizPlot" ;
$cgi = join("/", @P) . "/$ENV{'PATH_INFO'}?$cat" ;

open(IN, "fcat table$tr[0].dat |tail +$tr[1]|head -1|") 
  || die "Can't open table$tr[0]"; 
$_ = <IN> ; @P = split ;

### Get the Period
$Per = substr($_,46,5) ;
if ($tr[0] == 2) { $Per = substr($_,46,7) }
elsif ($tr[0] == 4) { $Per = "" }

if ($Per) { $txtPer = ", P=$Per" }
else {$txtPer = "" }

print "\\centerline{{\\large Light Curve for {\\bf\\fg{turquoise4}$P[0]$txtPer}"
    . " (table$tr[0])}}\n\\par\n" ;
#close(IN) ;

print "\\Beg{TABULAR}{CELLPADDING=8 CELLSPACING=0 \\bg{white}}{cc}\n" ;

#    . "\\tag{A HREF='/viz-bin/Echo'}"
#    . "\\tag{IMG SRC='$cgi\\&$t,\\htarg{$P[0]}\\&gif' ISMAP}"
#    . "}\\tag{/A}\\\\\n"  ;
print "\\multicolumn{2}{c}{" 
    . "\\tag{IMG SRC='$cgi\\&$t,\\htarg{$P[0]}\\&gif'}"
    . "}\\\\\n"  ;
print "{\\fg{green2}V} \\quad{ }{\\fg{blue}B} \\quad{ }{\\fg{red4}I}&"
    . "{\\em get the curve as a} \\A{$cgi\\&$t,$P[0]\\&ps}{postscript plot}\n"
    . "{\\em or get the} \\A{$cgi\\&$t,$P[0]\\&txt}{table}\\\\\n" ;

if ($Per) {
    print "\\End{TABULAR}\n\\par\n"
	. "\\Beg{TABULAR}{CELLPADDING=8 CELLSPACING=0 \\bg{white}}{cc}\n" ;
    print "\\multicolumn{2}{c}{" 
        . "\\tag{IMG SRC='$cgi\\&$t,\\htarg{$P[0],$Per}\\&gif'}"
	. "}\\\\\n"  ;
    print "{\\fg{green2}V} \\quad{ }{\\fg{blue}B} \\quad{ }{\\fg{red4}I}&"
    . "{\\em get the phase curve as a} \\A{$cgi\\&$t,$P[0],$Per\\&ps}"
    . "{postscript plot}\n"
    . "{\\em or get the} \\A{$cgi\\&$t,$P[0],$Per\\&txt}{phase table}\\\\\n"
}
print "\\End{TABULAR}\n" ;

exit 0 ;
