#!/usr/bin/perl
#
# Plots for transit lecture
# 
# MWR 3/18/2015

    
      ####################################################################
      #  intensity as function of position across solar disk at 6173 Angstroms
      #
      $output_file = "./solar_slice.ps";
      $term_options = "postscript color enhanced 'Helvetica,22' ";
    
      $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 size 1.0,1.0 \n";
      printf CMDFILE "set grid \n";
      printf CMDFILE "set key bot left Left reverse \n";
      printf CMDFILE "set xlabel 'Pixel in slice across image' \n";
      printf CMDFILE "set ylabel 'Intensity (arbitrary units)' \n";
      printf CMDFILE "set title 'Sun at 6173 Angstroms' \n";
      
      $cmd = "plot [0:1024][0:250] ";
      $cmd .= " './hmi_sun.slice.dat' index 0 ";
      $cmd .= "    using (\$1):(\$2) ";
      $cmd .= "    t '' ";
      $cmd .= "    with lines lw 3 lt 1 lc 1 ";

  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/.eps/.png/;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


    
      ####################################################################
      #  transit in WASP39
      #
      $output_file = "./wasp39_compare.ps";
      $term_options = "postscript color enhanced 'Helvetica,22' ";
    
      $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 size 1.0,1.0 \n";
      printf CMDFILE "set grid \n";
      printf CMDFILE "set key bot left Left reverse \n";
      printf CMDFILE "set xlabel 'Phase' \n";
      printf CMDFILE "set ylabel 'Intensity' \n";
      printf CMDFILE "set title '' \n";
      
      $cmd = "plot [-0.10:0.10][0.960:1.010] ";
      $cmd .= " '../../data/ricci/wasp39_i.dat' index 0 ";
      $cmd .= "    using (\$1):(\$2) ";
      $cmd .= "    t 'I-band' ";
      $cmd .= "    with points pt 4 ps 0.8 lw 3 lt 1 lc 1 ";
      $cmd .= " , ";
      $cmd .= " '../../data/ricci/wasp39_u2.dat' index 0 ";
      $cmd .= "    using (\$1):(\$2) ";
      $cmd .= "    t 'U-band' ";
      $cmd .= "    with points pt 4 ps 0.8 lw 3 lt 1 lc 3 ";

  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/.eps/.png/;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



    
      ####################################################################
      #  variations in transit due to off-center y-position of planet
      #
      $output_file = "./compare_ypos.ps";
      $term_options = "postscript color enhanced 'Helvetica,22' ";
    
      $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 size 1.0,1.0 \n";
      printf CMDFILE "set grid \n";
      printf CMDFILE "set key bot left Left reverse \n";
      printf CMDFILE "set xlabel 'Time (days)' \n";
      printf CMDFILE "set ylabel 'Intensity' \n";
      printf CMDFILE "set title 'Earth-size planet around Sun-like star' \n";
      
      $cmd = "plot [][] ";
      $cmd .= " '../../transit/transit.out_py0.0' index 0 ";
      $cmd .= "    using (\$2/86400):(\$8) ";
      $cmd .= "    t 'y=0' ";
      $cmd .= "    with lines lw 3 lt 1 lc 1 ";
      $cmd .= " , ";
      $cmd .= " '../../transit/transit.out_py0.5' index 0 ";
      $cmd .= "    using (\$2/86400):(\$8) ";
      $cmd .= "    t 'y=0.5' ";
      $cmd .= "    with lines lw 3 lt 1 lc 3 ";
      $cmd .= " , ";
      $cmd .= " '../../transit/transit.out_py0.9' index 0 ";
      $cmd .= "    using (\$2/86400):(\$8) ";
      $cmd .= "    t 'y=0.9' ";
      $cmd .= "    with lines lw 3 lt 1 lc 4 ";
      $cmd .= " , ";
      $cmd .= " '../../transit/transit.out_py0.95' index 0 ";
      $cmd .= "    using (\$2/86400):(\$8) ";
      $cmd .= "    t 'y=0.95' ";
      $cmd .= "    with lines lw 3 lt 1 lc -1 ";



  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/.eps/.png/;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




    
      ####################################################################
      #  one simple transit
      #
      $output_file = "./simple_transit.ps";
      $term_options = "postscript color enhanced 'Helvetica,22' ";
    
      $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 size 1.0,1.0 \n";
      printf CMDFILE "set grid \n";
      printf CMDFILE "set key bot left Left reverse \n";
      printf CMDFILE "set xlabel 'Time (days)' \n";
      printf CMDFILE "set ylabel 'Intensity' \n";
      printf CMDFILE "set title '' \n";
      
      $cmd = "plot [][] ";
      $cmd .= " '../../transit/transit.out_py0.0' index 0 ";
      $cmd .= "    using (\$2/86400):(\$8) ";
      $cmd .= "    t '' ";
      $cmd .= "    with lines lw 3 lt 1 lc -1 ";



  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/.eps/.png/;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }






exit 0;
