;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS_BAKEOUT_PERIODS
;
; Purpose     : Processes bakeout_periods.
;
; Explanation : Processes bakeout_periods.
;
; Use         : < result = eis_cpt_process_bakeout_periods ( orl, bakeout_periods, engineering_parameters ) >
;
; Inputs      : orl                     : OBJECT of class structure EIS_CPT_orl
;               bakeout_periods         : STRUCTURE ARRAY of type EIS_bakeout_period.
;               engineering_parameters  : STRUCTURE of type EIS_engineering_parameters.
;
; Opt. Inputs : None.
;
; Outputs     : result   : INT flag :
;                            1 : processed science plan OK.
;                            0 : processing failed.
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 09/05/05
;
; Modified    : Version 0.1, 12/09/05, MKC
;                 Changed bakeout procedure.
;                 Added engineering parameters argument.
;               Version 0.2, 19/06/06, MKC
;                 Moved check on whether stduy details defined out of this routine.
;                 Moved printout of ALL OBSTBLs out of this routine.
;               Version 0.3, 26/09/06, MKC
;                 Changed so that all commanding may be performed from GSE.
;               Version 0.4, 01/05/07, MKC
;                 Added orl argument.
;                 Replaced eis cpt command store with orl object.
;
; Version     : 0.4, 01/05/07
;-
;**********************************************************

FUNCTION eis_cpt_process_bakeout_periods, orl, bakeout_periods, engineering_parameters

    ; loop through bakeout periods

    FOR k = 0, N_ELEMENTS(bakeout_periods)-1 DO BEGIN

      ; add bakeout start command to command store

      IF NOT orl->add ( bakeout_periods[k].onboard, bakeout_periods[k].start_time, 'BAKEOUT_START', $
        engineering_parameters.bakeout_parameters.target_temperature, dummy, bakeout_periods[k].description) THEN GOTO, error

      ; allow for period of grace to switch off heaters

      itime = bakeout_periods[k].start_time + 30

      ; add increment bakeout duty cycle commands to command store every 5 minutes

      FOR i = 0, engineering_parameters.bakeout_parameters.number_of_duty_cycle_increments-1 DO BEGIN

        itime = itime + 300 + engineering_parameters.bakeout_parameters.extra_time_between_increments

        IF NOT orl->add ( bakeout_periods[k].onboard, itime, 'BAKEOUT_INCREMENT', dummy, dummy, 'Increment bakeout duty cycle') THEN GOTO, error

      ENDFOR

      ; calculate time to start decrementing bakeout duty cycle

      dtime = bakeout_periods[k].end_time - (1+engineering_parameters.bakeout_parameters.number_of_duty_cycle_increments)* $
        (300+engineering_parameters.bakeout_parameters.extra_time_between_decrements)

      ; check there is no overlap

      IF dtime LE itime THEN BEGIN

        eis_cpt_message, 'INVALID BAKEOUT CYCLE'

        GOTO, error

      ENDIF

      ; add decrement bakeout duty cycle commands to command store every 5 minutes
      ; NB one extra command to bring duty cycle to 0%

      FOR i = 0, engineering_parameters.bakeout_parameters.number_of_duty_cycle_increments DO BEGIN

        IF NOT orl->add ( bakeout_periods[k].onboard, dtime, 'BAKEOUT_DECREMENT', dummy, dummy, 'Decrement bakeout duty cycle') THEN GOTO, error

        dtime = dtime + 300 + engineering_parameters.bakeout_parameters.extra_time_between_decrements

      ENDFOR

      ; add bakeout end command to command store

      IF NOT orl->add ( bakeout_periods[k].onboard, bakeout_periods[k].end_time, 'BAKEOUT_END', dummy, dummy, 'End bakeout period') THEN GOTO, error

    ENDFOR

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





