#!/usr/bin/perl
#

#######################################################
# Make a graph showing the HR diagram for stars within 10 pc

  # get the arguments
  $title = "Gaia stars within 10 parsecs";
  $input_file = "./calc_gaia_nearby_A.out";
  $output_file = "./hr_A.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  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 3  \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 Magnitude' \n";
  printf CMDFILE "set xlabel 'Color (BP - RP) ' \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [-0.5:5.0][20:-5] '$input_file'  ";
  $cmd = $cmd . " using (\$6):(\$5)  with points pt 6 ps 0.8 t '' ";

  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 graph showing the HR diagram for stars within 10 pc
#   AND also for stars within 33 parsecs

  # get the arguments
  $title = "Gaia stars within 10 and 33 parsecs";
  $input_file_A = "./calc_gaia_nearby_A.out";
  $input_file_B = "./calc_gaia_nearby_B.out";
  $output_file = "./hr_A_B.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  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 3  \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 Magnitude' \n";
  printf CMDFILE "set xlabel 'Color (BP - RP) ' \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [-0.5:5.0][20:-5] ";
  $cmd = $cmd . " '$input_file_B' using (\$6):(\$5)  ";
  $cmd = $cmd . "       with points ls 3 pt 3 ps 0.2 t '33 pc' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . " '$input_file_A' using (\$6):(\$5)  ";
  $cmd = $cmd . "       with points ls 1 pt 6 ps 0.8 t '10 pc' ";

  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 graph showing the HR diagram for stars within 10 pc
#    place luminosity on Y-axis, temperature on X-axis
#    but no straight lines with given slopes

  # get the arguments
  $title = "Gaia stars within 10 parsecs";
  $input_file = "./calc_gaia_nearby_A.out";
  $output_file = "./hr_A_log.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  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 3  \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 'Luminosity' \n";
  printf CMDFILE "set xlabel 'Temperature' \n";
#  printf CMDFILE "set logscale x \n";
#  printf CMDFILE "set logscale y \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [3.9:3.4][-4.0:1.0] '$input_file'  ";
  $cmd = $cmd . " using (log10(\$7)):(log10(10**(0.4*((4.8-\$5)))))  with points pt 6 ps 0.8 t '' ";
 # $cmd = $cmd . " , ";
 # $cmd = $cmd . " -16+(4.0*x) t 'slope 4.0' ";
 # $cmd = $cmd . " , ";
 # $cmd = $cmd . " -30+(8.0*x) t 'slope 8.0' ";

  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 graph showing the HR diagram for stars within 10 pc
#    place luminosity on Y-axis, temperature on X-axis
#    add straight lines with representative slopes

  # get the arguments
  $title = "Gaia stars within 10 parsecs";
  $input_file = "./calc_gaia_nearby_A.out";
  $output_file = "./hr_A_log_slope.ps";
  $term_options = "postscript color enhanced 'Helvetica,18' ";

  $cmdfile = "gnuplot.in";
  
  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 3  \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 'Luminosity' \n";
  printf CMDFILE "set xlabel 'Temperature' \n";
#  printf CMDFILE "set logscale x \n";
#  printf CMDFILE "set logscale y \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [3.9:3.4][-4.0:1.0] '$input_file'  ";
  $cmd = $cmd . " using (log10(\$7)):(log10(10**(0.4*((4.8-\$5)))))  with points pt 6 ps 0.8 t '' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . " -16+(4.0*x) t 'slope 4.0' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . " -30+(8.0*x) t 'slope 8.0' ";

  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;
