#!/bin/tcsh -f 

if ( $#argv > 0 ) then
   if ( $argv[1] == '-help' || $argv[1] == '-h' ) then
      echo ""
      echo "@DriveAfni  - a demo program for driving 'afni' from a script"
      echo ""
      echo "The purpose is for one to review this script while executing it."
      echo ""
      echo "This program requres the AFNI_data6 class data directory and must"
      echo "be run just above it (i.e. typing 'ls' should include AFNI_data6"
      echo "in the output)."
      echo ""
      echo "     one output file is created: ./SavedAxiale.jpg"
      echo ""
      echo "See README.driver for details on driver commands."
      echo "See also 'plugout_drive -help'."
      echo "See also 'DriveSuma -help'."
      echo ""
      exit
   endif
endif

# NEEDS afni's class data archive AFNI_data?.tgz
set adir = AFNI_data6

if ( ! -d $adir/afni/ ) then
   echo ""
   echo "** Directory $adir/afni not found."
   echo ""
   echo "You could be in the wrong directory,"
   echo "or you may not have $adir on disk."
   echo ""
   echo "This directory is in archive:"
   echo "    http://afni.nimh.nih.gov/pub/dist/edu/data/$adir.tgz"
   echo ""
   echo "To install archive and rerun this script run the following commands"
   echo "    mkdir ./demo && cd demo"
   echo "    curl -O http://afni.nimh.nih.gov/pub/dist/edu/data/$adir.tgz"
   echo "    tar xzf $adir.tgz"
   echo "    `basename $0`"
   echo ""
   goto END
endif

set PIF = DriveAfniDemo    #A string identifying programs launched by this script

                           #Get a free line and tag programs from this script
set NPB = "-npb `afni -available_npb_quiet` -pif $PIF -echo_edu" 

@Quiet_Talkers -pif DriveAfniDemo   #Quiet previously launched programs


afni $NPB -niml -yesplugouts $adir/afni  >& /dev/null &

plugout_drive  $NPB                                               \
               -com 'SWITCH_SESSION A.afni'                       \
               -com 'OPEN_WINDOW A.axialimage geom=600x600+416+44 \
                     ifrac=0.8 opacity=9'                         \
               -com 'OPEN_WINDOW A.sagittalimage geom=+45+430     \
                     ifrac=0.8 opacity=9'                         \
               -com 'SWITCH_UNDERLAY anat'                        \
               -com 'SWITCH_OVERLAY strip'                        \
               -com 'SEE_OVERLAY +'                               \
               -com 'OPEN_WINDOW A.axialimage keypress=v'         \
               -quit
                                    
set l = `prompt_user -pause  "Hit OK to pause movie and move on"`
if ( $l == '0') goto STEND
 
plugout_drive  $NPB                                               \
               -com 'OPEN_WINDOW A.axialimage keypress=space      \
                                 geom=+56+44 mont=6x3:8'          \
               -com 'SET_DICOM_XYZ A 0 20 40'                     \
               -com 'SAVE_JPEG axialimage SavedAxiale.jpg'        \
               -quit
               
set l = `prompt_user -pause "Hit OK to view the saved image"`
if ( $l == '0') goto STEND

aiv SavedAxiale.jpg &

set l = `prompt_user -pause  "Close image then hit OK to examine time series."`
if ( $l == '0') goto STEND

#Now let us examine the time series
plugout_drive  $NPB                                               \
               -com 'OPEN_WINDOW B.sagittalimage                  \
                     geom=300x300+416+344 ifrac=0.8 opacity=9'    \
               -com 'SWITCH_UNDERLAY B.epi_r1'                    \
               -com 'OPEN_WINDOW B.sagittalgraph                  \
                     geom=400x300+0+344                           \
                     keypress=A'                                  \
               -com 'OPEN_WINDOW B.sagittalgraph keypress=v'      \
               -quit

#Show how sub-brick selection can be done.
#and how the threshold bar can be set
set l = `prompt_user -pause  \
      "Hit OK to pause movie and show sub-brick manipulation"`
if ( $l == '0') goto STEND
plugout_drive  $NPB                                               \
               -com 'OPEN_WINDOW B.sagittalgraph keypress=space'  \
               -com 'SET_SUBBRICKS B 13 5 6'                      \
               -com 'OPEN_WINDOW B.sagittalimage'                 \
               -com 'SET_THRESHNEW B 0.0001 *p'                   \
               -com 'SET_FUNC_RANGE B.5.0'                        \
               -com 'SEE_OVERLAY B.+'                             \
               -com 'SET_DICOM_XYZ B 30 80.7 15'                  \
               -quit   

set l = `prompt_user -pause "Hit OK to command all programs to quit"`
if ( $l == '0') goto STEND
plugout_drive  $NPB                                          \
               -com 'QUIT' -quit

goto END

STEND:
   echo "User initiated halt"
   
END:
