#!/usr/bin/perl
# 
# Make some graphs illustrating (O-C), using RR Lyr data as example
#
# MWR 2/22/2022



# datafile with measurements
$full_datafile = "./rrlyr_sap.txt";
$one_datafile = "./rrlyr_sap_one_cycle.txt";
$period = 0.5668677600;

# title for all graphs

# graph of a single cycle
$title = "One observed period ";
$cmdfile = "rrlyr_a.gnu";
$psfile = "rrlyr_a.ps";
open(CMDFILE, ">$cmdfile") || die("can't open $cmdfile");
printf CMDFILE "set output '$psfile' \n";
printf CMDFILE "set term postscript enhanced 'Helvetica' 18 color \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 


printf CMDFILE "set grid \n";
printf CMDFILE "set key top right \n";
printf CMDFILE "set xlabel 'time (days) ' \n";
printf CMDFILE "set ylabel 'Magnitude '  \n";
printf CMDFILE "set title '$title'  \n";
printf CMDFILE "plot [1686.1:1686.1+$period][12.8:11.9] ";
printf CMDFILE " '$one_datafile' every 10 ";
printf CMDFILE "     using 1:(25-2.5*log10(\$2)) with points ls 1 pt 7 ps 1.0 t ''  ";
printf CMDFILE " \n";

printf CMDFILE "quit \n";
close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
$giffile = $psfile;
$giffile =~ s/.ps/.png/;
$cmd = "convert -rotate 90 $psfile $giffile";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;



# graph of a prediction only 
$title = "Calculated values, based on earlier observations ";
$cmdfile = "rrlyr_b.gnu";
$psfile = "rrlyr_b.ps";
open(CMDFILE, ">$cmdfile") || die("can't open $cmdfile");
printf CMDFILE "set output '$psfile' \n";
printf CMDFILE "set term postscript enhanced 'Helvetica' 18 color \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'black' lw 2  \n"; 


printf CMDFILE "set grid \n";
printf CMDFILE "set key top right \n";
printf CMDFILE "set xlabel 'time (days) ' \n";
printf CMDFILE "set ylabel 'Magnitude '  \n";
printf CMDFILE "set title '$title'  \n";
printf CMDFILE "plot [1686.1+3*$period:1686.1+$period+3*$period][12.8:11.9] ";
printf CMDFILE " '$one_datafile' every 10 ";
printf CMDFILE "     using (\$1+3*$period):(25-2.5*log10(\$2)) with points ls 6 pt 6 ps 0.5 t 'calculated'  ";
printf CMDFILE " \n";

printf CMDFILE "quit \n";
close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
$giffile = $psfile;
$giffile =~ s/.ps/.png/;
$cmd = "convert -rotate 90 $psfile $giffile";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;


# graph of a prediction and observation
$title = "Observed and calculated ";
$cmdfile = "rrlyr_c.gnu";
$psfile = "rrlyr_c.ps";
open(CMDFILE, ">$cmdfile") || die("can't open $cmdfile");
printf CMDFILE "set output '$psfile' \n";
printf CMDFILE "set term postscript enhanced 'Helvetica' 18 color \n";

  #  revert to some oldish default colors 
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n"; 
  printf CMDFILE "set style line 2 lt rgb 'sea-green' lw 3  \n"; 
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n"; 
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n"; 
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n"; 
  printf CMDFILE "set style line 6 lt rgb 'black' lw 2  \n"; 


printf CMDFILE "set grid \n";
printf CMDFILE "set key top right \n";
printf CMDFILE "set xlabel 'time (days) ' \n";
printf CMDFILE "set ylabel 'Magnitude '  \n";
printf CMDFILE "set title '$title'  \n";
printf CMDFILE "plot [1686.1+3*$period:1686.1+$period+3*$period][12.8:11.9] ";
printf CMDFILE " '$one_datafile' every 10 ";
printf CMDFILE "     using (\$1+3*$period):(25-2.5*log10(\$2)) with points ls 6 pt 6 ps 0.5 t 'calculated'  ";
printf CMDFILE " , ";
printf CMDFILE " '$full_datafile' every 10 ";
printf CMDFILE "     using (\$1+3*$period+0.05):(25-2.5*log10(\$2)) with points ls 1 pt 3 ps 0.8 t 'observed'  ";
printf CMDFILE " \n";

printf CMDFILE "quit \n";
close(CMDFILE);

$cmd = "gnuplot $cmdfile ";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;
$giffile = $psfile;
$giffile =~ s/.ps/.png/;
$cmd = "convert -rotate 90 $psfile $giffile";
printf "cmd is ..$cmd.. \n";
$ret = `$cmd`;






exit 0;
