#!/usr/local/bin/perl
#++++++++++++++++
#.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] = ps or txt in vizPlot
#	ARGV[1] = 'ph' for phase plot
#----------------

#### Applications
%content_type = (
   "gif" => "image/gif",
   "ps"  => "application/postscript",
   "txt" => "text/plain"
) ;
#%color = (
#    "B"  => 0,
#    "V"  => 1,
#    "R"  => 2,
#    "I"  => 3,
#) ;

$tt = "/tmp/aPlot" ;

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

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

    $iB = 0 ;
    $iV = 0 ;
    $iR = 0 ;
    $iI = 0 ;
    if ($#ARGV>0) { $Per=1 }

    while(<IN>) {
	chomp ;
	$Obs = substr($_, 5, 12) ;
	if ($Obs > 2400000) { 
	    $Obs -= 2400000 ; 
	    $mag = substr($_, 25, 6) ;
	    $ObsB[$iB] = $Obs ;
	    $magB[$iB] = $mag ;
	    $phaB[$iB] = substr($_, 18, 6) ;
	    $iB += 1 
	}
	$Obs = substr($_, 32, 12) ;
	if ($Obs > 2400000) {
	    $Obs -= 2400000 ;
	    $mag = substr($_, 52, 6) ;
	    $ObsV[$iV] = $Obs ;
	    $magV[$iV] = $mag ;
	    $phaV[$iV] = substr($_, 45, 6) ;
	    $iV += 1 
	}
	$Obs = substr($_, 59, 12);
	if ($Obs > 2400000) {
	    $Obs -= 2400000 ;
	    $mag = substr($_, 79, 6) ;
	    $ObsR[$iR] = $Obs ;
	    $magR[$iR] = $mag ;
	    $phaR[$iR] = substr($_, 72, 6) ;
	    $iR += 1 
	}
	if (length > 86) {
	    $Obs = substr($_, 86, 12); 
	    $Obs -= 2400000 ;
	    $mag = substr($_, 106, 6) ;
	    $ObsI[$iI] = $Obs ;
	    $magI[$iI] = $mag ;
	    $phaI[$iI] = substr($_, 99, 6) ;
	    $iI += 1 
	}
    }

    ### Effective Plot
    if ($ARGV[0] eq "txt") { 
	$cmd = "cat" ;
    }
    else {
        $ENV{'BITMAPSIZE'} = "640x500" ;
        $cmd = "graph -T $ARGV[0] ";
	$cmd .= "--pen-colors 1=blue:2=green2:3=red:4=red3 -C" ;
        $cmd .= " -m -1 -w 0.85 -r 0.15 -h 0.80 -u 0.075 ";
        $cmd .= " -f 0.035 --title-font-size 0.045" ;
	if ($Per) { $cmd .= " -X Phase -x 0 1 -L 'V367 Cyg'" }
	else      { $cmd .= " -X  HJD-2400000 -L 'V367 Cyg'" }
        $cmd .=  " -S 1 -Y Dmag -y 0.1 -1.6" ;
    }
    open(OUT, "| tee $tt | $cmd") || die "Can't open $cmd" ;
    #open(OUT, "> $tt") || die "Can't open $cmd" ;
    select(OUT) ; $| = 1 ;
    print "#-- $cmd\n";
    if ($Per) {
    	print "#Phase  Dmag  in B\n"; 
    	for ($i=0; $i<$iB; $i++) { print "$phaB[$i] $magB[$i]\n" }
    	print "\n#Phase  Dmag  in V\n" ;
    	for ($i=0; $i<$iV; $i++) { print "$phaV[$i] $magV[$i]\n" }
    	print "\n#Phase  Dmag  in R\n" ;
    	for ($i=0; $i<$iR; $i++) { print "$phaR[$i] $magR[$i]\n" }
    	print "\n#Phase  Dmag  in I\n" ;
    	for ($i=0; $i<$iI; $i++) { print "$phaI[$i] $magI[$i]\n" }
    }
    else {
        print "#JD-2400000 Dmag  in B\n"; 
        for ($i=0; $i<$iB; $i++) { printf "%.4f %s\n", $ObsB[$i],$magB[$i] }
        print "\n#JD-2400000 Dmag  in V\n" ;
        for ($i=0; $i<$iV; $i++) { printf "%.4f %s\n", $ObsV[$i],$magV[$i] }
        print "\n#JD-2400000 Dmag  in R\n" ;
        for ($i=0; $i<$iR; $i++) { printf "%.4f %s\n", $ObsR[$i],$magR[$i] }
        print "\n#JD-2400000 Dmag  in I\n" ;
        for ($i=0; $i<$iI; $i++) { printf "%.4f %s\n", $ObsI[$i],$magI[$i] }
    }

    exit 0 ;
}

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

print "\\centerline{{\\large Light Curve for {\\bf\\fg{turquoise4}V367 Cyg}"
    . " (table1)}}\n\\par\n" ;

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

print "\\multicolumn{2}{c}{" 
    . "\\tag{IMG SRC='$cgi\\&gif'}"
    . "}\\\\\n"  ;
print "{\\fg{blue}B} \\quad{ }{\\fg{green2}V} \\quad{ }"
    . "{\\fg{red}R} \\quad{ }{\\fg{red4}I}&"
    . "{\\em get the curve as a} \\A{$cgi\\&ps}{postscript plot}\n"
    . "{\\em or get the} \\A{$cgi\\&txt}{table}\\\\\n" ;

print "\\End{TABULAR}\n\\par\n"
    . "\\Beg{TABULAR}{CELLPADDING=8 CELLSPACING=0 \\bg{white}}{cc}\n" ;
print "\\multicolumn{2}{c}{" 
    . "\\tag{IMG SRC='$cgi\\&gif\\&pha'}"
    . "}\\\\\n"  ;

print "{\\fg{blue}B} \\quad{ }{\\fg{green2}V} \\quad{ }"
    . "{\\fg{red}R} \\quad{ }{\\fg{red4}I}&"
    . "{\\em get the phase curve as a} \\A{$cgi\\&ps\\&pha}"
    . "{postscript plot}\n"
    . "{\\em or get the} \\A{$cgi\\&txt\\&pha}{phase table}\\\\\n"
    . "}\\\\\n"  ;

print "\\End{TABULAR}\n" ;

exit 0 ;
