;+
; Project     :	SOHO - CDS
;
; Name        :	DO_GSET_TTC
;
; Purpose     :	Writes out GSET lookup table commands to TTC file.
;
; Explanation :	Writes out the commands to set up the GIS lookup tables to the TTC file
;               used to load the deferred command store.
;              
; Use         : <do_gset_ttc, unit, gsetid, timenow, commands>
;
; Inputs      : unit     = logical unit no. for output
;               gsetid   = integer giving GSET ID for database
;               timenow  = double giving current tai time
;               commands = integer number of deferred commands so far
;
; Opt. Inputs : None.
;
; Outputs     : Writes initial GSET commands to TTC file.
;               Updates timenow, commands.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	get_gset, do_gset_lut_ttc.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	Created from do_gset_init.
;
; Written     :	Version 0.0, Martin Carter, RAL, 13/12/95
;               
; Modified    :	0.1, 2/1/96
;                    Added gset.inuse tag.
;               0.2, MKC, 8/1/96
;                 Added errmsg to gset call.
;               0.3, MKC, 16/1/96
;                 Shortened time between LUT commands to 340 secs.
;               0.4, MKC, 31/1/96
;                 GSET database modified.
;               0.5, 23/2/96
;                 Split LUT commands into separate file.
;               0.6, 8/7/96
;                 Added commands argument.
;               0.7, 4/10/96
;                 Moved LLD set up from do_gset_on.
;
; Version     :	Version 0.7, 4/10/96
;-
;**********************************************************

PRO do_gset_ttc, unit, gsetid, timenow, commands

    ; get GSET database entry

    errmsg = ''

    get_gset, gsetid, gset, ERRMSG=errmsg

    IF errmsg NE '' THEN MESSAGE, errmsg

    ; set up flags indicating if each detector in use

    inuse = gset.det_used

    ; check lookup table parameters are bytes

    IF MAX(ABS(gset.lut_par)) GT 255 THEN MESSAGE, 'INVALID GSET LUT PARAMETERS'

    ; set lookup tables

    PRINTF, unit, '# Set up GIS look up tables for detectors in use'
    PRINTF, unit

    IF inuse(0) THEN do_gset_lut_ttc, unit, gset.lut_par(0,*), timenow, commands

    IF inuse(1) THEN do_gset_lut_ttc, unit, gset.lut_par(1,*), timenow, commands

    IF inuse(2) THEN do_gset_lut_ttc, unit, gset.lut_par(2,*), timenow, commands

    IF inuse(3) THEN do_gset_lut_ttc, unit, gset.lut_par(3,*), timenow, commands

    ; set up low level discriminators 

    PRINTF, unit, '# Set up LLDs for GIS detectors'
    PRINTF, unit

    IF inuse(0) THEN BEGIN

      ; detector 1

      PRINTF, unit, '"', tai2utc ( timenow, /ECS),'"   "CBGD1LLT ', shorthex(gset.lld(0)), '"'
      PRINTF, unit

      timenow = timenow + 1 ; add 1 second delay

      commands = commands + 4  ;  increment no. of INT16 words used in deferred command store by entry

    ENDIF

    IF inuse(1) THEN BEGIN

      ; detector 2

      PRINTF, unit, '"', tai2utc ( timenow, /ECS),'"   "CBGD2LLT ', shorthex(gset.lld(1)), '"'
      PRINTF, unit

      timenow = timenow + 1 ; add 1 second delay

      commands = commands + 4  ;  increment no. of INT16 words used in deferred command store by entry

    ENDIF

    IF inuse(2) THEN BEGIN

      ; detector 3

      PRINTF, unit, '"', tai2utc ( timenow, /ECS),'"   "CBGD3LLT ', shorthex(gset.lld(2)), '"'
      PRINTF, unit

      timenow = timenow + 1 ; add 1 second delay

      commands = commands + 4  ;  increment no. of INT16 words used in deferred command store by entry

    ENDIF

    IF inuse(3) THEN BEGIN

      ; detector 4

      PRINTF, unit, '"', tai2utc ( timenow, /ECS),'"   "CBGD4LLT ', shorthex(gset.lld(3)), '"'
      PRINTF, unit

      timenow = timenow + 1 ; add 1 second delay

      commands = commands + 4  ;  increment no. of INT16 words used in deferred command store by entry

    ENDIF

END

