#!/usr/bin/perl
#

############################################################################
# Make a graph showing a filter bandpass, and spectra of hot and cool star
#
  
  $output_file = "./no_shift_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 'Transmission or Intensity' \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set title ''\n", $aper;
  
  $cmd = "plot [4000:7000][0:1.2] 'bess_v.pass' ";
  $cmd .=  "   with lines lt -1 lw 2 t 'V filter' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukb0v_dat' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 3 lw 2 t 'B0 V' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukk0v_dat.adj' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 1 lw 2 t 'K0 V' ";

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



############################################################################
# Make a graph showing a normal AND shifted filter bandpass, 
#   and spectra of hot and cool star
#
  
  $output_file = "./shift_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 'Transmission or Intensity' \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set title ''\n", $aper;
  
  $cmd = "plot [4000:7000][0:1.2] 'bess_v.pass' using (\$1+0):(\$2*1.0)";
  $cmd .=  "   with lines lt -1 lc -1 lw 2 t 'V filter' ";
  $cmd .=  " , ";
  $cmd .=  "   './bess_v.pass' using (\$1+100):(\$2*1.0) ";
  $cmd .=  "   with lines lt 2 lc -1 lw 4 t 'shifted V filter' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukb0v_dat' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 3 lw 2 t 'B0 V' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukk0v_dat.adj' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 1 lw 2 t 'K0 V' ";

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


############################################################################
# Make a graph showing shifted ONLY filter bandpass, 
#   and spectra of hot and cool star
#
  
  $output_file = "./shift_only_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 'Transmission or Intensity' \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set title ''\n", $aper;
  
  $cmd = "plot [4000:7000][0:1.2] 'bess_v.pass' using (\$1+100):(\$2*1.0)";
  $cmd .=  "   with lines lt 2 lc -1 lw 4 t 'shifted V filter' ";
  $cmd .=  " , ";
  $cmd .=  "   './bess_v.pass' using (\$1+100):(\$2*1.0) with lines ";
  $cmd .=  "   lt 1 lc -1 lw 0.5 t '' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukb0v_dat' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 3 lw 2 t 'B0 V' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukk0v_dat.adj' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 1 lw 2 t 'K0 V' ";

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


############################################################################
# Make a graph showing a filter bandpass, spectra of a star,
#    and spectra of two supernovae
#
  
  $output_file = "./star_vs_sne.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 'Transmission or Intensity' \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set title ''\n", $aper;
  
  $cmd = "plot [4000:7000][0:1.2] 'bess_v.pass' ";
  $cmd .=  "   with lines lt -1 lw 2 t 'V filter' ";
  $cmd .=  " , ";
  $cmd .=  "   './ukb0v_dat' using 1:(\$2*0.3) with lines ";
  $cmd .=  "   lt 1 lc 3 lw 2 t 'B0 V' ";
  $cmd .=  " , ";
  $cmd .=  "   './sn1aflux_day_+05.dat' using 1:(\$2*1e8) with lines ";
  $cmd .=  "   lt 1 lc 1 lw 2 t 'SN Ia (day +5)' ";
  $cmd .=  " , ";
  $cmd .=  "   './sn92h_031392_noz.abcor_plus_flux' using 1:(\$3*0.00003) ";
  $cmd .=  "    with lines lt 1 lc 2 lw 3 t 'SN IIP (day +34)' ";

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



############################################################################
# Make a graph showing evolution of SN Ia spectrum with age
#
  
  $output_file = "./sn1a_age.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 'Intensity' \n";
  printf CMDFILE "set xlabel 'Wavelength (Angstroms) ' \n";
  printf CMDFILE "set label 1 'day -10' at 6050,17e-9 tc lt -1 \n";
  printf CMDFILE "set label 2 'day 0' at 6050,13e-9 tc lt 1 \n";
  printf CMDFILE "set label 3 'day 10' at 6050,8.5e-9 tc lt 2 \n";
  printf CMDFILE "set label 4 'day 20' at 6050,3e-9 tc lt 3 \n";
  printf CMDFILE "set label 5 'day 30' at 6050,0.9e-9 tc lt 4 \n";
  printf CMDFILE "set title 'SN Ia spectral evolution '\n", $aper;
  
  $cmd = "plot [3000:7000][0:20e-9] ";
  $cmd .=  "    '~/z1/snap/zero_point/model_spectra/sn1aflux_day_-10.dat' ";
  $cmd .=  "   using 1:(\$2+15e-9) with lines lt -1 lw 1 t '' ";
  $cmd .=  " , ";
  $cmd .=  "    '~/z1/snap/zero_point/model_spectra/sn1aflux_day_+00.dat' ";
  $cmd .=  "   using 1:(\$2+9e-9) with lines lt 1 lc 1 lw 2 t '' ";
  $cmd .=  " , ";
  $cmd .=  "    '~/z1/snap/zero_point/model_spectra/sn1aflux_day_+10.dat' ";
  $cmd .=  "   using 1:(\$2+5e-9) with lines lt 1 lc 2 lw 3 t '' ";
  $cmd .=  " , ";
  $cmd .=  "    '~/z1/snap/zero_point/model_spectra/sn1aflux_day_+20.dat' ";
  $cmd .=  "   using 1:(\$2+3e-9) with lines lt 1 lc 3 lw 3 t '' ";
  $cmd .=  " , ";
  $cmd .=  "    '~/z1/snap/zero_point/model_spectra/sn1aflux_day_+30.dat' ";
  $cmd .=  "   using 1:(\$2+1e-9) with lines lt 1 lc 4 lw 3 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 -background white -flatten $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



############################################################################
# Make a graph showing change in (perfect vs. shifted) magnitudes
#   for a type Ia SN
#
  
  $output_file = "./sn1a_shift_diffs.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 'difference (mag) ' \n";
  printf CMDFILE "set xlabel 'Days since SN Ia peak in B-band ' \n";
  printf CMDFILE "set title 'Perfect-V vs. shifted-V measurements of SN Ia '\n";
  
  $cmd = "plot [-15:50][0.07:-0.07] ";
  $cmd .=  "    './calc_sn_diffs.out' ";
  $cmd .=  "   using 2:8 with points pt 3 ps 1.5 lc 1 lw 4 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 -background white -flatten $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }





exit 0;


