
C     ------- This subroutine writes the output data file 50 -------
      
      subroutine writeda(wnmax, wnmin, stepsi, dos4, domega,Temp,u,
     *     sigaf,sig0af,gafin,gaf,g0af,af,nocc,mu,mu0t,print_test,
     *     gm, om,ommax,stepm,g0a,sigat,l,imse_mode,g0m,imin,sigm)

      real*8 pi
      parameter(pi=3.141592653589793d0)

      integer n,imin
      parameter(n=2048*4)

C     (n must be a power of 2.)
      
      integer i,wnmax, wnmin, stepsi,print_test,stepm,l,imse_mode

      real*8 dop,wout,rout,iout
      real*8 domega, dos4(n),Temp,u,residue,nocc,mu,mu0t
      real*8 om(n),ommax
      double complex sigaf(n),sig0af(n),sigm(n),sigat(n)
      double complex gm(n),g0a(n),g0m(n),gafin(n),g0af(n)
      double complex af(n),gaf(n)
           
      open(50, file='hmptg2.dat',status='unknown')
      open(60, file='hmpt2.dat',status='unknown')
      open(42, file='ser.dat',status='unknown')
      open(77, file='residue_-delta.dat',status='unknown')
      open(78, file='imgr.dat',status='unknown')
      open(79, file='n_mus.dat',status='unknown')

      open(348, file='se0_T.dat',status='unknown')

      open(111, file='img0a.test',status='unknown')
      open(767, file='imsigat.test',status='unknown')

      open(108, file='imgm.dat',status='unknown')
      open(737, file='g0m.dat',status='unknown')
      open(729, file='imsem.dat',status='unknown')
      
      if (print_test.eq.1) then
         open(100, file='imse0f.test',status='unknown')
         open(101, file='gafin.test',status='unknown')
         open(103, file='gaf.test',status='unknown')
         open(105, file='g0af.test',status='unknown')
         open(107, file='reaf.test',status='unknown')
         open(225, file='mu0t_dop_spline.test',status='unknown')
         open(226, file='mu0t_dop_input.test',status='unknown')
      endif

      print*,'output'
      
      if (imse_mode.eq.1) then
         rout = real(sigaf(n/2))
         iout = real(dimag(sigaf(n/2)))
         write(348,*) Temp,rout,iout
      else
         
         dop = real(dfloat(2*l)*nocc-1.d0)
         
         write(50,22) real(Temp),real(u),dop
         write(42,22) real(Temp),real(u),dop
         write(78,22) real(Temp),real(u),dop
         
         if (print_test.eq.1) then      
            dop = real((nocc-0.5)*2.d0)
            write(100,22) real(Temp),real(u),dop
            write(101,22) real(Temp),real(u),dop
            write(103,22) real(Temp),real(u),dop
            write(105,22) real(Temp),real(u),dop
            write(107,22) real(Temp),real(u),dop
         endif
         
 22      format('" Temp=',F4.2,' u=',F4.2,' delta=',F4.2,'"')
 70      format(e12.4,'  ',e12.4)
         do i=wnmin, wnmax, stepsi
            
            wout = real((dfloat(i-n/2))*domega)
            
            write(50,70) wout,real(dos4(i))
            
            rout = real(dble(sigaf(i))) 
            iout = real(dimag(sigaf(i)))
            write(42,'(3e18.10)') wout,rout,iout

            write(78,*) wout,real(dos4(i)*pi)

            if (print_test.eq.1) then

               write(100,*) wout,real(dimag(sig0af(i)))
               
               rout = real(dble(gafin(i)))
               iout = real(dimag(gafin(i)))
               write(101,'(3e18.10)') wout,rout,iout
               
               rout = real(dble(gaf(i)))
               iout = real(dimag(gaf(i)))
               write(103,*) wout,rout,iout
               
               rout = real(dble(g0af(i)))
               iout = real(dimag(g0af(i)))
               write(105,*) wout,rout,iout
               
               write(107,*) wout,real(dble(af(i)))
               write(111,*) wout,real(dimag(g0a(i)))

            endif

         enddo       
         write(50,*) '   '
         write(42,*) '   '
         write(78,*) '   '
         if (print_test.eq.1) then
            write(100,*) '   '
            write(101,*) '   '
            write(103,*) '   '
            write(105,*) '   '   
            write(107,*) '   ' 
            do i=1,n 
               write(767,*) real(i),real(dble(sigat(i)))
            enddo
            write(767,*)  '   ' 
         endif
         
         do i=imin,n,stepm
            if (om(i).lt.ommax) then
               write(108,*) real(om(i)), real(dimag(gm(i)))
               write(737,*) om(i), g0m(i)
               write(729,*)  real(om(i)), real(dimag(sigm(i)))
            endif
         enddo
         residue=1.d0/(1.d0-dble(sigaf(n/2+1)
     *        -sigaf(n/2-1))/domega/2.d0)
         write(77,*) real(-(2.d0*l*nocc-1.d0)), real(residue)
         write(79,*) mu,mu0t,2.d0*l*nocc

      endif
      return
      end
      
C     ===================================================================      
       

