#!/usr/bin/perl
# Make a nice graph showing supernova redshifts over the past 20 years
#
# MWR 10/27/2007


# Spectrum of a type Ia supernova at max light (day 0)
  $output_file = "./sn_Ia_day0.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'A Type Ia supernova spectrum '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia at max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day0.dat' ";
  $cmd .=  "      using (\$2):(\$3*1.1e9) ";
  $cmd .=  "     with lines lt -1 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;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;

# Spectrum of a type Ia supernova at 20 days past max light
  $output_file = "./sn_Ia_day20.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'A Type Ia supernova spectrum '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia 20 days past max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day20.dat' ";
  $cmd .=  "      using (\$2):(\$3*3.2e9) ";
  $cmd .=  "     with lines lt -1 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;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;

# Spectrum of a type Ia supernova at 50 days past max light
  $output_file = "./sn_Ia_day50.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'A Type Ia supernova spectrum '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia 50 days past max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day50.dat' ";
  $cmd .=  "      using (\$2):(\$3*14.1e9) ";
  $cmd .=  "     with lines lt -1 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;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;





# Spectrum at 20 days past max with single standard passband

  $output_file = "./sn_filter_standard_day20.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'The effect of passband shifts on SN photometry '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia 20 days past max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day20.dat' ";
  $cmd .=  "      using (\$2):(\$3*3.1e9) ";
  $cmd .=  "     with lines lt -1 lw 3 t 'SN Ia at max light' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 1  lw 2 t 'Cousins R' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;





# SN spectrum at max light together with passbands
  
  $output_file = "./sn_filter_shift_day0.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'The effect of passband shifts on SN photometry '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia at max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day0.dat' ";
  $cmd .=  "      using (\$2):(\$3*1.1e9) ";
  $cmd .=  "     with lines lt -1 lw 3 t 'SN Ia at max light' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 1  lw 2 t 'Cousins R' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r_shift5percent.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 8  pt 6 lw 2 t 'shifted R' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;


# Now for day 20 past max

  $output_file = "./sn_filter_shift_day20.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'The effect of passband shifts on SN photometry '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia 20 days past max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day20.dat' ";
  $cmd .=  "      using (\$2):(\$3*3.1e9) ";
  $cmd .=  "     with lines lt -1 lw 3 t 'SN Ia at max light' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 1  lw 2 t 'Cousins R' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r_shift5percent.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 3  pt 6 lw 2 t 'shifted R' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;



# Now for day 50 past max

  $output_file = "./sn_filter_shift_day50.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'The effect of passband shifts on SN photometry '\n", $aper;
  printf CMDFILE "set label 1 'SN Ia 50 days past max' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'sn1a_spec_day50.dat' ";
  $cmd .=  "      using (\$2):(\$3*13.1e9) ";
  $cmd .=  "     with lines lt -1 lw 3 t 'SN Ia at max light' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 1  lw 2 t 'Cousins R' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r_shift5percent.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 3  pt 6 lw 4 t 'shifted R' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;




# Now for a simple A0V star -- with regular R passband only

  $output_file = "./sn_filter_vega.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'Photometry of ordinary stars '\n", $aper;
  printf CMDFILE "set label 1 'Vega, an ordinary star' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'uka0v_dat' ";
  $cmd .=  "      using (\$1):(\$2*4.5) ";
  $cmd .=  "     with lines lt -1 lw 3 t 'Vega' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 1  lw 2 t 'Cousins R' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;




# Now for a simple A0V star -- with regular and shifted R passband

  $output_file = "./sn_filter_shift_vega.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'The effect of passband shifts on SN photometry '\n", $aper;
  printf CMDFILE "set label 1 'Vega, an ordinary star' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'uka0v_dat' ";
  $cmd .=  "      using (\$1):(\$2*4.5) ";
  $cmd .=  "     with lines lt -1 lw 3 t 'Vega' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 1  lw 2 t 'Cousins R' ";
  $cmd .=  " , ";
  $cmd .=  " 'bess_r_shift5percent.pass' using (\$1):(\$2*10) ";
  $cmd .=  "     with linespoints lt 3  pt 6 lw 4 t 'shifted R' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;






# plots of stellar spectra, without modification

  $output_file = "./ordinary_stellar_spectra.ps";
  $term_options = "postscript color";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Intensity or Transmission' \n";
  printf CMDFILE "set title 'Spectra of ordinary stars '\n", $aper;
  #printf CMDFILE "set label 1 'Vega, an ordinary star' at 7300,8.5 font 'Helvetica,22' \n";
  
  $cmd =   "plot [3000:10000][0:11] 'ukk5v_dat' ";
  $cmd .=  "      using (\$1):(\$2*5.5) ";
  $cmd .=  "     with lines lt 1 lw 3 t 'K5V star' ";
  $cmd .=  " , ";
  $cmd .=  " 'foo.dat' using 1:2 t '' ";
  $cmd .=  " , ";
  $cmd .=  "    'uka0v_dat' ";
  $cmd .=  "      using (\$1):(\$2*4.5) ";
  $cmd .=  "     with lines lt 3 lw 5 t 'A0V star' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;




# plots of Johnson-Cousins passbands

  $output_file = "./johnson_cousins.ps";
  $term_options = "postscript color enhanced";

  $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 xlabel 'Wavelength (Angstroms)' \n";
  printf CMDFILE "set ylabel 'Transmission' \n";
  printf CMDFILE "set title 'The Johnson-Cousins passbands'\n", $aper;
  #printf CMDFILE "set label 1 'Vega, an ordinary star' at 7300,8.5 font 'Helvetica,22' \n";
  printf CMDFILE "set label 1 'U' at 3500,0.7 font 'Helvetica,24' \n";
  printf CMDFILE "set label 2 'B' at 4200,0.7 font 'Helvetica,24' \n";
  printf CMDFILE "set label 3 'V' at 5200,0.7 font 'Helvetica,24' \n";
  printf CMDFILE "set label 4 'R_c' at 6100,0.7 font 'Helvetica,24' \n";
  printf CMDFILE "set label 5 'I_c' at 8000,0.7 font 'Helvetica,24' \n";
  
  $cmd =   "plot [3000:9500][0:1.1] 'bess_u.pass' ";
  $cmd .=  "      using (\$1):(\$2) ";
  $cmd .=  "     with lines lt 4 lw 5 t '' ";
  $cmd .=  " , ";
  $cmd .=   " 'bess_b.pass' using 1:2 with lines lt 3 lw 5 t '' ";
  $cmd .=  " , ";
  $cmd .=   " 'bess_v.pass' using 1:2 with lines lt 2 lw 5 t '' ";
  $cmd .=  " , ";
  $cmd .=   " 'bess_r.pass' using 1:2 with lines lt 1 lw 3 t '' ";
  $cmd .=  " , ";
  $cmd .=   " 'bess_i.pass' using 1:2 with lines lt 7 lw 5 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;
  
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;


