view plots/plot5 @ 0:9dab44dcb331

Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
author Daniel O'Connor <darius@dons.net.au>
date Tue, 09 Feb 2010 13:44:25 +1030
parents
children
line wrap: on
line source

#!/usr/local/bin/bash
# 
# $Id: plot5,v 1.1 2010/01/10 06:13:52 grog Exp grog $
#
# Generate comparative temperature plots for 5 consecutive days.
# These may need changing
STATION=Dereel
TABLE=observations
COMMON=/home/grog/src/weather/WH-1080/plots/plot-common3.gnuplot
GRAPHFILE=/var/tmp/myplot.foo
OUTFILE=/home/grog/public_html/weather/myplot.png

# Generate some constants.  
# Calculate time zone offset from UTC
# Convert to seconds at UTC
NOW=`date +%Y%m%d%H%M`
UTC=`TZ=GMT date -j $NOW +%s`
# And in local time zone
LOCAL=`date -j $NOW +%s`
# The difference is the time zone offset.
TZOFFSET=`expr $UTC - $LOCAL`
# Number of seconds in a day
DAYSECS=86400
# 2000-1-1 0:0:0 UTC, the gnuplot epoch.  Or so it should be, but for
# some reason my plots come out offset by 2 hours.  Use GNUFUDGE until
# I find out why.
GNUFUDGE=7200
Y2K=`expr 946684800`
MY2K=`expr $Y2K + $TZOFFSET + $GNUFUDGE`
# End of constant generation

# Get the last date for this graph.  
if [ $# -ge 1 ]; then
  ENDDATE=$1
else
  ENDDATE=`date +%Y-%m-%d`
fi
# echo S $ENDDATE
# Frob these values
ENDTIME=`date -j -f "%F %T" "$ENDDATE 0:0:0" +%s`
# start 4 days earlier
STARTTIME=`expr $ENDTIME - $DAYSECS \* 4`
# Adjust to gnu epoch
GNUSTART=`expr $STARTTIME - $Y2K - $TZOFFSET - $GNUFUDGE`
# echo  $ENDTIME - $Y2K - $TZOFFSET - $GNUFUDGE

# echo end $ENDTIME start $STARTTIME
XTICS=10800  # tics every 3 hours

# echo DATE $STARTDATE ENDDATE $ENDDATE GNUEND $GNUEND DURATION $DURATION XTICS $XTICS

# **************************************************
# End of setup crap
# **************************************************

# Do the query
# Offset of day from FOO
OFFSET=`expr $Y2K + $TZOFFSET + $GNUFUDGE`
# echo O $OFFSET S $STARTTIME
for i in 1 2 3 4 5; do
  DAY=`date -r $STARTTIME +"%Y-%m-%d"`
  DAY[$i]=`date -r $STARTTIME +"%e %b"`
#  echo i $i DAY $DAY
  echo "SELECT unix_timestamp(timestamp(date, time))-$OFFSET, outside_temp from $TABLE WHERE station_id = '$STATION' and date ='$DAY' ORDER by time;" | \
  mysql weather > $GRAPHFILE.$i
  STARTTIME=`expr $STARTTIME + $DAYSECS` # next day
  OFFSET=`expr $OFFSET + $DAYSECS`       # next day
done

# And the plot

GRAPHSIZE="375,250"
OUTFILE=$HTML/5days-$ENDDATE-small.png
( cat $COMMON
cat << EOF
	set title "Daily temperature comparison"
	set ylabel "Temperature (°C)"
plot "GRAPHFILE.1"  using 1:2  title "${DAY[1]}" with lines smooth bezier, \
     "GRAPHFILE.2"  using 1:2  title "${DAY[2]}" with lines smooth bezier, \
     "GRAPHFILE.3"  using 1:2  title "${DAY[3]}" with lines smooth bezier, \
     "GRAPHFILE.4"  using 1:2  title "${DAY[4]}" with lines smooth bezier, \
     "GRAPHFILE.5"  using 1:2  title "${DAY[5]}" with lines smooth bezier
EOF
) |  sed "s:MIDNIGHT:$GNUSTART:; s:ENDTIME:$GNUEND:; s:GRAPHFILE:$GRAPHFILE:g; s:SIZE:$GRAPHSIZE:; s:OUTFILE:$OUTFILE:; s:SMOOTHED: (smoothed):; s:SMOOTH:smooth bezier:; s:XTICS:$XTICS:"   | \
  gnuplot
chmod 666 $OUTFILE

GRAPHSIZE="1024,720"
OUTFILE=$HTML/5days-$ENDDATE-big.png
( cat $COMMON
cat << EOF
	set title "Daily temperature comparison"
	set ylabel "Temperature (°C)"
plot "GRAPHFILE.1"  using 1:2  title "${DAY[1]}" with lines smooth bezier, \
     "GRAPHFILE.2"  using 1:2  title "${DAY[2]}" with lines smooth bezier, \
     "GRAPHFILE.3"  using 1:2  title "${DAY[3]}" with lines smooth bezier, \
     "GRAPHFILE.4"  using 1:2  title "${DAY[4]}" with lines smooth bezier, \
     "GRAPHFILE.5"  using 1:2  title "${DAY[5]}" with lines smooth bezier
EOF
) |  sed "s:MIDNIGHT:$GNUSTART:; s:ENDTIME:$GNUEND:; s:GRAPHFILE:$GRAPHFILE:g; s:SIZE:$GRAPHSIZE:; s:OUTFILE:$OUTFILE:; s:SMOOTHED: (smoothed):; s:SMOOTH:smooth bezier:; s:XTICS:$XTICS:"   | \
  gnuplot
chmod 666 $OUTFILE

for i in 1 2 3 4 5; do
  rm $GRAPHFILE.$i
done