      subroutine hist_gnu(iug0,nhist,ndat,hist,data,xmin,xmax)
c
c bernd berg, sep 27 2000.
c
c gnuplot for histogram. for iug0<0 the histogram will
c                        normalized to   sum h(i)*del = 1.
c
c  iug=iabs(iug0) write unit; for iug=8 only: distinct output format 
c                            suitable for a single histogram.                    
c  hist:       histogram array of dimension nhist.
c  data:       data array of dimension ndat.
c  xmin,xmax:  histograming range. if xmin.ge.xmax: minimum and
c              maximum of the data are chosen.
c  chist:      name of gnuplot file. e.g. hist1, hist2, ...
c              must be chosen to match character*5!
c
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      character ci*2
      dimension hist(nhist),data(ndat)
c possibility to access extra information:
      common/hist_infos/ n_omit
      data icnt /0/
      save icnt
      n_omit=0
c
      icnt=icnt+1
      if(icnt.gt.99) stop 'hist_gnu: inct=99 exhausted!'
      write(ci,'(i2.2)') icnt
c
c gnuplot script:
      iug=iabs(iug0)
      write(6,'(/," hist_gnu: open files ",a7," and "a5,".")') 
     & 'h'//ci//'.plt','h'//ci//'.d'
      open(iug,file='h'//ci//'.plt',status='unknown',form='formatted')
      write(iug,*) 'plot "h'//ci//'.d" using 1:2 with l lt 1'
      write(iug,*) 'pause -1'
      close(iug)
      write(6,'(" hist_gnu: open files ",a7," and "a5,".")')
     & 'i'//ci//'.plt','i'//ci//'.d'
      open(iug,file='i'//ci//'.plt',status='unknown',form='formatted')
      write(iug,*) 'plot "i'//ci//'.d" using 1:2 with impulses'
      write(iug,*) 'pause -1'
      close(iug)
c
      if(icnt.gt.1) then ! create his.plt to plot all histograms
        write(6,*) "hist_gnu: open file his.plt to plot all histograms."
        open(iug,file='his.plt',status='unknown',form='formatted')
        write(iug,*) 'plot "h01.d" using 1:2 with l lt 1,\' 
        do i=2,(icnt-1)
          write(iug,100) i,i
100       format('      "h',i2.2,'.d" using 1:2 with l lt ',i2,',\')
        end do
        write(iug,101) icnt,icnt
101     format('      "h',i2.2,'.d" using 1:2 with l lt ',i2)
        write(iug,*) 'pause -1'
        close(iug)
      endif
c
      do i=1,nhist
        hist(i)=zero
      end do
c
      if(xmax.le.xmin) then
        xmin=data(1)
        xmax=data(1)
        do idat=1,ndat
          xmin=min(xmin,data(idat))
          xmax=max(xmax,data(idat))
        end do
        xmin=xmin-((xmax-xmin)/10000)/nhist
        xmax=xmax+((xmax-xmin)/10000)/nhist
      endif
      write(6,'(" hist_gnu: xmin,xmax =",2g14.6)') xmin,xmax
c
c map data linearly such that xmin -> 1 and xmax -> nhist+1.
      factor=(nhist*one)/(xmax-xmin)
      xdat=zero
      do idat=1,ndat
        x=half+factor*(data(idat)-xmin)
        i=nint(x)
        if(i.le.0. or. i.gt.nhist) then 
          n_omit=n_omit+1
        else
          hist(i)=hist(i)+one
          xdat=xdat+one
        endif
      end do
c      
      del=(xmax-xmin)/(nhist*one)
      open(iug,file='h'//ci//'.d',status='unknown',form='formatted')
      x=xmin
      if(iug0.lt.0) factor=one/xdat/del
      if(iug0.lt.0) hist(1)=factor*hist(1)
      write(iug,'(2g16.7)') x,zero
      write(iug,'(2g16.7)') x,hist(1)
      do i=1,(nhist-1)
        if(iug0.lt.0) hist(i+1)=factor*hist(i+1)
        x=x+del
        write(iug,'(2g16.7)') x,hist(i)
        if(iug.eq.8) write(iug,'(2g16.7)') x,zero
        write(iug,'(2g16.7)') x,hist(i+1)
      end do
      x=x+del
      write(iug,'(2g16.7)') x,hist(nhist)
      write(iug,'(2g16.7)') x,zero
      close(iug)
      open(iug,file='i'//ci//'.d',status='unknown',form='formatted')
      x=xmin-del/2
      do i=1,nhist
        x=x+del
        write(iug,'(2g16.7)') x,hist(i)
      end do
      close(iug)
c
      if(n_omit.gt.0) 
     & write(6,'(" hist_gnu warning:",i10," data omitted")') n_omit
      write(6,*) "         "
      return
      end
