;+
; Project     :	SOHO - CDS
;
; Name        :	DO_GSET_ON
;
; Purpose     :	Writes commands to switch GIS on to series table.
;
; Explanation :	Outputs the commands which are needed immediately prior to each GIS raster. 
;
;               Perl script uses format required by CB5FILS i.e token list for getpar :
;
;               1) COMMAND 1 2 3 4                    # simple scc
;
;               2) SCRIPT                             # script
;                  0x1 
;                  0x2
;                  TERMINATE
;
;               NB All commands or scripts must be to macro and so begin CB5* ...
;               Perl script need format:
;              
; Use         : <do_gset_on, gsetid, gis>
;
; Inputs      : gsetid     = GSET database ID giving GIS parameters.
;               gis        = INTARR(4) indicating whether each detector used for this raster.
;
; Opt. Inputs : None.
;
; Outputs     : Writes commands to series table.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	shorthex, fileout, get_gset, cp_get_entry, macro_wait.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	Created from do_previous.
;
; Written     :	Version 0.0, Martin Carter, RAL, 12/12/95
;
; Modified    :	Version 0.1, MKC, 2/1/96
;                 Added check on whether each detector used.
;               Version 0.2, MKC, 8/1/96
;                 Added errmsg to gset call.
;               Version 0.3, MKC, 15/1/96
;                 Added wait after GIS checksum calculation
;                 Added macro_wait.
;               Version 0.4, MKC, 17/1/96
;                 Added 1 sec delays between GIS commands.
;                 This was to fix a CNA problem with GIS.
;               Version 0.5, MKC, 31/1/96
;                 GSET database modified.
;               Version 0.6, 4/10/96
;                 Moved LLD set up to do_gset_init and do_gset_ttc.
;                 Commented out calculate GIS checksum command.
;               Version 0.7, 10/6/98
;                 Added a 60 second delay after turning on high voltages.
;
; Version     :	Version 0.7, 10/6/98
;-
;**********************************************************

PRO do_gset_on, gsetid, gis

  ; get gset parameters from GSET database

  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 inuse if gis

  dummy = WHERE ( gis AND NOT inuse, count)

  IF count NE 0 THEN MESSAGE, 'GIS detector not available' 

  ; check if filament used 

  IF gset.fil_id NE 0 THEN BEGIN

    ; set up filaments

    fileout, '# Set up GIS filaments for use'
    fileout, ''

    ; get CDHS state database parameters

    ss = cp_get_entry ( 'CBEFILI', [0] )

    cbefili = ss(0).active

    ; set up filament ID

    IF gset.fil_id EQ 1 THEN BEGIN

      fileout, words=3, 'CB5SIC' + '          # use GIS filament 1' 
      fileout, 'CBEFIL1' 
      fileout, 'TERMINATE'
      fileout, ''
      macro_wait, 1000
      fileout, ''  

    ENDIF ELSE IF gset.fil_id EQ 2 THEN BEGIN

      fileout, words=3, 'CB5SIC' + '          # use GIS filament 2' 
      fileout, 'CBEFIL2' 
      fileout, 'TERMINATE'
      fileout, ''
      macro_wait, 1000
      fileout, ''  

    ENDIF ELSE MESSAGE, 'INVALID FILAMENT ID IN GSET DATABASE'      

    ; set up front face bias voltage

    IF gset.ffb EQ 1 THEN BEGIN

      fileout, words=3, 'CB5SIC' + '          # use +ve GIS ffb voltage' 
      fileout, 'CBGMCPBP' 
      fileout, 'TERMINATE'
      fileout, ''
      macro_wait, 1000
      fileout, ''  

    ENDIF ELSE IF gset.ffb EQ (-1) THEN BEGIN

      fileout, words=3, 'CB5SIC' + '          # use -ve GIS ffb voltage' 
      fileout, 'CBGMCPBM' 
      fileout, 'TERMINATE'
      fileout, ''
      macro_wait, 1000
      fileout, ''  

    ENDIF ELSE MESSAGE, 'INVALID FRONT FACE BIAS VOLTAGE IN GSET DATABASE'      

    ; set up GIS filament current

    fileout, words=3, 'CB5SIC' + '          # set GIS filament current' 
    fileout, 'CBEFILI ' + shorthex(cbefili) 
    fileout, 'TERMINATE'
    fileout, ''

    ; turn filament on

    fileout, words=3, 'CB5SIC' + '          # turn GIS filament on' 
    fileout, 'CBEFILN' 
    fileout, 'TERMINATE'
    fileout, ''
  
  ENDIF ; end of filament set up

  ; calculate checksums

;  fileout, words=3, 'CB5SIC' + '          # calculate checksums' 
;  fileout, 'CBGCHKSM'
;  fileout, 'TERMINATE'
;  fileout, ''  
;  macro_wait, 2000
;  fileout, ''  
    
  ; set up low level discriminators and high voltages for each detector

  fileout, '# Set up EHTs for GIS detectors' 
  fileout, ''  

  IF gis(0) THEN BEGIN

    ; detector 1

    fileout, words=3, 'CB5SIC' + '          # set up high voltage for detector 1' 
    fileout, 'CBGHV1V ' + shorthex(gset.hvolt(0)) 
    fileout, 'TERMINATE'
    fileout, ''
    macro_wait, 1000
    fileout, ''  

  ENDIF

  IF gis(1) THEN BEGIN

    ; detector 2

    fileout, words=3, 'CB5SIC' + '          # set up high voltage for detector 2' 
    fileout, 'CBGHV2V ' + shorthex(gset.hvolt(1)) 
    fileout, 'TERMINATE'
    fileout, ''
    macro_wait, 1000
    fileout, ''  

  ENDIF

  IF gis(2) THEN BEGIN

    ; detector 3

    fileout, words=3, 'CB5SIC' + '          # set up high voltage for detector 3' 
    fileout, 'CBGHV3V ' + shorthex(gset.hvolt(2)) 
    fileout, 'TERMINATE'
    fileout, ''
    macro_wait, 1000
    fileout, ''  

  ENDIF

  IF gis(3) THEN BEGIN

    ; detector 4

    fileout, words=3, 'CB5SIC' + '          # set up high voltage for detector 4' 
    fileout, 'CBGHV4V ' + shorthex(gset.hvolt(3)) 
    fileout, 'TERMINATE'
    fileout, ''
    macro_wait, 1000
    fileout, ''  
  
  ENDIF

  ; delay after turning on high voltages

  macro_wait, 60000
  fileout, ''  

END

