C---------------------------------------------------------------------------
	SUBROUTINE WRITEDAT(nfil,*)
C---------------------------------------------------------------------------
C
C	WRITE OUT A FILE IN ONE OF SEVERAL FORMATS
C
C  Note that the file name and FEXISTS is updated only after writing 
C  at DuPont file -- this is the only current "complete" format
C---------------------------------------------------------------------------
	include 'shared$common'

	INTEGER STATUS,CLI$GET_VALUE,CLI$PRESENT
	CHARACTER*80 filename,newext
	INTEGER LN
	LOGICAL*1 FLAG
	LOGICAL*1 FILEF,NBSF,JONESF,BINRYF,PEAKSF,ians
	LOGICAL*1 PROFPV,GSAS

	INTEGER nfil,NPT
	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
	INTEGER*2 iword
	byte byte1,byte2,byte3

C get the original file name (without the version number)
	call fparse(cfilnam(nfil),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)//type(:len_type)

C
	FILEF = .true.
	IF (CLI$PRESENT('FILENAME')) THEN
	  STATUS = CLI$GET_VALUE('FILENAME',filename,ln)
	  if (status .ne. %LOC(CLI$_ABSENT) .and. ln .gt. 0) FILEF = .false.
	ENDIF

29	NBSF   = .false.
	JONESF = .false.
	BINRYF = .false.
	PEAKSF = .false.
	PROFPV = .false.
	GSAS   = .false.
	FLAG = .false.
	IF (CLI$PRESENT('NBS')) THEN
	  NBSF   = .true.
	  FLAG = .true.
	ELSEIF (CLI$PRESENT('JONES')) THEN
	  JONESF = .true.
	  FLAG = .true.
	ELSEIF (CLI$PRESENT('PEAKS')) THEN
	  PEAKSF = .true.
	  FLAG = .true.
	ELSEIF (CLI$PRESENT('PROFPV')) THEN
	  PROFPV = .true.
	  FLAG = .true.
	ELSEIF (CLI$PRESENT('GSAS')) THEN
	  GSAS = .true.
	  FLAG = .true.
	ELSEIF (DEFAULT .or. CLI$PRESENT('BINARY')) THEN
	  BINRYF = .true.
	  FLAG = .true.
	ENDIF

	IF (.NOT. FLAG) THEN
	  WRITE (LO,11) 
11	  FORMAT(/,' Choose the format for the file.',/,
	1	'     J for JONES,',/,'     N for NBS,',/,
	3	'     P for Peaks,',/,'     C for PROFPV (COX),',/,
	5	'     G for GSAS,',/, '     D for DuPont (binary)',/,
	7	'$Enter a letter for the file choice [D]: ')
	  READ (LI,7) IANS
7	  FORMAT (A1)
	  IF (ians .eq. 'N' .or. ians .eq. 'n') THEN
	    NBSF   = .true.
	  ELSEIF (ians .eq. 'J' .or. ians .eq. 'j') THEN
	    JONESF = .true.
	  ELSEIF (ians .eq. 'P' .or. ians .eq. 'p') THEN
	    PEAKSF = .true.
	  ELSEIF (ians .eq. 'G' .or. ians .eq. 'g') THEN
	    GSAS = .true.
	  ELSEIF (ians .eq. 'c' .or. ians .eq. 'c') THEN
	    PROFPV = .true.
	  ELSE
	    BINRYF = .true.
	  ENDIF
	ENDIF

90	IF (BINRYF) THEN 
	  IF (FILEF) filename = newext(filename,'.DDAT')
	  IF (FILEF .and. .not. DEFAULT) call confname(lo,li,filename)
	  CALL WRITDU(nfil,filename,STATUS)
        ELSEIF (PEAKSF) THEN 
	  IF (FILEF) filename = newext(filename,'.ADR')
	  IF (FILEF .and. .not. DEFAULT) call confname(lo,li,filename)
	  CALL WRITPEAK(nfil,filename,STATUS)
        ELSEIF (NBSF) THEN 
	  IF (FILEF) filename = newext(filename,'.NBS')
	  IF (FILEF .and. .not. DEFAULT) call confname(lo,li,filename)
	  CALL WRITNIST(nfil,filename,STATUS)
        ELSEIF (PROFPV) THEN 
	  IF (FILEF) filename = newext(filename,'.PROF')
	  IF (FILEF .and. .not. DEFAULT) call confname(lo,li,filename)
	  CALL WRITPROF(nfil,filename,STATUS)
        ELSEIF (GSAS) THEN 
	  IF (FILEF) filename = newext(filename,'.GSAS')
	  IF (FILEF .and. .not. DEFAULT) call confname(lo,li,filename)
	  CALL WRITGSAS(nfil,filename,STATUS)
        ELSEIF (JONESF) THEN 
	  IF (FILEF) filename = newext(filename,'.JONES')
	  IF (FILEF .and. .not. DEFAULT) call confname(lo,li,filename)
	  call WRITJNS(nfil,filename,STATUS)
        ELSE
	  write (*,*) 'Illegal file format'
	  STATUS = 3
        ENDIF
        IF (STATUS .eq. 1) goto 99
        IF (STATUS .eq. 3) RETURN 1
        RETURN
C-----------------------------------------------------------------------------
99	ln = lench(filename)
	WRITE (LO,12) filename(:ln)
12	FORMAT (/' ERROR -- UNABLE TO CREATE FILE: ',a)
	IF (PROMPT) THEN
	  WRITE (LO,13)
13	  FORMAT ('$ENTER A NEW FILE NAME. <Default: don''t write file>: ')
	  READ (LI,'(a)') filename
	  ln = lench(filename)
	  if (ln .gt. 0) goto 90
	ENDIF
	WRITE (LO,'(a)') ' WARNING -- NO FILE WAS WRITTEN',' ',' '
	RETURN 1
C--------------------------------------------------------------------------
C Write a binary format file (no command line parse)
	ENTRY WRITESUB(nfil,*)
	FILEF = .true.
	NBSF   = .false.
	JONESF = .false.
	BINRYF = .true.
	PROFPV = .false.
	GSAS   = .false.
C get the original file name (without the version number)
	call fparse(cfilnam(nfil),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)//type(:len_type)
	goto 90
      END
C-----------------------------------------------------------------------------
	SUBROUTINE confname(lo,li,origname)
C-----------------------------------------------------------------------------
C  Confirm the file name choice
C-----------------------------------------------------------------------------
	CHARACTER*(*) origname
	CHARACTER*80 filename
	INTEGER lo,li
	INTEGER LN

	ln = lench(origname)
	WRITE (LO,10) origname(:ln)
10	FORMAT (/' Enter name for output file:'/,'$(Default=',A,'): ')
	READ (LI,'(a)') filename
	ln = lench(filename)
	if (ln .gt. 0) origname = filename
	RETURN
	END
