;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PARSE_ORL_DEFSEQ
;
; Purpose     : Parses ORL defseq command.
;
; Explanation : Parses ORL defseq command.
;
; Use         : < result = eis_cpt_parse_orl_defseq ( orl, study_detail, duration, decription, line ) >
;
; Inputs      : orl                     : OBJECT of class structure EIS_CPT_orl
;               study_detail            : STRUCTURE of type EIS_study_detail.
;               duration                : DOUBLE study duration in seconds.
;               description             : STRING ORL engineering study description
;               line                    : STRING text from ORL file.
;
; Opt. Inputs : None.
;
; Outputs     : result   : INT flag :
;                            1 : processed science plan OK.
;                            0 : processing failed.
;               line     : STRING unparsed text from ORL file.
;
; 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 28/02/08
;                 Adapted from eis cpt parse orl line.
;
; Modified    : Version 0.0, 28/02/08, MKC
;
; Version     : 0.0, 28/02/08
;-
;**********************************************************

FUNCTION eis_cpt_parse_orl_defseq, orl, study_detail, duration, description, line

  ; check if line is a complete OP/REAL/OG sequence
  ; DEFSEQ(REAL):test2_orl_command_sequence.orl_0010_0010_REAL {CMD:EIS_MODE_AUTO;}
  ; DEFSEQ ( 1 ) : 2{ 3}4

  text = STREGEX(line,'^ *DEFSEQ *\( *(OP|REAL|OG) *\) *: *([^{]+){ *([^}]+)}(.*)',/EXTRACT,/FOLD_CASE,/SUBEXPR)

  IF text[0] NE '' THEN BEGIN

    ; extract fields

    ; get sequence type

    type = STRUPCASE(text[1])

    ; remove trailing blanks, convert to upper case

    seq_name = STRUPCASE(STRTRIM(text[2]))

    IF NOT eis_cpt_parse_orl_seqname(seq_name) THEN BEGIN

      eis_cpt_message, 'ERROR, INVALID SEQUENCE NAME :'+seq_name, /INFORMATIONAL

      GOTO, error

    ENDIF

    ; get sequence text

    sequence_text = STRTRIM(text[3])

    ; throw away comments

    IF NOT eis_cpt_parse_orl_comments(sequence_text, /REMOVE, /CHECK) THEN GOTO, error

    ; parse sequence text

    IF NOT eis_cpt_parse_orl_sequence(type, sequence_text, COMMANDS=commands, MANUAL=manual) THEN GOTO, error

    ; check if manual command needed

    IF KEYWORD_SET(manual) THEN IF NOT orl->manual(/OG) THEN GOTO, error

    ; get remainder

    line = STRTRIM(text[4])

    ; add to appropriate store

    CASE type OF

      'OP' : IF NOT orl->seq_add( study_detail.start_time, seq_name, description, commands, /OP) THEN GOTO, error

      'REAL' : IF NOT orl->seq_add( study_detail.start_time, seq_name, description, commands, /REAL) THEN GOTO, error

      'OG' : IF NOT orl->seq_add( study_detail.start_time, seq_name, description, commands, /OG) THEN GOTO, error

    ENDCASE

  ENDIF

  ; return OK

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





