Software for generating the PhysioBank Index 1.0.0

File: <base>/make-pbi (3,029 bytes)
#! /bin/sh
# file: make-pbi		G. Moody	5 March 2008
#				Last revised:	24 May 2013
#
# If the PhysioBank Index is out-of-date, this script rebuilds and reloads it
# into the PhysioBank Simple Query Server (pbsqs), starting pbsqs if needed.
# Otherwise, it checks to see if pbsqs is running, restarts it if needed, and
# does nothing else.
#
# Rebuilding the entire index from scratch may require several hours.  The
# processing is input-bound since each .hea and each annotation file must be
# read, but they can be parsed very quickly.  If multiple copies of the data
# on different physical devices are available, it's easy to parallelize the
# process by building two or more single-collection indices simultaneously,
# but this won't help if all of the inputs are located on the same device.

BINDIR=/usr/local/bin
PBDIR=/home/physionet/html/physiobank/database
PATH=$BINDIR:$PATH:/sbin
export PATH

UPDATE=0
cd $PBDIR

# Update the single-collection indices as needed.
for D in `wfdbcat DBS | cut -f 1`
do
    I=`echo $D | sed s+/+_+g`
    # If a collection's RECORDS list is newer than its index, update the index.
    if [ $D/RECORDS -nt pbi/$I ]
    then
	(
	    # The order of records in the index is important!  Don't change the
	    # next line, which determines the record order. (The search
	    # algorithm exploits the ordering for speed;  the server checks the
	    # record order when loading the index and refuses to run if it
	    # finds sorting errors.)
	    for R in `wfdbcat $D/RECORDS | LC_ALL=C sort`
	    do
		echo $D/$R
		case $D in
		    mimic2db)
			T=`echo $R | cut -c 1-6`T
			echo $D/${R}${T} ;;
		    mimic2wdb/3*)
			N=`basename $R`n
			echo $D/$R$N ;;
		esac
	    done
	) | pbindex >/tmp/pbi-$I-$$ 2>/tmp/pbi-errors-$I-$$
	mv -f /tmp/pbi-$I-$$ pbi/$I
	if [ -s /tmp/pbi-errors-$I-$$ ]
	then
	    mv -f /tmp/pbi-errors-$I-$$ /tmp/pbi-errors-$I
	else
	    rm -f /tmp/pbi-errors-$I-$$
	fi
	UPDATE=1
    fi
done

case $UPDATE in
    0) # The index is up-to-date already -- just restart the PhysioBank Simple
       # Query Server if it isn't running.
       pidof pbsqsd >/dev/null || ( pbsqsd; echo pbsqsd restarted ) ;;
    1) # An update is needed. First, save the most recent version of the index.
       mv physiobank-index physiobank-index~
       # Rebuild the index from the single-collection indices.  Note that the
       # indices are ordered using the same sort as for the records within each
       # index.
       for D in `wfdbcat DBS | cut -f 1 | LC_ALL=C sort | sed s+/+_+g`
       do
           cat pbi/$D >>physiobank-index
       done
       echo PhysioBank Index updated
       # Force the server to reload the index, or start the server if needed.
       killall -HUP pbsqsd 2>/dev/null || ( pbsqsd; echo pbsqsd restarted )
       # Give the server a chance to start loading the index.
       sleep 2
       # Regenerate the list of all records known to the server by querying it.
       # This list is needed by 'pbs-not'.
       pbsqsc "record ?" >RECORDS-ALL
       ;;
esac