#!/bin/bash

#Target in MegaWatts
TARGET=4500

DATE=`date +%Y%m%d`
HOUR=`date +%H`
DATETITLE=`date +"%b %d, %Y"`
CSVFILE=$DATE$HOUR.csv
PNGFILE=$DATE$HOUR.png

curl -s "http://www.wesm.ph/chart/export/luzon_dmd_csv_export.php?date=$DATE&hour=$HOUR" | head -n 25 | tail -n 24 | awk -F ',' -v OFS=', ' -v "target=$TARGET" '{print $1,target,$2,$3,$4,$5,$6,$7}' > $CSVFILE 

gnuplot << EOF
set output "$PNGFILE"
set terminal png nocrop font small size 640,480
set xtics 1
set key outside center bottom Left reverse
set style data linespoints
set title "Luzon Market Demand (MW) - $DATETITLE"
set xlabel "HOURS"
set ylabel "MEGAWATT"
plot "$CSVFILE" using 1:2 title 'Target', "$CSVFILE" using 1:3 title 'Day Ahead Projection (DAP)', "$CSVFILE" using 1:4 title 'Hour Ahead Projection (RTD)', "$CSVFILE" using 1:5 title 'Current Demand (RTX)'
EOF
