#!/usr/bin/perl
#

  
  my($output_file, $term_options);

  # Rapid change in TRGB for young populations
  $output_file = "trgb_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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set ylabel 'Absolute mag in I-band '  \n";
  printf CMDFILE "set xlabel '(B-V) color ' \n";
  printf CMDFILE "set title 'Basti stellar models for Z=0.001 {/Symbol h}=0.4 '\n";
  
  $cmd = "plot [-0.4:1.8][7:-8]  ";
  $cmd .= "   './wz604y246s.t600100_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 0.5 lw 3 t '100 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t600500_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 0.5 lw 3 t '500 Myr' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # But slower change in TRGB for older populations
  $output_file = "trgb_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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set ylabel 'Absolute mag in I-band '  \n";
  printf CMDFILE "set xlabel '(B-V) color ' \n";
  printf CMDFILE "set title 'Basti stellar models for Z=0.001 {/Symbol h}=0.4 '\n";
  
  $cmd = "plot [-0.4:1.8][7:-8]  ";
  $cmd .= "   './wz604y246s.t600100_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 0.5 lw 3 t '100 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t600500_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 0.5 lw 3 t '500 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t601000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 0.5 lw 3 t '1 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t602000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 0.5 lw 3 t '2 Gyr' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Zoom in on the TRGB area 
  $output_file = "trgb_c.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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
  printf CMDFILE "set ylabel 'Absolute mag in I-band '  \n";
  printf CMDFILE "set xlabel '(B-V) color ' \n";
  printf CMDFILE "set title 'Basti stellar models for Z=0.001 {/Symbol h}=0.4 '\n";
  
  $cmd = "plot [1.0:1.7][-2:-7]  ";
  $cmd .= "   './wz604y246s.t600100_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '100 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t600500_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '500 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t601000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '1 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t602000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '2 Gyr' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # Zoom in on the TRGB area, show very old populations
  $output_file = "trgb_d.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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
  printf CMDFILE "set ylabel 'Absolute mag in I-band '  \n";
  printf CMDFILE "set xlabel '(B-V) color ' \n";
  printf CMDFILE "set title 'Basti stellar models for Z=0.001 {/Symbol h}=0.4 '\n";
  
  $cmd = "plot [1.0:1.7][-2:-7]  ";
  $cmd .= "   './wz604y246s.t600100_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '100 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t600500_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '500 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t601000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '1 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t602000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '2 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t605000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lc rgbcolor \"red\" lw 3 t '5 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t608000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lc rgbcolor \"green\" lw 3 t '8 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t611000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 3 ps 1.0 lc rgbcolor \"blue\" lw 3 t '11 Gyr' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Look at the same area in absolute V-band mag
  $output_file = "trgb_e.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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
  printf CMDFILE "set ylabel 'Absolute mag in V-band '  \n";
  printf CMDFILE "set xlabel '(B-V) color ' \n";
  printf CMDFILE "set title 'Basti stellar models for Z=0.001 {/Symbol h}=0.4 '\n";
  
  $cmd = "plot [1.0:1.7][0:-5]  ";
  $cmd .= "   './wz604y246s.t600100_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '100 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t600500_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '500 Myr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t601000_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '1 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t602000_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lw 3 t '2 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t605000_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lc rgbcolor \"red\" lw 3 t '5 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t608000_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lc rgbcolor \"green\" lw 3 t '8 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t611000_c03hbs' using (\$7):(\$5)  ";
  $cmd .= "       with points pt 3 ps 1.0 lc rgbcolor \"blue\" lw 3 t '11 Gyr' ";

  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Look at changes due to metallicity
  $output_file = "trgb_metal_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";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set ylabel 'Absolute mag in I-band '  \n";
  printf CMDFILE "set xlabel '(B-V) color ' \n";
  printf CMDFILE "set title 'Basti stellar models with {/Symbol h}=0.4 '\n";
  
  $cmd = "plot [1.0:2.0][-3:-6]  ";

  $cmd .= "   '../basti_c/wz803y256s.t605000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 2 ps 1.0 lc rgbcolor \"green\" lw 3 t 'Z=0.0003 5 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   '../basti_c/wz803y256s.t608000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 4 ps 1.0 lc rgbcolor \"green\" lw 3 t 'Z=0.0003 8 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   '../basti_c/wz803y256s.t611000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 6 ps 1.0 lc rgbcolor \"green\" lw 3 t 'Z=0.0003 11 Gyr' ";

  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t605000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 2 ps 1.5 lc rgbcolor \"red\" lw 3 t 'Z=0.001 5 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t608000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 4 ps 1.5 lc rgbcolor \"red\" lw 3 t 'Z=0.001 8 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   './wz604y246s.t611000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 6 ps 1.5 lc rgbcolor \"red\" lw 3 t 'Z=0.001 11 Gyr' ";

  $cmd .= " , ";
  $cmd .= "   '../basti_b/wz304y245s.t605000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 2 ps 1.0 lc rgbcolor \"blue\" lw 3 t 'Z=0.008 5 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   '../basti_b/wz304y245s.t608000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 4 ps 1.0 lc rgbcolor \"blue\" lw 3 t 'Z=0.008 8 Gyr' ";
  $cmd .= " , ";
  $cmd .= "   '../basti_b/wz304y245s.t611000_c03hbs' using (\$7):(\$5-\$8)  ";
  $cmd .= "       with points pt 6 ps 1.0 lc rgbcolor \"blue\" lw 2 t 'Z=0.008 11 Gyr' ";




  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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }





exit 0;
