!####################################################################

      subroutine sumstatsoutliers(nsamp0,xvar0,std,fmean,xmin,xmax)
      implicit none
      integer nsamp,i,j,nsamp0,isoutlier_2sides
      double precision xvar(nsamp0),std,fmean,xmin,xmax,
     &  xvar0(nsamp0),gap
      parameter(gap=-9999.0d0)
     
      nsamp=0
      do j=1,nsamp0        
        if(dabs(xvar0(j)-gap).gt.1.0d-5)then
          nsamp=nsamp+1
          xvar(nsamp)=xvar0(j)
        endif
      enddo
      std=gap
      fmean=gap 
      xmin=gap
      xmax=gap
      if(nsamp.eq.0)return
      if(nsamp.eq.1)then
        fmean=xvar(1)
        return
      endif

10    i=isoutlier_2sides(nsamp,xvar)
      if(i.lt.0)goto 100
      do j=i,nsamp-1
        xvar(j)=xvar(j+1)
      enddo
      nsamp=nsamp-1
      goto 10

100   if(nsamp.lt.1)then
        std=gap
        fmean=gap
        xmin=gap
        xmax=gap
        return
      endif

      fmean=0.0d0
      xmin=xvar(1)
      xmax=xvar(1)
      do j=1,nsamp
        fmean=fmean+xvar(j)
        if(xvar(j).gt.xmax)then
          xmax=xvar(j)
        endif
        if(xvar(j).lt.xmin)then
          xmin=xvar(j)
        endif
      enddo
      fmean=fmean/dble(nsamp)
      std=0.0d0
      do j=1,nsamp
        std=std+(xvar(j)-fmean)*(xvar(j)-fmean)       
      enddo
      std=dsqrt(std/dble(nsamp-1))
      end
