;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_SET_AEC_TABLE
;
; Purpose     : Sets up AEC table parameters.
;
; Explanation : Sets up AEC table parameters.
;
; Use         : < table = eis_cpt_set_AEC_table ( control) >
;
; Inputs      : control                 : STRUCTURE of type EIS_AEC_control
;
; Opt. Inputs : None.
;
; Outputs     : result      : INT flag :
;                             1 : processed science plan OK.
;                             0 : processing failed.
;               table       : BYTARR[16] AEC table
;
; 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 24/06/05
;
; Modified    : Version 0.0, 24/06/05, MKC
;               Version 0.1, 22/01/08, MKC
;                 Added big-endian comments.
;                 Corrected bug where last 7 bits of binning factor were zeroed.
;
; Version     : Version 0.1, 22/01/08
;-
;**********************************************************

FUNCTION eis_cpt_set_aec_table, control

  ; set up AEC table

  table = BYTARR(16)

  ; set up upper threshold
  ; NB note big-endian byte order

  table[0:1] = eis_cpt_uinttobytes(control.upper_threshold)

  ; set up lower threshold
  ; NB note big-endian byte order

  table[2:3] = eis_cpt_uinttobytes(control.lower_threshold)

  ; set up high energy pixel count limit
  ; NB note big-endian byte order

  table[4:7] = eis_cpt_ulongtobytes(control.high_energy_pixel_count_limit)

  ; set up low energy pixel count limit
  ; NB note big-endian byte order

  table[8:11] = eis_cpt_ulongtobytes(control.low_energy_pixel_count_limit)

  ; set up exposure ID (NB bits 0-4)
  ; NB note big-endian byte/bit order

  table[12] = ISHFT(control.exposure_ID, 3)

  ; set up Binning (NB bits 5-14)
  ; NB note big-endian byte/bit order

  ; 3 MSBits

  table[12] = table[12] OR ISHFT(control.binning_factor, -7)

  ; 7 LSBits

  table[13] = ISHFT(control.binning_factor, 1)

  ; set up AEC maximum run time
  ; NB note big-endian byte/bit order

  table[13] = table[13] OR ISHFT(control.run_time, -16)

  table[14:15] = eis_cpt_uinttobytes(control.run_time)

  ; return table

  RETURN, table

END





