;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS
;
; Purpose     : Processes studies and other time line objects needed for current plan..
;
; Explanation : This routine processes the science plan timeline. producing the necessary entries into
;               the OBSTBL and ORL files.
;
;               Items in the timeline consist of : science studies, engineering studies, orl command sequences,
;               suspension periods, bakeout periods and coarse mirror movements.
;               Studies are either science or engineering studies.
;
; Use         : < result = eis_cpt_process ( orl, engineering_parameters, control_details, study_details, $
;                                            suspension_periods, abort_times, bakeout_periods, contact_info, coarse_mirror_commands, $
;                                            orl_command_sequences, time_period, upload, $
;                                            MSSL=mssl, INTEL=intel ) >
;
; Inputs      : orl                        : OBJECT of class structure EIS_CPT_orl
;               engineering_parameters     : STRUCTURE of type EIS_engineering_parameters.
;               control_details            : STRUCTURE of type EIS_control_details.
;               study_details              : STRUCTURE ARRAY of type EIS_study_detail.
;               suspension_periods         : STRUCTURE ARRAY of type EIS_suspend_period.
;               abort_Times                : STRUCTURE ARRAY of type EIS_abort.
;               bakeout_periods            : STRUCTURE ARRAY of type EIS_bakeout_period.
;               contact_info               : STRUCTURE containing contact period info.
;               coarse_mirror_commands     : STRUCTURE ARRAY of type EIS_coarse_mirror_command.
;               orl_command_sequences      : STRUCTURE ARRAY of type EIS_list_orl_detail
;               time_period                : DBLARR[2] TAI time to start and finish timeline.
;               upload                     : STRUCTURE of type EIS_upload.
;
; Opt. Inputs : None.
;
; Outputs     : result   : INT flag :
;                            1 : processed science plan OK.
;                            0 : processing failed.
;               upload                     : Gets defined if not already defined
;
; 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.
;               REVISED_PLAN        : INT flag indicating plan has been revised before previous plan uploaded
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 21/10/04
;
; Modified    : Version 0.1, 25/11/04, MKC
;                 Added rearrange studies list.
;               Version 0.2, 20/01/05, MKC
;                 Changed order of processing studies.
;               Version 0.3, 21/01/05, MKC
;                 Added coarse_mirror_command_list to arguments.
;                 Moved to processing study links after rocessing  studies.
;                 Removed eis cpt arrange studies list.
;                 Added eis cpt resolve study links.
;                 Moved eis cpt refresh obstbl.
;               Version 0.4, 08/02/05, MKC
;                 Removed re-usable study code.
;               Version 0.5, 22/02/05, MKC
;                 Added AEC control.
;               Version 0.6, 01/03/05, MKC
;                 Added ENGINEERING keyword to EIS get study details.
;                 Added EIS CPT process studies.
;                 Added control_details.
;               Version 0.7, 18/03/05, MKC
;                 Renamed arguments
;                 Renamed keywords.
;                 Added INTEL keyword.
;               Version 0.8, 13/04/05, MKC
;                 Added check of engineering parameters.
;               Version 0.9, 21/04/05, MKC
;                 Allowed EIS_CPT to be called without a time period defined.
;               Version 1.0, 27/04/05, MKC
;                 Removed ISACS keyword.
;               Version 1.1, 09/05/05, MKC
;                 Added process bakeout periods.
;               Version 1.2, 24/06/05, MKC
;                 Added sequence_starts to the argument list of eis cpt process studies.
;               Version 1.3, 27/06/05, MKC
;                 Moved eis cpt [process control tables..
;               Version 1.4, 19/07/05, MKC
;                 Added real command sequences argument.
;               Version 1.5, 12/09/05, MKC
;                 Added engineering parameters to process bakeout routine.
;                 Added EIS OBSTBL RAM sub ID argument.
;                 Added contact periods argument.
;                 Added time_period argument.
;               Version 1.6, 03/10/05, MKC
;                 Added upload time and pass id.
;               Version 1.7, 07/10/05, MKC
;                 Moved command store check.
;               Version 1.8, 14/06/06, MKC
;                 Used EIS_upload structure.
;               Version 1.9, 19/06/06, MKC
;                 Moved check on whether relevant details defined into this routine.
;               Version 2.0, 20/08/06, MKC
;                 Added engineering parameters to eis cpt data rate arguments.
;               Version 2.1, 03/10/06, MKC
;                 Changed contact periods structure.
;                 Added abort_times argument.
;                 Added string argument to eis cpt command store.
;               Version 2.2, 14/01/07, MKC
;                 Removed engineering parameters from eis cpt data rate.
;               Version 2.3, 01/05/07, MKC
;                 Added orl argument.
;                 Replaced eis cpt command store with orl object.
;               Version 2.4, 08/05/07, MKC
;                 Removed REAL COMMAND sequences.
;               Version 2.5, 27/02/08, MKC
;                 Added new EIS TBL upload procedure.
;               Version 2.6, 05/05/11, MKC
;                 Added STAMP keyword.
;                 Added REVISED PLAN keyword. Used time=-2 for BLOCK upload.
;
; Version     : 2.6, 05/05/11
;-
;**********************************************************

FUNCTION eis_cpt_process, orl, engineering_parameters, control_details, study_details, suspension_periods, abort_times, $
                          bakeout_periods, contact_info, coarse_mirror_commands, time_period, upload, $
                          MSSL=mssl, INTEL=intel, STAMP=stamp, REVISED_PLAN=revised_plan

  ; update obstbl
  ; checks whether any of the tables in OBSTBL have expired and updates the OBSTBL map accordingly.
  ; if upload not defined then comes back with a default

  IF NOT eis_cpt_obstbl ( /REFRESH, UPLOAD=upload) THEN GOTO, error

  ; check engineering parameters if any studies

  IF KEYWORD_SET(study_details) OR KEYWORD_SET(bakeout_periods) OR KEYWORD_SET(coarse_mirror_commands) THEN $
    IF NOT eis_cpt_check_engineering_parameters ( engineering_parameters) THEN GOTO, error

  ; check control parameters

  IF KEYWORD_SET(study_details) THEN $
    IF NOT eis_cpt_check_control_parameters ( control_details, study_details) THEN GOTO, error

  ; process timeline

  IF KEYWORD_SET(study_details) THEN $
    IF NOT eis_cpt_process_timeline( engineering_parameters, study_details, control_details, time_period, timeline, expiry_times ) THEN GOTO, error

  ; process studies used in current plan

  IF KEYWORD_SET(study_details) THEN $
    IF NOT eis_cpt_process_studies ( orl, engineering_parameters, study_details, control_details, expiry_times, MSSL=mssl, INTEL=intel, STAMP=stamp) THEN GOTO, error

  ; process suspension periods

  IF KEYWORD_SET(suspension_periods) THEN $
    IF NOT eis_cpt_process_suspension_periods( orl, suspension_periods) THEN GOTO, error

  ; process abort_times

  IF KEYWORD_SET(abort_times) THEN $
    IF NOT eis_cpt_process_abort_times( orl, abort_times) THEN GOTO, error

  ; process bakeout periods

  IF KEYWORD_SET(bakeout_periods) THEN $
    IF NOT eis_cpt_process_bakeout_periods ( orl, bakeout_periods, engineering_parameters) THEN GOTO, error

  ; process contact periods

  IF NOT eis_cpt_data_rate ( study_details, contact_info, timeline, time_period, /PROCESS) THEN GOTO, error

  ; process coarse mirror commands

  IF KEYWORD_SET(coarse_mirror_commands) THEN $
    IF NOT eis_cpt_process_coarse_mirror_commands ( orl, coarse_mirror_commands, engineering_parameters) THEN GOTO, error

  ; add block mode change and load OBSTBL commands to command store
  ; Check if revised plan
  ; NB if no JSide output required then upload time may be 0
  ; NB time is not checked if POINT is set. However, time is still used for order and must be unique.
  ; NB this is the only place where POINT commands are added

  IF NOT KEYWORD_SET(revised_plan) THEN BEGIN

    IF NOT orl->add( 0, -1, 'BLOCK', '', '', 'Stop EIS observation and temporarily disable EIS mode changes', POINT='OPOG_UPLOAD_TI ~+ 240') THEN GOTO, error

    IF NOT orl->add( 0, upload.time, 'LOAD', upload.RAM_sub_id, upload.version, 'Load OBSTBL, dump OBSTBL, enable EIS mode changes') THEN GOTO, error

  ENDIF ELSE BEGIN

    IF NOT orl->add( 0, -2, 'BLOCK', '', '', 'Stop EIS observation and temporarily disable EIS mode changes', POINT='OPOG_UPLOAD_TI_retry') THEN GOTO, error

    IF NOT orl->add( 0, -1, 'LOAD', upload.RAM_sub_id, upload.version, 'Load OBSTBL, dump OBSTBL, enable EIS mode changes', POINT='OPOG_UPLOAD') THEN GOTO, error

  ENDELSE

  ; check command store and arrange commands in time order

  IF NOT orl->check() THEN GOTO, error

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





