;---------------------------------------------------------------------------
; Document name: display_2d.pro
; Created by:    Andre Csillaghy, June 1991
;
; Last Modified: Thu Dec 10 11:08:58 1998 (csillag@soleil)
;---------------------------------------------------------------------------
;
;+
; NAME:
;	Display 2D Image
; PURPOSE:
;	User Interface for displaying 
;	two-dimensional data and provide 
;	image handling facilities: Zoom, move, read values,
;	color table handling. This
;	is a  part of the RagView image processing system. 
; CATEGORY:
;	User Interface
; CALLING SEQUENCE
;	Display_2D, image [, xAxis, yAxis 
;		[,xMin, xMax, yMin, yMax ] ]
; INPUTS:
;	image: the 2D data array;
;	xAxis, yAxis: the two axes. They must have the same
;		dimension as their corresponding one in
;		the variable "image"
;	xMin, xMax, yMin, yMax: the selected boundaries of
;		the image if just a part is to be displayed.
;		Units: image pixels
; OUTPUTS: 
;	xMin, xMax, yMin, yMax: the boundaries of
;		the image displayed, which may have
;		change by zooms, moves, etc. (image pixels)
; KEYWORD:
;	TITLE: The name of the image (can be different from the
;		value of !p.title). Will be used for naming
;		e.g postscript files.
; SIDE EFFECTS:
;	Display opens a lot of windows and create 
;	files, dependent of the user's choices.
; RESTRICTIONS:
;	Works only in interactive mode.
; MODIFICATION HISTORY:
;	Created: (4/91)  A.Csillaghy csillag@astro.phys.ethz.ch
;		       Institute for Astronomy, ETH Zurich
;	Modified for printing sixel files
;		Dec. 92, A.Cs
;	Logo eliminated in Nov 93, A.cs
;       Modified for IDL5 / SSW / RagView in March 98 -- ACS
;       Added three new menu entries to calculate drift rate and
;       polarization
;               Jan. 02, Oliver Trachsel <oliver.trachsel@email.ch>
;-

PRO ChangeSize, wSize

CASE wSize OF
    'SMALL' : BEGIN
        PutWinSize, 600, 600
        PutImageSize, undef, undef, 510, 474
    END
    'MEDIUM': BEGIN
        PutWinSize, 1000, 800
        PutImageSize, undef, undef, 910, 674
    END
    'LARGE': BEGIN
        PutWinSize, 1250, 1000
        PutImageSize, undef, undef, 1160, 874
    END
ENDCASE

END                             ; change window and image sizes


PRO Display_2D, zReal, xAxis, yAxis, $
           xMin, xMax, yMin, yMax, $
           TITLE=title, $
           SIZES_ON=sizes_on, SELECTION_ON=selection_on

IF NOT Keyword_Set(SIZES_ON) THEN Init_Display 
IF NOT Keyword_Set(SELECTION_ON) THEN Init_Selection
IF N_Elements( title ) EQ 0 THEN title = ''

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

IF nbParams EQ 0 THEN BEGIN
  Error, 5, 'Display_2D'
  RETURN
ENDIF
IF nbParams EQ 1 THEN BEGIN
  xAxis = LIndGen(nx)
  yAxis = LIndGen(ny)
ENDIF 
IF nbParams LT 7 THEN BEGIN
  xMin = 0 & xMax = N_Elements(zReal(*,0))-1
  yMin = 0 & yMax = N_Elements(zReal(0,*))-1
ENDIF ELSE IF $
  (nbParams NE 1) AND (nbParams NE 3) AND (nbParams NE 7) $
  THEN BEGIN
    Error, 4, 'Display_2D'
    RETURN
  ENDIF

  mainMenuLength = 16

mainMenu = StrArr(mainMenuLength)
winSizeMenu = StrArr(3)

mainMenu(0)='Show Total Image'
mainMenu(1)='Zoom Region ... '
mainMenu(2)='Read Values ... '
mainMenu(3)='Drift Rate Determination (LIN FREQ) ...'
mainMenu(4)='Drift Rate Determination (LOG FREQ) ...'
mainMenu(5)='Polarization Determination ...'
mainMenu(6)='Load/Save/Modify  Color Table ... '
mainMenu(7)='Move in the Image ... '
mainMenu(8)='Change Window Size ... '
mainMenu(9)='Adjust Color Table ... '
mainMenu(10)='Surface & Contour ... '
mainMenu(11)='Make Postscript File / Print ...'
mainMenu(12)='Quick Zoom'
mainMenu(13)='Histogram of the Displayed Image'
mainMenu(14)='Return to Enrad Main Menu'
mainMenu(15) = 'Redisplay This Menu'

winSizeMenu(0)='SMALL'
winSizeMenu(1)='MEDIUM'
winSizeMenu(2)='LARGE'

winSizeIndex = 0

REPEAT BEGIN
 
  choice=General_Menu(mainMenu ,$
	'Image Handling: Select Action ')
  
  CASE choice OF
      1: BEGIN
          xMin = 0 & yMin=0 & xMax=nx-1 & yMax=ny-1
          strP = StrPos( title, '_z' ) 
          WHILE  strP NE -1 DO BEGIN
              strL = StrLen( title )
              title = StrMid(title, 0, strP ) + StrMid( title, strP+2, strL)
              strP = StrPos( title, '_z' ) 
          ENDWHILE
          Image_Info, zReal, xAxis, yAxis, FILE=title
          Show_Image, zReal, xAxis, yAxis, /SIZES_ON, $
              WINNR=GetWNr()
      END
      2: BEGIN
          TV_Zoom, xAxis, yAxis, xMin, xMax, yMin, yMax, $
              STATUS=status
          IF (status EQ "Done") AND (!d.name EQ'X') THEN BEGIN
              Show_Image, zReal(xMin:xMax, yMin:yMax), $
                  xAxis(xMin:xMax), yAxis(yMin:yMax), $
                  /SIZES_ON, WINNR=GetWNr()
              title = title + '_z'
              Image_Info, zReal(xMin:xMax, yMin:yMax), $
                  xAxis(xMin:xMax), yAxis(yMin:yMax), FILE=title
              Put_Selection, zReal, xAxis, yAxis, xMin, xMax, yMin, yMax, $
                  FILESTATE='temporary', FNAME=title 
          ENDIF
      END
      3: Pixel_Read, zReal(xMin:xMax, yMin:yMax), $
          xAxis(xMin:xMax), yAxis(yMin:yMax), $
          /SIZES_ON
      4: Drift_Rate, zReal, xAxis, yAxis, xMin, xMax, yMin, yMax, 2
      5: Drift_Rate, zReal, xAxis, yAxis, xMin, xMax, yMin, yMax, 2, $
        /LOGFREQ
      6: Polarization, xAxis[xMin:xMax], yAxis[yMin:yMax]
      7: CT_Handling, BytScl( zReal( xMin: xMax, yMin: yMax ) )
      8: BEGIN
          Move_Image, zReal, xAxis, yAxis, $
              xMin, xMax, yMin, yMax, $
              TITLE=title, /SIZES_ON, /WINCHANGE
          Put_Selection, zReal, xAxis, yAxis, xMin, xMax, yMin, yMax, $
              FILESTATE='temporary', FNAME=title + '_m'
         END
      9: BEGIN
         winSizeIndex=WMenu(['Select Window Size',winSizeMenu],$
                            TITLE=0)-1
         ChangeSize, winSizeMenu( winSizeIndex )
         Show_Image, zReal(xMin:xMax, yMin:yMax), $
             xAxis(xMin:xMax), yAxis(yMin:yMax), $
             /SIZES_ON
         END
     10: Rag_Adjct
     11: Cont_Surf, zReal( xMin:xMax, yMin:yMax), $
	xAxis(xMin:xMax),yAxis(yMin:yMax), $
	WINNR = GetWNr(), /SIZES_ON
     12: BEGIN
          PrintInterface, status
          Show_Image,zReal(xMin:xMax,yMin:yMax),$
	xAxis(xMin:xMax),yAxis(yMin:yMax), $
	/PS, $
	/SIZES_ON
          PrintInterface, status, /PRINT
        END
     13: Zoom
     14: HistoFacility, zReal( xMin: xMax, yMin:yMax), $
	 XTITLE = !z.title
   ELSE:
  END

END UNTIL choice EQ mainMenuLength-1

END

