;+
; Project     :	SOLAR-B - EIS
;
; Name        :	EIS_CPT_CHECK_WINDOW
;
; Purpose     :	Checks window parameters are in range.
;
; Explanation : Checks various parameters associated with the definition of a data extraction window are valid.
;
; Use         :	< result = eis_cpt_check_window ( hardware_window, extraction_window, readout) >
;
; Inputs      :	hardware_window    : UINT[4] coordinates of Camera hardware window
;                                            0 : x start
;                                            1 : y start
;                                            2 : x size
;                                            3 : y size
;               extraction_window  : STRUCTURE of type EIS_data_extraction_window.
;               readout            : STRUCTURE of type EIS_CAM_readout
;
; Opt. Inputs : None.
;
; Outputs     : result   : status flag
;                       0 : if checks failed.
;                       1 : checks OK.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	EIS_CPT.
;
; Prev. Hist. :	None.
;
; Written     :	Martin Carter RAL 11/04/05
;
; Modified    :	Version 0.0, 11/04/05, MKC
;               Version 0.1, 08/06/05, MKC
;                 Corrected check on data extraction window.
;               Version 0.2, 24/06/05, MKC
;                 Changed checks since input UINT values.
;               Version 0.3, 29/06/05, MKC
;                 Corrected check on data extraction window.
;               Version 0.4, 03/04/07, MKC
;                 Added more info on error.
;                 Corrected check on y hardware window to 1024.
;
; Version     :	0.4, 03/04/07
;-
;**********************************************************

FUNCTION eis_cpt_check_window, hardware_window, extraction_window, readout

  ; check readout node

  IF extraction_window.node GT 3 THEN BEGIN

    eis_cpt_message, 'INVALID CAMERA READOUT NODE : '+eis_cpt_strtrim(extraction_window.node)

    GOTO, error

  ENDIF

  ; check node enabled

  IF NOT readout.Readout_nodes[extraction_window.node] THEN BEGIN

    eis_cpt_message, 'CAMERA READOUT NODE NOT ENABLED FOR WINDOW, NODE : '+eis_cpt_strtrim(extraction_window.node)

    GOTO, error

  ENDIF

  ; check if CCD for this readout node has split readout and set up X range for hardware window

  IF readout.Readout_nodes[extraction_window.node] AND readout.Readout_nodes[extraction_window.node XOR 1] THEN BEGIN

    ; both left and right readouts enabled

    maxx = 1074

  ENDIF ELSE BEGIN

    ; both left and right not enabled

    maxx = 2148

  ENDELSE

  ; check hardware window valid for the CCD with this readout node

  IF hardware_window[0]+hardware_window[2] GT maxx OR $
     hardware_window[1]+hardware_window[3] GT 1024 THEN BEGIN

    eis_cpt_message, 'INVALID CAMERA HARDWARE WINDOW'

    eis_cpt_message, '        CAMERA HARDWARE WINDOW = ['+ $
                     eis_cpt_strtrim(hardware_window[0])+', '+ $
                     eis_cpt_strtrim(hardware_window[1])+', '+ $
                     eis_cpt_strtrim(hardware_window[2])+', '+ $
                     eis_cpt_strtrim(hardware_window[3])+']'

    GOTO, error

  ENDIF

  ; check data extraction window coords
  ; the data extraction window is specified in coords xwidth=0-2147, yheight=0-1023
  ; the data extraction window must lie within the hardware window and its mirrors
  ; if both left and right readout nodes are enabled then windows spanning the CCD centre are not allowed.

  ; check if both readout nodes enabled and right readout node selected
  ;                   node                     : UINT readout node :
  ;                                                0 : CCDA(CCD0) right hand side
  ;                                                1 : CCDA(CCD0) left hand side
  ;                                                2 : CCDB(CCD1) right hand side
  ;                                                3 : CCDB(CCD1) left hand side

  IF readout.Readout_nodes[extraction_window.node] AND readout.Readout_nodes[extraction_window.node XOR 1] AND $
     (extraction_window.node AND 1) EQ 0 THEN BEGIN

    ; check extraction window inside hardware window for right node

    IF extraction_window.xsize  LT 1 OR $
       extraction_window.xstart LT 2*1074-1-(hardware_window[0]+hardware_window[2]) OR $
       extraction_window.xstart+extraction_window.xsize GT 2*1074-hardware_window[0] THEN BEGIN

      eis_cpt_message, 'INVALID DATA EXTRACTION WINDOW COORDINATES'

      eis_cpt_message, '        [XSTART,XSIZE] = ['+ $
                       eis_cpt_strtrim(extraction_window.xstart)+', '+ $
                       eis_cpt_strtrim(extraction_window.xsize)+']'

      eis_cpt_message, '        CAMERA HARDWARE WINDOW = ['+ $
                       eis_cpt_strtrim(hardware_window[0])+', '+ $
                       eis_cpt_strtrim(hardware_window[1])+', '+ $
                       eis_cpt_strtrim(hardware_window[2])+', '+ $
                       eis_cpt_strtrim(hardware_window[3])+']'

      GOTO, error

    ENDIF

  ENDIF ELSE BEGIN

    ; check extraction window inside hardware window

    IF extraction_window.xsize  LT 1 OR $
       extraction_window.xstart LT hardware_window[0] OR $
       extraction_window.xstart+extraction_window.xsize GT hardware_window[0]+hardware_window[2] THEN BEGIN

      eis_cpt_message, 'INVALID DATA EXTRACTION WINDOW COORDINATES'

      eis_cpt_message, '        [XSTART,XSIZE] = '+ $
                       eis_cpt_strtrim(extraction_window.xstart)+', '+ $
                       eis_cpt_strtrim(extraction_window.xsize)+']'

      eis_cpt_message, '        CAMERA HARDWARE WINDOW = ['+ $
                       eis_cpt_strtrim(hardware_window[0])+', '+ $
                       eis_cpt_strtrim(hardware_window[1])+', '+ $
                       eis_cpt_strtrim(hardware_window[2])+', '+ $
                       eis_cpt_strtrim(hardware_window[3])+']'
      GOTO, error

    ENDIF

  ENDELSE

  ; return OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





