#!/usr/bin/perl
#
# make plots illustrating the amplifier glow in HDI after summer 2018
#
# MWR 9/12/2018


  $output_file = "./amp_glow_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 left \n";
  printf CMDFILE "set ylabel 'Counts after overscan and sky subtraction ' \n";
  printf CMDFILE "set xlabel 'Exposure time' \n";
  printf CMDFILE "set title 'Evidence for amplifier glow in HDI '\n";
  
  # first, c7864t0105, with sky=40
  $sky = 40;
  $cmd = "plot [0:1600][-20:150] 'calc_stats.txt' index 0 using 2:(\$3-$sky) ";
  $cmd .= "   with points lt 1 pt 3 lw 2 t 'before lower' ";
  $cmd .= " , " ;
  $cmd .= "         'calc_stats.txt' index 0 using 2:(\$5-$sky) ";
  $cmd .= "   with points lt 1 pt 3 lw 2 t '' ";
  $cmd .= " , " ;
  $cmd .= "         'calc_stats.txt' index 0 using 2:(\$7-$sky) ";
  $cmd .= "   with points lt 1 pt 6 lw 2 t 'before upper' ";
  $cmd .= " , " ;
  $cmd .= "         'calc_stats.txt' index 0 using 2:(\$9-$sky) ";
  $cmd .= "   with points lt 1 pt 6 lw 2 t '' ";
  $cmd .= " , " ;
  $cmd .= "         'calc_stats.txt' index 0 using 2:(\$11-$sky) ";
  $cmd .= "   with points lt 1 pt 5 lw 2 t 'before center' ";

# next, c8223t0098, with sky=62
$sky = 62;
$cmd .= " , " ;
$cmd .= "                     'calc_stats.txt' index 1 using 2:(\$3-$sky) ";
$cmd .= "   with points lt 1 pt 3 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 1 using 2:(\$5-$sky) ";
$cmd .= "   with points lt 1 pt 3 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 1 using 2:(\$7-$sky) ";
$cmd .= "   with points lt 1 pt 6 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 1 using 2:(\$9-$sky) ";
$cmd .= "   with points lt 1 pt 6 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 1 using 2:(\$11-$sky) ";
$cmd .= "   with points lt 1 pt 5 lw 2 t '' ";

# final pre-summer 2018 image, c8229t0094, with sky=75
$sky = 75;
$cmd .= " , " ;
$cmd .= "                     'calc_stats.txt' index 2 using 2:(\$3-$sky) ";
$cmd .= "   with points lt 1 pt 3 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 2 using 2:(\$5-$sky) ";
$cmd .= "   with points lt 1 pt 3 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 2 using 2:(\$7-$sky) ";
$cmd .= "   with points lt 1 pt 6 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 2 using 2:(\$9-$sky) ";
$cmd .= "   with points lt 1 pt 6 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 2 using 2:(\$11-$sky) ";
$cmd .= "   with points lt 1 pt 5 lw 2 t '' ";




# now, the post-summer 2018 image, c8371t0145, with sky=0
$sky = 0;
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 3 using 2:(\$3-$sky) ";
$cmd .= "   with points lt 3 pt 3 lw 2 t 'after lower' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 3 using 2:(\$5-$sky) ";
$cmd .= "   with points lt 3 pt 3 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 3 using 2:(\$7-$sky) ";
$cmd .= "   with points lt 3 pt 6 lw 2 t 'after upper' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 3 using 2:(\$9-$sky) ";
$cmd .= "   with points lt 3 pt 6 lw 2 t '' ";
$cmd .= " , " ;
$cmd .= "         'calc_stats.txt' index 3 using 2:(\$11-$sky) ";
$cmd .= "   with points lt 3 pt 5 lw 2 t 'after center' ";

# and draw a line to guide the eye
$cmd .= " , ";
$cmd .= "   0.080*x with lines lt -1 t '0.08 count/sec ' ";


  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;
