C*************************************************************************
	SUBROUTINE RELOAD(filename,ln,sname,s_ln,dname,d_ln,
	1	bitmap,bitmap_ln,*,*)
C*************************************************************************
C Subroutine to load a "LOAD" file 
C   The filename to be read is filename(1:ln), the default extension
C     is .SEA_IN
C*************************************************************************
	CHARACTER*(*) filename,sname,dname,bitmap
	INTEGER ln,s_ln,d_ln,bitmap_ln
	INTEGER*2 i1,i2
	LOGICAL STATUS
C
C   The first RETURN point is used if the filename does not 
C     exist -- none of the LOAD parameters will have been changed
C   The second RETURN point is used if an ERROR is encountered while 
C     reading, in which case the load parameters will need to be reset.
C
      	include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'

	LOGICAL DTOTT,TTTOD,DTOPDF,PDFTOD,DTOIND,INDTOD,flag
	character*40 node,device,dir,name*60,type*20,ver*5
	integer*2 len_node,len_device,len_dir,len_name,len_type,len_ver

C The LOAD parameters (in addition to those in SEARCH.PAR) are:
C   DNAME(:d_ln)          	the name of the raw data file
C   bitmap(:bitmap_ln)	SUBFILE name (NSET=-1 only)

C If the read is successful SNAME(:s_ln), the search root name, is set
C to be the file name of the LOAD file

C get the full file name
	INQUIRE(file=filename(1:ln),name=name,
	1	defaultfile='.SEA_IN')
	len_name = lench(name)
	open(unit=in,file=filename(1:ln),defaultfile='.SEA_IN'
	1	,err=722,status='old')
	write (lo,'(A)') ' Reading "LOAD" file: '//name(1:len_name)

C read the contents of the LOAD file 
	read (in,'(a)',err=702) TITLES
72	FORMAT(A)
	read (in,72,err=702) DNAME
	d_ln = LENCH(DNAME)
	read (in,*,err=702) IMIN,JCMSMX,mphase,MINMATCH,iomin
	read (in,*,err=702) D2T,FFWHM
	read (in,*,err=702) numob
	read (in,*,err=702) (DS(i),i=1,numob)
	read (in,*,err=702) (inten(i),i=1,numob)
	read (in,'(10i6)',err=702) nset,pindex,pimin,iindex,iimin,
	1	NSORT,NFOM
	bitmap = ' '
	IF (nset .eq. -1) read(in,72,err=702) bitmap
	bitmap_ln = LENCH(bitmap)

C set the excluded areas
	call exclr(extbl)
100	read (in,*,err=702,end=101) i1,i2
	IF (i1 .gt. i2) goto 101
	call exset(extbl,i1,i2,.true.)
	goto 100
101	close(unit=in)

C set SNAME from the specified load file name
	call fparse(filename(1:ln),node,len_node,device,len_device,
	1	dir,len_dir,name,len_name,type,len_type,ver,len_ver)
	sname = node(:len_node)//device(:len_device)//dir(:len_dir)
	1	//name(:len_name)
	s_ln = len_node + len_device + len_dir + len_name
C read in the bitmap
430	CALL GETBIT(nset,bitmap,bitmap_ln,*9)
C check that the datafile is readable
	IF (d_ln .gt. 0) call GETDATA(dname,d_ln,*33)
C wait for a prompt to continue
440	CALL PS_PAUSE(prompt)
	RETURN
C error -- unable to load the data file 
33	write (lo,'(2A)') ' WARNING -- unable to reread data file: ',
	1	dname(:d_ln)
	d_ln = 0
	goto 440
C error reading bitmap; set NSET to 0 and try again
9	NSET = 0
	WRITE (LO,'(A)') ' RELOAD WARNING -- unable to read SUBFILE '//
	1	bitmap(:bitmap_ln),
	2	' ...Setting subfile to ENTIRE JCPDS',' '
	bitmap_ln = 0
	CALL GETBIT(nset,bitmap,bitmap_ln,*111)
	goto 430
C------------------------------------------------------------------------
702	WRITE (LO,'(A)') ' RELOAD ERROR -- error reading "LOAD" file:'//
	1	name(1:len_name)
	CLOSE (IN)
	RETURN 2
C------------------------------------------------------------------------
722	WRITE (LO,'(A)') ' RELOAD ERROR -- unable to open "LOAD" file:'//
	1	name(1:len_name)
	CLOSE (IN)
	RETURN 1
111	stop 'RELOAD ERROR -- unable to recover from GETBIT error'
	END
