#!/usr/bin/perl
#
# gen_daily_plots.pl - Steven Lerner 5/2005
#                      Woods Hole Oceanographic Institution
#
#       Description: Quick script that reads through power obc data (generated
#                    by get_power_data.pl) and generates corresponding
#                    gnuplots.
#
#       Usage: gen_daily_plots.pl [yyyymmdd]
#
#       Output: Generates gnuplots 
#
# History:
#       Date       Who   Description
#       ----       ---   ---------------------------------------------------
#      06/07/2005  SL    Create
#      06/12/2005  SL    Added switch states
################################################################################
#

#If arg/date specified, use it, other use today's date
if ($#ARGV >= 0)
  {$date = $ARGV[0];}
else
  {$date = gmtimestamp_file();
   }

$datafile = "Data/$date";

#
#generate plots for all 16 loads (Currents)
#
for ($i=1;$i<=16;$i++)
  {$num = sprintf("%02d",$i);
   $ind = $i + 2;
open(GP,"|/usr/bin/gnuplot");
print GP <<"EndScript";

set terminal png
set xdata time
set timefmt "%Y/%d/%m %H:%M:%S"
set format x "%H:%M"
set grid
set xtics nomirror
set ytics nomirror
set ylabel "ma"

set title "Current Measurement: Load$num"
set output "Plots/$date.load$num.png"
plot '$datafile.cur.dat' using 1:$ind notitle with points
EndScript
close(GP);
  } #end-for each load



#
#generate plots for all 16 swicth states
#
for ($i=1;$i<=16;$i++)
  {$num = sprintf("%02d",$i);
   $ind = $i + 2;
open(GP,"|/usr/bin/gnuplot");
print GP <<"EndScript";
set terminal png
set xdata time
set timefmt "%Y/%d/%m %H:%M:%S"
set format x "%H:%M"
set grid
set xtics nomirror
set ytics nomirror
set ylabel "0-off 1-on"
set yrange [0:1]

set title "Switch State: Load$num"
set output "Plots/$date.switch$num.png"
plot '$datafile.state.dat' using 1:$ind notitle with points
EndScript
close(GP);
  } #end-for each switch state



#
#generate plots for Voltages 
#
$ind = 2;
foreach $voltage ("v12", "v48", "v-12", "v5")
  {$ind++;
   open(GP,"|/usr/bin/gnuplot");
   print GP <<"EndScript";
set terminal png
set xdata time
set timefmt "%Y/%d/%m %H:%M:%S"
set format x "%H:%M"
set grid
set xtics nomirror
set ytics nomirror
set ylabel "mv"

set title "Voltage Measurement: $voltage"
set output "Plots/$date.$voltage.png"
plot '$datafile.vlt.dat' using 1:$ind notitle with points
EndScript
close(GP);
  } #end-for each Voltage



#
#generate plots for Temperature, Pressure, Humidity Sensors
#
$ind = 2;
foreach $sen ("mv1","mv2","mv3","mv4","mv5","mv6","edfaE","mv8","xlca","edfaW","bottom","top","ilc","press_lv","laserE","bm","xlc","press_fluid","mv7","humid","laserW","tbd1","commsa","idc","press_mv","commsb" )
  {$ind++;
   if ($sen =~ /^press_lv|^press_fluid/) 
      {$ylabel = "volts";
       $title = "Pressure: $sen";
       $filetag = "press";
       }
   elsif ($sen =~ /^press_mv/) 
      {$ylabel = "psi";
       $title = "Pressure: $sen";
       $filetag = "press";
       }
   elsif ($sen =~ /^humid/) 
      {$ylabel = "volts"; 
       $title = "Humidity";
       $filetag = "humid";
       }
   else {$ylabel = "degrees C"; 
         $title = "Temperature: $sen";
         $filetag = "temp";
         }

   open(GP,"|/usr/bin/gnuplot");
   print GP <<"EndScript";
set terminal png
set xdata time
set timefmt "%Y/%d/%m %H:%M:%S"
set format x "%H:%M"
set grid
set xtics nomirror
set ytics nomirror
set ylabel "$ylabel"

set title "$title"
set output "Plots/$date.$filetag.$sen.png"
plot '$datafile.sen.dat' using 1:$ind notitle with points
EndScript
close(GP);
  } #end-for each sensor


#
# Subroutines follow
#
sub gmtimestamp_file {
        local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
        # zero fill hour,min,sec and month
        if ($sec < 10) {$sec = "0$sec";}
        if ($min < 10) {$min = "0$min";}
        if ($hour < 10) {$hour = "0$hour";}
        $mon += 1; #from gmtime defined 0-11, make it 1-12
        if ($mon < 10) {$mon = "0$mon";}
        if ($mday < 10) {$mday = "0$mday";}
        #make year 4-digits - Note: Year is years since 1900
        $year += 1900;
        return "$year$mon$mday";
}

