;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_OBSTBL_GET
;
; Purpose     : Get current OBSTBL and OBSTBL map from files
;
; Explanation : The current OBSTBL is kept in an INTEL Hex file.
;               The current OBSTBL map is kept in an ASCII file.
;               If read fails then returns 0.
;
; Use         : < result = eis_cpt_obstbl_get(obstbl) >
;
; Inputs      : None.
;
; Opt. Inputs : None.
;
; Outputs     : result   : INT flag :
;                              1 : processed science plan OK.
;                              0 : processing failed.
;               obstbl   :   STRUCTURE of type EIS_CPT_OBSTBL.
;
; Opt. Outputs: None.
;
; Keywords    : reset : INT flag indicating should initialize OBSTBL
;                         1 : soft reset (only OBSTBL map)
;                         2 : hard reset (OBSTBL also)
;                         3 : control table reset (Only event control tables in OBSTBL map)
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 26/10/04
;
; Modified    : Version 0.1, 25/11/04, MKC
;                 Added !EIS_CPT_CONSTANTS.
;               Version 0.2, 07/02/05, MKC
;                 Renamed from eis cpt get obstbl.
;                 Renamed eis cpt read obstbl map.
;                 Renamed eis cpt read obstbl.
;               Version 0.3, 04/03/05, MKC
;                 Moved OBSTBL indexes to !EIS_CPT_CONSTANTS.
;                 Removed index tags.
;                 Changed call to eis cpt obstbl read.
;               Version 0.4, 26/04/05, MKC
;                 Changed so that preserves OBSTBL data on RESET.
;                 Added check on ulba.
;                 Added start_address.
;               Version 0.5, 09/05/05, MKC
;                 Modified RESET behaviour.
;                 Replaced eis cpt obstbl read with direct call to eis cpt read intel hex file.
;                 Changed so that returns OBSTL through argument list.
;               Version 0.6, 05/07/05, MKC
;                 Changed so that resets resets OBSTBL to 'FF's..
;               Version 0.7, 20/08/06, MKC
;                 Fixed bug where if reset=1 used and OBSTBL read fails then OBSTBL not initialized to 'FF's.
;                 If no OBSTBL now insists start with reset=2.
;               Version 0.8, 03/10/06, MKC
;                 Changed names of internal obstbl files.
;               Version 0.9, 07/07/08, MKC
;                 Changed name of internal obstbl directory.
;               Version 1.0, 06/10/11, MKC
;                 Added control table reset option.
;
; Version     : 1.0, 06/10/11
;-
;**********************************************************

FUNCTION eis_cpt_obstbl_get, obstbl, RESET=reset

  ; initialize OBSTBL structure

  obstbl = {EIS_CPT_obstbl}

  ; check reset

  IF (NOT KEYWORD_SET(reset)) THEN res = 0 ELSE res = reset

  ; take appropriate action for reset

  IF res EQ 0 OR res EQ 3 THEN BEGIN

    ; if no reset or control table reset then read OBSTBL map file

    map = eis_cpt_obstbl_map_read()

    IF NOT KEYWORD_SET(map) THEN GOTO, error

    obstbl.map = map

    ; if control table reset then remove protection on event control tables

    IF res EQ 3 THEN BEGIN

      null = {EIS_CPT_obstbl_map_entry}

      obstbl.map.xrt_flare = null

      obstbl.map.eis_flare = null

      obstbl.map.eis_event = null

    ENDIF

  ENDIF ELSE IF res EQ 2 THEN BEGIN

    ; initialize OBSTBL to 'FF's and RETURN OK

    obstbl.data[*] = 'FF'XB

    RETURN, 1

  ENDIF ELSE IF res NE 1 THEN MESSAGE, 'IMPLEMENTATION ERROR, INVALID VALUE FOR RESET'

  ; get OBSTBL file name

  obstbl_file = eis_cpt_file_name('OBSTBL',STAMP='internal',/INTERNAL)

  ; get OBSTBL data

  IF NOT eis_cpt_read_intel_hex_file( obstbl_file, start_address, header, ulba, data) THEN GOTO, error

  ; check obstbl

  IF NOT eis_cpt_obstbl_check ( start_address, header, ulba, data) THEN GOTO, error

  ; keep header

  obstbl.header = header

  ; keep ulba

  obstbl.ulba = ulba

  ; NB not all addresses need be set
  ; pads out obstbl array if necessary with 0's

  obstbl.data = data

  ; return OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error

  RETURN, 0

END





