PRO Pixel_Read, zReal, xAxis, yAxis, zValue, xValue, yValue, $
	WINNR = winNr, TEXT = text, $
	SIZES_ON = sizes_on, PIXELX = pixelX, $
	PIXELY = pixelY
;+
; NAME:
;	Pixel_Read
; PURPOSE:
;	Reads the x- y- and z-value of an image pixel 
;	clicked with the mouse
; 	on a window produced with Show_Image. 
; CALLING SEQUENCE:
; 	Pixel_Read,image [ ,xAxis ,yAxis [ ,imageValue
;		 ,xValue ,yValue ] ] 
; CATEGORY:
;	Image processing
; INPUTS:
;	image: the image actually displayed on the screen
;	xAxis,yAxis: the corresponding Axes
; OUTPUTS:
;	imageValue, xValue, yValue: the values read. When
;		not present, the values are displayed
;		on the screen
; KEYWORDS:
;	WINNR: must be set to the correct window number if the
;		image window is not the currently
;		selected window.
;	TEXT: If another text as the default is wanted.
;	PIXELX, PIXELY: the pixel numbers in the image.
; SIDE EFFECT:
; 	When the values read are displayed on the screen, a 
;	window is opened, and closed when exiting the
;	procedure.
; RESTRICTION:
;	The procedure works only  on X-windows.
;
; MODIFICATION HISTORY:
;	Created 4/91, A.Csillaghy
;	Keywords PIXELn added in Jan 93, A.Cs
;	ERROR routine eliminated and replaced by MESSAGE, 
;		Aug. 93, A.Cs.
; NON STANDARD PROCS USED:
; 	Get_Winsize, Get_ImageSizes, GetBreaks,
;	Round, ElimBreaks, GetOffsets
;-

  IF !d.name NE 'X' THEN BEGIN
    Message,  'This procedure requires an x-windo terminal', $
	/INFO, /CONT
    RETURN
  ENDIF
  IF Keyword_Set(WINNR) THEN BEGIN
    oldWin = !d.window
    !d.window = winNr
  ENDIF

  vals = N_Params() EQ 6 OR Keyword_Set( PIXELX) OR $
	Keyword_Set( PIXELY )

  nx = N_Elements( zReal( *, 0 ) )
  ny = N_Elements( zReal( 0, * ) )
  nPar = N_Params()

  CASE nPar OF
    1: BEGIN 
           xAxis=LIndGen(nx)
           yAxis=LIndGen(ny)
         END
    2: xAxis = LIndGen(nx)
    3:
    ELSE: IF (nPar GT 6) OR (nPar LT 1) THEN BEGIN
          Message, 'Usage, zReal [, xAxis, ' + $
	'yAxis [ , zValue, xValue, yValue ] ] ', /INFO, /CONT
          RETURN
        ENDIF
  ENDCASE

  IF NOT vals THEN BEGIN
    answ = ''
    Read, 'Do you want to write the points in a file (y/n) ? ', answ
    IF StrUpcase(answ) EQ 'Y' THEN BEGIN
      writeInFile = 1
      OpenW, unit, 'pixelread.lst', /GET_LUN
      Printf, unit, 'List of values read, image title: ' + !p.title
      Printf, unit, ' '
    ENDIF ELSE writeInFile = 0
  ENDIF ELSE writeInFile = 0

  xValue = 0. & yValue = 0. & zValue = 0.

  result = StrArr(10)
  result(0) = 'Value of the clicked pixel:'
  result(5) = 'z-value:'
  result(8) = 'Press the left mouse button to read another pixel value '
  result(9) = 'Press the right button to stop reading values'

  IF NOT Keyword_Set( TEXT ) THEN $
    text = 'Press the left mouse button on the pixel you want to read'
   
  GetImageSizes, xPos, yPos, width, height
  hms = GetHMS()
  GetBreaks, xBreaks, yBreaks
  GetOffsets, d, offset

  imgWinNr = !d.window
  IF imgWinNr EQ -1 THEN BEGIN
    Message, 'No windows are open.'
    RETURN
  ENDIF

  msgWinNr=WMesg( $
                text, 'Read an image pixel value',100,600)
  WSet,imgWinNr
  Cursor,x,y,1,/DEVICE
  WDelete,msgWinNr

  REPEAT BEGIN
    pixelX = DevToPix( x,  xPos, width, xBreaks, offset, nx )
    pixelY = DevToPix( y,  yPos, height, yBreaks, offset, ny )

    zValue = zReal(pixelX,pixelY)
    xValue = xAxis(pixelX)
    yValue = yAxis(pixelY)

    IF NOT vals THEN BEGIN

      IF hms EQ 1 THEN $
  	result(2)='               x= '+HMSConvert(xValue) $
      ELSE  result(2)='               x= '+StrTrim(Float(xValue), 2) 
      result(3)='               y= '+ StrTrim(Float(yValue),2)
      result(7)='               z= ' + StrTrim(Float(zValue),2)
  
      resWinNr = WMesg(result,'Read Pixel: Result',100,600)
      WSet,imgWinNr
      Cursor,x,y,/DEVICE
      WDelete,resWinNr

      IF writeInFile THEN $
        PrintF, unit, StrTrim(result(2),2), '; ', $
	StrTrim(result(3),2), '; ', $
	StrTrim(result(7),2)

    ENDIF ELSE !err = 4

  END UNTIL !err EQ 4

  IF Keyword_Set(WINNR) THEN !d.window = oldWin
  IF writeInFile THEN Free_Lun, unit

END ; Pixel Read



