#!/bin/sh


FILE="th082003-data.log"
CMDFILE=solar.plot
HTML=solar-data-2003.html

cat > ${CMDFILE} << _EOF_
# Script generated Gnuplot file 
set timefmt "%H:%M %m/%d"
set xdata time
set xlabel "time [hours:minutes] CEST"
set ylabel "Temperature [deg]"
set format x "%H:%M"
set isosamples 24
_EOF_

cat > yearly.plot <<EOF
# Yearly plot of the max temperarure outdoor and dhw container
set timefmt "%m/%d"
set xdata time
set xlabel "date"
set ylabel "Temperature [deg]"
set format x "%m/%d"
set isosamples 12
set term png transparent color
set output "yearly.png"
plot "yearly.data" using 1:2 title "max outdoor temp" with lines, "yearly.data" using 1:3 title "max DHW tank temp" with lines, "yearly.data" using 1:4 title "min DHW tank temp" with lines
EOF
echo "# max temperarure outdoor and dhw container" > yearly.data

cat > ${HTML} <<EOF
<html><head><title>Histograms for solar regulation</title>
<body>
<h1>Solar regulation histograms</h1>
<script language=javascript>
var date = new Array(
EOF
[ ! -e dayplot ] && mkdir dayplot

for MONTH in 01 02 03 04 05 06 07 08 09 10 11 12  ; do
    for DAY in  01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 \
		16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ; do
    DATE=${MONTH}/${DAY}
if `grep -q ${DATE} ${FILE}` ; then
grep ${DATE} ${FILE} | awk 'BEGIN{min_dhw = 120; max_outdoor = -50;}\
{date = $2;\
if ($3 > max_outdoor) max_outdoor = $3;\
if ($5 < min_dhw ) min_dhw = $5;\
if ($5 > max_dhw) max_dhw = $5;}\
END{print date,max_outdoor, max_dhw, min_dhw >> "yearly.data";}'
echo "\"${DATE}\", " >> ${HTML}
cat >> ${CMDFILE} << _EOF_
set title "Solar regulation at ${MONTH}/${DAY} 2003"
set xrange ["0:0 ${MONTH}/${DAY}":"24:00 ${MONTH}/${DAY}"]
set output "dayplot/solar-${DAY}.${MONTH}.2003.png"
set term png transparent color
plot "${FILE}" using 1:3 title "outdoor temp" with lines, "${FILE}" using 1:9 title "collector temp" with lines, "${FILE}" using 1:10 title "solar tank temp" with lines, "${FILE}" using 1:11 title "heat exchanger temp" with lines, "${FILE}" using 1:5 title "DHW tank temp" with lines, "${FILE}" using 1:(int(\$13)&64)/64*12 title "solar pump status" with steps,  "${FILE}" using 1:(int(\$13)&128)/128*10 title "inter-tank pump status" with steps
_EOF_
fi

done
done

cat >> ${HTML} <<EOF
0);
</script>
<img name=year src=yearly.png>
</body>
</html>
EOF

# gnuplot ${CMDFILE}
gnuplot yearly.plot

syntax highlighted by Code2HTML, v. 0.9.1