;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_CHECK_STUDY
;
; Purpose     : Checks various study parameters valid.
;
; Explanation : Checks various study parameters valid.
;
; Use         : < result = eis_cpt_check_study ( study_detail, study) >
;
;               study_detail           : STRUCTURE of type EIS_study_detail.
;               study                  : STRUCTURE ARRAY of type EIS_raster_detail or EIS_engineering_detail.
;
; Opt. Inputs : None.
;
; Outputs     : result   : status flag
;                       0 : if checks failed.
;                       1 : checks OK.
;
; Opt. Outputs: None.
;
; Keywords    : RESPONSE_RASTER_ID : ULONG raster identifier indicating response study response raster
;               ADJUST_Y           : INT Flag indicating YSTART adjust allowed
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 17/12/04
;
; Modified    : Version 0.0, 17/12/04, MKC
;               Version 0.1, 05/05/05, MKC
;                 Added check that study is defined.
;               Version 0.2, 20/06/05, MKC
;                 Added extra check on study repeat number.
;                 Added check that if study uses event control or AEC then corresponding
;                 flag is set in studies list.
;               Version 0.3, 13/07/05, MKC
;                 Added check that no. of slit/slot changes agrees with study_detail.
;               Version 0.4, 13/06/06, MKC
;                 Added Study ID to more error messages.
;               Version 0.5, 12/10/06, MKC
;                 Added check on linelist details for engineering study.
;               Version 0.6, 08/12/06, MKC
;                 Removed check on number of slit/slot changes.
;               Version 0.7, 10/01/07, MKC
;                 Corrected check on number of slit/slot changes.
;                 Used eis cpt changes.
;                 Added checks for disallowed tag.
;               Version 0.8, 20/04/07, MKC
;                 Added response raster KEYWORD.
;                 Moved checks for disallowed tag.
;               Version 0.9, 23/03/10, MKC
;                 Added keyword ADJUST_Y.
;               Version 1.0, 12/05/13, MKC
;                 Changed keyword RESPONSE RASTER to RESPONSE RASTER ID.
;
; Version     : 1.0, 12/05/13
;-
;**********************************************************

FUNCTION eis_cpt_check_study, study_detail, study, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y

  ; check study is defined

  IF NOT KEYWORD_SET(study) THEN BEGIN

    eis_cpt_message, 'STUDY : '+eis_cpt_strtrim(study_detail.study_identifier) + ', NOT DEFINED'

    GOTO, error

  ENDIF

  ; check if science or engineering study

  IF study_detail.engineering GT 0 THEN BEGIN

    ; engineering study

    ; check EIS mechanism change structures are equal

    IF study[0].slit_slot.initial NE study_detail.slit_slot.initial OR $
       study[0].slit_slot.final   NE study_detail.slit_slot.final OR $
       study[0].slit_slot.number_of_changes NE study_detail.slit_slot.number_of_changes OR $
       study[0].slit_slot.disallowed NE study_detail.slit_slot.disallowed THEN BEGIN

       eis_cpt_message, 'STUDY DOES NOT AGREE WITH STUDY DETAIL, ID : '+eis_cpt_strtrim(study_detail.study_identifier)

       GOTO, error

    ENDIF

    ; check if engineering study

    IF study_detail.engineering EQ 1 THEN BEGIN

      ; check study type identified correctly

      IF TAG_NAMES(study[0],/STRUCTURE_NAME) NE 'EIS_ENGINEERING_DETAIL' THEN BEGIN

        eis_cpt_message, 'INVALID STUDY TYPE, ID : '+eis_cpt_strtrim(study_detail.study_identifier)

        GOTO, error

      ENDIF

      ; check linelist details
      ; if linelist file name is ne blank then linelist will be loaded into OBSTBL

      list = WHERE(study[0].linelist_filenames NE '', count)

      ; the linelist indexes used must be unique

      IF count GT 0 THEN BEGIN

        ; check linelists indexes used in study are unique

        IF NOT eis_cpt_unique(study[0].linelist_indexes[list],/CHECK) THEN BEGIN

          eis_cpt_message, 'INVALID LINELISTS FOR ENGINEERING STUDY DETAIL, ID : '+eis_cpt_strtrim(study_detail.study_identifier)

          GOTO, error

        ENDIF

      ENDIF

    ENDIF ELSE BEGIN

      ; ORL engineering study

      ; check study type identified correctly

      IF TAG_NAMES(study[0],/STRUCTURE_NAME) NE 'EIS_ORL_DETAIL' THEN BEGIN

        eis_cpt_message, 'INVALID STUDY TYPE, ID : '+eis_cpt_strtrim(study_detail.study_identifier)

        GOTO, error

      ENDIF

    ENDELSE

  ENDIF ELSE BEGIN

    ; science study

    ; check study type identified correctly

    IF TAG_NAMES(study[0],/STRUCTURE_NAME) NE 'EIS_RASTER_DETAIL' THEN BEGIN

      eis_cpt_message, 'INVALID STUDY TYPE, ID : '+eis_cpt_strtrim(study_detail.study_identifier)

      GOTO, error

    ENDIF

    ; check study repeat valid

    IF study_detail.study_repeat_number EQ 0 OR $
       (eis_multiple_sequence_tables(study, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y) AND study_detail.study_repeat_number NE 1) THEN BEGIN

       eis_cpt_message, 'STUDY REPEAT NUMBER NOT VALID : MUST BE ONE IF MULTIPLE SEQUENCE TABLES, ID : ' + $
                        eis_cpt_strtrim(study_detail.study_identifier)

       GOTO, error

    ENDIF

    ; check event control and AEC flags

    IF (TOTAL(study.XRT_flare_control_flag) < 1) NE study_detail.XRT_flare_control_flag OR $
       (TOTAL(study.EIS_flare_control_flag) < 1) NE study_detail.EIS_flare_control_flag OR $
       (TOTAL(study.EIS_event_control_flag) < 1) NE study_detail.EIS_event_control_flag OR $
       (TOTAL(study.AEC_control_flag) < 1) NE study_detail.AEC_control_flag THEN BEGIN

       eis_cpt_message, 'STUDY CONTROL FLAG SET BUT STUDY LIST CONTROL FLAG NOT SET, ID : ' + $
                        eis_cpt_strtrim(study_detail.study_identifier)

       GOTO, error

    ENDIF

    ; check no. of slit/slot changes

    IF eis_cpt_changes(study.slit_slot) NE study_detail.slit_slot.number_of_changes THEN BEGIN

       eis_cpt_message, 'NUMBER OF MECHANISM CHANGES IN STUDY DOES NOT AGREE WITH STUDY DETAIL, ID : ' + $
                        eis_cpt_strtrim(study_detail.study_identifier)

       GOTO, error

    ENDIF

    ; check start and end slit/slot

    IF study[0].slit_slot NE study_detail.slit_slot.initial OR $
       study[N_ELEMENTS(study)-1].slit_slot NE study_detail.slit_slot.final THEN BEGIN

       eis_cpt_message, 'START OR END SLIT/SLOT IN STUDY DOES NOT AGREE WITH STUDY DETAIL, ID : ' + $
                        eis_cpt_strtrim(study_detail.study_identifier)

       GOTO, error

    ENDIF

  ENDELSE

  ; return OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





