       subroutine mediqr(dat,ndat,xmed,xiqr)
c
c  Purpose:  This routine computes the median and interquartile range
c		for an input data array.
c
c  Calling Arguments:
c
c  Name         Type    I/O     Description
c  --------     ----    ---     -----------
c  dat		R*4	 I      Input data array
c  ndat		I*4	 I	Number of items in input data array
c  xmed		R*4	 O	Median of data
c  xiqr		R*4	 O	Interquartile range of data
c
c  By: Frederick S. Patt, GSC, January 4, 1994
c
c  Notes:  Uses Select routine from Numerical Recipes for FORTRAN, p334.
c
c  Modification History:
c

      real*4 dat(*),xmed,xiqr
      real*4 da1(200000),select
      integer*4 ndat,i,iq,ir,im,im1

c  Initialize index array
      do i=1,ndat
	da1(i) = dat(i)
      end do

c  Compute median and IQR by calling function Select

      im = ndat/2 
      im1 = im + 1
      iq = (ndat-1)/4 + 1
      ir = ndat - iq + 1
      if (ndat.eq.(2*im)) then
        xmed = (select(im,ndat,da1)+select(im1,ndat,da1))/2.
      else
	xmed = select(im1,ndat,da1)
      end if
      xiqr = select(ir,ndat,da1) - select(iq,ndat,da1)

      return
      end
      FUNCTION select(k,n,arr)
      INTEGER*4 k,n
      REAL*4 select,arr(n)
      INTEGER*4 i,ir,j,l,mid
      REAL*4 a,temp
      l=1
      ir=n
1     if(ir-l.le.1)then
        if(ir-l.eq.1)then
          if(arr(ir).lt.arr(l))then
            temp=arr(l)
            arr(l)=arr(ir)
            arr(ir)=temp
          endif
        endif
        select=arr(k)
        return
      else
        mid=(l+ir)/2
        temp=arr(mid)
        arr(mid)=arr(l+1)
        arr(l+1)=temp
        if(arr(l+1).gt.arr(ir))then
          temp=arr(l+1)
          arr(l+1)=arr(ir)
          arr(ir)=temp
        endif
        if(arr(l).gt.arr(ir))then
          temp=arr(l)
          arr(l)=arr(ir)
          arr(ir)=temp
        endif
        if(arr(l+1).gt.arr(l))then
          temp=arr(l+1)
          arr(l+1)=arr(l)
          arr(l)=temp
        endif
        i=l+1
        j=ir
        a=arr(l)
3       continue
          i=i+1
        if(arr(i).lt.a)goto 3
4       continue
          j=j-1
        if(arr(j).gt.a)goto 4
        if(j.lt.i)goto 5
        temp=arr(i)
        arr(i)=arr(j)
        arr(j)=temp
        goto 3
5       arr(l)=arr(j)
        arr(j)=a
        if(j.ge.k)ir=j-1
        if(j.le.k)l=i
      endif
      goto 1
      END

