Difference between revisions of "Quick Hacks"
From assela Pathirana
Jump to navigationJump to search
Line 35: | Line 35: | ||
awk -v intv=24 -f accum.awk cntr_hly_1.csv.dat | awk -v intv=24 -f accum.awk cntr_hly_1.csv.dat | ||
</pre> | </pre> | ||
===Plotting those data with GNUplot=== | |||
<source lang=bash> | |||
#!/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 | |||
</source> |
Revision as of 16:17, 22 February 2011
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