;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS_STUDY
;
; Purpose     : Processes science or engineering study.
;
; Explanation : Constructs the OBSTBL linelist tables and sequence tables for the science or engineering study.
;               The sequence tables are stored as an array of {EIS_CPT_sequence}.
;               The linelist tables are written directly to the OBSTBL.
;
; Use         : < result = eis_cpt_process_study ( engineering_parameters, study_detail, study, time_for_reuse, $
;                                                  list_of_lines, sequences ) >
;
; Inputs      : engineering_parameters  : STRUCTURE of type EIS_engineering_parameters.
;               study_detail            : STRUCTURE of type EIS_study_detail.
;               study                   : STRUCTURE ARRAY of type EIS_raster_detail or STRUCTURE of type EIS_engineering_detail or EIS_orl_detail.
;               time_for_reuse          : DOUBLE TAI time after which study can be overwritten.
;
; Opt. Inputs : None.
;
; Outputs     : result           : INT flag :
;                                   1 : processed science plan OK.
;                                   0 : processing failed.
;               list_of_lines    : INTARR() list of all linelist tables used.
;               sequences        : STRUCTURE ARRAY of type EIS_CPT_sequences
;
; 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: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 26/10/04
;
; Modified    : Version 0.0, 26/10/04, MKC
;               Version 0.1, 21/01/05, MKC
;                 Added sequence table output in ASCII text file format.
;               Version 0.2, 01/02/05, MKC
;                 Changed study_index argument to seq_start and seq_end.
;                 Removed obstbl and COMMON block.
;               Version 0.3, 24/03/05, MKC
;                 Renamed keywords.
;                 Added INTEL keyword.
;               Version 0.4, 02/04/05, MKC
;                 Added linelists argument to eis cpt ascii linelists for study.
;                 Added sequences and lines to argument list.
;                 Removed ASCII output.
;               Version 0.5, 10/05/05, MKC
;                 Renamed routine from eis cpt process science studies.
;                 Renamed routine eis cpt process sequences for study.
;               Version 0.6, 12/09/05, MKC
;                 Changed so that uses an intermediate structure array of sequences.
;                 Added keyword RESPONSE RASTER.
;                 Added time for reuse to argument list.
;               Version 0.7, 13/06/06, MKC
;                 Added error messages.
;                 Added response raster keyword.
;               Version 0.8, 09/05/07, MKC
;                 Changed to CASE statement to include ORL engineering studies.
;               Version 0.9, 31/01/08, MKC
;                 Changed study ID output to hex if error.
;                 Removed processing of ORL engineering studies.
;               Version 1.0, 23/03/10, MKC
;                 Added keyword ADJUST_Y.
;               Version 1.1, 12/05/13, MKC
;                 Changed keyword RESPONSE RASTER to RESPONSE RASTER ID.
;                 Changed order of argument list in eis cpt process science sequences.
;                 Added check on multiple sequences.
;
; Version     : 1.1, 12/05/13
;-
;**********************************************************

FUNCTION eis_cpt_process_study, engineering_parameters, study_detail, study, time_for_reuse, list_of_lines, sequences, $
                                RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y

  ; check study parameters

  IF NOT eis_cpt_check_study(study_detail, study, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y) THEN GOTO, error

  ; check whether science, engineering or ORL engineering study

  CASE study_detail.engineering OF

    0 : BEGIN

      ; process science study

      ; process line lists

      IF NOT eis_cpt_process_linelists_for_study(engineering_parameters, study, time_for_reuse, $
                                                 linelists, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y) THEN BEGIN

         eis_cpt_message, 'LINELIST ERROR, STUDY ID : '+STRING(FORMAT='(Z4.4)',study_detail.study_identifier)

       GOTO, error

      ENDIF

      ; add to list of lines used

      eis_cpt_add, list_of_lines, linelists

      ; remember size of sequences array

      initial_sequences = N_ELEMENTS(sequences)

      ; process sequences

      IF NOT eis_cpt_process_science_sequences(study, engineering_parameters, study_detail, time_for_reuse, linelists, sequences, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y) THEN BEGIN

        eis_cpt_message, 'SCIENCE STUDY ERROR, STUDY ID : '+STRING(FORMAT='(Z4.4)',study_detail.study_identifier)

        GOTO, error

        ; check number of sequence tables calculation

        IF eis_multiple_sequence_tables(study, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=adjust_y) XOR ((N_ELEMENTS(sequences)-initial_sequences) GT 1) THEN $
          MESSAGE, 'IMPLEMENTATION ERROR'

      ENDIF

    END

    1 : BEGIN

      ; process OBSTBL engineering study

      ; process line lists

      IF NOT eis_cpt_process_linelists_for_study(engineering_parameters, study, time_for_reuse, $
                                                 linelists, /ENGINEERING) THEN BEGIN

         eis_cpt_message, 'LINELIST ERROR, STUDY ID : '+STRING(FORMAT='(Z4.4)',study_detail.study_identifier)

         GOTO, error

      ENDIF

      ; add to list of lines used
      ; NB if linelists not defined then does not get added to lines

      eis_cpt_add, list_of_lines, linelists

      ; process sequences

      IF NOT eis_cpt_process_engineering_sequences(engineering_parameters, study_detail, study, time_for_reuse, $
                                                   linelists, sequences) THEN BEGIN

         eis_cpt_message, 'ENGINEERING STUDY ERROR, STUDY ID : '+STRING(FORMAT='(Z4.4)',study_detail.study_identifier)

         GOTO, error

      ENDIF

    END

    ELSE : MESSAGE, 'IMPLEMENTATION ERROR'

  ENDCASE

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





