*
*  Name:	display
*  Purpose:	This subroutine generates a color display 2D grid of 
*		dimension mxn, with values ranging between amin and
*		amax.
*  Arguments: 	a -- 2D real array
*		m,n -- dimensions of a
* 		amin,amax -- min. and max. values of elements in a
*  Note: 	This program works only with a color monitor.
*
      SUBROUTINE display(a,m,n)
      integer m,n,colors
      integer a(m,n)

      INTEGER pixel(m,n)

      include 'color_display.inc'
      integer, array(maxcolors) :: red,green,blue
      integer CMXDisplay, disp, win, gc, cmap, ncolors, base, widget, depth
      logical iscolor, isrgb, ismap
      integer, save, data:: init = 0
      REAL s
      INCLUDE '/usr/cm5/cmx11/include/cm/cmx-cmf.h'

*	First time open the display window
	if (init == 0) then
           init = 1
           ncolors = maxcolors
           call CMXSetArgCArgVF()
           CMXDisplay = CMXCreateSimpleDisplay(ncolors,m,n)
           disp       = CMXGetDisplay(CMXDisplay)
           win        = CMXGetDrawable(CMXDisplay)
           gc         = CMXGetGC(CMXDisplay)
           cmap       = CMXGetColormap(CMXDisplay)
           widget     = CMXGetWidget(CMXDisplay)
           depth      = CMXGetDepth(widget)
C	   print *, 'cmap', cmap, ' ncolors ', ncolors,' widget ', widget
C	   print *, ' depth ', depth
           iscolor    = CMXIsColor(widget)
           isrgb      = CMXIsRGB(widget)
           ismap      = CMXHasColormap(widget)
C           print *, 'color ', iscolor, ' rgb ',isrgb, ' map', ismap
           call CMXAllocateColors(disp,cmap,ncolors,base)
C           print *, "Colors Allocated",ncolors, base
           call CMXLoadColorArrays('density',ncolors,red,green,blue)
           call CMXStoreColors(disp,cmap,base,ncolors,red,green,blue)
           call CMXClearDisplay(disp, win)
	endif

        pixel = MIN(maxcolors,MAX(0,a)) + base
        call CMXPutImage(disp, win, gc, pixel, 8, 0, 0, 0, 0, m, n)

        RETURN
        END