;******************************************************************************
;* File name: ql_init_dctrl.pro                                               *
;*                                                                            *
;* Content  :   ql_init_i_display_ctrl                                        *
;*            	ql_init_p_display_ctrl                                        *
;*              ql_init_dctrl                                                 *
;*                                                                            *
;* Date		Author		        Comment                               *
;* -------------------------------------------------------------------------- *
;* 19-01-94    	E.PETIT	   	    Creation                                  *
;* 08-02-94    	E.PETIT	   	    SPR 19 Number of tick for profile         *
;* 18-02-94    	E.PETIT	   	    Change height of profile display          *
;* 21-02-94    	E.PETIT	   	    Initial color for profile                 *
;* 24-02-94    	E.PETIT	   	    New format                                *
;* 23-03-94    	E.PETIT	   	    Add init of main title                    *
;* 13-04-94    	E.PETIT	   	    remove discret format                     *
;* 18-04-94    	E.PETIT	   	    Add min/max management                    *
;* 02-05-94    	E.PETIT	   	    Remove unused field                       *
;* 18-05-94    	E.PETIT	   	    Change nd tick computing                  *
;* 22-08-94    	E.PETIT	   	    extrema value depends on binning          *
;* 08-09-94    	E.PETIT	   	    Axis label                                *
;* 17-01-95    	E.PETIT	   	    Binning Pb	                              *
;* 26-01-95    	E.PETIT	   	    Multi prf 	                              *
;*                                                                            *
;******************************************************************************


;***              
;##############################################################################
;#Function  name   : ql_init_i_display_ctrl                                   #
;#LEVEL            :                                                          #
;#                                                                            #
;#Author           : Emmanuel PETIT                                           #
;#Language         : IDL                                                      #
;#Purpose          : Load data from selected rrd item                         #
;#Creation date    : 19-01-94                                                 #
;#Update date      :                                                          #
;#External modules :                                                          #
;#                                                                            #
;#Call sequence    :                                                          #
;#                                                                            #
;#Arguments        : err                                                      #
;#                                                                            #
;#Description      : Init of control image display structure                  # 
;#                                                                            #
;#                                                                            #
;##############################################################################
;*** 

PRO ql_init_i_display_ctrl, err

  ;============================================================================
  ;                       Common Block in use
  ;============================================================================
  COMMON curr_data_i_com
  COMMON format_com
  COMMON const_com
  COMMON color_com

  ;============================================================================
  ;                       Initialization
  ;============================================================================
  err = 0
  xt  = 1 
  yt  = 1   
  xm  = 0
  ym  = 0

  ;============================================================================
  ;                       Processing 
  ;============================================================================
  
  s = size (curr_data_i)   

  curr_dctrl_i.grid_flg        = 0
  curr_dctrl_i.grid_style      = 0
  curr_dctrl_i.contour_flg     = 0
  curr_dctrl_i.color           = -1
  curr_dctrl_i.symbol          = 0
  curr_dctrl_i.process_id(*)   = 0


  curr_dctrl_i.x.min           = 0.0
  curr_dctrl_i.x.max           = FLOAT(s(1)*curr_header_i.SSBINNY-1)
  curr_dctrl_i.x.nb_dot        = s(1)
  curr_dctrl_i.x.plot_min      = curr_dctrl_i.x.min
  curr_dctrl_i.x.plot_max      = curr_dctrl_i.x.max
  curr_dctrl_i.x.plot_sz       = s(1)
  curr_dctrl_i.x.index_min     = 0
  curr_dctrl_i.x.index_max     = s(1)-1
  curr_dctrl_i.x.offset        = IMAGE_X_OFFSET
  curr_dctrl_i.x.tick_len      = 2     
  curr_dctrl_i.x.type          = 0
  curr_dctrl_i.x.style         = 1
  curr_dctrl_i.x.comment       = STRING('Axis 1, Co-Pixels',FORMAT=Comment_Format)
  curr_dctrl_i.x.nb_tick       = CEIL (s(1)/60.0)
  curr_dctrl_i.x.nb_minor      = 4

  curr_dctrl_i.y.min           = 0.0
  curr_dctrl_i.y.max           = FLOAT(s(2)*curr_header_i.SSBINNZ-1)  
  curr_dctrl_i.y.nb_dot        = s(2)
  curr_dctrl_i.y.plot_min      = curr_dctrl_i.y.min    
  curr_dctrl_i.y.plot_max      = curr_dctrl_i.y.max
  curr_dctrl_i.y.index_min     = 0
  curr_dctrl_i.y.index_max     = s(2)-1
  curr_dctrl_i.y.plot_sz       = s(2)
  curr_dctrl_i.y.offset        = IMAGE_Y_OFFSET
  curr_dctrl_i.y.tick_len      = 2     
  curr_dctrl_i.y.type          = 0
  curr_dctrl_i.y.style         = 1
  curr_dctrl_i.y.comment       = STRING('Axis 2, Pixels', FORMAT=Comment_Format) 
  curr_dctrl_i.y.nb_tick       = CEIL (s(2)/30.0)
  curr_dctrl_i.y.nb_minor      = 4

  ; Compute position
  ; in case of raw data
  ; (part of detector)
  ; -------------------
  IF (curr_header_i.DATATYPE EQ 1) THEN BEGIN
    IF (curr_dctrl_i.x.nb_dot*curr_header_i.SSBINNY LT 1024) THEN BEGIN
      det_win_sz = curr_dctrl_i.x.nb_dot*curr_header_i.SSBINNY

      curr_dctrl_i.x.min = curr_header_i.SSREFPIX - FIX(det_win_sz/2)
      curr_dctrl_i.x.max = curr_header_i.SSREFPIX + FIX(det_win_sz/2)
      IF ((det_win_sz mod 2) EQ 0 ) THEN curr_dctrl_i.x.max = curr_dctrl_i.x.max - 1

      curr_dctrl_i.x.plot_min = curr_dctrl_i.x.min
      curr_dctrl_i.x.plot_max = curr_dctrl_i.x.max
  
      dat_win_sz = curr_dctrl_i.x.nb_dot
      data_offset = curr_header_i.SSREFPIX - FIX(dat_win_sz/2)
      IF ((dat_win_sz mod 2) EQ 0 ) THEN  data_offset =  data_offset+1
      curr_dctrl_i.x.offset   = IMAGE_X_OFFSET + data_offset 
    ENDIF
    curr_dctrl_i.x.det_min = curr_dctrl_i.x.plot_min
  
    IF (curr_dctrl_i.y.nb_dot*curr_header_i.SSBINNZ LT 360) THEN BEGIN
      slit =  curr_header_i.SSSLITNB 
      CASE  1 OF
        (slit EQ 1) OR (slit EQ 2) OR (slit EQ 4) OR (slit EQ 7) :vert_ref_pix = 179		; Center
        (slit EQ 3) OR (slit EQ 6)			 	 :vert_ref_pix = 299    	; Top
        (slit EQ 5) OR (slit EQ 8)				 :vert_ref_pix = 59    		; Bottom
        ELSE					         	 :vert_ref_pix = 179    	; default
      ENDCASE
  
      det_win_sz = curr_dctrl_i.y.nb_dot*curr_header_i.SSBINNZ
      curr_dctrl_i.y.min = vert_ref_pix - FIX(det_win_sz/2)
      curr_dctrl_i.y.max = vert_ref_pix + FIX(det_win_sz/2)
      IF ((det_win_sz mod 2) EQ 0 ) THEN curr_dctrl_i.y.min = curr_dctrl_i.y.min + 1
      curr_dctrl_i.y.plot_min = curr_dctrl_i.y.min
      curr_dctrl_i.y.plot_max = curr_dctrl_i.y.max
  
      dat_win_sz = curr_dctrl_i.y.nb_dot
      data_offset = vert_ref_pix - FIX(dat_win_sz/2)
      IF ((dat_win_sz mod 2) EQ 0 ) THEN  data_offset =  data_offset+1
      curr_dctrl_i.y.offset   = IMAGE_Y_OFFSET + data_offset 
    ENDIF
    curr_dctrl_i.y.det_min = curr_dctrl_i.y.plot_min
  ENDIF
END

;***              
;##############################################################################
;#Function  name   : ql_init_p_display_ctrl                                   #
;#LEVEL            :                                                          #
;#                                                                            #
;#Author           : Emmanuel PETIT                                           #
;#Language         : IDL                                                      #
;#Purpose          : Load data from selected rrd item                         #
;#Creation date    : 19-01-94                                                 #
;#Update date      : 08-02-94    	E.PETIT	   	SPR 19                #
;#External modules :                                                          #
;#                                                                            #
;#Call sequence    :                                                          #
;#                                                                            #
;#Arguments        : err                                                      #
;#                                                                            #
;#Description      : Init of control profile display structure                #
;#                                                                            #
;#                                                                            #
;##############################################################################
;*** 

PRO ql_init_p_display_ctrl, err

  ;============================================================================
  ;                       Common Block in use
  ;============================================================================
  COMMON curr_data_p_com
  COMMON format_com
  COMMON const_com
  COMMON color_com

  ;============================================================================
  ;                       Initialization
  ;============================================================================
  err = 0
 
  ;============================================================================
  ;                       Processing 
  ;============================================================================
  
  s = SIZE (curr_data_p)  

  ymax = max(curr_data_p, xmax, min=ymin) 

  curr_dctrl_p.grid_flg        = 0
  curr_dctrl_p.grid_style      = 0
  curr_dctrl_p.contour_flg     = 0
  curr_dctrl_p.color           = yellow
  curr_dctrl_p.process_id(*)   = 0
  curr_dctrl_p.symbol          = 0 

  curr_dctrl_p.x.min       = 0.0
  curr_dctrl_p.x.max       = FLOAT(s(2)*curr_header_p.SSBINNZ-1)
  curr_dctrl_p.x.nb_dot    = s(2)
  curr_dctrl_p.x.plot_min  = curr_dctrl_p.x.min 
  curr_dctrl_p.x.plot_max  = curr_dctrl_p.x.max
  curr_dctrl_p.x.plot_sz   = curr_dctrl_p.x.nb_dot
  curr_dctrl_p.x.index_min = 0
  curr_dctrl_p.x.index_max = s(2)-1
  curr_dctrl_p.x.offset    = PROFILE_X_OFFSET    
  curr_dctrl_p.x.tick_len  = 2
  curr_dctrl_p.x.type      = 0
  curr_dctrl_p.x.style     = 1
  curr_dctrl_p.x.comment   = STRING('Axis 1', FORMAT=Comment_Format)
  curr_dctrl_p.x.nb_tick   = CEIL (s(2)/60.0)
  curr_dctrl_p.x.nb_minor  = 4
  curr_dctrl_p.x.det_min   = 0

  curr_dctrl_p.y.min       = ymin
  curr_dctrl_p.y.max       = ymax
  curr_dctrl_p.y.nb_dot    = PROFILE_Y
  curr_dctrl_p.y.plot_min  = curr_dctrl_p.y.min
  curr_dctrl_p.y.plot_max  = curr_dctrl_p.y.max
  curr_dctrl_p.y.plot_sz   = curr_dctrl_p.y.nb_dot
  curr_dctrl_p.y.index_min = -1
  curr_dctrl_p.y.index_max = -1
  curr_dctrl_p.y.offset    = PROFILE_Y_OFFSET
  curr_dctrl_p.y.nb_tick   = 5
  curr_dctrl_p.y.tick_len  = 2
  curr_dctrl_p.y.nb_minor  = 5
  curr_dctrl_p.y.type      = 0
  curr_dctrl_p.y.style     = 1
  curr_dctrl_p.y.comment   = STRING('Axis 2', FORMAT=Comment_Format)
  curr_dctrl_p.y.det_min   = 0

  slit =  curr_header_p.SSSLITNB 
  CASE  1 OF
    (slit EQ 1) OR (slit EQ 2) OR (slit EQ 4) OR (slit EQ 7) 	:vert_ref_pix = 179	; Center
    (slit EQ 3) OR (slit EQ 6)			            	:vert_ref_pix = 299    	; Top
    (slit EQ 5) OR (slit EQ 8)			          	:vert_ref_pix = 59    	; Bottom
    ELSE					        	:vert_ref_pix = 179    	; default
  ENDCASE

  ; Compute position
  ; in case of raw data
  ; (part of detector)
  ; -------------------
  IF (curr_header_p.DATATYPE EQ 9) THEN BEGIN
    IF (curr_dctrl_p.x.nb_dot*curr_header_p.SSBINNZ LT 360) THEN BEGIN
      det_win_sz = curr_dctrl_p.x.nb_dot*curr_header_p.SSBINNZ
      curr_dctrl_p.x.min = vert_ref_pix - FIX(det_win_sz/2)
      curr_dctrl_p.x.max = vert_ref_pix + FIX(det_win_sz/2)
      IF ((det_win_sz mod 2) EQ 0 ) THEN curr_dctrl_p.x.min = curr_dctrl_p.x.min + 1
  
      curr_dctrl_p.x.plot_min = curr_dctrl_p.x.min
      curr_dctrl_p.x.plot_max = curr_dctrl_p.x.max
    ENDIF
  ENDIF

  ; Compute x values 
  ; ----------------
  curr_x_p = FINDGEN (s(2))
  curr_x_p = curr_dctrl_p.x.min + curr_x_p*(curr_dctrl_p.x.max-curr_dctrl_p.x.min)/(s(2)-1)

  ; Avoid display Pb
  ; ----------------
  IF (curr_dctrl_p.y.plot_min EQ curr_dctrl_p.y.plot_max) THEN $
    curr_dctrl_p.y.plot_max = curr_dctrl_p.y.plot_min + 1

END

;***              
;##############################################################################
;#]Function  name   : ql_init_dctrl                                           #
;#LEVEL            :                                                          #
;#                                                                            #
;#Author           : Emmanuel PETIT                                           #
;#Language         : IDL                                                      #
;#Purpose          : initialization of display control structure              #
;#Creation date    : 20-01-94                                                 #
;#Update date      :                                                          #
;#External modules :                                                          #
;#                                                                            #
;#Call sequence    :                                                          #
;#                                                                            #
;#Arguments        : err                                                      #
;#                 : mode: 0 for image display control structure              #
;#                 : mode: 1 for profile display control structure            #
;#                                                                            #
;#Description      : Init of control image display structure                  #
;#                                                                            #
;#                                                                            #
;##############################################################################
;*** 

PRO ql_init_dctrl, mode, err

  ;============================================================================
  ;                       Common Block in use
  ;============================================================================
  COMMON const_com
  ;============================================================================
  ;                       Initialization
  ;============================================================================
  err = 0
 
  ;============================================================================
  ;                       Processing 
  ;============================================================================
  CASE mode OF
    IMAGE  : ql_init_i_display_ctrl
    PROFILE: ql_init_p_display_ctrl
    else   : ql_init_i_display_ctrl
  ENDCASE


END

