#!/usr/bin/perl
#
# Calculate a blackbody spectrum and print out intensity 
#   as a function of wavelength over a range of wavelengths.
#   Print numbers to stdout with a format like this:
#
#        nu    lambda     spec 
#
# where 
#           "nu"          is the frequency in Hz
#           "lambda"      is the wavelength in meters
#           "spec"        is the energy per square meter per second
#                              per steradian per meter
#
# usage:  bb.pl  temp  
#
# where "temp" is temperature in Kelvin
# 
# MWR 8/19/2003

use POSIX;

$temp = $ARGV[0];

# wavelengths in meters
$start_lambda = 1e-2;
$end_lambda = 1e-10;
$mult_lambda = 0.90;

# this should be 2*h*c^2
$const = 1.0;
$h = 6.626e-34;
$c = 3.0e8;
$k = 1.38e-23;
$exponent_const = ($h*$c)/($k*$temp);

printf "#%8s %9s %9s \n", "freq(Hz)", "lambda(m)", "spectrum";
 

for ($lambda = $start_lambda; $lambda >= $end_lambda; $lambda *= $mult_lambda) {

  $logl = log10($lambda);

  $top = $const/(pow($lambda, 5.0));
  $exponent = $exponent_const/$lambda;
  # we are careful here to avoid numerical underflow
  if ($exponent < 100) {
    $bot = exp($exponent);
    $blam = $top/$bot;
  }
  else {
    $blam = 1e-90;
  }

  # compute frequency, too
  $nu = $h*$c/$lambda;

  printf "%9.4e %9.4e %9.4e \n", $nu, $lambda, $blam; 

}

exit 0;

