#!/usr/bin/perl
#

$cmdfile = "make_plots.gnu";
open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
printf CMDFILE "set term png color \n";
printf CMDFILE "set output 'mag15_byfilt.png' \n";
printf CMDFILE "set logscale y \n";
printf CMDFILE "set xlabel 'Filter index number' \n";
printf CMDFILE "set ylabel 'Signal-to-noise ratio' \n";
printf CMDFILE "set title 'Stars of mag V=15' \n";
printf CMDFILE "set key bottom left \n";
printf CMDFILE "   plot 'mag_15_exp_1.table' using 2:3 t 'A0V t=1' with linespoints lt 1 ps 1.3 ";
#printf CMDFILE " ,      'mag_15_exp_10.table' using 2:3 t 'A0V t=10' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_15_exp_1.table' using 2:4 t 'G0V t=1' with linespoints lt 2 ps 1.3 ";
#printf CMDFILE " ,      'mag_15_exp_10.table' using 2:4 t 'G0V t=10' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_15_exp_1.table' using 2:5 t 'M0V t=1' with linespoints lt 3 ps 1.3 ";
#printf CMDFILE " ,      'mag_15_exp_10.table' using 2:5 t 'M0V t=10' with linespoints lt 3 ps 1.3 ";

printf CMDFILE " \n";

close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
printf "ret is ..$ret.. \n";



$cmdfile = "make_plots.gnu";
open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
printf CMDFILE "set term png color \n";
printf CMDFILE "set output 'mag20_byfilt.png' \n";
printf CMDFILE "set logscale y \n";
printf CMDFILE "set xlabel 'Filter index number' \n";
printf CMDFILE "set ylabel 'Signal-to-noise ratio' \n";
printf CMDFILE "set title 'Stars of mag V=20' \n";
printf CMDFILE "set key bottom left \n";
printf CMDFILE "   plot 'mag_20_exp_1.table' using 2:3 t 'A0V t=1' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_10.table' using 2:3 t 'A0V t=10' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_100.table' using 2:3 t 'A0V t=100' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_1.table' using 2:4 t 'G0V t=1' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_10.table' using 2:4 t 'G0V t=10' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_100.table' using 2:4 t 'G0V t=100' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_1.table' using 2:5 t 'M0V t=1' with linespoints lt 3 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_10.table' using 2:5 t 'M0V t=10' with linespoints lt 3 ps 1.3 ";
printf CMDFILE " ,      'mag_20_exp_100.table' using 2:5 t 'M0V t=100' with linespoints lt 3 ps 1.3 ";

printf CMDFILE " \n";

close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
printf "ret is ..$ret.. \n";



$cmdfile = "make_plots.gnu";
open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
printf CMDFILE "set term png color \n";
printf CMDFILE "set output 'mag25_byfilt.png' \n";
printf CMDFILE "set logscale y \n";
printf CMDFILE "set xlabel 'Filter index number' \n";
printf CMDFILE "set ylabel 'Signal-to-noise ratio' \n";
printf CMDFILE "set title 'Stars of mag V=25' \n";
printf CMDFILE "set key bottom left \n";
printf CMDFILE "   plot 'mag_25_exp_10.table' using 2:3 t 'A0V t=10' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_100.table' using 2:3 t 'A0V t=100' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_1000.table' using 2:3 t 'A0V t=1000' with linespoints lt 1 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_10.table' using 2:4 t 'G0V t=10' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_100.table' using 2:4 t 'G0V t=100' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_1000.table' using 2:4 t 'G0V t=1000' with linespoints lt 2 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_10.table' using 2:5 t 'M0V t=10' with linespoints lt 3 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_100.table' using 2:5 t 'M0V t=100' with linespoints lt 3 ps 1.3 ";
printf CMDFILE " ,      'mag_25_exp_1000.table' using 2:5 t 'M0V t=1000' with linespoints lt 3 ps 1.3 ";

printf CMDFILE " \n";

close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
printf "ret is ..$ret.. \n";



#
# now we create temp files collecting data by filter,
#     make_plots_temp.0
#     make_plots_temp.1
#         etc.
#
foreach $filt (0, 1, 2, 3, 4, 5, 6, 7, 8) {
 foreach $mag (15, 20, 25) {
  @tables = glob("mag_${mag}_*table");
  $tempfile = "make_plots_temp.${filt}_${mag}";
  `cp /dev/null $tempfile`;
  foreach $table (@tables) {
    $cmd = "awk '{ if (\$2 == $filt) { print \$0 } }' $table >> $tempfile";
    #  printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }
 }
}
# now we make the plots, S/N vs. exposure time, for a selection of filters:
#   pick filters 1, 5, 7

# this is the plot for filter 1
$cmdfile = "make_plots.gnu";
open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
printf CMDFILE "set term png color \n";
printf CMDFILE "set output 'filt1_byexptime.png' \n";
printf CMDFILE "set logscale x \n";
printf CMDFILE "set logscale y \n";
printf CMDFILE "set xlabel 'Exposure time (seconds)' \n";
printf CMDFILE "set ylabel 'Signal-to-noise ratio' \n";
printf CMDFILE "set title 'Fiducial filter index 1 (like V-band)' \n";
printf CMDFILE "set key bottom left \n";
printf CMDFILE "   plot [0.8:1200] 'make_plots_temp.1_15' using 1:3 t 'A0V V=15' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_20' using 1:3 t 'A0V V=20' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_25' using 1:3 t 'A0V V=25' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_15' using 1:4 t 'G0V V=15' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_20' using 1:4 t 'G0V V=20' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_25' using 1:4 t 'G0V V=25' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_15' using 1:5 t 'M0V V=15' with linespoints lt 3 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_20' using 1:5 t 'M0V V=20' with linespoints lt 3 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.1_25' using 1:5 t 'M0V V=25' with linespoints lt 3 ps 1.3 ";

printf CMDFILE " \n";

close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
printf "ret is ..$ret.. \n";

# this is the plot for filter 5
$cmdfile = "make_plots.gnu";
open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
printf CMDFILE "set term png color \n";
printf CMDFILE "set output 'filt5_byexptime.png' \n";
printf CMDFILE "set logscale x \n";
printf CMDFILE "set logscale y \n";
printf CMDFILE "set xlabel 'Exposure time (seconds)' \n";
printf CMDFILE "set ylabel 'Signal-to-noise ratio' \n";
printf CMDFILE "set title 'Fiducial filter index 5 (like I-band)' \n";
printf CMDFILE "set key bottom left \n";
printf CMDFILE "   plot [0.8:1200] 'make_plots_temp.5_15' using 1:3 t 'A0V V=15' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_20' using 1:3 t 'A0V V=20' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_25' using 1:3 t 'A0V V=25' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_15' using 1:4 t 'G0V V=15' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_20' using 1:4 t 'G0V V=20' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_25' using 1:4 t 'G0V V=25' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_15' using 1:5 t 'M0V V=15' with linespoints lt 3 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_20' using 1:5 t 'M0V V=20' with linespoints lt 3 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.5_25' using 1:5 t 'M0V V=25' with linespoints lt 3 ps 1.3 ";

printf CMDFILE " \n";

close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
printf "ret is ..$ret.. \n";


# this is the plot for filter 7
$cmdfile = "make_plots.gnu";
open(CMDFILE, ">$cmdfile") || die("can't open file $cmdfile");
printf CMDFILE "set term png color \n";
printf CMDFILE "set output 'filt7_byexptime.png' \n";
printf CMDFILE "set logscale x \n";
printf CMDFILE "set logscale y \n";
printf CMDFILE "set xlabel 'Exposure time (seconds)' \n";
printf CMDFILE "set ylabel 'Signal-to-noise ratio' \n";
printf CMDFILE "set title 'Fiducial filter index 7 (like J-band)' \n";
printf CMDFILE "set key bottom left \n";
printf CMDFILE "   plot [0.8:1200] 'make_plots_temp.7_15' using 1:3 t 'A0V V=15' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_20' using 1:3 t 'A0V V=20' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_25' using 1:3 t 'A0V V=25' with linespoints lt 1 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_15' using 1:4 t 'G0V V=15' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_20' using 1:4 t 'G0V V=20' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_25' using 1:4 t 'G0V V=25' with linespoints lt 2 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_15' using 1:5 t 'M0V V=15' with linespoints lt 3 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_20' using 1:5 t 'M0V V=20' with linespoints lt 3 ps 1.3 ";
printf CMDFILE "               ,   'make_plots_temp.7_25' using 1:5 t 'M0V V=25' with linespoints lt 3 ps 1.3 ";

printf CMDFILE " \n";

close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
printf "ret is ..$ret.. \n";



# and now remove the temp files
@tempfiles = glob("make_plots*temp*");
foreach $tempfile (@tempfiles) {
  unlink($tempfile);
}




  

exit 0;
