#!/bin/tcsh -f

set strt = $PWD

if ("$1" == '-h' || "$2" == '-help') then
   goto HELP
endif

set subj = 620
set fs = 50
set single = 0
set duration = $1

set dt = `ccalc 1/$fs`
if ("$duration" != "") then
   set nsamp = `ccalc -i "$fs * $duration"`
   set sampsel = "{0..$nsamp}"
else
   set sampsel = ''
endif

if ($single) then
   1dplot   -dt $dt \
            -xlabel 'time sec' -title 'photoplethysmograph'  \
            -ylabel 'Pulse. Ox. signal'   \
            ../RetroTS.all/s{$subj}_ecg_rest.dat"$sampsel"  &

   1dplot   -dt $dt \
            -xlabel 'time sec' -title 'respiration'  \
            -ylabel 'Chest Belt signal'   \
            ../RetroTS.all/s{$subj}_respir_rest.dat"$sampsel"  &
else
   1dplot   -sepscl -dt $dt \
            -xlabel 'time sec' \
            -ynames respiration photoplethysmograph - \
            -title 'Breathing and Heart Rhythm Traces' \
            ../RetroTS.all/s{$subj}_respir_rest.dat"$sampsel"  \
            ../RetroTS.all/s{$subj}_ecg_rest.dat"$sampsel"   &
endif

echo ""
echo "Hit Enter to start matlab and show demo"
echo ""
set jnk = $<
echo "One more time"
echo ""
cd ../RetroTS.all/

matlab -nodesktop -r RunRetroTS_Demo'(620)'

echo ""
echo "Hit Enter to see regressors for slice 0"
echo ""
set jnk = $<
echo "One more time"
echo ""

set l = `\grep ColumnLabels demo.s620_RTS.slibase.1D | cut -d'"' -f 2  `
set n = 1;
set ll = ()
while ($n<14)
   set ll = ($ll `echo $l | cut -d';' -f $n`)
   @ n ++
end
1dplot   -sepscl -dt 3.5 \
         -xlabel 'time sec' \
         -ynames $ll - \
         -title 'Breathing and Heart Rhythm Traces for slice 0'\
         demo.s${subj}_RTS.slibase.1D'[0..12]'  &
         

goto END

HELP:

echo "Usage: `basename $0` [duration]"
echo ""
echo "Show respiration and pulse oximeter signals"
echo "If you specify a duration in seconds, the"
echo "signal is plotted from 0 to the chosen duration."
echo "Otherwise the whole time series is plotted."
echo ""

END:
cd $strt
