#! /usr/local/bin/perl -w
#++++++++++++++++
#.IDENTIFICATION plot_sp.pl
#.LANGUAGE       C-shell
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT    VizieR , Graph, vizPlot
#.KEYWORDS       
#.VERSION  1.0   17-Apr-2001
#.PURPOSE        Plot the Spectra -- Arguments are: M= T= logg= or Filename
#.COMMENTS       Assumed to be in Catalogue Directory
#                To generate the GIF image, this program is called back
#			via vizPlot
#----------------

####################################################################
#  Write the FORM
####################################################################
sub Option {	### Write option, options in @A chosen in $_[1]
    my ($name, $val) ;
    $name = $_[0]; shift @_ ;
    $val  = $_[0]; shift @_ ;
    print "\\begin{SELECT name='$name'}"; 
    foreach $o (@_) {
	if ($o == $val) { print "\\Option{$o}" }
	else { print "\\option{$o}" }
    }
    print "\\end{SELECT}\n" ;
}
sub Form {
  print "\\begin{FORM action='$SCRIPT_PLOT/$pgm' target='T$T'}\n" ;
  print "\\tag{INPUT TYPE='hidden' NAME='cat' VALUE='$theCat'}\n" ;
  print "\\Beg{TABULAR}{\\bg{DarkSeaGreen1} CELLSPACING=0 CELLPADDING=5}"
	 . "{ccccc}\n"; 
  print "{\\small T\$_{eff}\$}\\br"; &Option("T", $T, @T) ; print "&" ;
  print "{\\small log g}\\br";     &Option("logg", $logg, @G) ; print "&";
  print "{\\small [M/H]}\\br";     &Option("M", $M, @M) ; print "&&";
  print "{\\small Wavelength range (\\AA)}\\br" ; 
  print "\\tag{INPUT NAME='W' width=10 value='$w[0]-$w[1]'}\\\\\n"; 
  print "\\multicolumn{5}{c}{\\begin{TABULAR}{cccc}" 
  . "&\\tag{INPUT TYPE='submit' name='-go' value=' Show New Spectrum '}\n"
  . "&\\tag{INPUT TYPE='submit' name='-go' value=' Postscript Spectrum '}\n";
  print "&\\end{TABULAR}}\\End{TABULAR}\n"; 
  print "\\end{FORM}\n" ;
}

####################################################################
#  Variables used
####################################################################

#$tt = "/tmp/pl" . $$ ;		# Temporary File Name
$pgm = $0; $pgm =~ s%.*/%% ;	# Name of this Program
$verbop = 0 ;			# Verbose Option
$plot   = 0;			# Actual plot 
$HTTP_HOST   = $ENV{'HTTP_HOST'}  ;
$SCRIPT_NAME = $ENV{'SCRIPT_NAME'}  ;
$theCat      = $ENV{'theCat'} ;
$type        = "image/gif" ;	# Default Content-type
$gtype       = "gif" ;		# Default type for Graph program
if (! $HTTP_HOST) 	{ $HTTP_HOST = "no host" }
if (! $SCRIPT_NAME) 	{ $SCRIPT_NAME = $0 }
if (! $theCat) 		{ $theCat = "J/A+AS/126/267" }
if ($SCRIPT_NAME =~ /local/)   { $verbop = 1 }
if ($SCRIPT_NAME =~ /vizPlot/) { $verbop = 0; $plot=1 }
if (!$ENV{'BITMAPSIZE'}) { $ENV{'BITMAPSIZE'} = "800x640" }
$SCRIPT_PLOT = $SCRIPT_NAME ; $SCRIPT_PLOT =~ s/vizExec/vizPlot/ ;

@T = (4000, 4250, 4500, 4750, 5000, 5250, 5500, 5750, 6000, 7000, 8000) ;
@G = ("1.0", "1.5", "2.0", "2.5", "3.0", "3.5", "4.0", "4.5") ;
@M = ("-1.0", "-0.5", " 0.0", "+0.1", "+0.2", "+0.3", "+0.5") ;
@w = (4850, 5410) ;

#print "Content-type: text/html\n\n"; 
#open(STDOUT, "|tee /tmp/plot_sp") ;
select(STDOUT); $| = 1 ;

if (! $ARGV[0]) { 
    &Form ;
    exit 0 ;
}

if ($verbop) { 
    print "Arguments:\\quad " ;
    print join("\\quad ", @ARGV) ;
    print "\\br\n"
}

####################################################################
#  Convert the paramters into a file name
####################################################################

while ($#ARGV >= 0) {
    # print "ARG=$ARGV[0]\\br\n"; 
    if ($ARGV[0] =~ /logg=/) { $logg=substr($ARGV[0],5) }
    elsif ($ARGV[0] =~ /T=/) { $T=substr($ARGV[0],2) }
    elsif ($ARGV[0] =~ /M=/) { $M=substr($ARGV[0],2) }
    elsif ($ARGV[0] =~ /W=/) { $W=substr($ARGV[0],2) }
    elsif ($ARGV[0] =~ /^[^=]*$/) { $file = $ARGV[0] }
    elsif ($ARGV[0] =~ /-go=/) { 
	if ($ARGV[0] =~ /postscript/i) { 
	    $type  = "application/postscript" ;
	    $gtype = "ps" 
	}
    }
    else { $file = $ARGV[0] }
    shift ;
}

if (! $file) {
    if ($M) { 
	$Ma = sprintf("%+03d", int($M*10)) ;
	$Ma =~ tr/-+/mp/ ;
    }
    $xi = "2" ;

    $file = int($T/100) . int($logg*10) . $Ma . $xi . ".asc" ;
}
else {	# Convert file into values
    $T    = substr($file,0,2) * 100 ;
    if (($T%250) == 200) { $T += 50 }
    $logg = substr($file,2,2)/10;
    $M    = substr($file,5,2)/10;
	  if (substr($file,4,1) eq "m") { $M = 0 -$M }
}
if (! $W) { $W = "4850-5410" }
### Which are the lambda limits ?
@w = split(/[-\/, ]/, $W) ;
if ($verbop) { print "w=$w[0], $w[1]\\br\n" }

# fcat -f tests the existence of the file (eventuelly compressed)
$_ = system("fcat -f dat/" . $file) ;
if ($_) {
    $cwd = `pwd` ; chop($cwd) ;
    print STDERR "****No such file: $file    (from directory $cwd)\n" ;
    exit 1 ;
}
#print "....Returned status: $_\n" ;

##################################################################
#       In the HTML document, give the image source
#	Terminate by the Form for other parameters if wished.
##################################################################

if (! $plot) {
    if ($verbop) {
        print "\\par{IMG src='$SCRIPT_PLOT/$pgm?$file&W=$W'}\\par\n"; 
    }
    print "\\par{\\bf\\fg{green}------------\\qquad continuum}\n"; 
    print "\\par\\tag{IMG src='$SCRIPT_PLOT/$pgm?$theCat&$file&W=$W'}\\par\n";
    &Form ;
    exit 0;
}

##################################################################
#       Plot the figure: directly call the graph function
##################################################################

open(IN, "fcat dat/$file|") ;
$cmd = "graph -T $gtype --pen-colors 1=red:2=green -C" ;
$cmd .= " -w 0.80 -r 0.15 -h 0.75 -u 0.1" ;
$cmd .=  " -f 0.025 --title-font-size 0.045" ;
$_ = <IN> ; @P = split ;
$cmd .=  " -L 'T\\sbeff\\eb=$P[1] logg=$P[3]"; 
$_ = <IN> ; @P = split ;
$cmd .=  " [M/H]=$P[2] \\*c=$P[4]'" ;
$cmd .= " -X '\\*l [\\oA]'"; 				# X legend
$cmd .= " -Y 'F(\\*n) [mW.m\\sp-2\\ep.Hz\\sp-1\\ep]'" ;	# Y legend
$cmd .= " -x $w[0] $w[1]" ;				# X limits
open(OUT, "| tee /tmp/vizPlot | $cmd") || die "Can't open $cmd" ;
print "Content-type: $type\n\n" ;
select(OUT) ;

#open(OUT, ">$tt") ;
#$_ =<IN> ; @P = split ;
#print OUT "#--graph -T gif --pen-colors 1=red:2=green -C";
#print OUT " -w 0.80 -r 0.15 -h 0.75 -u 0.1" ;
#print OUT " -f 0.025 --title-font-size 0.045" ;
#print OUT " -L 'T\\sbeff\\eb=$P[1] logg=$P[3]"; 
#$_ = <IN> ; @P = split ;
#print OUT " [M/H]=$P[2] \\*c=$P[4]'" ;
#print OUT " -X '\\*l [\\oA]'"; 				# X legend
#print OUT " -Y 'F(\\*n) [mW.m\\sp-2\\ep.Hz\\sp-1\\ep]'" ;	# Y legend
#print OUT " -x $w[0] $w[1]" ;					# X limits
#print OUT "\n" ;
#if (! $plot) then	# Just indicate the Plot
#    echo "\par\tag{IMG src='/viz-bin/vizPlot/$pgm?$theCat&-gif&$1'}"
#    #echo "\par{IMG src='/viz-bin/vizPlot/$pgm?$theCat&-gif&$1'}"
#    exit 0
#endif

### Write the columns within the limits

$_ = <IN>; $_ = <IN>; 	# Skip RESOLVING POWER + Titles
$np=0;
while(<IN>) {
    @P = split; 
    if ($P[0] lt $w[0]) { next }
    if ($P[0] gt $w[1]) { last }
    print "$P[0] $P[1]\n" ;
    $c[$np] = "$P[0] $P[2]" ;
    $np++;
    #$c = $c . "$P[0] $P[2]\n" ; 
}
print "\n"; 
for ($i=0; $i<$np; $i++) { print $c[$i] ; print "\n" }
close(IN) ;
exit 0;
open(IN, "fcat dat/$file|") ;
$_ = <IN>; $_ = <IN>; 	# Skip 
$_ = <IN>; $_ = <IN>; 	# Skip RESOLVING POWER + Titles
while(<IN>) {
    @P = split; 
    if ($P[0] lt $w[0]) { next }
    if ($P[0] gt $w[1]) { last }
    print "$P[0] $P[2]\n" ;
}
#print $c ;					# Continuum
close(OUT) ;

#if ($verbop) { print "\n\nFile $tt written ($np points)\n\n" }
exit 0;

