;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_CHECK_TIME_PERIOD
;
; Purpose     : Checks time_period argument for eis_cpt.
;
; Explanation : Checks argument OK.
;
; Use         : < tai_time_period = eis_cpt_check_time_period( study_details, suspension_periods, $
;                                                              abort_times, bakeout_periods, $
;                                                              coarse_mirror_commands, $
;                                                              time_period ) >
;
; Inputs      : 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.
;               coarse_mirror_commands     : STRUCTURE ARRAY of type EIS_coarse_mirror_command.
;               time_period                : ARRAY[2] time of start and end of period for processing in
;                                            any CDS time format.
;                                            Changed to TAI on exit.
;
; Opt. Inputs : None.
;
; Outputs     : time_period                : DOUBLE[2] Time period in TAI format.
;                                            UNDEFINED if error.
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: 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/04/05, MKC
;                 Allowed EIS_CPT to be called without a time period defined.
;               Version 0.2, 27/09/05, MKC
;                 Changed so that alters time period.
;               Version 0.3, 14/07/06, MKC
;                 Added further arguments and checks.
;               Version 0.4, 05/10/06, MKC
;                 Added abort_times argument.
;               Version 0.5, 08/05/07, MKC
;                 Removed REAL command sequences.
;
; Version     : 0.5, 08/05/07
;-
;**********************************************************

FUNCTION eis_cpt_check_time_period, study_details, suspension_periods, abort_times, bakeout_periods, $
                                    coarse_mirror_commands, time_period

  ; check if time period for processing is undefined

  IF NOT KEYWORD_SET(time_period) THEN BEGIN

    eis_cpt_message, 'WARNING, NO TIME_PERIOD DEFINED'

    ; Nb this is not a fatal error : allow processing even if no time period defined

    RETURN, 1

  ENDIF

  ; check input is ARRAY[2]

  IF (SIZE(time_period))[1] NE 2 THEN BEGIN

    eis_cpt_message, 'TIME_PERIOD ARGUMENT WRONG DIMENSIONS'

    GOTO, error

  ENDIF

  ; convert time period argument to TAI

  time_period = anytim2tai(time_period)

  ; check sensible time period

  IF time_period[1] LE time_period[0] THEN BEGIN

    eis_cpt_message, 'TIME_PERIOD ARGUMENT NOT VALID'

    GOTO, error

  ENDIF

  ; check if any studies outside of time period
  ; only check if time tagged
  ; daisy-chained studies are allowed to start outside of time period
  ; event response studies are allowed to start outside of time period

  IF KEYWORD_SET(study_details) THEN BEGIN

    list = WHERE(study_details.time_tagged AND $
      (study_details.start_time LT time_period[0] OR study_details.start_time GT time_period[1]), count)

    IF count GT 0 THEN BEGIN

      eis_cpt_message, 'STUDY OUTSIDE TIME PERIOD, ID : ' + $
        eis_cpt_bytestohex(eis_cpt_ulongtobytes(study_details[list[0]].study_identifier),/OX)

      GOTO, error

    ENDIF

  ENDIF

  ; check if any suspension_periods outside of time period

  IF KEYWORD_SET(suspension_periods) THEN BEGIN

    list = WHERE(suspension_periods.pause_time LT time_period[0] OR suspension_periods.pause_time GT time_period[1], count)

    IF count GT 0 THEN BEGIN

      eis_cpt_message, 'SUSPENSION PERIOD OUTSIDE TIME PERIOD, DESCRIPTION : ' + $
                       suspension_periods[list[0]].description

      GOTO, error

    ENDIF

  ENDIF

  ; check if any abort_times outside of time period

  IF KEYWORD_SET(abort_times) THEN BEGIN

    list = WHERE(abort_times.abort_time LT time_period[0] OR abort_times.abort_time GT time_period[1], count)

    IF count GT 0 THEN BEGIN

      eis_cpt_message, 'ABORT TIME OUTSIDE TIME PERIOD, DESCRIPTION : ' + $
                       abort_times[list[0]].description

      GOTO, error

    ENDIF

  ENDIF

  ; check if any bakeout_periods outside of time period

  IF KEYWORD_SET(bakeout_periods) THEN BEGIN

    list = WHERE(bakeout_periods.start_time LT time_period[0] OR bakeout_periods.start_time GT time_period[1], count)

    IF count GT 0 THEN BEGIN

      eis_cpt_message, 'BAKEOUT PERIOD OUTSIDE TIME PERIOD, DESCRIPTION : ' + $
                       bakeout_periods[list[0]].description

      GOTO, error

    ENDIF

  ENDIF

  ; check if any coarse_mirror_commands outside of time period

  IF KEYWORD_SET(coarse_mirror_commands) THEN BEGIN

    list = WHERE(coarse_mirror_commands.time LT time_period[0] OR coarse_mirror_commands.time GT time_period[1], count)

    IF count GT 0 THEN BEGIN

      eis_cpt_message, 'COARSE MIRROR COMMAND OUTSIDE TIME PERIOD, DESCRIPTION : ' + $
                       coarse_mirror_commands[list[0]].description

      GOTO, error

    ENDIF

  ENDIF

  ; return OK

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  RETURN, 0

END





