c==========================================================================
c
c NAME: spatial_contrast
c
c PURPOSE: Determines if given HIRS pixel is cloudy or undecided using HIRS
c          IR surface channel using spatial contrast test. See Rossow and
c          Gardner (1993,JClim).
c
c INPUTS:
c          pixel  -> HIRS8 brightness temperature (K)
c          chn    -> HIRS channel
c          ii     -> longitude index corresponding to pixel
c          jj     -> latitude index corresponding to pixel
c          lmask  -> land mask array of dimension nlon x nlat
c                    2=coastal,1=land,0=ocean
c          region -> gridded max TB values
c   
c OUTPUTS:
c          space : 0 -> cloudy, 1 -> undecided, 2 -> coastal region
c
c EXTERNALS:
c            hirs1c.inc
c            gmax_r.f
c HISTORY:
c          Darren Jackson CIRES/ETL April 2002
c
c=============================================================================
      subroutine spatial_contrast(pixel,chn,ii,jj,lmask,region,space)

      implicit none

      include 'hirs1c.inc'

      byte    lmask(nlon,nlat)
      real    tbmax,pixel,region(nlon,nlat),delta1(0:1)
      integer space
      integer ii,jj,chn

      data delta1/3.5,6.5/
c
c* set space variable to coastal value
c
      space=2
c
c* test IR data for cloudy conditions over sea and land (no coastlines)
c
      if(lmask(ii,jj) .eq. 1) then
         call gmax_r(region,nlon,nlat,ii,jj,3,3,missing,tbmax)
         space=1
         if((tbmax-delta1(1)).gt. pixel) space=0
      elseif(lmask(ii,jj) .eq. 0) then
         call gmax_r(region,nlon,nlat,ii,jj,9,9,missing,tbmax)
         space=1
         if((tbmax-delta1(0)).gt. pixel) space=0
      endif
      
      return
      end
