#!/usr/bin/perl
#
# Calculate the temperature of extrasolar planets, using data from the
#   California-Carnegie table
#

use POSIX;

$debug = 1;

$table = "known_planets.dat";
$link_head = "http://exoplanets.org/esp/";
$temp_file = "calc_temp.tmp";
$solar_absmag = 4.77;
$solar_luminosity = 3.86E26;
$au = 1.497E11;
$pi = 3.15159;
$sigma = 5.67E-8;


# First, we walk through the list of extrasolar planets,
#    getting the star's name and the planet's orbital radius in AU
#
open(TABLE, "$table") || die("can't open file $table");
while (<TABLE>) {

  $line = " " . $_;
  chomp($line);

  if ($debug > 0) {
    printf "next is ..$line.. \n";
  }

  # grab the star's name
  @words = split(/\s+/, $line);
  $name = $words[2];
  if ($debug > 0) {
    printf "name is ..$name.. \n";
  }

  # get the orbital radius of planet, in AU
  $orbital_radius = $words[5];
  if ($debug > 0) {
    printf "orbital radius is ..$orbital_radius.. \n";
  }

  # create the URL of a page which contains detailed information on the star
  $url = sprintf "%s%s/%s.shtml", $link_head, $name, $name;
  if ($debug > 0) {
    printf "url is ..$url.. \n";
  }

  # run the wget command 
  $cmd = sprintf "wget $url --quiet --output-document=$temp_file";
  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  # check to see if the result is empty -- if so, then just move on
  #   to next star
  $cmd2 = "wc $temp_file";
  $ret = `$cmd2 | awk '{ print $1 }'`;
  if ($ret < 1) {
    printf "$name failed \n";
    next;
  }

  # if we get here, we were able to grab the detailed data file for
  #   this star.  Now, we need to find the absolute magnitude.
  #   So, we walk through the info, looking for lines like
  #       <!-- Apparent magnitude here -->
  #       <!-- Distance (pc) here -->
  #   and then we calculate the absolute mag from apparent mag and distance.
  $cmd2 = "grep 'Apparent magnitude here' $temp_file";
  $appmag = `$cmd2`;
  $appmag =~ s/<TD>//;
  $appmag =~ s%</TD>%%;
  @words = split(/\s+/, $appmag);
  $appmag = $words[0];
  if ($debug > 0) {
    printf "appmag is ..$appmag.. \n";
  }
  $cmd2 = "grep 'Distance (pc) here' $temp_file";
  $dist = `$cmd2`;
  $dist =~ s/<TD>//;
  $dist =~ s%</TD>%%;
  @words = split(/\s+/, $dist);
  $dist = $words[0];
  if ($debug > 0) {
    printf "dist is ..$dist.. \n";
  }

  # if we weren't really able to grab the distance, 
  #   ask the user for it here.
  if ($dist eq "") {
    printf "for star $name  gimme appmag and parallax (mas) ";
    $line2 = " " . <STDIN>;
    chomp($line2);
    printf "line2 is ..$line2.. \n";
    @words = split(/\s+/, $line2);
    $appmag = $words[1];
    $mas = $words[2];
    printf "appmag $appmag   parallax $mas \n";
  }

  # calculate absolute magnitude
  $absmag = ($appmag + 5.0) - 5*(log10($dist));
  if ($debug > 0) {
    printf "absmag is ..$absmag.. \n";
  }

  # calculate luminosity in solar units
  $delta_mag = $absmag - $solar_absmag;
  $ratio_intensity = 10**(-0.4*$delta_mag);
  if ($debug > 0) {
    printf "ratio_intensity is ..$ratio_intensity.. \n";
  }
  $lum = $solar_luminosity * $ratio_intensity;
  if ($debug > 0) {
    printf "luminosity is ..$lum.. \n";
  }

  # and finally we can calculate temperature
  $rad = $orbital_radius * $au;
  $temp = $lum / (16.0*$pi*$sigma*$rad*$rad);
  $temp = $temp ** (0.25);
  if ($debug > 0) {
    printf "temp is ..$temp.. \n";
  }

  # now print out a pretty line for this star
  printf " %-15s   %6.3f  %8.3e  %6.2f   %6.0f \n", 
          $name, $absmag, $lum, $orbital_radius, $temp;

  
  

  if ($debug > 0) {
    printf "hit return to continue ... ";
    $xx = <STDIN>;
  }

}


exit 0;
