#!/usr/bin/perl
#

#######################################################
# Make a graph showing the histogram of parallax values for the query
#    around open cluster NGC 2323 = M50

  # get the arguments
  $title = "Histogram of parallax values ";
  $input_file = "./query_f.hist";
  $output_file = "./hist_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 'Number of stars' \n";
  printf CMDFILE "set xlabel 'Parallax value (mas) ' \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [-0.5:15.0][] '$input_file'  ";
  $cmd = $cmd . " using (\$1):(\$2)  with boxes 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 histogram of parallax values for the query
#    around open cluster NGC 2323 = M50
#    This is zoomed in around the peak 

  # get the arguments
  $title = "Histogram of parallax values ";
  $input_file = "./query_f.hist";
  $output_file = "./hist_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 'Number of stars' \n";
  printf CMDFILE "set xlabel 'Parallax value (mas) ' \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [-0.1:4.0][] '$input_file'  ";
  $cmd = $cmd . " using (\$1):(\$2)  with boxes 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 at the 
#    estimated distance of the cluster

  # get the arguments
  $title = "Gaia stars with parallax values near the peak ";
  $input_file = "./query_h.csv";
  $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 datafile separator ',' \n";


  printf CMDFILE "set title '$title' \n";
  
  $cmd = "plot [-0.5:3.0][10:-5] ";
  $cmd = $cmd . " '$input_file' using (\$3):(\$4)  ";
  $cmd = $cmd . "       with points ls 1 pt 3 ps 0.2 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`;
  }


exit 0;



#######################################################
# 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;
