;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_OBSTBL_ASCII
;
; Purpose     : Writes out OBSTBL tables to individual ASCII files.
;
; Explanation : This routine outputs OBSTBL tables either as Intel hex files or MSSL command mnemonic files.
;
; Use         : < result = eis_cpt_obstbl_ascii ( MSSL=mssl, INTEL=intel, $
;                         SEQUENCE=sequence, LINELIST=linelist, $
;                         AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
;                         XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, $
;                         EIS_EVENT=eis_event  ) >
;
; Inputs      : None.
;
; Opt. Inputs : None.
;
; Outputs     : result          : INT flag :
;                                   1 : OK.
;                                   0 : failed.
;
; 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.
;               SEQUENCE          : INTARR list of sequence tables to output.
;               LINELIST          : INTARR list of linelist tables to output.
;               AEC_EXPOSURE      : FLAG indicating that processing an AEC exposure table
;               AEC_TABLE         : FLAG indicating that processing an AEC control table
;               XRT_FLARE         : FLAG indicating that processing an XRT flare trigger  control table
;               EIS_FLARE         : FLAG indicating that processing an EIS flare trigger  control table
;               EIS_EVENT         : FLAG indicating that processing an EIS event trigger  control table
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : Adapted from EIS CPT process.
;
; Written     : Martin Carter RAL 13/07/05
;
; Modified    : Version 0.0, 13/07/05, MKC
;               Version 0.1, 28/09/05, MKC
;                 Corrected bug if mssl or intel undefined.
;                 Changed logic slightly.
;                 Removed ALL keyword.
;               Version 0.2, 13/01/07, MKC
;                 Changed print out to use eis cpt message.
;               Version 0.3, 20/09/07, JAR
;                 Changed some of the output formatting.
;               Version 0.4, 07/07/08, MKC
;                 Changed printout if directory not defined.
;               Version 0.5, 05/05/11, MKC
;                 Added STAMP keyword.
;
; Version     : 0.5, 05/05/11
;-
;**********************************************************

FUNCTION eis_cpt_obstbl_ascii, MSSL=mssl, INTEL=intel, STAMP=stamp, $
                         SEQUENCE=sequence, LINELIST=linelist, $
                         AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                         XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, $
                         EIS_EVENT=eis_event

  ; printout where output is going

  IF KEYWORD_SET(mssl)  THEN BEGIN

    ; get directory

    directory = eis_cpt_environment(/MSSL)

    IF KEYWORD_SET(directory) THEN eis_cpt_message, 'MSSL output files       : ' + directory, /INFORMATIONAL

  ENDIF

  IF KEYWORD_SET(intel) THEN BEGIN

    ; get directory

    directory = eis_cpt_environment(/INTEL)

    IF KEYWORD_SET(directory) THEN    eis_cpt_message, 'INTEL output files      : ' + directory, /INFORMATIONAL

  ENDIF

  ; ensure mssl, intel defined

  IF N_ELEMENTS(mssl) EQ 0 THEN mssl = 0

  IF N_ELEMENTS(intel) EQ 0 THEN intel = 0

  ; only write out all tables if flag is 2

  IF mssl EQ 2 THEN mssl_flag = 1

  IF intel EQ 2 THEN intel_flag = 1

  ; check if all table output required

  IF KEYWORD_SET(mssl_flag) OR KEYWORD_SET(intel_flag) THEN BEGIN

    ; write out all tables to text files

    IF NOT eis_cpt_ascii_tables(INDGEN(!EIS_CPT_CONSTANTS.nll), MSSL=mssl_flag, INTEL=intel_flag, /LINELIST, STAMP=stamp) THEN GOTO, error

    IF NOT eis_cpt_ascii_tables(INDGEN(!EIS_CPT_CONSTANTS.nsq), MSSL=mssl_flag, INTEL=intel_flag, /SEQUENCE, STAMP=stamp) THEN GOTO, error

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /XRT_FLARE, STAMP=stamp) THEN GOTO, error

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /EIS_FLARE, STAMP=stamp) THEN GOTO, error

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /EIS_EVENT, STAMP=stamp) THEN GOTO, error

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /AEC_TABLE, STAMP=stamp) THEN GOTO, error

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /AEC_EXPOSURE, STAMP=stamp) THEN GOTO, error

  ENDIF

  ; only write out specified tables if flag is 1

  IF mssl EQ 1 AND NOT KEYWORD_SET(mssl_flag) THEN mssl_flag = 1

  IF intel EQ 1 AND NOT KEYWORD_SET(intel_flag) THEN intel_flag = 1

  ; write out linelists used to text files

  IF N_ELEMENTS(linelist) NE 0 THEN BEGIN

    IF NOT eis_cpt_ascii_tables(linelist, MSSL=mssl_flag, INTEL=intel_flag, /LINELIST, STAMP=stamp) THEN GOTO, error

  ENDIF

  ; write out sequences used to text files

  IF N_ELEMENTS(sequence) NE 0 THEN BEGIN

    IF NOT eis_cpt_ascii_tables(sequence, MSSL=mssl_flag, INTEL=intel_flag, /SEQUENCE, STAMP=stamp) THEN GOTO, error

  ENDIF

  ; write out control tables if they are used

  IF KEYWORD_SET(xrt_flare) THEN BEGIN

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /XRT_FLARE, STAMP=stamp) THEN GOTO, error

  ENDIF

  IF KEYWORD_SET(eis_flare) THEN BEGIN

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /EIS_FLARE, STAMP=stamp) THEN GOTO, error

  ENDIF

  IF KEYWORD_SET(eis_event) THEN BEGIN

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /EIS_EVENT, STAMP=stamp) THEN GOTO, error

  ENDIF

  IF KEYWORD_SET(aec_table) THEN BEGIN

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /AEC_TABLE, STAMP=stamp) THEN GOTO, error

  ENDIF

  IF KEYWORD_SET(aec_exposure) THEN BEGIN

    IF NOT eis_cpt_ascii_tables(0, MSSL=mssl_flag, INTEL=intel_flag, /AEC_EXPOSURE, STAMP=stamp) THEN GOTO, error

  ENDIF

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





