;+
; NAME:
;	Data Display
; PURPOSE:
;	Plotting facility for more than one plots which have
;	optionally:
;		-- different number of relevant values
;		-- error bars
;		-- different plotting symbols, line thickness, 
;			color, range, etc
; CALLING SEQUENCE:
;	Data_Display [, x],  y [, q, dx, dy, userSym]
; INPUT PARAMETERS:
; 	x: the x-values, a 2D array. If not present, the x range is set 
;		from 0 to the number of elements minus 1.
;	y: the y-values also real 2D array, each line is a plot.
;	q: the qualifiers for each value to plot. The number follows
;		the definition of the !psym system variable.
;		It is a 2D integer array
;	dx, dy; the errors in x- and y-direction. If the error bars 
;		are symmetrical, then "dx" and "dy" are
;		2D real arrays. If they are not symmetrical,
;		then dx(*,*,0) and dx(*,*,1) contain the errors
;		left and right, respectively and dy(*,*,0) and
;		 dy(*,*,1) contain the errors up and down,
;		respectively.
;	userSym; the value of a user defined symbol, see the
;		mdisp manual for more information.
; KEYWORDS:
;	LINESTYLE: for each plot a value corresponding to the
;		!p.linestyle system variable. A 1D array.
;	THICK: The line thickness, corresponding to the !p.thick
;		system variable. A 1D real array.
;	OFFSET: An y-offset added to all y-values of a plot,
;		a 1D real array.
;	COLOR: a color for each plot, 1D integer array
;	PSYM: the plot symbol used to draw all points having a
;		qualifier 0, 1D array, see the list at the
;		!psym system variable description.
;	XRANGE, YRANGE Two arrays of two values, defining
;		the range if it is not the minimum and
;		maximum value in the x and y variables.
;	LENGTH: If the plots have different number of relevant
;		values, the length gives how much points of the
;		array should be drawn.
;	RESET: If the old x and y range have to be reset.
;	TITLE, SUBTITLE, XTITLE, YTITLE: the titles.
; 	HMS: if set, the x axis is drawn in hour/min/sec.
; SIDE EFFECT: 
;	A window is opened (on x Terminals)
; MODIFICATION HISTORY:
;	Created by A.Csillaghy in May 1991
;		Institute of Astronomy, ETH Zurich
;	Totally rewritten in October 1991, A.Cs.
;	Modification for h/m/s and wave 4 in Dec. 92, A.Cs.
;-

		
FUNCTION SetBound, value, error, length, nPlots, OFFSET = offset, $
	YDIR = yDir

  IF NOT Keyword_Set( OFFSET ) THEN offset = 0*length
  sizErr = Size( error )
  IF sizErr(0) EQ 3 THEN right = 1 ELSE right = 0

  maximum = Max( value( 0: length(0)-1, 0 ), MIN = minimum ) $
	+ offset(0)
  minimum = minimum + offset(0)

  errMax = 0. & errMin = 0.

  IF Total(error) GT 1e-6  THEN BEGIN
      errMax = Max( error( 0: length(0)-1, 0, 0 ) )
      IF right THEN errMin = Max( error( 0: length(0) - 1, 0, 1) )$
      ELSE errMin = errMax
  ENDIF

  FOR i = 1, nPlots-1 DO BEGIN
    maximum = maximum > (Max( value( 0: length(i)-1, i ), $
	MIN = minTmp ) + offset(i))
    minimum = minimum < (minTmp + offset(i))

    IF Total( error ) NE 0 THEN BEGIN
      errMax = errMax > Max( error( 0: length(i)-1, i,0 ) )
      IF right THEN errMin = errMin > Max( error( 0: length(i)-1, i,1)) $
      ELSE errMin = errMax
    ENDIF
  ENDFOR

  IF NOT Keyword_set( YDIR ) THEN BEGIN
    tmp = errMin & errMin = errMax & errMax = tmp
  ENDIF

  IF Total( error ) GT 1e-6 THEN BEGIN
    minimum = minimum - errMin
    maximum = maximum + errMax
  ENDIF

  RETURN, [ minimum, maximum ]

END ; Set Bounds


PRO PlotOne, x, y, q, ls, THICK = thick, $
	COLOR = color, $
	USERSYM = userSym, SYMSIZE = symsize

  ; in this proc, x, y, q, are 1D  

  n = N_Elements( x )
  IF N_Elements( q ) NE n THEN q = Replicate( 0, n )
  idx = LIndGen( n )
  breaks = Where( q( idx+1 ) NE q(idx) OR ls( idx+1 ) NE ls(idx) )
  IF Total( breaks ) EQ -1 THEN breaks = n-1 $
  ELSE breaks = [ breaks, n-1]

 l = 0L & i = 0L

  REPEAT BEGIN
    r = breaks(i)
    xCoord = x(l:r)
    IF Abs(q(r)) EQ 8 THEN LoadUserSym, userSym(r)
    IF thick EQ 0 THEN thick = !p.thick
    IF (q(r) LE 0) AND (l GT 0) THEN BEGIN
      xPts = x( l-1:l )
      OPlot, xPts, y( l-1:l ), PSYM = -3, THICK = thick, $
	LINESTYLE = ls(r), COLOR = color, $
	SYMSIZE = symsize
    ENDIF
    OPlot, xCoord, y( l: r ), PSYM = q(r), THICK = thick, $
	LINESTYLE = ls(r), COLOR = color, $
	SYMSIZE = symsize
    i = i+1
    l = r+1
  END UNTIL l  GT n-1

END ; Plot One
  

PRO Data_Display, xP, y, q, dx, dy, userSym, $
	LINESTYLE = lineStyle, THICK = thick, $
	OFFSET = offset, COLOR = color,  PSYM = pSym, $
	XRANGE = xRange, YRANGE = yRange, $
	LENGTH = length, RESET = reset, $
 	TITLE = title, SUBTITLE = subTitle, $
	XTITLE = xTitle, YTITLE = yTitle, $
	MDISP = mdisp, NOCOLOR = noColor, HMS = hms, $
	SYMSIZE = symsize

  IF (N_Params() LT 1) AND NOT Keyword_Set( MDISP )$
	 THEN BEGIN
    Error, 1, 'Data_Display'
    RETURN
  ENDIF ELSE IF Keyword_Set( MDISP ) THEN BEGIN
    LoadSelection, d1, length, xP, y, q, dx, dy, color, lineStyle, $
	thick, offset, overallQual, userSym, $
	title, subtitle, xTitle, yTitle, text, symsize
    GetTitles, title, xTitle, yTitle, subTitle
    hms = GetHMS()
    !p.title = title & !x.title = xTitle & !y.title = yTitle 
    !p.subtitle = subTitle
  ENDIF

  nElsX = N_Elements(xP)
  IF nElsX EQ 0 THEN BEGIN
    Error, 6, 'Data_Display'
    RETURN
  ENDIF

  n = N_Elements( xP(*,0) )
  nPlots = N_Elements( xP(0,*) )
  IF N_Elements(y) EQ 0  THEN BEGIN
    y = xP
    x = LIndGen( n, nPlots ) MOD n
  ENDIF ELSE  x = xP

  nq = N_Elements( q(*,0) )
  IF N_Elements( q ) EQ 0 THEN  BEGIN q = 0 & qs = 0 & ENDIF $
  ELSE qs = 1
  IF N_Elements( dx ) EQ 0 THEN dx = 0
  IF N_Elements( dy ) EQ 0 THEN dy = 0
  IF N_Elements( userSym ) EQ 0 THEN userSym = 0

; Here are all parameters defined

  IF NOT Keyword_Set( MDISP ) THEN BEGIN
    IF NOT Keyword_Set( LINESTYLE ) THEN $
	lineStyle = Replicate( 0, nplots )
    IF NOT Keyword_Set(THICK ) THEN $
 	thick = Replicate( 0, nPlots )
    IF NOT Keyword_Set( OFFSET ) THEN $
	offset = Replicate( 0, nPlots )
    IF N_Elements( color ) EQ 0  THEN $
	color = Replicate( 255B, nPlots )
    IF NOT Keyword_Set( LENGTH ) THEN $
	length = Replicate(n, nPlots)
    IF NOT Keyword_Set( SYMSIZE ) THEN $
	symsize = Replicate( 0, nPlots )
    IF NOT Keyword_Set( TITLE ) THEN title = !p.title
    IF NOT Keyword_Set( SUBTITLE ) THEN subTitle = !p.subtitle
    IF NOT Keyword_Set( XTITLE ) THEN xTitle = !x.title
    IF NOT Keyword_Set( YTITLE ) THEN yTitle = !y.title
    IF NOT Keyword_Set( PSYM ) THEN pSym = 0
    IF NOT Keyword_Set( HMS ) THEN hms = 0 ELSE hms = 1
  ENDIF

  IF Keyword_Set( RESET ) OR $
	( !x.range(0) EQ 0 AND !y.range(0) EQ 0 $
	AND !x.range(1) EQ 0 AND !y.range(1) )  $
	THEN BEGIN
    IF NOT Keyword_Set( XRANGE ) THEN $
        xRange = SetBound( x, dx, length, nPlots )
    IF NOT Keyword_Set( YRANGE ) THEN $
      yRange = SetBound( y, dy, OFFSET = offset, length, nPlots )
    !x.range = xRange 
    !y.range = yRange
  ENDIF

   IF Keyword_Set( NOCOLOR ) THEN color = Replicate( 0B, nPlots )
   IF !d.name EQ 'TEK' THEN BEGIN
     Erase
     EraseVT
    ENDIF

   Plot, x(*,0), y(*,0), /NODATA, /YNOZERO, TITLE = title, $
	XTITLE = xTitle, YTITLE = yTitle,  $
	SUBTITLE = subTitle, XTYPE = !x.type, $
	YTYPE = !y.type, XSTYLE = 4*(hms EQ 1) + 1
    IF hms THEN TimeAxis, TITLE = xTitle, CHARSIZE = !p.charSize
;   IF hms THEN AxisTime, yp, !x.range, ps, CHARSIZE= !P.charSize

  dxs = Size( dx ) & dxs = dxs(0) 
  dys = Size( dy ) & dys = dys(0) 
  IF N_Elements( overallQual ) EQ 0 THEN overallQual = 0*thick

  FOR i = 0, nPlots - 1 DO BEGIN
    
    thisLen = length(i)-1
    xOne = x( 0: thisLen, i ) 
    yOne = y( 0: thisLen, i ) + offset(i)
    IF qs THEN BEGIN
      qOne = q( 0: thisLen, i ) 
      lsOne = lineStyle( 0: thisLen, i )
      IF  N_Elements(VectCmp( qOne, 'EQ' )) EQ  thisLen THEN $
	qOne = overallQual(i) + 0*qOne $
      ELSE BEGIN
        idx = Where( qOne EQ 0 )
        IF Total( idx ) NE -1 THEN qOne( idx ) = overallQual(i)
      ENDELSE
    ENDIF ELSE qOne = 0*xOne + pSym
    IF N_Elements( userSym ) GT 1 THEN $
	userSymOne = UserSym( 0:thisLen, i) $
    ELSE userSymOne = 0

    PlotOne, xOne, yOne, qOne, lsOne,  $
	THICK = thick(i),  $
	COLOR = color(i), USERSYM = userSymOne, $
	SYMSIZE = symsize(i)
 
    IF dxs GE 1   THEN BEGIN
      dxl = dx( 0:thisLen, i, 0 ) 
      IF Total( dxl ) GT 1e-6 THEN BEGIN
        IF dxs EQ 3 THEN dxr = dx( 0:thisLen, i, 1 ) ELSE dxr = dxl 
        Err_Plot, yOne, xOne - dxl, xOne + dxr , /XDIR
      ENDIF
    ENDIF
    IF dys GE 1 THEN BEGIN
      dyu = dy( 0:thisLen, i, 0 ) 
      IF Total( dyu ) GT 1e-6 THEN BEGIN
        IF dys EQ 3 THEN dyd = dy( 0:thisLen, i, 1 ) ELSE dyd = dyu 
        Err_Plot, xOne, yOne - dyd, yOne + dyu
      ENDIF
    ENDIF
 	
  ENDFOR 

  nbTexts = N_Texts()
  IF nbTexts GE 1 THEN $
    FOR i = 1, nbTexts DO BEGIN
      ls = !p.linestyle & !p.linestyle = 0
      Get_Text,i,xPos,yPos,text,sizeTxt
      XYOuts, xPos,yPos, text, CHARSIZE = sizeTxt
      !p.linestyle = ls
    ENDFOR

  IF !d.name EQ 'TEK' THEN Reset

END ; data display
