;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT
;
; Purpose     : Generate uploadable files for science plan.
;
; Explanation : If the EIS_CPT processing fails then a simple error message will be returned in !ERR_STRING.
;
; Use         : < result = eis_cpt( ... ) >
;
; Inputs      : None.
;
; Opt. Inputs : None.
;
; Outputs     : result   : INT flag :
;                            1 : processed science plan OK.
;                            0 : processing failed.
;
; Opt. Outputs: None.
;
; Keywords    : RESET                 : INT FLAG indicating that OBSTBL and OBSTBL map should be reset,
;                                         1 : soft reset (only OBSTBL map)
;                                         2 : hard reset (OBSTBL also)
;                                         3 : control table reset (Only event control tables in OBSTBL map)
;               QUIET                 : INT FLAG indicating that no printout to the terminal window should be produced.
;               MSSL                  : INT Flag indicating should produce individual ASCII files for each OBSTBL table
;                                        0 : no output
;                                        1 : outputs new tables
;                                        2 : outputs all tables
;                                       and line list table in MSSL command mnemonic form.
;               INTEL                 : INT Flag indicating should produce individual Intel hex files for each OBSTBL table
;                                       and line list table.
;                                        0 : no output
;                                        1 : outputs new tables
;                                        2 : outputs all tables
;               JSIDE                 : INT flag controlling Jside files produced.
;                                        0 : no output
;                                        1 : outputs ORL, DR and OBSTBL files (also internal OBSTBL and MAP).
;                                        2 : outputs DR file and test ORL file.
;               STAMP                 : STRING stamp appended to Jside files produced for test purposes.
;               CONTROL               : STRUCTURE of type EIS_control_details.
;               ENGINEERING           : STRUCTURE of type EIS_engineering_parameters
;               TIME_PERIOD           : ARRAY[2] time of start and end of period for processing in
;                                       any CDS time format.
;               UPLOAD                : STRUCTURE of type EIS_upload.
;               STUDIES               : STRUCTURE ARRAY of type EIS_study_detail.
;               SUSPENSIONS           : STRUCTURE ARRAY of type EIS_suspend_period.
;               ABORTS                : STRUCTURE ARRAY of type EIS_abort.
;               BAKEOUTS              : STRUCTURE ARRAY of type EIS_bakeout_period.
;               CONTACT_INFO          : STRUCTURE containing contact period info.
;               MIRROR_COMMANDS       : STRUCTURE ARRAY of type EIS_coarse_mirror_command.
;               WIDGET                : LONG widget ID for text output.
;               REVISED_PLAN          : INT flag indicating plan for following period is to be revised.
;                                       Previous plan may or may not have been uploaded.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: Returns to calling routine if error.
;               Resets !ERROR_STATE system variable.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 06/10/04
;
; Modified    : Version 0.0, 06/10/04, MKC
;               Version 0.1, 21/01/05, MKC
;                 Added coarse_mirror_command processing.
;               Version 0.2, 01/03/05, MKC
;                 Added control_details.
;               Version 0.3, 18/03/05, MKC
;                 Renamed internal variables.
;                 Renamed keywords.
;                 Added INTEL and JSIDE keywords.
;               Version 0.4, 03/04/05, MKC
;                 Added TIMESTAMP keyword.
;               Version 0.5, 22/04/05, MKC
;                 Added extra options to ISACS, MSSL, INTEL flags.
;               Version 0.6, 27/04/05, MKC
;                 Removed ISACS keyword.
;               Version 0.7, 09/05/05, MKC
;                 Modified RESET behaviour.
;                 Added bakeout periods.
;               Version 0.8, 20/07/05, MKC
;                 Added real command sequences.
;               Version 0.9, 13/09/05, MKC
;                 Added EIS OBSTBL RAM sub ID keyword.
;                 Added UPLOAD_DATE and UPLOAD_PASS_ID keywords.
;                 Changed TIMESTAMP keyword to STAMP.
;               Version 1.0, 22/09/05, MKC
;                 Added ground contact periods.
;                 Add Data Rate file generation.
;                 Added time period argument to eis cpt process.
;               Version 1.1, 03/10/05, MKC
;                 Added checks on upload and changed to UPLOAD_TIME keyword.
;               Version 1.2, 07/10/05, MKC
;                 Removed JSIDE keyword from eis cpt complete.
;               Version 1.3, 14/06/06, MKC
;                 Changed MK PLAN interface.
;                 Passed interface structures as arguments rather than getting them
;                 through function calls.
;                 Corrected bug where error state variable not reset until after some errors generated.
;                 Moved setup constants to eis cpt preliminaries.
;                 Changed so that requires upload defined even if JSIDE not selected.
;               Version 1.4, 14/07/06, MKC
;                 Moved check on time period into routine.
;               Version 1.5, 20/08/06, MKC
;                 Changed logic so that only require upload to be defined if Jside output required.
;                 Moved calculation of upload pass string.
;                 Changed arguments of eis complete and eis cpt check upload.
;                 Took internal copy of upload.
;               Version 1.6, 03/10/06, MKC
;                 Changed eis cpt complete arguments.
;                 Changed contact periods structure.
;                 Added ABORTS keyword.
;               Version 1.7, 13/01/07, MKC
;                 Added WIDGET keyword.
;               Version 1.8, 08/02/07, MKC
;                 Extended use of JSIDE keyword.
;               Version 1.9, 01/05/07, MKC
;                 Replaced eis cpt command store with orl object.
;               Version 2.0, 08/05/07, MKC
;                 Removed REAL COMMANDS keyword.
;                 Added eis cpt check contacts.
;               Version 2.1, 26/02/08, MKC
;                 Replace stamp with OBSTBL and ORL stamps in eis cpt complete.
;               Version 2.2, 05/05/11, MKC
;                 Added stamp keyword to eis pct process.
;                 Added REVISED PLAN keyword.
;               Version 2.3, 08/06/11, MKC
;                 Passed REVISED PLAN keyword to eis cpt check upload.
;               Version 2.4, 06/10/11, MKC
;                 Added Control Table Reset option.
;
; Version     : 2.4, 06/10/11
;-
;**********************************************************

FUNCTION eis_cpt, ENGINEERING     = engineering_parameters, $
                  CONTROL         = control_details, $
                  TIME_PERIOD     = time_period, $
                  UPLOAD          = upload, $
                  STUDIES         = study_details, $
                  SUSPENSIONS     = suspension_periods, $
                  ABORTS          = abort_times, $
                  BAKEOUTS        = bakeout_periods,  $
                  CONTACT_INFO    = contact_info, $
                  MIRROR_COMMANDS = coarse_mirror_commands, $
                  RESET           = reset, $
                  QUIET           = quiet, $
                  JSIDE           = jside, $
                  MSSL            = mssl, $
                  INTEL           = intel, $
                  STAMP           = stamp, $
                  WIDGET          = widget, $
                  REVISED_PLAN    = revised_plan

  ; set up widget ID

  IF N_ELEMENTS(widget) EQ 0 THEN widget = 0L

  dummy = eis_cpt_widget(SET=widget)

  ; output version info

  eis_cpt_message, 'Running EIS_CPT version : ' + eis_cpt_version()

  ; Return to caller of this routine if error (provided not debugging)

  IF eis_cpt_debug() THEN ON_ERROR, 0 ELSE ON_ERROR, 2

  ; set up local variable for time period

  IF N_ELEMENTS(time_period) GT 0 THEN period = time_period

  ; check time_period
  ; NB period changed to TAI

  IF NOT eis_cpt_check_time_period(study_details, suspension_periods, abort_times, bakeout_periods, coarse_mirror_commands, period) THEN GOTO, error

  ; do preliminaries

  IF NOT eis_cpt_preliminaries ( orl, RESET=reset, QUIET=quiet ) THEN GOTO, error

  ; if defined, get local copy of upload
  ; NB upload will be modified in eis cpt process, do not want to propagate out

  IF N_ELEMENTS(upload) NE 0 THEN upload_local = upload

  ; check upload contact OK (only if jside output required)

  IF KEYWORD_SET(jside) THEN IF NOT eis_cpt_check_upload( period, upload_local, REVISED_PLAN=revised_plan) THEN GOTO, error

  ; process time line
  ; check structures
  ; only output is MSSL + INTEL files

  IF NOT eis_cpt_process ( orl, engineering_parameters, control_details, study_details, $
                           suspension_periods, abort_times, bakeout_periods, contact_info, coarse_mirror_commands, $
                           period, upload_local, MSSL=mssl, INTEL=intel, STAMP=stamp, REVISED_PLAN=revised_plan ) THEN GOTO, error

  ; check contact periods and ORL files are consistent

  IF NOT eis_cpt_check_contacts ( orl, contact_info) THEN GOTO, error

  ; finish up
  ; check if Jside files wanted
  ; output JSIDE files + internal EIS_CPT structures

  IF KEYWORD_SET(jside) THEN BEGIN

    IF KEYWORD_SET(stamp) THEN BEGIN

      ; set up OBSTBL and ORL stamps

      obstbl_stamp = stamp

      orl_stamp = stamp

    ENDIF ELSE BEGIN

      ; check upload defined

      IF N_ELEMENTS(upload) EQ 0 THEN MESSAGE, 'IMPLEMENTATION ERROR'

      ; set up stamp

      obstbl_stamp = upload.obstbl_upload_id

      orl_stamp = upload.orl_upload_id

    ENDELSE

    IF NOT eis_cpt_complete (orl, obstbl_stamp, orl_stamp, jside) THEN GOTO, error

  ENDIF

  ; clean up objects

  OBJ_DESTROY, orl

  ; return finished OK flag

  RETURN, 1

error :

  ; clean up objects

  IF KEYWORD_SET(orl) THEN OBJ_DESTROY, orl

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





