#!/usr/bin/perl
#
# Analyze datafiles from Gaia which contain information on stars
#    in pencil beams located in different directions.
#
# MWR 8/8/2025

$debug = 1;

$infile = "./gaia_vert_a.csv";


# list_columns($infile);



# compute distance (pc), absolute magnitude for each star, print a new line
#   with just a few bits of interesting info
calc_abs_mag($infile);


exit 0;



#########################################################
# SUBROUTINE: list_columns
#
# DESCRIPTION: List the contents of the columns, using first line
#              in the given datafile.  
#
# USAGE:      list_columns   infile
#
#         where
#              infile                is name of the file to describe
#
# RETURNS:    nothing
#
sub list_columns {

  my($infile, @words, $line, $i);

  $infile = $_[0];

		  # figure out which column has which data
		  open(INFILE, "$infile") || die("can't open file $infile");
		  while (<INFILE>) {
			 $line = $_;
			 @words = split(/,/, $line);
			 for ($i = 0; $i <= $#words; $i++) {
				printf " words [%2d] = ..%s.. \n", $i, $words[$i];
			 }
			 last;
		  }
		  close(INFILE);

  return;
}





#########################################################
# SUBROUTINE: calc_abs_mag
#
# DESCRIPTION: For each star, compute distance and absolute mag,
#              print one line with interesting info
#
# USAGE:      calc_abs_mag   infile
#
#         where
#              infile                is name of the file to describe
#
# RETURNS:    nothing
#
sub calc_abs_mag {

  my($infile, @words, $line, $i);
  my($dist, $abs_mag);

  $infile = $_[0];

		  # figure out which column has which data
		  open(INFILE, "$infile") || die("can't open file $infile");
		  while (<INFILE>) {
			 $line = $_;
          if ($line =~ /^#/) {
            next;
          }
			 @words = split(/,/, $line);

          $parallax = $words[0];
          $app_g_mag = $words[1];
          $bp_rp = $words[2];
          $abs_mag = $words[3];

          if ($parallax <= 0.0) {
            next;
          }

          $dm = $app_g_mag - $abs_mag;
          if (($dm < -10) || ($dm > 20)) {
            next;
          }
          $distance = 1000.0 / $parallax;

          printf " app_g %6.3f bp_rp %6.3f  abs %6.3f  dist %6.1f \n",
                 $app_g_mag, $bp_rp, $abs_mag, $distance;

		  }
		  close(INFILE);

  return;
}


