         Subroutine xsel_init(prefix_str,prefixlen)
c --------------------------------------------------------------------
c
c This is the starting-up routine of the xselect. 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 prefix_str
      integer*2 prefixlen
      character*255 str1
      character*255 tmpf
c General reusable integers for lengths of strings 
      integer len1, len2, len3,len4
c ---------------------------------------------
c To avoid meaningless compilation errors on the Decstation
      logical MAINLP
c ---------------------------------------------
c To test for presence of saved session
      logical SAVED
c ---------------------------------------------
c LENACT, and i
      integer LENACT
      integer i
c OLD is for setting values if you use an old save file
      integer old

C previous xslname 
      character*255 prev_xslnam 

c ---------------------------------------------
c DATA STATEMENTS
c ---------------------------------------------
c Fill logicals and filenames with their startup values.

      data READ, SPEC, CURV, SELCT       / 4*.FALSE. /
      data MERGED, MANY, MADE, WORK      / 4*.FALSE. /
      data MERGTI, FILTER, USEGTI        / 3*.FALSE. /
      data MERGHK, USEHK, EXPAND,HKSEL   / 4*.FALSE. /
      data MANYHK, HKREAD, WORKHK,LOADED / 4*.FALSE. /
      data IMAGE,CLEAN,CLEANI,ECHO       / 4*.FALSE. /
      data FITTFL,ASCTFL,XWNTFL,REGION   / 4*.FALSE. /
      data FFTFL,UNBIN,EVTSAV,CLNSAV     / 4*.FALSE. /
      data HKCURV,FFCURV,USEQDP          / 3*.FALSE. /
      data SMOOTH,SAVSMOOTH              / 2*.FALSE. /
      data FAST,FAINT,INTENS             / 3*.FALSE. /
      data WTMAPB                        / .FALSE.  /
      data MAINLP,SHOWOC                 / 2*.TRUE.  /
      data VIMAGE,VSPEC,VREGION,VPHACUT  / 4*.TRUE.  /
      data VGISCLEAN,HAVEMKF             / 2*.FALSE.  /
      data BINOUT,BININ                  / 2*.FALSE. /
      data DETFL                         /   .FALSE. /
c ---------------------------------------------
c Number of files read in
      data nfiles /0/
c Number of HK files read in
      data nhkfil /0/
c Number of hk parameters read in
      data npar /0/
c Number of ff parameters read in
      data nffpar /0/
c Set the number of entered filters to 0
      data numreg,numdet,numasc,numgti,numcti,numhnd /6*0/
c Filter file name
      data ffilin /'def'/
c Number of filter files entered
      data nmkf /0/      
c set the data dir and mkfdir to empty values
      data datdir,mkfdir,mkfdnm / 3*'NONE'/
c Set the plot device to NONE
      data plotdv /'NONE'/
c This flags that the list of drivers has not yet been loaded
      data devlst(1) /'NOT_LISTED'/
c This flags that the list of mkf parameters have not been loaded
      data mkfpar(1) /'NOT_LISTED'/
c Set instrument to none at start
      data instru /'NONE'/
c set the datamode to it's empty value
      data datamode /'NONE'/
c There is no dfe file yet:
      data usrdfe /'-'/
c Set the default pagewidth size
      data pgwth /128/
c Set the selection, HK & catalogue selection to NONE:
      data hkstr,catflt,choflt,catsel /4*'NONE'/
      do i=1,MXHIST
         strsel(i) = 'NONE'
         ffstr(i) = 'NONE'
      enddo
      data nstrsel,nffstr /2*0/
c Set number of phase and intensity intervals to 0
      data nphase,numint /2*0/
c Set the grade filter to NONE
      data gfilter /'NONE'/
c Set the column filter to a blank string
      data colfilter /' '/

c Set DEFAULT MISSION to ASCA
      data keymis /'ASCA'/
c Set DEFAULT SUBMIS to NONE
      data submis /'NONE'/
c Initialize the status parameter:
      data status /0/
c Initialize the timdel keyword(-ve means it has not been found yet):
      data timedel /-1.0d0/
c Initialize the catalogue name:
      data catnam /'NONE'/

c Set the evtnum and gtinum to the ASCA defaults:
      data evtnum,gtinum /'1  ','2  '/
      
c Negative values for phalcut and phahcut signal not yet set:
      data phalcut,phahcut /2*-20/
c set the phamin and max
      data phamin,phamax /100000,0/
c The default pha binsize should be 1
      data phabin / 1 /
c The default x binning factor for the image
      data xbinf /1/
c Set the offset and size factors:
      data  xch,ych,xcf,ycf,sizeh,sizef /4*0,2 * -1/
c The day2sec constant:
      data day2sec / 86400D0 /


c ############################################################
c NOTE: PLACES TO FIDDLE WITH TO GET OTHER MISSIONS TO WORK:
c Any mission using event files that requires no special ftools
c can be added by modifying the xsel14.mdb file. The best way
c to start is to copy the entries for an existing mission.
c If the mission requires special ftools then look in the file
c xsel_mission.f file for the current mission-specific routines
c (ASCA and ROSAT) and see whether any of these can be adapted.
c ############################################################
 

      do i=1,MXNSEL
         CTINDX(i) = 0
         HNDNDX(i) = 0
         INTNDX(i) = 0
      enddo

c ------------------------------
c Now set up version number, and so on...

      include 'xsel_ver.inc'

c ------------------------------

c  Initialize the filename arrays:
      do i=1,MAXFIL
         filenm(i) = ' '
         hkflnm(i) = ' '
         gtifnm(i) = ' '
      enddo

c
c
c ---------------------------------------------
c PROGRAM STARTS IN EARNEST
c ---------------------------------------------
c
c

c First, load the par file:
      len1 = LENACT(xslnam)
      len2 = LENACT(xsldsk)
      len3 = lenact(xsldir)
      CALL TBLDSTAND(xslnam(:len1),xsldsk(:len2),xsldir(:len3),
     &               ierr)
C      do i = 1, tblpcnt
C        print*,tblpname(i)(1:lenact(tblpname(i))),',', 
C     &          tblptype(i)(:lenact(tblptype(i))),',', 
C     &          tblpupd(i)(:lenact(tblpupd(i))),',', 
C     &          tblpdefl(i)(:lenact(tblpdefl(i))),',', 
C     &          tblpminp(i)(:lenact(tblpminp(i))),',', 
C     &          tblpmaxp(i)(:lenact(tblpmaxp(i))),',', 
C     &          tblpdesc(i)(:lenact(tblpdesc(i))),',', 
C     &          tblpupda(i)(:lenact(tblpupda(i))),',' 
C      enddo 

C Then load the .cmd1, .kw, .key , and .udc1 files. 
      prev_xslnam = xslnam
      xslnam = "t"//xslnam
      len1 = LENACT(xslnam)
      str1=xslnam(:len1)

      call ptend(xsldsk(:len2),xsldir(:len3),str1) 
      len4 = lenact(str1)
      tmpf = str1(:len4)//'.cmd1'
      CALL XPITBLDCM(tmpf,ierr)  
      if(ierr.ne.0) return 
     
      tmpf = str1(:len4)//'.kw'
      CALL TBLDKW1(tmpf,ierr)  
      if(ierr.ne.0) return 

      tmpf = str1(:len4)//'.key'
      CALL TBLDKY(tmpf,ierr)  
      if(ierr.ne.0) return 

C      do i = 1, tblkcnt
C        print*,tblkname(i)(1:lenact(tblkname(i))),',',
C     &          tblkparm(i)(:lenact(tblkparm(i))),',',
C     &          tblkposo(i),' ',tblkcnt 
C      enddo 
C      pause

      
      xslnam = prev_xslnam
      len1 = LENACT(xslnam)
      str1=xslnam(:len1)
      call ptend(xsldsk(:len2),xsldir(:len3),str1) 
      len4 = lenact(str1)
      tmpf = str1(:len4)//'.udc1'
      CALL TBLDAL(tmpf,ierr)  
      if(ierr.ne.0) return 

c
c Set up the chattiness for XSELWRITE
C      CALL XCHATY(6,10)
      
c type the startup banner
      
      call XSELWRITE(' ',5)
      write(str1,10)xslver(1:1),xslver(2:4)
 10   format(25x,'**  XSELECT V',a1,'.',a3,' ** ')
      call XSELWRITE(str1,5)
      call XSELWRITE(' ',5)

C     initialize the yaccfor common blocks
      call ldpospar("XSELECT_INIT")
c Now prompt for the prefix, and load the file names:

     
      call XSL_LOAD_NAMES(0)

      call XSL_RESTORE(SAVED,old)

c Check whether this process has been told that it is running in
c background

      call txsel_uclgsb('batchmode',QBATCH,status)

c Locate ref data dir and the leapsecond file 
      
      call txsel_uclgst('ref_data_dir',refdir,status)
      call XSL_EXPAND_DIR(refdir,status)

      if ( status.ne.0 ) then
         call XSELWRITE
     &        ('Warning: could not find ref data directory at',5)
         str1 = '   '//refdir
         call XSELWRITE(str1,5)
         call XSELWRITE('The commands FILTER TIME UT, GISCLEAN and'//
     &        ' FAINT bright=no require this.',5)
         call XSELWRITE('It can be found at the same place you got '//
     &        'the FTools tar file.',5)
         call XSELWRITE('If you stored it in a different place than'//
     &        ' the FTools area, quit Xselect, set the path with:',5)
         str1 = '    pset xsel'//' ref_data_dir=your_path'
         call XSELWRITE(str1,5)
         call XSELWRITE('and then restart Xselect.',5)
         refdir = 'NONE'
         status = 0
      endif

c If there is no save file, the mission is set for the value
c given in the default_mission parameter

      IF(SAVED) then

C Load all the generic Mission parameters:

         call XSL_SET(3)
         IF(old.eq.1)THEN
            call XSELWRITE('Save files before .96 don''t store '//
     &           'coordinates',5)
            call XSELWRITE('Setting them to default values',5)
            call XSL_SET(2)
         ENDIF

C Now set the prompt:

         prompt = prefix(:LENACT(prefix))
         IF(keymis.ne.'NONE') THEN
            prompt = prompt(:LENACT(prompt))//':' 
     &           //keymis(:LENACT(keymis))
         ENDIF
         IF(instru.ne.'NONE') then
            prompt = prompt(:LENACT(prompt))//'-'//
     &           instru(:LENACT(instru))
         ENDIF          
         IF(datamode.ne.'NONE') then
            prompt = prompt(:LENACT(prompt))//'-'//
     &           datamode(:LENACT(datamode))
         ENDIF          
         prompt = prompt(:LENACT(prompt))//' >'
         SAVED = .FALSE.

      ELSE

C Reset the values stored in the par file.

         call txsel_uclgsr('stored_binsize',binsiz,status)

c Don't reset binsize to 0 by default:

         IF(binsiz.eq.0) THEN
            binsiz = 16.0
            call XSELWRITE(
     &           'Warning - resetting binsize from 0.0 to 16.0',5)
         ENDIF
         status = 0
         call txsel_uclgsi('stored_pharebin',phabin,status)
         status = 0
         call txsel_uclgst('stored_devicetype',str1,status)
         plotdv = str1
         call XSL_SET(2)

      ENDIF

c If plotdv has not been set by either of these, then 
c look for the PGPLOT_TYPE environment variable, and if present,
c set plotdv:

      IF(plotdv.eq.'NONE'.or. plotdv.eq.'/NONE') THEN
         call TRLOG('PGPLOT_TYPE',11,str1,len1)
         if(len1.ne.0) then
	    tmpf = 'Setting plot device to '//str1(:len1)
            call XSELWRITE(tmpf,5)
            plotdv = str1(:len1)
         endif
      ENDIF


c First set up the prompt

      prompt = prefix(:LENACT(prefix))
      IF(keymis.ne.'NONE') THEN
         prompt = prompt(:LENACT(prompt))//':'
     &        //keymis(:LENACT(keymis))
      ENDIF
      IF(instru.ne.'NONE') then
         prompt = prompt(:LENACT(prompt))//'-'
     &           //instru(:LENACT(instru))
      ENDIF
      IF(datamode.ne.'NONE') then
         prompt = prompt(:LENACT(prompt))//'-'
     &           //datamode(:LENACT(datamode))
      ENDIF
      prompt = prompt(:LENACT(prompt))//' >'
      prefix_str = prompt(:lenact(prompt))
      prefixlen=lenact(prompt)
C
C     initialize the logicals
C
      call xsel_set_control
      Return 
      End 


