#!/usr/local/bin/perl -w
#++++++++++++++++
#.IDENTIFICATION getsp
#.LANGUAGE       Perl
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   23-Mar-2001
#.VERSION  1.1   23-May-2007: Link to IRAS data
#.PURPOSE        Display the LRS spectrum as 2-column table
#.COMMENTS       ARGV[0] = IRAS number
#		The plot is generated in a file /tmp/vGraph:xx containing
#	-> the first line starts by "#--graph" with plotting option
#	-> the remaining is the input to the "graph" program
#	The GIF plot is generated by the cgi-bin/vizGraph
#----------------

#########################################################################
### Subroutine removes files matching a Template
#########################################################################
# Argument 0 = File templates to Remove (e.g. *.gif)
# Argument 1 = Age of files (in seconds) -- remove files older than arg
sub RemoveFiles {
    my $now = time() ;
    my $age = $_[1] ;
    my ($f, $t, $s) ;
    foreach $f (<$_[0]>) {
        $t = (stat($f))[10] ;		# Creation Date
        $s = $now - $t ;		# Age of file in sec
        if ($s > $age) { unlink $f }
	# { print "----Please remove '$f', age=$s > $age\n" }
    }
}
#########################################################################


$#ARGV >= 0 || die "*** Missing IRAS name ***" ;

#########################################################################
# If run by HTTP, copy the result to a file
# Plotting option: -C = Colour output
#########################################################################

$plot=0 ; $tex=0 ;

if ($ENV{'HTTP_HOST'}) { 
    &RemoveFiles("/tmp/vGraph:*", 900) ; 	# Remove files older than 15min
    $plot = 1 ; $tex=1 ;
}

### Get first the list of Wavelengths
open CALIB, "fcat calib.dat|" ;
$_ = <CALIB> ;
for ($i=0; $i<100; $i++) { 
      $lam1[$i] = substr($_, 800+$i*8, 8);
      $lam2[$i] = substr($_,1600+$i*8, 8);
}

open LRS, "fcat lrs.dat | grep '^$ARGV[0]' |" ;
$found = 0 ;
while(<LRS>) {
    $IRAS = substr($_, 0, 11) ;
    if ($tex) { 
	#print "\\section*{LRS Fluxes for {\\bf IRAS $IRAS}}\n" ;
	print "\\section*{LRS Fluxes for " 
	    . "\\glutag{VizDetail 3197 \"IRAS===$IRAS\"|IRAS $IRAS}}\n" ;
	if ($plot) {
	    $out = "/tmp/vGraph:$$-$found" ;
	    $found++; 
    	    open (OUT, ">$out") ;
    	    print OUT "#--graph -T gif --pen-colors 1=green3:2=red -C " .
		"-L 'IRAS $IRAS' -X 'Lambda [µm]' -Y 'Flux [W/m2/µm]' " .
		"-r 0.25\n" ;
	    print "\\tag{IMG src='/viz-bin/vizGraph?$out' ALIGN='left'}\n"; 
	}
	print "\\begin{tabular}{|rr|}\n" ;
	print '\\multicolumn{1 \\bg{LightCyan}}{c}' .
	      '{{\\bf $\\lambda$}\\br($\\mu$m)}&' .
	      '\\multicolumn{1 \\bg{LightCyan}}{c}' .
	      '{{\\bf Flux}\\br(W/m$^2$/$\mu$m)}\\\\'; 
	print "\n";
    }
    else {
        print "#IRAS " . substr($_, 0, 11) . "\n";
    	print "#Lmda(um)\tFlux(W/m2/um)\n"; 
    }
    $scale = 1. * substr($_, 149,11) ;
    #printf("Scale=%g\n", $scale) ;
    #$noise1 = 0 + substr($_, 96, 4) ;
    #$noise2 = 0 + substr($_,100, 4) ;
    #print "Noise levels: $noise1 / $noise2\n" ;
    ### Ignore the 25 first/last points
    for ($i=25; $i<75; $i++) { 
	$flux = substr($_,160+$i*4,4) ; #- $noise1 ;
	if ($tex) { 
	    printf("%s&%e\\\\\n", $lam1[$i], $scale * $flux) ;
	    printf OUT "%s\t%e\n", $lam1[$i], $scale * $flux ;
	}
	else { printf("%s\t%10.4e\n",$lam1[$i], $scale * $flux) }
    }
    if ($tex) { printf "\\\\" }
    if ($plot){ print OUT "\n"}
    print "\n" ;
    for ($i=25; $i<75; $i++) { 
	$flux = substr($_,560+$i*4,4) ; #- $noise2 ;
	if ($tex) { 
	    printf("%s&%e\\\\\n", $lam2[$i], $scale * $flux) ;
	    printf OUT "%s\t%e\n", $lam2[$i], $scale * $flux ;
	}
	else { printf("%s\t%10.4e\n",$lam2[$i], $scale * $flux) }
    }
    if ($tex) { print "\\end{tabular}\n" }
}

if (! $out) { exit }
#########################################################################
#  HTTP Access: Rewrite the data
#########################################################################
#print join("\n", @lam1);
#print join("\n", @lam2);
