#!/usr/bin/perl
#
# Run through a CSV file with Gaia data.
#   Compute the luminosity of each star in solar units.
#   Also estimate a radius, assuming perfect blackbody.
#   
# Print one line per star, with format
#
#      Temperature  dist  app_g_mag  abs_g_mag  Luminosity
#         (K)        (pc)                        (solar)
#
# Special case: skip stars with temperatures within +/- 10 K
#   of 50010 -- there seems to be a bunch of bogus entries
#   in Gaia with that value.
#
# MWR 1/9/2026

use POSIX;
$debug = 0;

# sun's absolute magnitude in G
$solar_MG = 4.69;
# sun's temperature (K)
$solar_teff = 5772.0;

$infile = "./gaia_temp_mag.csv";
open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {

  $line = $_;
  if ($line =~ /^#/) {
    next;
  }

  @words = split(/,/, $line);
  $temp = $words[0];
  $dist = $words[1];
  $app_g_mag = $words[2];
  if ($debug > 0) {
    printf " read temp %6.0f  dist %6.0f  g_mag %6.3f \n",
         $temp, $dist, $app_g_mag;
  }

  # skip some entries with temperatures in a narrow range
  if (($temp > 15000) && ($temp < 15015)) {
    next;
  }

  # Compute absolute G mag
  $dist_mod = 5.0*log10($dist) - 5.0;
  $abs_g_mag = $app_g_mag - $dist_mod;

  # compute lumimosity in solar units
  $diff_mag = $abs_g_mag - $solar_MG;
  $lum = 10.0**(-0.4*$diff_mag);

  # estimate a radius, in solar units
  $sb_ratio = ($temp / $solar_teff)**4.0;
  $area_ratio = $lum / $sb_ratio;
  $radius_ratio = sqrt($area_ratio);

  # print one line per star
  printf "  %6.0f  %8.1f  %6.3f %6.3f  %12.5e  %9.4f\n", 
      $temp, $dist, $app_g_mag, $abs_g_mag, $lum, $area_ratio;

}

exit 0;

