;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_ASCII_TABLES
;
; Purpose     : Outputs OBSTBL tables as ASCII files
;
; Explanation : Writes out OBSTBL tables as ASCII text files in a number of different formats.
;
; Use         : < result = eis_cpt_ascii_tables ( table_indexes, MSSL=mssl, INTEL=intel, $
;                          SEQUENCE=sequence, LINELIST=linelist, AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
;                          XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, ) >
;
; Inputs      : table_indexes : INTARR() list of sequence tables used.
;
; Opt. Inputs : None.
;
; Outputs     : result   : INT flag :
;                            1 : processed science plan OK.
;                            0 : processing 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.
;               INTEL             : INT Flag indicating should produce individual Intel hex files for each sequence table
;                                   and line list table.
;               STAMP             : STRING stamp indicating should stamp file names.
;               LINELIST          : INT FLAG indicating if table is linelist table
;               SEQUENCE          : INT FLAG indicating if table is sequence table
;               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. : None.
;
; Written     : Martin Carter RAL 21/01/05
;
; Modified    : Version 0.0, 21/01/05, MKC
;               Version 0.1, 03/02/05, MKC
;                 Removed obstbl argument.
;               Version 0.2, 24/03/05, MKC
;                 Renamed keywords.
;                 Added INTEL keyword.
;               Version 0.3, 03/04/05, MKC
;                 Added table_indexes argument.
;                 Added LINELIST and SEQUENCE keywords.
;                 Renamed routine.
;               Version 0.4, 25/04/05, MKC
;                 Added description to text output.
;                 Do not output invalid tables.
;               Version 0.5, 27/04/05, MKC
;                 Removed ISACS keyword.
;                 Removed eis cpt convert table.
;               Version 0.6, 11/05/05, MKC
;                 Used TEXT and CHECK keywords.
;               Version 0.7, 26/06/05, MKC
;                 Added AEC_EXPOSURE, AEC_TABLE, XRT_FLARE, EIS_FLARE, EIS_EVENT keywords.
;               Version 0.8, 07/07/08, MKC
;                 Changed so that if file name blank then does not output.
;               Version 0.9, 05/05/11, MKC
;                 Added STAMP keyword.
;
; Version     : 0.9, 05/05/11
;-
;**********************************************************

FUNCTION eis_cpt_ascii_tables, table_indexes, MSSL=mssl, INTEL=intel, STAMP=stamp, LINELIST=linelist, SEQUENCE=sequence, $
  AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, EIS_EVENT=eis_event

  ; check some output required

  IF KEYWORD_SET(mssl) OR KEYWORD_SET(intel) THEN BEGIN

    ; sort table indexes

    table_indexes = table_indexes[SORT(table_indexes)]

    ; get unique table indexes

    table_indexes = table_indexes[UNIQ(table_indexes)]

    ; loop through unique table indexes

    FOR k = 0, N_ELEMENTS(table_indexes)-1 DO BEGIN

      ; extract table from OBSTBL

      IF NOT eis_cpt_obstbl(table, table_indexes[k], time_for_reuse, description, ULBA=ulba, $
                           /EXTRACT, /CHECK, TEXT=text, $
                           LINELIST=linelist, SEQUENCE=sequence, AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                           XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, EIS_EVENT=eis_event) THEN GOTO, error

      ; check table valid
      ; NB [0] is not a valid table

      IF KEYWORD_SET(table) THEN BEGIN

        ; check output required

        IF KEYWORD_SET(mssl) THEN BEGIN

          ; set up stamp

          IF KEYWORD_SET(stamp) THEN istamp = 'mssl_'+stamp ELSE istamp = 0

          ; get table file name

          file = eis_cpt_file_name('MSSL', STAMP=istamp, table_indexes[k], LINELIST=linelist, SEQUENCE=sequence, $
                                    AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                                    XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, EIS_EVENT=eis_event)

          ; check file defined

          IF KEYWORD_SET(file) THEN BEGIN

            ; write table to file

            IF NOT eis_cpt_write_ascii( file, text) THEN GOTO, error

          ENDIF

        ENDIF

        IF KEYWORD_SET(intel) THEN BEGIN

          ; set up start address for table

          IF NOT eis_cpt_obstbl_index(table_indexes[k], start_address, length, number, $
                                SEQUENCE=sequence, LINELIST=linelist, $
                                AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                                XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, EIS_EVENT=eis_event) THEN GOTO, error

          ; pad out table

          IF N_ELEMENTS(table) LT length THEN table = [table, REPLICATE('FF'XB,length-N_ELEMENTS(table))]

          ; set up stamp

          IF KEYWORD_SET(stamp) THEN istamp = 'intel_'+stamp ELSE istamp = 0

          ; get table file name

          file = eis_cpt_file_name('INTEL', STAMP=istamp, table_indexes[k], LINELIST=linelist, SEQUENCE=sequence, $
                                    AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                                    XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, EIS_EVENT=eis_event)

          ; check file defined

          IF KEYWORD_SET(file) THEN BEGIN

            ; write table to intel hex file

            IF NOT KEYWORD_SET(eis_cpt_write_intel_hex_file(file, table, START_ADDRESS=start_address, ULBA=ulba, /NOENDOFFILE) ) THEN GOTO, error

          ENDIF

        ENDIF

      ENDIF

    ENDFOR

  ENDIF

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





