      subroutine qdf_gnu2(iug,n,x,fmin,fmax) ! bernd berg, june 18 2002.
c gnuplot for the peaked cumulative distribution function (qcdf).
c the array x has to be sorted on input. 
c probability range restricted to 0<=fmin<fmax<=1. fmin and fmax 
c are on return overwritten by correspontind xmin and xmax.
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      character ci*2
      dimension x(n)
      data icnt /0/
      save icnt
c
      if(fmin.lt.zero.or.fmin.ge.fmax.or.fmax.gt.one) then
        print*,"qdf_gnu2: fmin,fmax =",fmin,fmax
      stop "qdf_gnu2: fmin or fmax set false."
      endif
      icnt=icnt+1
      if(icnt.gt.99) stop 'df_gnu: inct=99 exhausted!'
      write(ci,'(i2.2)') icnt
c
c gnuplot script:
      open(iug,file='qdf'//ci//'.plt',status='unknown',form='formatted')
      write(iug,*) 'set noyzeroaxis'
      write(iug,*) 'set yrange [:0.525]'
      write(iug,*) 'plot "qdf'//ci//'.d" using 1:3 with line 1'
      write(iug,*) 'pause -1'
      close(iug)
c
      if(icnt.gt.1) then ! create qdf.plt to plot all histograms
      open(iug,file='qdf.plt',status='unknown',form='formatted')
      write(iug,*) 'set noyzeroaxis'
      write(iug,*) 'set yrange [:0.512]'
      write(iug,*) 'plot "qdf01.d" using 1:3 with line 1,\\'
      do i=2,(icnt-1)
        write(iug,101) i,i
      enddo
101   format('      "qdf',i2.2,'.d" using 1:3 with line ',i2,',\\')
      write(iug,103) icnt,icnt
103   format('      "qdf',i2.2,'.d" using 1:3 with line ',i2)
      write(iug,*) 'pause -1'
      close(iug)
      endif
c
      open(iug,file='qdf'//ci//'.d',status='unknown',form='formatted')
c     write(iug,'(3g16.7)') x(1),zero,zero
      xmin=x(n)
      xmax=x(1) ! not used. purpose to get rid of compiler warning.
      do i=1,(n-1)
        if(x(i+1).lt.x(i)) stop "qdf_gnu: data not sorted."
        f=(i*one)/(n*one)
        if(f.ge.fmin.and.f.le.fmax) then
          xmin=min(xmin,x(i))
          xmax=x(i+1)
          fq=min(f,one-f)
          write(iug,'(3g16.7)') x(i),  f,fq
          write(iug,'(3g16.7)') x(i+1),f,fq
        endif
      enddo
c     write(iug,'(3g16.7)') x(n),one,zero
      close(iug)
      fmin=xmin
      fmax=xmax
c
      return
      end
