14ba57ec4e62f3e81d5a1a5656d2a4483651ac53
[blog.git] / posts / inotify / monfold.sh
1 #!/bin/bash
2 #
3 # W. Trevor King, 2009-2013.  Released into the public domain.
4 #
5 # Monitor an unfolding data directory and output a small png of the most
6 # recent unfolding curve
7 #
8 # usage: monfold <unfold_dir>
9 # e.g. : monfold ~/rsrch/data/unfold/20090331
10 #
11 # Requires the inotify-tools package:
12 #   https://github.com/rvoicilas/inotify-tools/wiki
13 #
14 # Running something along the lines of
15 #   qiv --watch $OUTFILE # run qiv tracing the image file
16 # is a useful way to monitor the generated image without constantly
17 # grabbing the upper window.
18
19 if [ $# -ne 1 ]; then
20         echo "usage: monfold <unfold_dir>"
21         exit 1
22 fi
23
24 DATADIR="$1"
25 GPFILE="./monfold.gp"
26 DATALINK="./monfold.data"
27 OUTFILE="./monfold.png"
28 ACTION="scp $OUTFILE einstein:./public_html/rsrch/monfold.png" # upload to web
29
30 cat > "$GPFILE" <<EOF
31 set terminal png
32 set output '$OUTFILE'
33 plot '$DATALINK' using ((\$1/2**15-1)*10):((\$2/2**15-1)*10)"
34 EOF
35
36 # The way output from the unfolding software works, we get a bunch of
37 #   YYYYMMDDHHMMSS_unfold
38 # files (which are what we want to be monitoring), along with some
39 #   YYYYMMDDHHMMSS_unfold_<blablabla>
40 # files with information we ignore.
41 #
42 # The files come out sequentially in time, but are written in multiple chunks
43 #   $ inotifywait -q -m -e CLOSE --exclude '.*unfold_.*' 20090331/
44 #   20090331/ CLOSE_WRITE,CLOSE 20090331163023_unfold
45 #   ...
46 #   20090331/ CLOSE_WRITE,CLOSE 20090331163023_unfold
47 #   20090331/ CLOSE_WRITE,CLOSE 20090331163044_unfold
48 #   ...
49 #   20090331/ CLOSE_WRITE,CLOSE 20090331163044_unfold
50 # Triggering on CLOSE isn't actually a good idea, since our monitoring
51 # activity involves reading the new file, which produces a bunch of
52 # CLOSE_NOWRITE,CLOSE events.  In the loop below we only look at
53 # CLOSE_WRITES, and we trigger off the transition from one file to the
54 # next, e.g. the 23->44 second example shown above, after which we
55 # process the 23 second data.
56
57 LASTFILE=""
58 # Multiple close events for the same file as data is written in blocks
59 while read LINE; do
60         DIR=`echo "$LINE" | awk '{print  $1}'` # has trailing slash
61         EVENT=`echo "$LINE" | awk '{print  $2}'`
62         FILE=`echo "$LINE" | awk '{print  $3}'`
63         if [ "$EVENT" != "CLOSE_WRITE,CLOSE" ]; then
64                 echo "Error (unexpected event '$EVENT'):"
65                 echo "$LINE"
66                 exit 1
67         fi
68         if [ "$FILE" != "$LASTFILE" ] && [ "$LASTFILE" != "" ]; then
69                 # We must have finished writing to $LASTFILE...
70                 echo "process $DIR$LASTFILE -> $OUTFILE"
71                 ln -s "$DIR$LASTFILE" "$DATALINK"
72                 gnuplot "$GPFILE"
73                 rm "$DATALINK"
74                 echo $ACTION
75                 $ACTION
76         fi
77         LASTFILE="$FILE"
78 done < <(inotifywait -q -m -e CLOSE_WRITE --exclude '.*unfold_.*' "$DATADIR")
79
80 rm "$GPFILE"