#!/usr/bin/perl


######################################################################
# Create HR diagram from query of stars closer than 10 pc
#    and G < 15
#
  
  my($output_file, $term_options);
  $output_file = "hw1_hr_a.ps";
  $term_options = "postscript color '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 datafile separator ','  \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 'Gaia (BP - RP) color index ' \n";


  printf CMDFILE "set title 'Gaia HR diagram, stars < 10 pc and G < 15 '\n";
  
  $cmd = "plot [-1:6][20:-10] 'gaia_query_a.csv' using ";
  $cmd .=   " (\$6):((\$5-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 1 pt 1 lw 4 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`;
  }



######################################################################
# Create HR diagram from query of stars closer than 20 pc
#    and G < 15
#
#    Watch out -- the fields in the datafile aren't the same as
#    those in the one used above
  
  my($output_file, $term_options);
  $output_file = "hw1_hr_b.ps";
  $term_options = "postscript color '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 datafile separator ','  \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 'Gaia (BP - RP) color index ' \n";


  printf CMDFILE "set title 'Gaia HR diagram, stars < 20 pc and G < 15 '\n";
  
  $cmd = "plot [-1:6][20:-10] 'gaia_query_50.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 3 pt 1 lw 4 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`;
  }



######################################################################
# Create HR diagram showing locations of stars at 
#    distances < 20 pc and < 50 pc 
#
#    Watch out -- the fields in the datafile aren't the same as
#    those in the first one used at top of this Perl script
  
  my($output_file, $term_options);
  $output_file = "hw1_hr_c.ps";
  $term_options = "postscript color '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 datafile separator ','  \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 'Gaia (BP - RP) color index ' \n";


  printf CMDFILE "set title 'Gaia HR diagram, stars G < 15 '\n";
  
  $cmd = "plot [-1:6][20:-10] 'gaia_query_50.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 1 pt 1 lw 4 t '20 pc' ";
  $cmd .=   "   , ";
  $cmd .=   "                    'gaia_query_20.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 2 pt 6 ps 0.5 lw 1 t '50 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`;
  }




######################################################################
# Create HR diagram showing locations of stars at 
#    distances < 20 pc and < 50 pc and < 100 pc
#
#    Watch out -- the fields in the datafile aren't the same as
#    those in the first one used at top of this Perl script
  
  my($output_file, $term_options);
  $output_file = "hw1_hr_d.ps";
  $term_options = "postscript color '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 datafile separator ','  \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 'Gaia (BP - RP) color index ' \n";


  printf CMDFILE "set title 'Gaia HR diagram, stars G < 15 '\n";
  
  $cmd = "plot [-1:6][20:-10] 'gaia_query_50.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 1 pt 1 lw 4 t '20 pc' ";
  $cmd .=   "   , ";
  $cmd .=   "                    'gaia_query_20.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 2 pt 6 ps 0.5 lw 1 t '50 pc' ";
  $cmd .=   "   , ";
  $cmd .=   "                    'gaia_query_10.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 3 pt 3 ps 0.2 lw 1 t '100 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`;
  }



######################################################################
# Create HR diagram showing locations of stars at 
#    distances < 20 pc and < 50 pc and < 100 pc and < 200 pc
#
#    Watch out -- the fields in the datafile aren't the same as
#    those in the first one used at top of this Perl script
  
  my($output_file, $term_options);
  $output_file = "hw1_hr_e.ps";
  $term_options = "postscript color '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 datafile separator ','  \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 'Gaia (BP - RP) color index ' \n";


  printf CMDFILE "set title 'Gaia HR diagram, stars G < 15 '\n";
  
  $cmd = "plot [-1:6][20:-10] 'gaia_query_50.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 1 pt 1 lw 4 t '20 pc' ";
  $cmd .=   "   , ";
  $cmd .=   "                    'gaia_query_20.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 2 pt 6 ps 0.5 lw 1 t '50 pc' ";
  $cmd .=   "   , ";
  $cmd .=   "                    'gaia_query_10.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt 3 pt 3 ps 0.2 lw 1 t '100 pc' ";
  $cmd .=   "   , ";
  $cmd .=   "                    'gaia_query_5.csv' using ";
  $cmd .=   " (\$5-\$6):((\$4-(5.0*(log(1000.0/\$3)/log(10.0))-5))) ";
  $cmd .=   "     lt -1 pt 3 ps 0.1 lw 1 t '200 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`;
  }






exit 0;
