;+
; Project     :	SOHO - CDS
;
; Name        :	DO_FLAG_RECEIVER
;
; Purpose     :	Writes flag receiver commands to series table.
;
; Explanation :	Outputs the commands needed in flag receiver mode. 
;               In flag receiver mode the series must contain a command to set the IEF study in the macro.
;               This uses the locations within the series table of the mirror and slit position commands.
;               In addition the series contains a command to modify the flag study series table
;               to restart the current series when it finishes. At present this feature is disabled.
;               The locations of these commands within the flag study series table depends on whether :
;                   the flag study is GIS or NIS
;                   SFT enabled or not    
;
;               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_flag_receiver, series, sciplan_details, first_raster, flag_study_info>
;
; Inputs      : series          = structure array giving information on series tables;
;               sciplan_details = structure containing study information 
;                                 from sci_details planning database
;                                 including deferred pointing information;
;               first_raster    = structure array giving information on first raster;
;               flag_study_info = structure giving information on associated flag study
;
; Opt. Inputs : None.
;
; Outputs     : Writes commands to series table.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	shorthex, fileout.
;                
; 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, 3/1/96
;                 Removed jump back to calling sequence for now.
;               Version 0.2, MKC, 15/1/96
;                 removed call to RBOTH.
;
; Version     :	Version 0.2, 15/1/96
;-
;**********************************************************

PRO do_flag_receiver, series, sciplan_details, first_raster, flag_study_info

    ; The use of fixed or deferred positions is catered for by telling
    ; macro false slit and mirror position command locations 
 
    ; with RBOTH command

;    IF flag_study_info.detector EQ 'G' THEN BEGIN
;      slitp_loc   = 10
;      mirrorp_loc = 12
;      runs_loc    = 30
;    ENDIF ELSE IF flag_study_info.detector EQ 'N' THEN BEGIN
;      slitp_loc   = 13
;      mirrorp_loc = 15
;      runs_loc    = 33
;    ENDIF ELSE MESSAGE, 'Unknown detector for flag study = ' + flag_study_info.detector

    ; no RBOTH command

    IF flag_study_info.detector EQ 'G' THEN BEGIN
      slitp_loc   = 8
      mirrorp_loc = 10
      runs_loc    = 28
    ENDIF ELSE IF flag_study_info.detector EQ 'N' THEN BEGIN
      slitp_loc   = 11
      mirrorp_loc = 13
      runs_loc    = 31
    ENDIF ELSE MESSAGE, 'Unknown detector for flag study = ' + flag_study_info.detector

    ; adjust locations if SFT on

    IF sciplan_details.tracking THEN runs_loc = runs_loc + 3

    ; check pointing

    IF flag_study_info.pointing EQ 'FIXED' OR flag_study_info.pointing EQ 'DEFERRED' THEN BEGIN

      ; set locations just short of end of series table

      slitp_loc   = 124
      mirrorp_loc = 124

    ENDIF

    ; send flag study command
    ; NB Only span of raster actually matters so can use step size = 1
    ;    Assume only one raster position will be defined.
    ;    This implies that there is only one raster in the flag study (which may 
    ;    be repeated).

    fileout, words=8, 'CB5IEF'                                + '          # send IEF flag receiver study details' 
    fileout, shorthex ( flag_study_info.cdhsid )              + '          # flag study ID'
    fileout, shorthex ( first_raster.mirrorspan )             + '          # mirror span in steps'
    fileout, '0x0001'                                         + '          # mirror step'
    fileout, shorthex ( - first_raster.slitspan )             + '          # slit span in steps'
    fileout, '0x0001'                                         + '          # slit step'
    fileout, shorthex ( mirrorp_loc )                         + '          # mirror location'
    fileout, shorthex ( slitp_loc )                           + '          # slit location'
    fileout, 'TERMINATE'                 
    fileout, ''

;    ; add run sequence command to flag study

;    fileout, words=5, 'CB5FILS'                              + '         # modify flag study return ID'
;    fileout, shorthex ( flag_study_info.cdhsx )              + '          # flag study index'
;    fileout, shorthex ( runs_loc )                           + '          # location in flag study'
;    fileout, 'CB5RUNS ' + shorthex ( series(0).cdhsid )      + '  # run current sequence 
;    fileout, 'TERMINATE'                 
;    fileout, ''

    ; dump flag series

    fileout, words=2, 'CB5DMPS ' + shorthex ( flag_study_info.cdhsx ) + '  # dump flag study'
    fileout, ''

END

