
      SUBROUTINE autpro()

      INCLUDE '../../inc/xspec.inc'

c **	** 	fwj haberl 	28 oct 1986
c **	**	kaa		26 dec 1989
c **	** 	fwj haberl 	26 apr 1991
c               adjusted to XSPEC 7.1 with more than 1 data group
c               dah             01 apr 1996
c               Adapted from WRTPRO
c
c	subroutine to write out profile for auto save function


      INTEGER profunit, ierrsf

      CHARACTER filnam*25, tmpstr*255

      LOGICAL qexist
      INTEGER checkopen

      REAL csmgH0, csmgq0, csmgl0, xgmsys      
      CHARACTER xgstat*11, fgsolr*4, fgxsct*4, fgslfn*255
      LOGICAL xgfclc
      EXTERNAL  xgstat, fgsolr, fgxsct, xgfclc, csmgH0, csmgq0, csmgl0
      EXTERNAL xgmsys, fgslfn

      DATA filnam/'xautosav.xcm'/


      CALL getlun(profunit)

c  open the output command file

      tmpstr = filnam
      if (checkopen(tmpstr,'w').eq.1) then
      
           CALL openwr(profunit, tmpstr, 'unknown', ' ', 'l', 256, 
     &          0, ierrsf)
           
      else
         CALL xwrite(' Unable to open file for autosave, autosaving '//
     &               'will be disabled.', 5)
         CALL cpasav(0)
         RETURN
      ENDIF

c  files

      CALL prfile(profunit)
      CALL prchns(profunit)
      CALL prrsp(profunit)

c  model

      IF ( xgstat() .EQ. 'Chi-Squared' ) THEN
         WRITE(profunit, '(1x,a)') 'statistic chi'
      ELSEIF ( xgstat() .EQ. 'C-statistic' ) THEN
         WRITE(profunit, '(1x,a)') 'statistic cstat'
      ELSEIF ( xgstat() .EQ. 'L-statistic' ) THEN
         WRITE(profunit, '(1x,a)') 'statistic lstat'
      ENDIF
      IF ( fgsolr() .EQ. 'file') THEN
         WRITE(profunit, '(1x,a,a,1x,a)') 'abund ', fgsolr(), fgslfn()
      ELSE
         WRITE(profunit, '(1x,a,a)') 'abund ', fgsolr()
      ENDIF
      WRITE(profunit, '(1x,a,a)') 'xsect ', fgxsct()
      IF ( xgfclc() ) THEN
         WRITE(profunit, '(1x,a,a)') 'xset forcecalc on'
      ELSE
         WRITE(profunit, '(1x,a,a)') 'xset forcecalc off'
      ENDIF
      WRITE(profunit, '(1x,a,3(1x,f7.3))') 'cosmo ', csmgH0(), 
     &                                     csmgq0(), csmgl0()
      IF ( xgmsys() .NE. 0.0 ) THEN
         WRITE(profunit, '(1x,a,1pg12.5)') 'systematic ', xgmsys()
      ENDIF

      CALL prmodl(profunit)

      CLOSE (profunit)
      CALL frelun(profunit)

      RETURN
      END
