#!/usr/bin/perl
#

  ###########################################################
  #  Plot temperature vs. (BP-RP) color for Gaia stars
  #  Add a simple fit which isn't very good

  $output_file = "gaia_color_temp_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 'Teff' \n";
  printf CMDFILE "set xlabel '(BP - RP) color ' \n";
  printf CMDFILE "set title 'Sample of Gaia stars with good measurements '\n";

  # these are params of our fitted function
  $A = 9160;
  $b = -2.37;
  $c = 0.15;
  $d = 4400.0;
  
  $cmd = "plot [-0.5:2.5][0.0:22000] 'gaia_color_temp.dat' u 18:19 ";
  $cmd .= "   ps 0.2 ";
  $cmd .= " , ";
  $cmd .= " $A*exp($b*(x+$c))+$d with lines ls -1 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`;
  }


  ###########################################################
  #  Plot temperature vs. (BP-RP) color for Gaia stars
  #    Subset in range of colors 0.1 - 1.1, 
  #     and discard stars with T > 9000
  #


  $output_file = "gaia_color_temp_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 'Teff' \n";
  printf CMDFILE "set xlabel '(BP - RP) color ' \n";
  printf CMDFILE "set title 'Restricted sample of Gaia stars with moderate colors '\n";

  # these are params of our fitted function
  $A = 9279;
  $b = -6005.0;
  $c = 2054;
  
  $cmd = "plot [0.1:1.1][4500:9000] 'gaia_color_temp_restr.dat' u 18:19 ";
  $cmd .= "   ps 0.2 ";
  $cmd .= " , ";
  $cmd .= " $A + $b*x + $c*(x*x) with lines ls -1 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;
