         Subroutine xsel_cmd(commnd, noption)

c --------------------------------------------------------------------
c
c This is the routine to process xselect command. It will be called by the
C Tcl-main program.
C
C The codes are borrowed from the xsel.f. For details and Modification
C history, please read the  original code.
C
c  Authors:    Ning Gan, 1999 January V2.0
c
c -------------------------------------------------------------------

      implicit none
      INCLUDE 'xsel.inc'
      INCLUDE 'xselvar.inc'

c ---------------------------------------------
c DECLARATIONS
c ---------------------------------------------
c Scratch variables
c Strings used, reused, reused again as temporary space
      character*255 str1
c General reusable integers for lengths of strings 
      integer len1, len2, len3

      character*16 commnd 
      integer noption     

      character*255 tmpstr


c ---------------------------------------------
c To test for presence of saved session
      logical SAVED
c ---------------------------------------------
c True if the batchmode parameter is set
      integer LENACT
      integer i
c OLD is for setting values if you use an old save file
      integer old

      IF(status.ne.0) THEN
            write(str1,'(a,i4)') 'Error in Xselect, no. ',status
            call XSELWRITE(str1,7)
C            call GTBUFSTOPSCRIPT()
            status = 0
            IF ( QBATCH ) call EXIT(1)
            return
      ENDIF

c set the option number  
      comno = noption

c Now process the command line

         IF ( commnd.EQ.'QUIT' .OR. commnd.eq.'STOP' .OR.
     &        (commnd.eq.'EXIT' ) ) THEN
            status = 0
            call txsel_uclgsb('save_session',SAVED,status)
c Next save the par file
            IF(SAVED) THEN
               call XSL_SAVE(1)
            ELSE
               CALL XSL_CLEAR(1)
               call XSL_RMFILE(cmdfil)
               CALL XSL_CLEANPAR()
               ierr = 0
               call XPISAVEPAR(ierr)
               call XSL_RMFILE(cmdfil)
               call XSL_EXIT(status)
            ENDIF

c ---------------------------------------------
         ELSE IF(commnd.eq.'BIN'.or.commnd.eq.'EXTRACT')then
c ---------------------------------------------
c New BINning, using EXTRACTor **

            call XSL_BIN(0)
            
c ---------------------------------------------
         ELSE IF(commnd.eq.'CHOOSE')then
c ---------------------------------------------

            call XSL_CHOOSE()

c ---------------------------------------------
         ELSE IF(commnd.eq.'CLEAR')then
c ---------------------------------------------
c CLEAR **
c XSL_CLEAR() combines the functions of the old _CLEAR and
c _EXIT; it removes all the temporary files that have been
c lying around. and resets logicals and filenames.  

            call XSL_CLEAR(0)
c ---------------------------------------------
         ELSE IF(commnd.eq.'CPD') THEN            
c ---------------------------------------------
c CPD device = set dev:
            call XSL_CPD()

c ---------------------------------------------
         ELSE IF(commnd.eq.'ECHO')then
c ---------------------------------------------
c ECHO **
c Toggle Echoing command files to the screen

            call XSL_ECHO(ECHO)
            
c ---------------------------------------------
         ELSE IF (commnd.eq.'FILTER') then
c ---------------------------------------------
c FILTER **
c Enter filter regions
            call XSL_ENTER_FILTER() 

c ---------------------------------------------
         ELSE IF(commnd.eq.'FAINT')then
c ---------------------------------------------
c FAINT **

            call XSL_FAINT()
c ---------------------------------------------
         ELSE IF(commnd.eq.'FAST')then
c ---------------------------------------------
c FAST **

            call XSL_FAST()

c ---------------------------------------------
            ELSE IF(commnd.eq.'GISCLEAN') THEN
c ---------------------------------------------
c GISCLEAN **

               call XSL_GISCLEAN()

c ---------------------------------------------
C         ELSE IF(commnd.eq.'XHELP')then
c ---------------------------------------------
c HELP **
C            status = 0
C            call txsel_uclgst('help_what',str1,status)
C            IF ( status .EQ. 0 ) THEN
C               call UPC(str1)
C               IF ( LENACT(str1) .GT. 0 ) then
C                  str1 = 'Xselect_commnds commnd_Description '
C     &                   //str1
C               ELSE
C                  str1 = 'help'
C               ENDIF
C               tmpstr = xslnam(:lenact(xslnam))//'.hlp'
C               CALL PTEND(xsldsk, xsldir, tmpstr)
C               CALL F_IHF(tmpstr, str1)
C            ENDIF
c ---------------------------------------------
         ELSEIF(commnd.eq.'XLIST')then
c ---------------------------------------------
c LIST **
            call XSL_LISTSTUFF()
            
c ---------------------------------------------
         ELSEIF(commnd.eq.'XLOAD')then
c ---------------------------------------------
c LOAD **
            call XSL_LOAD()
            

c ---------------------------------------------
         ELSEIF(commnd.eq.'MKFBIN')then
c ---------------------------------------------
c MKFBIN **

            call XSL_MKFBIN()

c ---------------------------------------------
         ELSEIF(commnd.eq.'HKBIN')then
c ---------------------------------------------
c HKBIN **

            call XSL_HKBIN()


c ---------------------------------------------
         ELSEIF(commnd.eq.'MAKE')then
c ---------------------------------------------
c MAKE **

            call XSL_MAKEO(0)


c ---------------------------------------------
         ELSEIF(commnd.eq.'PLOT')then
c ---------------------------------------------
c PLOT **

            call XSL_PLOT()

c ---------------------------------------------
         ELSEIF(commnd.eq.'XREAD')then
c ---------------------------------------------
c READ **
             
c Set up default keywords for the mission.  
c For now we do this at the very beginning.
c           IF(.not.READ.and. .not.HKREAD) then
c              call XSL_MISSION()
c           ENDIF
c Do the read
            call XSL_READ()

c ---------------------------------------------
         ELSEIF(commnd.eq.'SAOIMAGE')then
c ---------------------------------------------
c IMAGE **

            call XSL_SAOIMAGE(0)

c ---------------------------------------------
         ELSEIF(commnd.eq.'SAVE')then
c ---------------------------------------------
c SAVE **

            call XSL_SAVE(0)

c ---------------------------------------------
         ELSEIF(commnd.eq.'SELECT')then
c ---------------------------------------------
c SELECT **
            call XSL_SELECT()

c ---------------------------------------------
         ELSEIF(commnd.eq.'XSET')then
c ---------------------------------------------
c SET **
            call XSL_SET(0)

c ---------------------------------------------
         ELSEIF(commnd.eq.'SHOW')then
c ---------------------------------------------
c SHOW **

            call XSL_SHOW()


c ---------------------------------------------
         ELSEIF(commnd.eq.'SISPI')then
c ---------------------------------------------
c SHOW **

            call XSL_SISPI()



c ---------------------------------------------
         ELSEIF(commnd.eq.'SMOOTH')then
c ---------------------------------------------
c SMOOTH **

            call XSL_SMOOTH()


c ---------------------------------------------
         ELSEIF(commnd.eq.'SISCLEAN')then
c ---------------------------------------------
c SISCLEAN **

            call XSL_SISCLEAN()

c ---------------------------------------------
         ELSEIF(commnd.eq.'UNSELECT')then
c ---------------------------------------------
c UNSELECT **
            call XSELWRITE(' UNSELECT is not available yet.',5)

c ---------------------------------------------
c We're done.
c ---------------------------------------------
         ELSE   
            str1 = 'Unknown Command: '//commnd 
            call xselwrite(str1,5)
         ENDIF
C ------------------------------------------
C Scan the control logicals
C ------------------------------------------

      call xsel_set_control
      RETURN
      END
c
c ---------------------------------------------
c Code ends.
c --------------------------------------------- 
      
      Subroutine xsel_set_control()
      implicit none
      INCLUDE 'xsel.inc'
      INCLUDE 'xselvar.inc' 
      LOGICAL mytestt, mytestf,mytestu
      
      mytestf = .false.
      call set_xsel_ctrl("mytestf",mytestf)
      mytestt = .true.
      call set_xsel_ctrl("mytestt",mytestt)
      call set_xsel_ctrl("mytestu",mytestu)

      call set_xsel_ctrl("READ",read)
      call set_xsel_ctrl("SPEC",spec)
      call set_xsel_ctrl("CURV",curv)
      call set_xsel_ctrl("IMAGE",image)
      call set_xsel_ctrl("CLEANI",cleani)
      call set_xsel_ctrl("CLEAN",clean)
      call set_xsel_ctrl("DIRTY",dirty)
      call set_xsel_ctrl("FFCURV",ffcurv)
      call set_xsel_ctrl("HKCURV",hkcurv)
      call set_xsel_ctrl("SELCT",selct)
      call set_xsel_ctrl("HKSEL",hksel)
      call set_xsel_ctrl("FFTFL",fftfl)
      call set_xsel_ctrl("MERGED",merged)
      call set_xsel_ctrl("USEHK",usehk)
      call set_xsel_ctrl("HKREAD",hkread)
      call set_xsel_ctrl("MANYHK",manyhk)
      call set_xsel_ctrl("MERGHK",merghk)
      call set_xsel_ctrl("EXPAND",expand)
      call set_xsel_ctrl("MANY",many)
      call set_xsel_ctrl("MADE",made)
      call set_xsel_ctrl("LOADED",loaded)
      call set_xsel_ctrl("SHOWOC",showoc)
      call set_xsel_ctrl("WORK",work)
      call set_xsel_ctrl("WRKGTI",wrkgti)
      call set_xsel_ctrl("WORKHK",workhk)
      call set_xsel_ctrl("FILTER",filter)
      call set_xsel_ctrl("REGION",region)
      call set_xsel_ctrl("ASCTFL",asctfl)
      call set_xsel_ctrl("XWNTFL",xwntfl)
      call set_xsel_ctrl("XPHTFL",xphtfl)
      call set_xsel_ctrl("XPHSAV",xphsav)
      call set_xsel_ctrl("FITTFL",fittfl)
      call set_xsel_ctrl("DETFL",detfl)
      call set_xsel_ctrl("BININ",binin)
      call set_xsel_ctrl("BINOUT",binout)
      call set_xsel_ctrl("EVTSAV",evtsav)
      call set_xsel_ctrl("CLNSAV",clnsav)
      call set_xsel_ctrl("WTMAPB",wtmapb)
      call set_xsel_ctrl("ECHO",echo)
      call set_xsel_ctrl("TORDER",torder)
      call set_xsel_ctrl("UNBIN",unbin)
      call set_xsel_ctrl("USEQDP",useqdp)
      call set_xsel_ctrl("FAST",fast)
      call set_xsel_ctrl("FAINT",faint)
      call set_xsel_ctrl("VIMAGE",vimage)
      call set_xsel_ctrl("VSPEC",vspec)
      call set_xsel_ctrl("VREGION",vregion)
      call set_xsel_ctrl("VPHACUT",vphacut)
      call set_xsel_ctrl("VGISCLEAN",vgisclean)
      call set_xsel_ctrl("INTENS",intens)
      call set_xsel_ctrl("HAVEMKF",havemkf)
      call set_xsel_ctrl("SMOOTH",smooth)
      call set_xsel_ctrl("SAVSMOOTH",savsmooth)
      return 
      end
