      SUBROUTINE BINING(NDAT,DATA,NBINS,DATB) ! Bernd Berg, Jan 14 1992.
C
C    PURPOSE:                                                         
C    CALCULATION OF  NBINS  BINNED DATA FROM  NDAT   ORIGINAL DATA.
C                                                                     
C    INPUT:  (UNCHANGED ON EXIT)
C    NDAT:    NUMBER OF ORIGINAL DATA.
C    DATA:    ARRAY VECTOR CONTAINING THE ORIGINAL DATA.
C    NBINS:   NUMBER OF BINS REQUESTED.
C                                                                     
C    OUTPUT:
C    DATB:    ARRAY CONTAINING THE BINNED DATA.
C
      include '../../Libs/Fortran/implicit.sta' 
      include '../../Libs/Fortran/constants.par' 
      DIMENSION DATA(NDAT),DATB(NBINS)
      DATA IWARN /0/
C
      NBIN=NDAT/NBINS
      NTEST=NBIN*NBINS
      IF(IWARN.EQ.0 .AND. NTEST.NE.NDAT) THEN
        PRINT*,'WARNING FROM SUBROUTINE BINING:'
        PRINT*,'NDAT =      ',NDAT
        PRINT*,'NBIN*NBINS =',NTEST
        PRINT*,'NBIN,NBINS: ',NBIN,NBINS
        IWARN=IWARN+1
        PRINT*,'NO WARNINGS IN FUTURE CALLS!'
      END IF
C
      DO I=1,NBINS
        DATB(I)=zero
        DO J=1,NBIN
          JJ=J+(I-1)*NBIN
          DATB(I)=DATB(I)+DATA(JJ)
        END DO 
        DATB(I)=DATB(I)/NBIN
      END DO    
C
      RETURN
      END
