Quick Hacks

From assela Pathirana
Jump to navigationJump to search

What is this

These are primarily notes for myself. Things that I often need to do in computing. Instead of keeping them in a private location I list them here. Perhaps someone else will also find a use..

Awk script for accumulating time series data

#accum.awk
BEGIN{
    ct=0
     avg=0.0
}
{
 if(++ct%intv==0){
    print x,avg/ct
    ct=0
    avg=0.0
 }else{
    if(ct%intv==1){
        x=$1
    }
    avg+=$2
 }
}

Data:

01/01/1945-00:00 0.0
01/01/1945-01:00 0.0
01/01/1945-02:00 0.0
01/01/1945-03:00 0.0
...

Running:

awk -v intv=24 -f accum.awk  cntr_hly_1.csv.dat

Plotting those data with GNUplot

#!/bin/bash
if [ $# -lt 2 ]; then
   echo "Usage: $0 <timeseriesfile> <accumulation> [<startdate>] [<enddate>]"
   exit 5
fi
file=$1
intv=$2
startd=`head -n1 $file|awk '{print $1}'`
endd=`tail -n1 $file|awk '{print $1}'`
if [ $# -ge 3 ]; then
   startd=$3
   endd=$4
fi
cat << EOF > $$.awk
#accum.awk
BEGIN{
    ct=0
    avg=0.0
}
{
 avg+=\$2
 if(++ct%intv==0){
    x=\$1
    print x,avg/ct
    ct=0
    avg=0.0
 }

}
EOF
gnuplot -persist << EOF
set xdata time
set timefmt x "%m/%d/%Y-%H:%M"
set key title ""
set xlabel "Time"
set xlabel  offset character 0, 0, 0 font "" textcolor lt -1 norotate
set xrange [ "$startd" : "$endd" ] noreverse nowriteback
set ylabel "Rainfall (mm)"
set ylabel  offset character 0, 0, 0 font "" textcolor lt -1 rotate by -270
#set boxwidth 1
plot "<awk -v intv=$intv -f $$.awk $file" using 1:2 with boxes fs solid
#    EOF
EOF
rm -f $$.awk

Multiple Plots with the script

  • Save the awk script as tmp.awk
set xdata time
set timefmt x "%m/%d/%Y-%H:%M"
set key title ""
set xlabel "Time"
set xlabel  offset character 0, 0, 0 font "" textcolor lt -1 norotate
set ylabel "Rainfall (mm)"
set ylabel  offset character 0, 0, 0 font "" textcolor lt -1 rotate by -270
set format x "%Y"
set multiplot
set size 1.0,.33
set origin 0,.66
plot "<awk -v intv=1 -f tmp.awk cntr_hly_99.csv.dat" using 1:2 title "hourly" with boxes fs solid
set origin 0,.33
plot "<awk -v intv=24 -f tmp.awk cntr_hly_99.csv.dat" using 1:2 title "daily" with boxes fs solid
set origin 0,.0
plot "<awk -v intv=168 -f tmp.awk cntr_hly_99.csv.dat" using 1:2 title "weekly" with boxes fs solid
unset multiplot

Save to LaTex

#save as latex1.gp
set terminal push
set terminal tikz standalone header "\\usepackage{txfonts}"
set output "$0.tex"
replot
set output
set terminal pop
#save as latex2.gp
set terminal push
set terminal tikz standalone header "\\usepackage{txfonts}"
set output "$0.tex"
call "$0.gp"
set output
set terminal pop
  • Simple (single) plots: (After plotting on viewable terminal like ext)
call "latex1.gp" "<name>" # where <name>.tex is the output file. 
  • Multiple plots: (Save the script as <name>.gp, then
call "latex2.gp" "<name>" # where <name>.tex is the output file and <name>,gp is the script for generating the plot. 

Multiple Plots

set xdata time
set timefmt x "%m/%d/%Y-%H:%M"
set key title ""
set xlabel "Time"
set xlabel  offset character 0, 0, 0 font "" textcolor lt -1 norotate
set ylabel "Rainfall (mm)"
set ylabel  offset character 0, 0, 0 font "" textcolor lt -1 rotate by -270
set format x "%Y"
set multiplot
set size 1.0,.33
set origin 0,.66
plot "<awk -v intv=1 -f tmp.awk cntr_hly_99.csv.dat" using 1:2 title "hourly" with boxes fs solid
set origin 0,.33
plot "<awk -v intv=24 -f tmp.awk cntr_hly_99.csv.dat" using 1:2 title "daily" with boxes fs solid
set origin 0,.0
plot "<awk -v intv=168 -f tmp.awk cntr_hly_99.csv.dat" using 1:2 title "weekly" with boxes fs solid
unset multiplot