;+
; Project     :	SOHO - CDS
;
; Name        :	DO_GSET_OFF
;
; Purpose     :	Writes commands to switch GIS off to series table.
;
; Explanation :	Outputs the commands which are needed immediately after 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_off, 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 2 second 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, MKC, 7/10/96
;                 Commented out calculate GIS checksum command.
;
; Version     :	Version 0.6, 7/10/96
;;-
;**********************************************************

PRO do_gset_off, 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' 

  ; get CDHS state database parameters

  ; get parking high voltage for detectors

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

  cbghv1v = ss(0).active
  
  ss = cp_get_entry ( 'CBGHV2V', [0] )

  cbghv2v = ss(0).active
  
  ss = cp_get_entry ( 'CBGHV3V', [0] )

  cbghv3v = ss(0).active
  
  ss = cp_get_entry ( 'CBGHV4V', [0] )

  cbghv4v = ss(0).active
  
  ; get nominal front face bias

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

  cbgmcpb = ss(0).active

  ; set GIS detectors to parking voltage

  fileout, '# Park GIS detectors' 
  fileout, ''  

  ; turn off high voltages for each detector
  ; turn off even if not used

  ; detector 1

  fileout, words=3, 'CB5SIC' + '          # park high voltage for detector 1' 
  fileout, 'CBGHV1V ' + shorthex(cbghv1v) 
  fileout, 'TERMINATE'
  fileout, ''
  macro_wait, 1000
  fileout, ''  

  ; detector 2

  fileout, words=3, 'CB5SIC' + '          # park high voltage for detector 2' 
  fileout, 'CBGHV2V ' + shorthex(cbghv2v) 
  fileout, 'TERMINATE'
  fileout, ''
  macro_wait, 1000
  fileout, ''  

  ; detector 3

  fileout, words=3, 'CB5SIC' + '          # park high voltage for detector 3' 
  fileout, 'CBGHV3V ' + shorthex(cbghv3v) 
  fileout, 'TERMINATE'
  fileout, ''
  macro_wait, 1000
  fileout, ''  

  ; detector 4

  fileout, words=3, 'CB5SIC' + '          # park high voltage for detector 4' 
  fileout, 'CBGHV4V ' + shorthex(cbghv4v) 
  fileout, 'TERMINATE'
  fileout, ''
  macro_wait, 1000
  fileout, ''  

  ; calculate checksums

;  fileout, words=3, 'CB5SIC' + '          # calculate checksums' 
;  fileout, 'CBGCHKSM'
;  fileout, 'TERMINATE'
;  fileout, ''  
;  macro_wait, 2000
;  fileout, ''  

  ; check if filament used 

  IF gset.fil_id NE 0 THEN BEGIN

    fileout, '# Turn GIS filaments off'
    fileout, ''

    ; turn filament off

    fileout, words=3, 'CB5SIC' + '          # turn GIS filament off' 
    fileout, 'CBEFILF' 
    fileout, 'TERMINATE'
    fileout, ''
  
    ; set up nominal front face bias voltage

    IF cbgmcpb 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 cbgmcpb 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 CDHS DATABASE'      

    ; explicitly turn off GIS filament current

    fileout, words=3, 'CB5SIC' + '          # turn off GIS filament current' 
    fileout, 'CBEFILI 0x0000'
    fileout, 'TERMINATE'
    fileout, ''

  ENDIF ; end of filament switch off
 
END

