*
*  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,amin,amax)
      real a(m,n)
      real amin,amax

      integer, parameter :: maxcolors = 64
      integer, array(maxcolors) :: red,green,blue

      INTEGER pixel(m,n)
      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)
	   print *, 'cmap', cmap, ' ncolors ', ncolors,' widget ', widget
	   print *, ' depth ', depth
           iscolor = CMXIsColor(widget)
           isrgb = CMXIsRGB(widget)
           ismap = CMXHasColormap(widget)
           print *, 'color ', iscolor, ' rgb ',isrgb, ' map', ismap
           call CMXAllocateColors(disp,cmap,ncolors,base)
           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

        s= 1.0/(amax-amin)
        pixel = INT(ncolors*s*(a-amin))
        pixel = MIN(ncolors-1,MAX(0,pixel)) + base
        call CMXPutImage(disp, win, gc, pixel, 8, 0, 0, 0, 0, m, n)

        RETURN
        END