	SUBROUTINE WRLOAD(sname,s_ln,dname,d_ln,bitmap,bitmap_ln,*)
C
C Subroutine to write a "LOAD" file 
C   the alternate RETURN point is used if the filename cannot be created
C
	CHARACTER*(*) sname,dname,bitmap
	INTEGER s_ln,d_ln,bitmap_ln

       include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'

	LOGICAL DTOTT,TTTOD,DTOPDF,PDFTOD,DTOIND,INDTOD,flag
	LOGICAL GET_NUMBER,GET_LOGICAL,DEF_LOGICAL,GET_FNUMBER
	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
	CHARACTER*100 filename
	INTEGER ln
	LOGICAL EXRtst
	INTEGER*2 i1,i2,i,j

C write the LOAD file with default extension .SEA_IN
	open(unit=out,defaultfile='.SEA_IN',file=Sname(1:S_ln),
	1	err=701,status='new',carriagecontrol='list')
	INQUIRE(defaultfile='.SEA_IN',file=Sname(1:S_ln)
	1	,NAME=filename)
	ln = lench(filename)
	
	write(LO,'(A)') ' Writing LOAD file:'//filename(:ln)
	write (out,'(A80,a)') TITLES,'SEARCH '//ver_no
	write (out,'(A)') DNAME(1:d_ln)
	write (out,*) IMIN,JCMSMX,mphase,MINMATCH,iomin
	write (out,*) D2T,FFWHM
	write (out,*) numob
	write (out,*) (DS(i),i=1,numob)
	write (out,*) (inten(i),i=1,numob)
	write (out,'(10I6)') NSET,PINDEX,pimin,iindex,iimin,NSORT,NFOM
	IF (NSET .eq. -1) WRITE(OUT,'(A)') bitmap(1:bitmap_ln)

C Determine and write the excluded regions
	amin = -31417
	amax = 31416

C   Find a bit that has been set as excluded
430	do i=amin+1,amax
	  if (EXRtst(extbl,i)) then 
C  got an excluded region, how far does it go?
	    i1 = i
	    i2 = i
	    amin = i
	    do j=i+1,amax
	      amin = j
	      if (.not. EXRtst(extbl,j)) goto 431
	      i2 = j
	    enddo
431	    WRITE(OUT,'(2i8)') i1,i2
	    goto 430
	  endif
	enddo

C no more regions -- write a dummy record
	WRITE(OUT,'(2i8)') 31999,-31999
	close(unit=out)
C parse the file name and use set LOGICAL name SEARCH_FILE to the 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)
	filename = node(:len_node)//device(:len_device)//dir(:len_dir)
	1	//name(:len_name)
	ln = len_node + len_device + len_dir + len_name 
	IF (type(:len_type) .ne. '.SEA_IN') then 
	  filename = filename(:ln)//type(:len_type)
	  ln = ln + len_type
	ENDIF
	flag = DEF_LOGICAL('SEARCH_FILE',filename(1:ln),ln,1,lgprc)
1	return
701     filename = sname
	write(LO,'(A)') ' Error opening output file:'//filename
	RETURN 1
	end
