#!/usr/bin/perl
# Parse the results of a query into Gaia database
#
use POSIX;
use Scalar::Util qw(looks_like_number);

  $debug = 1;

  # this is the name of the "clean" datafile we'll create 
  #    and use for plotting
  $maglimit_file = "./dr2_glt10.dat";
  $distlimit_file = "./dr2_glt17_pigt20.dat";
  
  my($output_file, $term_options);

if (0 == 1) {

  ###################################################################################
  # Create a datafile which contains (B-R) color, apparent G mag, absolute G mag
  #    from the given datafile.

  $gaia_file = "./dr2_glt10.csv";
  open(CLEAN_FILE, ">$clean_file") || die("can't create file $clean_file");
  open(GAIA_FILE, "$gaia_file") || die("can't open file $gaia_file");
  while (<GAIA_FILE>) {
    $line = $_;
    if ($line =~ /^#/) {
      next;
    }
    @words = split(/,/, $line);
    $id = $words[0];
    $ra = $words[1];
    $dec = $words[3];
    #  in milliarcsec
    $parallax = $words[5];
    #  in milliarcsec
    $parallax_error = $words[6];
    $g_mag = $words[7];
    $bp_minus_rp = $words[8];
    # check to see if the (Bp - Rp) value is an empty string; if so, ignore
    if (!(looks_like_number($bp_minus_rp))) {
      next;
    }
    $teff = $words[12];
    $a_g = $words[13];

    # compute distance in pc
    $dist = 1.0 / ($parallax * 0.001);

    # compute absolute g mag
    $abs_g_mag = $g_mag + 5.0 - 5.0*log10($dist);
    

    printf CLEAN_FILE " para %8.2f  dist %8.2f  g %6.3f  (B-R) %6.3f  G %6.3f  \n",
           $parallax, $dist, $g_mag, $bp_minus_rp, $abs_g_mag;



  }
  close(GAIA_FILE);
  close(CLEAN_FILE);

}


  #########################################################################
  # Make a nice HR diagram
  #   for stars in the "apparent mag < 10" set

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_a.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Absolute g mag' \n";
  printf CMDFILE "set xlabel 'Color (Bp - Rp)' \n";
  printf CMDFILE "set title 'Gaia DR2, stars with g < 10 and <= 10 percent uncert in parallax' \n";
  
  $cmd = "plot [-1:6][17:-8] '$maglimit_file' using 8:10 ";
  $cmd .= "     every 1 ";
  $cmd .= "     with points ls 1 pt 3 ps 0.1 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  #########################################################################
  # Make a nice HR diagram
  #   for stars in the "apparent mag < 10" set
  #   this is "sparse" version, with fewer points and small dots

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_a_sparse.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 0.1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Absolute g mag' \n";
  printf CMDFILE "set xlabel 'Color (Bp - Rp)' \n";
  printf CMDFILE "set title 'Gaia DR2, stars with g < 10 and <= 10 percent uncert in parallax' \n";
  
  $cmd = "plot [-1:6][17:-8] '$maglimit_file' using 8:10 ";
  $cmd .= "     every 3 ";
  $cmd .= "     with points ls 1 pt 3 ps 0.01 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




  #########################################################################
  # Make a nice HR diagram
  #   for stars in the "distance < 50 pc" set

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_b.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Absolute g mag' \n";
  printf CMDFILE "set xlabel 'Color (Bp - Rp)' \n";
  printf CMDFILE "set title 'Gaia DR2, stars with g < 17 and dist < 50 pc and <= 10 percent uncert in parallax' \n";
  
  $cmd = "plot [-1:6][17:-8] '$distlimit_file' using 8:10 ";
  $cmd .= "     every 1 ";
  $cmd .= "     with points ls 1 pt 3 ps 0.1 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  #########################################################################
  # Make a nice HR diagram
  #   for stars in the "distance < 50 pc" set
  #   with "sparse" look (sampling and finer points)

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_b_sparse.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 0.1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Absolute g mag' \n";
  printf CMDFILE "set xlabel 'Color (Bp - Rp)' \n";
  printf CMDFILE "set title 'Gaia DR2, stars with g < 17 and dist < 50 pc and <= 10 percent uncert in parallax' \n";
  
  $cmd = "plot [-1:6][17:-8] '$distlimit_file' using 8:10 ";
  $cmd .= "     every 3 ";
  $cmd .= "     with points ls 1 pt 3 ps 0.01 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  #########################################################################
  # Make a diagram which shows, basically, a random scatterplot.
  #    distance vs. temperature
  #   for stars in the "distance < 10 pc" set

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_random_a.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 0.1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Distance (pc)' \n";
  printf CMDFILE "set xlabel 'Temperature (Kelvin) ' \n";
  printf CMDFILE "set title 'A set of stars from the Gaia catalog (A)' \n";
  
  $cmd = "plot [][] './dr2_paragt100_extra.dat' using 12:4 ";
  $cmd .= "     with points ls 1 pt 3 ps 1.0 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  #########################################################################
  # Make a diagram which shows, basically, a random scatterplot.
  #    declination vs. color    
  #   for stars in the "distance < 10 pc" set

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_random_b.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 0.1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Declination (degrees)' \n";
  printf CMDFILE "set xlabel 'Blue    <--    (B-R) color   -->    Red ' \n";
  printf CMDFILE "set title 'A set of stars from the Gaia catalog (B)' \n";
  
  $cmd = "plot [][] './dr2_paragt100_extra.dat' using 8:20 ";
  $cmd .= "     with points ls 1 pt 3 ps 1.0 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  #########################################################################
  # Make a diagram which shows a NOT random plot
  #     Luminosity vs. color
  #   for stars in the "distance < 10 pc" set

  $cmdfile = "gnuplot.in";
  $term_options = "postscript color 'Helvetica,18' ";
  $output_file = "./dr2_hr_random_c.ps";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 0.1  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 



  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set logscale y  \n";
  printf CMDFILE "set ylabel 'Luminosity' \n";
  printf CMDFILE "set xlabel 'Blue    <--    (B-R) color   -->    Red ' \n";
  printf CMDFILE "set title 'A set of stars from the Gaia catalog (C)' \n";
  
  $cmd = "plot [][] './dr2_paragt100_extra.dat' using 8:16 ";
  $cmd .= "     with points ls 1 pt 3 ps 1.0 t '' \n";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }









exit 0;
