;---------------------------------------------------------------------------
; Document name: ragview.pro
; Created by:    Andre Csillaghy, June 1991
;
; Last Modified: Wed Jan  6 11:22:54 1999 (csillag@soleil)
;---------------------------------------------------------------------------

PRO RAGView, FILENAME = fileName, NOERASE = noerase

;+
; PROGECT:
;       Phoenix 2 solar radio spectrometer
;
; NAME:
;	Radio Astronomy Group View (RAG View)
;
; PURPOSE:
;	Ragview is the top level menu interface to the data
;	analysis software package of the Radio and Plasma Astrophysics
;	Group at ETH in Zurich. It is designed for, but not limited to,
;	the analysis of solar radio spectrograms.
;
;       More information is available at
;       http://www.astro.phys.ethz.ch/rag/software/ragview-manual.ps
;
; CATEGORY
;       Radio (/ssw/radio)
; CALLING SEQUENCE:
;	RagView
;
; KEYWORDS:
;       FILENAME: the name of the file to read in. Allows to avoid the
;                 file menu interface when the filename is known.
;       
; COMMON BLOCKS:
;	ImageReal, image, xAxis, yAxis, fName: 
;		image: contains the 2D image array in byte, int or
;                      real types
;		x-axis, y-axis: contain 1D arrays determining the
;                               time (x) and frequency (y) values of
;                               the image pixels.
;               filename: a string with the file name of the current image
;	Boundaries, xMin, xMax, yMin, yMax: these four
;		parameters are used to determine which part of the
;		image is actually displayed. 
;
;	These common blocks are only used in the top-level user interface..
;	The procedures called by RagView do not use them. They are
;	used to access RagView data outside of the RagView program
;
; SIDE EFFECTS:
;	RagView opens a lot of windows and creates files, dependent
;	of the user's choices.
;
; RESTRICTIONS:
;       THIS SOFTWARE WORKS ONLY IN THE SSW ENVIRONMENT. 
;           You must install it using instructions related to the ssw.
;           See the documntation in http://www.lmsal.com/solarsoft/
;       RagView requires IDL version 5.1 
;	Works only in interactive mode.
;       Works only on X-Windows systems
;       Currently only tested on Unix systems
;
; MODIFICATION HISTORY:
;	Created: (4/91)  (under the name of enrad) A.Csillaghy 
;		       Institute for Astronomy, ETH Zurich
;	Window+Selection Management added in August 1991, Cs.
;	NOERASE keyword in June, 93
;	titles kept even outside the session, Nov 93, A.cs
;	IDL transformations Jan 95 ACs
; 	SSW adaptation, renamed from enrad to ragview,
;       IDL 5 version March 1998 ACs
;       IDL 5.1 version in October 1998, csillag@ssl.berkeley.edu
;               The procedure General_Menu was replaced by XChoice
;               since WMenu (used in general_menu) crashes on
;               solaris/idl 5.1
;       xchoice changed to ethz_xchoice 12/9/98 ACs
;       Parameters in call to FILE_BREAK adapted to avoid setting
;               fName to an empty string. Oliver Trachsel,
;               oliver.trachsel@email.ch, 2002/01/30
;               
;-

  COMMON ImageReal, zReal,xAxis,yAxis, fName ; Data
  COMMON Boundaries, xMin, xMax, yMin, yMax ; in Pixels
  COMMON Colors, r, g, b, rr, gg, bb
  COMMON Selection2D, images, wNr, nbTemps, nbWMax, dir
  
  font = (get_dfont())(0)
  IF font EQ '' THEN font = 'fixed'
 
  oldQuiet = !quiet
  !x.type = 0 & !y.type=0
  IF !x.charsize EQ 0 THEN  !x.charsize=1.0
  IF !y.charsize EQ 0 THEN  !y.charsize=1.0
  IF !p.charsize EQ 0 THEN  !p.charsize=1.0


  IF Keyword_Set( FILENAME ) THEN fName = fileName

  IF N_Elements(images) EQ 0  THEN Init_Display
  Init_Selection
  
  ; checks for old .enrad setting file
  defaults = FindFile( '.enrad')
  IF defaults(0) EQ '.enrad' THEN Spawn, 'mv .enrad .ragview' 
  defaults = FindFile( '.ragview')
  IF defaults(0) EQ '.ragview' THEN  Settings, /RESTORE

  mainMenuLength = 9
  mainMenu = StrArr(mainMenuLength)

  mainMenu(0) = 'Files ... '
  mainMenu(1) = 'Image Display ...'
  mainMenu(2) = 'Modify Image ...'
  mainMenu(3) = 'Customize ... '
  mainMenu(4) = 'X-Plot(s) (access to mdisp) ... '
  mainMenu(5) = 'Y-Plot(s) (access to mdisp) ... '
  mainMenu(6) = 'Windows + Selection Management ... '
  mainMenu(7) = 'Store Session and Exit'
  mainMenu(8) = 'Exit without Store'

  IF NOT Keyword_Set( FILENAME ) THEN BEGIN
      file = FindFile('ragviewsave.dat')
      IF  file(0) EQ 'ragviewsave.dat' THEN BEGIN
          choice = Respond_Widg( MESSAGE = $
                                 'Do you want to restore from the last ' + $
                                 'RagView session ?', $
                                 BUTTONS = ['yes','no'] )
          
          IF choice EQ 0 THEN BEGIN
              Restore,'ragviewsave.dat'
              IF N_Elements(r) NE 0 THEN TvLCT, rr,gg,bb
          ENDIF
      ENDIF
  ENDIF
  
  status = 'Not Read'
  IF N_Elements(zReal) EQ 0 OR N_Elements(yMax) EQ 0 $
	 OR Keyword_Set( FILENAME ) THEN BEGIN
    IF Keyword_Set ( FILENAME ) THEN $
      Files_Handling,zReal,xAxis,yAxis, fileName, STATUS = status, $
      FILENAME = fName, /SIZES_ON $
                                ; the other things read are
                                ; stored in the system variables.
    ELSE Files_Handling,zReal,xAxis,yAxis, STATUS = status, $
      FILENAME = fName, /SIZES_ON
    xMin = 0 & xMax = N_Elements(xAxis)-1
    yMin = 0 & yMax = N_Elements(yAxis)-1
ENDIF ELSE status = "Read"

plot, indgen(10), /nodata, ys=4, xs=4, tit='' ; to establish the coord. system
IF !d.name EQ 'X' AND N_Elements(r) EQ 0 THEN $
  LoadCT, 39, /SILENT

IF status EQ "Read"  THEN BEGIN
    Break_File, fName, disk, directory, newfName, ext
    fName =  newfName ; + ext
    IF (!d.name EQ 'X')  THEN BEGIN
        Show_Image,zReal( xMin: xMax, yMin: yMax ), $
          xAxis( xMin: xMax ), yAxis( yMin: yMax ), /SIZES_ON, $
          WINNR = GetWNr()
        Image_Info,zReal( xMin: xMax, yMin: yMax ), $
          xAxis( xMin: xMax ), yAxis( yMin: yMax ), FILE=fName
        Put_Selection, zReal, xAxis, yAxis, xMin, xMax, yMin, yMax, $
          FILESTATE = "permanent", FNAME = fName
    ENDIF
ENDIF

choice = 0

REPEAT BEGIN 

    choice = ETHZ_XChoice('RagView: Main Menu', mainMenu, $
                          TFONT=font, BFONT=font, /NOPUSH, $
                          XOFF=1)

    CASE choice OF
        0: BEGIN
            Files_Handling, zReal, xAxis, yAxis, $ 		
                STATUS=status, FILENAME=fName, $
                BOUNDARY=[xMin, xMax, yMin, yMax ], $
                /SIZES_ON
            IF STATUS EQ "Read"  THEN BEGIN
                Show_Image, zReal, xAxis, yAxis, WINNR=GetWNr(), /SIZES_ON
                Break_File, fName, disk, directory, newfName, ext
                fName = newfName ; + ext
                Image_Info, zReal, xAxis, yAxis, FILE=fName
                xMin = 0 & xMax=N_Elements(xAxis)-1
                yMin = 0 & yMax=N_Elements(yAxis)-1
                Put_Selection, zReal, xAxis, yAxis, xMin, xMax, yMin, $
                    yMax, FILESTATE="permanent", FNAME=fName
            ENDIF 
        END
        1: IF N_Elements(zReal) GT 1 THEN $
            Display_2D, zReal, xAxis, yAxis, $
            xMin, xMax, $
            yMin, yMax, TITLE=fName, $
            /SIZES_ON, /SELECTION_ON
        2: Modify, zReal, xAxis, yAxis, xMin, xMax, yMin, yMax, fName
        3: Custom_2D
        4: BEGIN
          xVar = !x & yVar=!y & zVar=!z
          RagView_MDisp_Int, zReal(xMin:xMax, yMin:yMax), $, 
              xAxis(xMin:xMax), $
              yAxis(yMin:yMax), !x.title, /RESET, TITLE=!p.title
          MDisp
          !x = xVar & !y=yVar & !z=zVar
          Image_Info, zReal(xMin:xMax, yMin:yMax), $
              xAxis( xMin: xMax ), yAxis( yMin: yMax ), FILE=title
      END
      5: BEGIN
            xVar = !x & yVar = !y & zVar = !z & pVar = !p
            hms = GetHMS()
            RagView_MDisp_Int, $
	Transpose( zReal(xMin:xMax, yMin:yMax) ), $
	yAxis(yMin: yMax), xAxis(xMin: xMax), !y.title, $
	 /RESET, /YDIR, TITLE = title
            MDisp
            PutHMS, hms
            !x = xVar & !y = yVar & !z = zVar & !p = pVar
             Image_Info, zReal(xMin:xMax, yMin:yMax), $
	xAxis( xMin:xMax ), yAxis( yMin: yMax ), FILE=title
          END
      6: BEGIN
             Window_Management, zReal, xAxis, yAxis, $
                 xMin, xMax, yMin, yMax, STATUS=status, $
                 FNAME=fName
             IF status EQ "Done" THEN BEGIN
                 Image_Info, zReal(xMin:xMax, yMin:yMax), $
                     xAxis( xMin:xMax ), yAxis( yMin: yMax ), FILE=title
             ENDIF
           END
      7:  Save, /COMM, /VARIABLES, $
	FILENAME='ragviewsave.dat'
    ELSE:
    ENDCASE
  
  END UNTIL (choice GE mainMenuLength-2) AND $
    (choice LT mainMenuLength)

  IF NOT Keyword_Set( NOERASE ) THEN $
    WHILE !d.window NE -1 DO WDelete
  IF (choice LT mainMenuLength) AND $
	(choice GE mainMenuLength-2) THEN $
	Sel_Del,/ALL, /DIR

  !quiet = oldQuiet

END
