3 # W. Trevor King, 2009-2013. Released into the public domain.
5 # Monitor an unfolding data directory and output a small png of the most
6 # recent unfolding curve
8 # usage: monfold <unfold_dir>
9 # e.g. : monfold ~/rsrch/data/unfold/20090331
11 # Requires the inotify-tools package:
12 # https://github.com/rvoicilas/inotify-tools/wiki
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.
20 echo "usage: monfold <unfold_dir>"
26 DATALINK="./monfold.data"
27 OUTFILE="./monfold.png"
28 ACTION="scp $OUTFILE einstein:./public_html/rsrch/monfold.png" # upload to web
33 plot '$DATALINK' using ((\$1/2**15-1)*10):((\$2/2**15-1)*10)"
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.
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
46 # 20090331/ CLOSE_WRITE,CLOSE 20090331163023_unfold
47 # 20090331/ CLOSE_WRITE,CLOSE 20090331163044_unfold
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.
58 # Multiple close events for the same file as data is written in blocks
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'):"
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"
78 done < <(inotifywait -q -m -e CLOSE_WRITE --exclude '.*unfold_.*' "$DATADIR")