;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS_STUDIES
;
; Purpose     : Processes studies in time line.
;
; Explanation : This routine processes the studies used in the science plan. producing the necessary entries into
;               the OBSTBL and ORL file.
;
;               Studies are either science or engineering studies.
;
;               Studies are processed individually and placed in the OBSTBL.
;               Each study is processed according to whether it is a science or engineering study.
;               The links between studies are resolved.
;               The time-tagged studies are added to the OP store.
;
; Use         : < result = eis_cpt_process_studies ( orl, engineering_parameters, study_details, control_details,$
;                                                    expiry_times, MSSL=mssl, INTEL=intel ) >
;
; Inputs      : orl                        : OBJECT of class structure EIS_CPT_orl
;               engineering_parameters     : STRCUTURE of type EIS_engineering_parameters.
;               study_details              : STRUCTURE ARRAY of type EIS_study_detail.
;               control_details            : STRUCTURE of type EIS_control_details.
;               expiry_times               : DBLARR[nstudies] TAI time study can be safely overwritten.
;
; Opt. Inputs : None.
;
; Outputs     : result          : INT flag :
;                                   1 : processed science plan OK.
;                                   0 : processing failed.
;
; Opt. Outputs: None.
;
; Keywords    : MSSL                : INT Flag indicating should produce individual ASCII files for each sequence table
;                                     and line list table in MSSL command mnemonic form.
;                                        0 : no output
;                                        1 : outputs new tables
;                                        2 : outputs all tables
;               INTEL               : INT Flag indicating should produce individual Intel hex files for each sequence table
;                                     and line list table.
;                                        0 : no output
;                                        1 : outputs new tables
;                                        2 : outputs all tables
;               STAMP               : STRING stamp indicating should stamp file names.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : Adapted from EIS CPT process.
;
; Written     : Martin Carter RAL 21/10/04
;
; Modified    : Version 0.0, 01/03/05
;               Version 0.1, 09/03/05, MKC
;                 Added time-tagged studies to OP store.
;               Version 0.2, 18/03/05, MKC
;                 Renamed eis get study details.
;                 Renamed argument.
;               Version 0.3, 24/03/05, MKC
;                 Used eis cpt command store.
;                 Allowed studies to be run from GSE.
;                 Renamed keywords.
;                 Added INTEL keyword.
;               Version 0.4, 03/04/05, MKC
;                 Added ACSII output of lines and sequences.
;               Version 0.5, 21/04/05, MKC
;                 Allowed EIS_CPT to be called without a time period defined.
;               Version 0.6, 27/04/05, MKC
;                 Removed ISACS keyword.
;               Version 0.7, 09/05/05, MKC
;                 Removed Bakeout study processing.
;                 Added in engineering file processing.
;                 Changed to use eis cpt process study.
;               Version 0.8, 24/06/05, MKC
;                 Added sequence_starts to the argument list.
;               Version 0.9, 27/06/05, MKC
;                 Changed argument list.
;                 Added eis cpt process control tables.
;               Version 1.0, 12/07/05, MKC
;                 Added engineering parameters to eis cpt check studies list arguments.
;                 Used eis cpt obstbl to output ASCII files.
;               Version 1.1, 19/09/05, MKC
;                 Added reuse of sequences.
;                 Added timeline processing.
;                 Added expiry times argument.
;               Version 1.2, 19/06/06, MKC
;                 Moved check on whether study details defined out of this routine.
;               Version 1.3, 08/09/06, MKC
;                 Added comments.
;               Version 1.4, 03/10/06, MKC
;                 Removed unnecessary ASCII file printout with /ALL.
;               Version 1.5, 10/01/07, MKC
;                 Added disallowed tag in check for reusable studies.
;               Version 1.6, 19/04/07, MKC
;                 Corrected bug in finding the response raster id.
;                 Added check.
;                 Used routine eis cpt call studies.
;               Version 1.7, 01/05/07, MKC
;                 Added orl argument.
;                 Added ORL engineering studies.
;               Version 1.8, 31/01/08, MKC
;                 Changed study ID output to hex if error.
;                 Corrected bug where ORL engineering study not included if repeated with same timeline ID.
;               Version 1.9, 12/06/08, MKC
;                 Incorporated pointing for response study.
;               Version 2.0, 23/03/10, MKC
;                 Used separate internal keyword for ADJUST_Y.
;               Version 2.1, 05/05/11, MKC
;                 Added STAMP keyword.
;               Version 2.2, 30/04/13, MKC
;                 Used new version of eis cpt response study.
;                 Removed eis cpt response raster.
;                 NB This corrected a bug in which studies following a response study
;                 allowing adjust y could have special window ystart command omitted and
;                 be prevented from re-using linelists.
;                 Changed keyword STUDY DATABASE to RESPONSE STUDY in eis get study.
;                 Changed keyword RESPONSE RASTER to RESPONSE RASTER ID in eis cpt process study.
;                 Changed argument list of eis cpt set response pointing.
;                 Used routine eis cpt get hunter info.
;               Version 2.3, 24/05/13, MKC
;                 No longer adjust response study pointing. Let planning tools set pointing.
;                 Removed eis cpt set response pointing.
;
; Version     : 2.3, 24/05/13
;-
;**********************************************************

FUNCTION eis_cpt_process_studies, orl, engineering_parameters, study_details, control_details, expiry_times, MSSL=mssl, INTEL=intel, STAMP=stamp

    ; check studies

    IF NOT eis_cpt_check_studies_list(study_details, engineering_parameters) THEN GOTO, error

    ; loop through list of studies extracting study details and updating obstbl and obstbl map
    ; studies are processed individually in isolation i.e does not assume any ordering

    FOR s = 0, N_ELEMENTS(study_details)-1 DO BEGIN

      ; check if ORL engineering study

      IF study_details[s].engineering EQ 2 THEN BEGIN

        ; get study details from timeline database
        ; this is a structure type EIS_ORL_DETAIL

        study = eis_get_study(study_details[s].study_identifier, ENGINEERING=study_details[s].engineering)

        ; process ORL engineering study

        IF NOT eis_cpt_process_orl_engineering_study(orl, study_details[s], study) THEN BEGIN

          eis_cpt_message, 'ORL ENGINEERING STUDY ERROR, STUDY ID : '+STRING(FORMAT='(Z4.4)',study_details[s].study_identifier)

          GOTO, error

         ENDIF

      ENDIF ELSE BEGIN

        ; OBSTBL study : science or engineering

        ; get list of studies identical with the present one

        list = eis_cpt_find_study(study_details, study_details[s])

        ; only process if this study is the latest of these

        IF study_details[s].start_time EQ MAX(study_details[list].start_time) THEN BEGIN

          ; check if study used as an event response study
          ; get response study ID for study database if study is a response study as indicated by its study identifier
          ; engineering studies cannot be used as response studies

          IF eis_cpt_response_study(control_details, study_details[s].study_identifier, $
                                    RESPONSE_STUDY_ID =response_study_id, $
                                    RESPONSE_RASTER_ID=response_raster_id, $
                                    ADJUST_Y=response_adjust_y) THEN BEGIN

            ; get hunter info

            IF NOT eis_cpt_get_hunter_info (study_details[s].study_identifier, $
                                            study_details, hunter_study_id) THEN BEGIN

              eis_cpt_message, 'ERROR, NO HUNTER STUDY FOR EVENT RESPONSE STUDY OF TYPE : '+eis_cpt_strtrim(study_details[s].study_identifier)

              GOTO, error

            ENDIF

            ; study is used as a response study
            ; get study details from study database
            ; this is a structure array of type EIS_RASTER_DETAIL

            study = eis_get_study(response_study_id, /RESPONSE_STUDY, HUNTER_STUDY_ID=hunter_study_id)

            ; no longer adjust response study pointing, let planning tools set response pointing

          ENDIF ELSE BEGIN

            ; study is not a response study
            ; get study details from timeline database
            ; this is a structure array of type either EIS_RASTER_DETAIL or EIS_ENGINEERING_DETAIL or EIS_ORL_DETAIL

            study = eis_get_study(study_details[s].study_identifier, ENGINEERING=study_details[s].engineering)

          ENDELSE

          ; process study
          ; sequences contains the sequence table data
          ; list of lines contain the table indexes used so far
          ; does science studies, engineering studies but not ORL engineering studies

          IF NOT eis_cpt_process_study(engineering_parameters, study_details[s], study, expiry_times[s], $
                                       list_of_lines, sequences, RESPONSE_RASTER_ID=response_raster_id, ADJUST_Y=response_adjust_y) THEN GOTO, error

        ENDIF

      ENDELSE

    ENDFOR

    ; link up sequences
    ; convert sequence identifiers into indexes in sequences array

    IF NOT eis_cpt_sequence_link(sequences) THEN GOTO, error

    ; check for sequence reuse
    ; check if can find exact match to sequences in OBSTBL

    IF NOT eis_cpt_sequence_match(sequences) THEN GOTO, error

    ; place sequences

    IF NOT eis_cpt_sequence_place(sequences) THEN GOTO, error

    ; resolve sequence links

    IF NOT eis_cpt_sequence_resolve_links(sequences) THEN GOTO, error

    ; add time tagged studies to orl

    IF NOT eis_cpt_call_studies ( orl, sequences, study_details ) THEN GOTO, error

    ; process control tables

    IF NOT eis_cpt_process_control_tables ( control_details, study_details, sequences, engineering_parameters ) THEN GOTO, error

    ; check any sequences produced

    IF N_ELEMENTS(sequences) GT 0 THEN BEGIN

      ; write out obstbl tables used to individual files
      ; NB logical comparisons are 0 or 1

      IF NOT eis_cpt_obstbl( MSSL=mssl, INTEL=intel, STAMP=stamp, SEQUENCE=sequences.index, LINELIST=list_of_lines, $
                             XRT_FLARE=TOTAL(study_details.XRT_flare_control_flag) GT 0, $
                             EIS_FLARE=TOTAL(study_details.EIS_flare_control_flag) GT 0, $
                             EIS_EVENT=TOTAL(study_details.EIS_event_control_flag) GT 0, $
                             AEC_TABLE=TOTAL(study_details.AEC_control_flag) GT 0, $
                             AEC_EXPOSURE=TOTAL(study_details.AEC_control_flag) GT 0 ) THEN GOTO, error

    ENDIF

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





