      subroutine gridstats(fname,ave,std,med)
      implicit real*8(a-h,o-z)
      parameter(nmax = 1561*3541)
      real*8 med
      real*4 data(5000),s1,select2,arr(nmax)
      integer*4 nla,nlo,ikind
      character*200 fname
      open(11,file=fname,status='old',form='unformatted')

      ave = 0.d0
      rms = 0.d0

      read(11)glamn,glomn,dla,dlo,nla,nlo,ikind
      if(ikind.eq.0)stop 20304

      ikt = 0
      do 1 i=1,nla
        if(ikind.eq.1)read(11)(data(j),j=1,nlo)
        do 3 j=1,nlo
          ikt = ikt + 1
          arr(ikt) = data(j)
    3   continue
        do 2 j=1,nlo
          ave = ave + dble(data(j))
          rms = rms + dble(data(j))**2
    2   continue
    1 continue
      fact = dble(nla*nlo) / dble(nla*nlo - 1)

      ave = ave / dble(nla*nlo)
      rms = sqrt(rms / dble(nla*nlo))
      std = dsqrt(fact*(rms**2 - ave**2))

      npts = ikt
      nmed = 1+ ikt/2

      s1 = select2(nmed,npts,arr,nmax)

      med = s1

      write(6,101)npts,nmed,s1,med
  101 format(
     *12x,'gridstats.f: npts = ',i18,/,
     *12x,'gridstats.f: nmed = ',i18,/,
     *12x,'gridstats.f: s1   = ',f18.6,/,
     *12x,'gridstats.f: med  = ',f18.6)

      write(6,100)trim(fname),ave,std,med
  100 format(12x,'gridstats for ',a,' = ',3f20.10)

      close(11)

      return
      end
