;+
; PROGECT:
;       Phoenix 2 solar radio spectrometer
;
; NAME:
; 	Cont_Surf
;
; PURPOSE:
;	User interface to use the three-dimensional standard functions
;	"Contour" and "Surface".  It is possible to change  
;	contour parameters: annotation, character sizes, 
;	colors used to represent the levels, labels of the levels, 
;	line styles and thickness,  maximum value and number of 
;	levels drawn. An option for smoothing is given and 
;	surface or contour plots can be printed and written in 
;	PostScript files.
;
; CATEGORY:
;	ETHZ Radio (/ssw/radio)
;
; CALLING SEQUENCE:
; 	Cont_Surf, image [, xAxis,  yAxis]
;
; INPUTS:
;	image: The two-dimensionnal array to be contoured
;		or surfaced. If "image" contains more than
;		200 elements in one direction, it is
;		compressed to that number of elements,
;		using bilinear interpolation. "image"
;		can be of any type.
;	xAxis, yAxis: These two 1D array ( of any type) 
;		have dimensions corresponding those of the array
;;		"image" and contain its axis values.
; KEYWORDS:
;	FILENAME: The name of the PostScript file. 
;		If not present, the file will have
;		the same name as the title of the 
;		(surface or contour) plot.
;	LANDSCAPE: If present, the PostScript file is written in
;		landscape mode, otherwise in portrait mode.
;
; SIDE EFFECTS:
;	As all user interfaces, it opens windows on
;	X-windows terminals. Files can be produced, which will be written 
;	in the current directory.
;
; RESTRICTIONS:
;       Tested only on Unix.
;
; MODIFICATION HISTORY
;	Created: A.Csillaghy May 91
;		Institute of Astronomy, ETH Zurich
;       IDL 5.1 version in October 1998, csillag@ssl.berkeley.edu 
;            Replaced the Printing interface 
;            Replaced
;-

PRO Change_Pars,annotation,charsize, colors,labels, linestyle, $
	thick, maxVal, nLevels

mainMenuLength = 10
mainMenu = StrArr(mainMenuLength)


  mainMenu(0) = 'Annotation: '+annotation
  mainMenu(1) = 'Characters Size: '+ STRING(charsize)
  mainMenu(2) = 'Colors Used: '+STRING(colors)
  mainMenu(3) = 'Labels Used: '+labels
  mainMenu(4) = 'Lines Style: '+ STRING(linestyle) 
  mainMenu(5) = 'Thickness Of Lines: ' + STRING(thick)
  mainMenu(7) = 'Maximum Value: ' + STRING(maxVal)
  mainMenu(8) = 'Number of Levels: ' + STRING( nLevels )
  mainMenu(mainMenuLength-2) = 'Return to Preceeding Menu'

REPEAT BEGIN
    
    choice = XChoice( 'Contour Parameters: Choose what to Change',$
                      mainMenu )
    
    CASE choice OF
        0: BEGIN
            temp = ''
            annotation = ''
            REPEAT BEGIN
                Read,'New annotation (<return> alone to stop): ',temp
                annotation = [annotation, temp]
            END UNTIL temp EQ ''
            IF N_Elements( annotation ) GT 2 THEN BEGIN
                annotation = annotation( 1: N_Elements(annotation)-2 )
                mainMenu(0) = 'Annotations: '
                FOR i=0, N_Elements( annotation )-1 DO $
                  mainMenu(0) = mainMenu(0) + annotation(i) + ' '
            ENDIF
        END
        1: BEGIN
            Print,'A normal character size is 1.0'
            Read,'New character size: ',charsize
            mainMenu(1) = 'Characters Size: '+ STRING(charsize)
        END 
        2: BEGIN
            Print,'Colors are ranging from 0 to 255 '
            Read,'Number of colors: ',nColors
            colors = BytArr(nColors)
            Read,'New colors (separated by commas): ',colors
            mainMenu(2) = 'Colors Used: '+STRING(colors)
        END
        3: BEGIN
            temp = ''
            REPEAT BEGIN
                Read,'New label (separated by <return>): ',temp
                label = [label, temp]
            END UNTIL temp EQ ''
            mainMenu(2) = 'Labels Used: '+STRING(labels)
        END
        4: BEGIN
            Print,'Line Styles: '
            Print,'                    0: Solid'
            Print,'                    1: Dotted'
            Print,'                    2: Dashed'
            Print,'                    3: Dash Dot'
            Print,'                    4: Dash Dot Dot Dot'
            Print,'                    5: Long Dashes'
            Read,'Number of line styles: ', nls
            lineStyle = BytArr(nls)
            Read,'New line styles (separated by commas): ',linestyle
            mainMenu(4) = 'Lines Style: '+ STRING(linestyle) 
        END
        5: BEGIN
            Read,'New tickness of lines: ',thick
            mainMenu(5) = 'Thickness Of Lines: ' + STRING(thick)
        END
        6: BEGIN
            Read,'New maximum value: ',maxVal
            mainMenu(7) = 'Maximum Value : ' + STRING(maxVal)
        END
        7: BEGIN
            Read,'New number of levels: ',nLevels
            mainMenu(8) = 'Number of Levels: ' + STRING( nLevels )
        END
        ELSE:
    END
    
END UNTIL choice EQ mainMenuLength-1

END

PRO Test_Axis, axis, nels, STATUS = status

status = "Done"

  CASE N_Elements(axis) OF
      nels:
      0: axis = IndGen(nels)
      ELSE: BEGIN
          Print,' The axis dimension should be equal to the'
          Print,' corresponding array dimension '
          Print, ''
          status = 'Not Done'
      END
  ENDCASE
  
END                             ; Test Axes

PRO Cont_Surf, imageP, xP,yP, FILENAME = fileName, $
        LANDSCAPE = landscape, $
        WINNR = winNr, SIZES_ON = sizes_on


IF N_Params() EQ 0 THEN BEGIN
    Error,  5, 'Cont_Surf'
    RETURN
ENDIF

oldP = !p & oldX = !x & oldY = !y

IF !d.name EQ 'X' THEN BEGIN
    IF Keyword_Set( WINNR ) THEN Window, winNR $
    ELSE Window, 0
ENDIF


pixelLimit = 200
nx = N_Elements( imageP(*,0))
ny = N_Elements( imageP(0,*))

Test_Axis, xP, nx, STATUS = status
IF status  NE "Done" THEN RETURN
Test_Axis, yP, ny, STATUS = status
IF status NE "Done" THEN RETURN

CASE 1 OF
  (nx GT pixelLimit) AND (ny GT pixelLimit): BEGIN
      image = ConGrid( imageP, pixelLimit, pixelLimit, /INTERP )
      x = Congrid1D( xP, pixelLimit, 1, /INTERP )
      y = Congrid1D( yP, pixelLimit, 1, /INTERP )
  END
  (nx GT pixelLimit): BEGIN
      image = Congrid( imageP, pixelLimit, ny, /INTERP )
      x = Congrid1D( xP, pixelLimit, 1 ) & y = yP
  END
  (ny GT pixelLimit): BEGIN
      image = Congrid( imageP, nx, pixelLimit )
      y = Congrid1D( yP, pixelLimit,1 ) & y = yP
  END
  (nx LE pixelLimit) AND (ny LE pixelLimit): BEGIN
      image = imageP & x = xP & y = yP
  END
ENDCASE

mainMenuLength = 8
mainMenu = StrArr(mainMenuLength)

mainMenu(0) = 'Contour'
mainMenu(1) = 'Surface'
mainMenu(2) = 'Smooth Data ... '
mainMenu(3) = 'Change Contour Parameters ... '
mainMenu(4) = 'Make Contour PostScript File / Print ...'
mainMenu(5) = 'Make Surface PostScript File / Print ...' 
mainMenu(6) = 'Setup print parameters ...'
mainMenu(7) = 'Exit this Menu'

smoothP = 0
annotation = [''] & charsize = 1 & colors = [255] & labels = ['']
linestyle = [0] & thick = [1] & nLevels = 5 & maxVal = MAX(image)

Set_XY, Min(x), Max(x), Min(y), Max(y)
!p.font = 0

REPEAT BEGIN
    choice = XChoice( 'Contour and Surface: Choose Option', $
                      mainMenu )
    
    CASE choice OF
        0: IF smoothP LE 1 THEN $
          Contour, image, x,y, $
          C_ANNOTATION = annotation, $
          C_CHARSIZE = charsize, $
          C_COLORS = colors, $
          C_LABELS = labels,$
          NLEVELS = nlevels, $
          MAX_VALUE = maxVal $
        ELSE Contour, Smooth(image, smoothP) , x,y , $
          C_ANNOTATION = annotation, $
          C_CHARSIZE = charsize, $
          C_COLORS = colors, $
          C_LABELS = labels, $
          NLEVELS = nlevels, $
          MAX_VALUE = maxVal 
        1: IF smoothP LE 1 THEN Surface, BytScl(image), x, y, $
          TITLE = '' ,SUBTITLE = '' , XTITLE = '', $
          YTITLE = '' , XSTYLE = 4,  YSTYLE = 4, $
          ZTITLE = '', ZSTYLE = 4, /SKIRT $
        ELSE Surface, BytScl(Smooth(image, smoothP)), x, y, $
          TITLE = '' ,SUBTITLE = '' , XTITLE = '', $
          YTITLE = '' , XSTYLE = 4,  YSTYLE = 4, $
          ZTITLE = '', ZSTYLE = 4, /SKIRT
        2: BEGIN
            Read_Test,' New smooth factor ', smoothP,  tmp, $
              MAXIMUM = nx < ny < 200., MINIMUM = 1.
            smoothP = Fix( tmp )
        END
        3: Change_Pars, annotation,charsize, colors,labels, linestyle, $
          thick, maxVal, nlevels
        4: BEGIN
            IF NOT Keyword_Set( FILENAME )THEN $
              filename = 'enrad.ps'
            IF NOT Keyword_Set(LANDSCAPE) THEN landsc = 0
            dev = !d.name
            Set_Plot,'PRINTER'
            IF Keyword_Set( SIZES_ON ) THEN BEGIN
                GetImageSizes, xPos, yPos, width, height, /PS 
                GetMiscPs, encaps, landsc
                IF landsc THEN BEGIN
                    Device, FILENAME = fileName,  /TIMES, /BOLD, $
                      XOFFSET = xPos, $
                      YOFFSET = yPos, XSIZE = width, YSIZE = height;, $
;                      ENCAPSULATED = encaps
;                    Device, OUTPUT= STRING(1000*(21-2*xPos))+ $
;                      ' 3000 translate 90 rotate'                 
                ENDIF ELSE BEGIN
                    Device, $
                                ;/TIMES, $
                                ; FILENAME = fileName, $
                      XOFFSET = xPos, $
                      YOFFSET = yPos, XSIZE = width, YSIZE = height, $
                      /PORTRAIT
                                ; ,ENCAPSULATED = encaps
                ENDELSE
            ENDIF 
            ;ELSE 
             ;   Device, /TIMES, $
              ;    FILENAME = fileName, LANDSCAPE = landscape
            IF smoothP LE 1 THEN $
              Contour,image, x,y, $
              C_ANNOTATION = annotation, $
              C_CHARSIZE = charsize, $
              C_LABELS = labels, $
              NLEVELS = nlevels, $
              MAX_VALUE = maxVal  $
            ELSE Contour, Smooth(image, smoothP) , x,y , $
              C_ANNOTATION = annotation, $
              C_CHARSIZE = charsize, $
              C_LABELS = labels, $
              NLEVELS = nlevels, $
              MAX_VALUE = maxVal 
            Device, /CLOSE_DOCUMENT
            Set_Plot, dev
        END
        5: BEGIN
            dev = !d.name
            Set_Plot,'PRINTER'
            IF smoothP LE 1 THEN Surface, BytScl(image), x,y, $
              TITLE = '' ,SUBTITLE = '' , XTITLE = '', $
              YTITLE = '' , XSTYLE = 4,  YSTYLE = 4, $
              ZTITLE = '', ZSTYLE = 4, /SKIRT  $
            ELSE Surface, BytScl(Smooth(image, smoothP)), x, y, $
              TITLE = '' ,SUBTITLE = '' , XTITLE = '', $
              YTITLE = '' , XSTYLE = 4,  YSTYLE = 4, $
              ZTITLE = '', ZSTYLE = 4, /SKIRT
            Device,/CLOSE_DOCUMENT
            Set_Plot, dev
        END
        6: status = Dialog_PrinterSetup()
        ELSE:
    ENDCASE
    
    
    
END UNTIL choice EQ mainMenuLength-1

!p = oldP & !x = oldX & !y = oldY

END
