#!/usr/bin/perl
#
# Make plots showing the properties of objects in SXDS
#
$debug = 1;


  $output_file = "number_hist.ps";
  $term_options = "postscript color";

  $cmdfile = "gnuplot.in";

  # determine the limits of the graph
  $minmag = 18.0;
  $maxmag = 28.0;
  $miny = 0.0;
  $maxy = 7000.0;
  
  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 left Left reverse \n";
  printf CMDFILE "set xlabel 'Apparent V-band magnitude' \n";
  printf CMDFILE "set ylabel 'Number of objects' \n";
  printf CMDFILE "set title 'Objects classified as star in the SXDS' \n";
  
  $cmd = "plot [$minmag:$maxmag][$miny:$maxy] ";
  $cmd .= " 'morph_low_hist.dat' using (\$2+0.07):4 with impulse lw 2  ";
  $cmd .= " t 'morph only, low-quality' ";
  $cmd .= " , ";
  $cmd .= " 'color_low_hist.dat' using (\$2+0.00):4 with points pt 5 ps 1 lw 1  ";
  $cmd .= " t 'color only, low-quality' ";
  $cmd .= " , ";
  $cmd .= " 'both_low_hist.dat' using (\$2+0.00):4 with linespoints ps 1 lw 2  ";
  $cmd .= " t 'morph and color, low-quality' ";
  $cmd .= " , ";
  $cmd .= " 'both_high_hist.dat' using (\$2+0.00):4 with boxes lt -1 lw 2  ";
  $cmd .= " t 'morph and color, high-quality' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }
  

# now a graph showing fraction of objects which are classified
#   as stars

  $output_file = "fraction_hist.ps";
  $term_options = "postscript color";

  $cmdfile = "gnuplot.in";

  # determine the limits of the graph
  $minmag = 18.0;
  $maxmag = 28.0;
  $miny = -0.02;
  $maxy = 0.7;
  
  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 Right \n";
  printf CMDFILE "set xlabel 'Apparent V-band magnitude' \n";
  printf CMDFILE "set ylabel 'Fraction of objects' \n";
  printf CMDFILE "set title 'Objects classified as star in the SXDS' \n";
  
  $cmd = "plot [$minmag:$maxmag][$miny:$maxy] ";
  $cmd .= " 'morph_low_hist.dat' using (\$2+0.07):10 with impulse lw 2  ";
  $cmd .= " t 'morph only, low-quality' ";
  $cmd .= " , ";
  $cmd .= " 'color_low_hist.dat' using (\$2+0.00):10 with points pt 5 ps 1 lw 1  ";
  $cmd .= " t 'color only, low-quality' ";
  $cmd .= " , ";
  $cmd .= " 'both_low_hist.dat' using (\$2+0.00):10 with linespoints ps 1 lw 2  ";
  $cmd .= " t 'morph and color, low-quality' ";
  $cmd .= " , ";
  $cmd .= " 'both_high_hist.dat' using (\$2+0.00):10 with boxes lt -1 lw 2  ";
  $cmd .= " t 'morph and color, high-quality' ";
  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }
  



exit 0;
