      SUBROUTINE averag(string, ipar, dset)
c		calculate average values of data sets
c		fwj haberl july 1988
c	implicit undefined(a-z)
      INCLUDE 'xplot.inc'
      INTEGER*4 dset, ipar, nret, iflg, idlm
      INTEGER*4 iset_m, is, ise, i, isum
      REAL*4 sum, xmin, xmax
      PARAMETER (iset_m=30)
      INTEGER*4 iset(iset_m)
      CHARACTER*(*) string
      DATA xmin/ - 1.e25/, xmax/1.e25/
      DATA iset/iset_m*1/
 
      IF (dset.EQ.0) THEN
         WRITE (*, '(a)') ' No data sets yet'
         RETURN
      ENDIF
      CALL xgtrl4(string, ipar, 1, 'x low', 1, xmin, nret, iflg, idlm)
      CALL xgtrl4(string, ipar, 1, 'x high', 1, xmax, nret, iflg, idlm)
      CALL xgtint(string, ipar, iset_m, 'data sets', 1, iset, nret,
     &            iflg, idlm)
      sum = 0.
      isum = 0
      DO is = 1, nret
         ise = iset(is)
         IF (ise.LE.0 .OR. ise.GT.dset) THEN
            WRITE (*, '(a,i4)') ' No data set no.', ise
         ELSE
            DO i = nbins(ise), nbine(ise)
               IF (datax(i).GT.xmin .AND. datax(i).LT.xmax) THEN
                  sum = sum + datay(i)
                  isum = isum + 1
               ENDIF
            ENDDO
         ENDIF
      ENDDO
      IF (isum.EQ.0) THEN
         WRITE (*, '(a)') ' No data in selected interval'
      ELSE
         WRITE (*, '(2(a,g15.5))') ' Selection between ', xmin, ' and ',
     &                             xmax
         WRITE (*, '(a,i8)')    ' data points: ', isum
         WRITE (*, '(a,g15.5)') '         sum: ', sum
         sum = sum/isum
         WRITE (*, '(a,g15.5)') '     average: ', sum
      ENDIF
      RETURN
      END
