#!/usr/bin/perl
#

$JDBASE = 2456500;
$snfile = "./sn2013ej_mags.out";
$B_offset = 1;
$V_offset = 0;
$R_offset = -1;
$I_offset = -2;
$B_offset_str = sprintf "%+d", $B_offset;
$V_offset_str = sprintf "%+d", $V_offset;
$R_offset_str = sprintf "%+d", $R_offset;
$I_offset_str = sprintf "%+d", $I_offset;

$sn1994d_dir = "/home/richmond/z1/ritobs/aug30_2011/work/pff/sn1994d";
$SN94DBASE = (9432.0-813);
$SN94D_B = (11.89-10.2);
$SN94D_V = (11.90-10.2);
$SN94D_R = (11.92-10.2);
$SN94D_I = (12.06-10.2);


############################################################################
# create a plot showing RIT Obs measurements of SN 2011fe
#
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "sn2013ej_ritobs.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 label 1 'B $B_offset_str' at -8,13.8  tc lt  3 \n";
  printf CMDFILE "set label 2 'V' at -8,12.7  tc lt 2 \n";
  printf CMDFILE "set label 3 'R $R_offset_str' at -8,11.7  tc lt 1 \n";
  printf CMDFILE "set label 4 'I $I_offset_str' at -8,10.6  tc lt -1 \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set ylabel 'Johnson-Cousins magnitude' \n";
  printf CMDFILE "set xlabel 'Julian Date - $JDBASE' \n";
  printf CMDFILE "set title 'SN 2013ej photometry from RIT Observatory \n";
  
  $cmd = "plot [-12.0:100.0][16.0:9.5] ";
  # first, I-band
  $cmd .= " '$snfile' index 3 using (\$2-$JDBASE):(\$3+$I_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc -1  lw 3 t 'I-band + $I_offset' ";
  $cmd .= " , ";
  # R-band
  $cmd .= " '$snfile' index 2 using (\$2-$JDBASE):(\$3+$R_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc 1  lw 3 t 'R-band + $R_offset' ";
  $cmd .= " , ";
  # V-band
  $cmd .= " '$snfile' index 1 using (\$2-$JDBASE):(\$3+$V_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc 2  lw 3 t 'V-band + $V_offset' ";
  $cmd .= " , ";
  # B-band
  $cmd .= " '$snfile' index 0 using (\$2-$JDBASE):(\$3+$B_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc 3  lw 3 t 'B-band + $B_offset' ";
  


  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`;

    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.smaller.png/;
    $cmd = "convert -geometry 33% -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;



  }

exit 0;


############################################################################
# create a plot showing RIT Obs measurements of SN 2011fe
#     and measurements of SN 1994D as well
#
  
  my($output_file, $term_options);


  # get the arguments
  $output_file = "sn2011fe_comp94d.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 label 1 'B $B_offset_str' at 786.1,14.8  tc lt  3 \n";
  printf CMDFILE "set label 2 'V' at 786.1,13.7  tc lt 2 \n";
  printf CMDFILE "set label 3 'R $R_offset_str' at 786.1,12.7  tc lt 1 \n";
  printf CMDFILE "set label 4 'I $I_offset_str' at 786.1,11.6  tc lt -1 \n";
  printf CMDFILE "set nokey \n";
  printf CMDFILE "set ylabel 'Johnson-Cousins magnitude' \n";
  printf CMDFILE "set xlabel 'Julian Date - 2,455,000' \n";
  printf CMDFILE "set title 'SN 2011fe compared to SN 1994D (shifted by 1.7 mag)\n";
  
  #$cmd = "plot [797.5:900.0][20.5:8] ";
  $cmd = "plot [785:985][16.5:7] ";
  # first, I-band
  $cmd .= " '$snfile' index 3 using (\$2-$JDBASE):(\$3+$I_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc -1  lw 3 t 'I-band + $I_offset' ";
  $cmd .= " , ";
  # R-band
  $cmd .= " '$snfile' index 2 using (\$2-$JDBASE):(\$3+$R_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc 1  lw 3 t 'R-band + $R_offset' ";
  $cmd .= " , ";
  # V-band
  $cmd .= " '$snfile' index 1 using (\$2-$JDBASE):(\$3+$V_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc 2  lw 3 t 'V-band + $V_offset' ";
  $cmd .= " , ";
  # B-band
  $cmd .= " '$snfile' index 0 using (\$2-$JDBASE):(\$3+$B_offset) ";
  $cmd .= "      with points pt 3 lt 1 ps 1.5 lc 3  lw 3 t 'B-band + $B_offset' ";

  # SN 1994D measurements from Leuschner 20-inch
  $cmd .= " , ";
  $cmd .= " '$sn1994d_dir/twenty-cor.B' using (\$1-$SN94DBASE):(\$2+$B_offset-$SN94D_B) ";
  $cmd .= "      with points pt 3 lt 1 ps 0.2 lc 3  lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " '$sn1994d_dir/twenty-cor.V' using (\$1-$SN94DBASE):(\$2+$V_offset-$SN94D_V) ";
  $cmd .= "      with points pt 3 lt 1 ps 0.2 lc 2  lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " '$sn1994d_dir/twenty-cor.R' using (\$1-$SN94DBASE):(\$2+$R_offset-$SN94D_R) ";
  $cmd .= "      with points pt 3 lt 1 ps 0.2 lc 1  lw 3 t '' ";
  $cmd .= " , ";
  $cmd .= " '$sn1994d_dir/twenty-cor.I' using (\$1-$SN94DBASE):(\$2+$I_offset-$SN94D_I) ";
  $cmd .= "      with points pt 3 lt 1 ps 0.2 lc -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;
  
  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`;

    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.small.png/;
    $cmd = "convert -geometry 75% -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;


    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.smaller.png/;
    $cmd = "convert -geometry 33% -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




exit 0;



