;+
; Project     :	SOHO - CDS
;
; Name        :	WR_SERIES_FILE
;
; Purpose     :	Writes out CDS SERIES tables to file.
;
; Explanation :	Writes out the study parameters in the format required by 
;               the Perl script files for loading onto the CDS.
;               The series table contains the commands to move to the OPS position of
;               each raster. This is output even if the position does not differ from the
;               current OPS position because the macro needs to be informed of the current 
;               position. The table also contains the command to move to the required slit 
;               number when this differs from the current slit number. 
;               At the start of a study CDS will be at the position of the first raster 
;               and have the correct slit number and the required GSET ID parameters.
;               The OPS, slit number and GSET commands are incorporated into the deferred
;               command store or the previous study series table.
;               If the study is not time-tagged it follows on sequentially from the 
;               previous study. 
;                
;               Even if the study is already loaded the tables are written out again. 
;
;               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* ...
;
;               If the version keyword is set the routine simply returns the current SW 
;               version number.
;
;               The routine determines whether the study is a simple study; a flag master study
;               and/or a flag receiver study; a flag study or alternative plan study; 
;               or an engineering study.
;               If the study is an engineering study the routine copies the study into 
;               a series table file and then returns.
;     
;               series.istate contains the state for the first raster in the study and this state
;               is reached by commands external to the series table.
;
;               If study is a flag study then can have event, fixed or deferred pointing.
;               If has fixed or deferred pointing then must explicitly command to go to the
;               selected position ( usually this is handled in the deferred command store 
;               or the previous study) with a long enough delay to get there from anywhere.
;
; Use         : <wr_series_file, series, fseries, sciplan_details, study_details, 
;                                raster_list, cut_off_time, flag_study_info, flag_master_info>
;
;                 or if version number required
;
;               <wr_series_file, series, VERSION=version>.
;
; Inputs      : series          = structure array giving information on series
;                                 tables;
;               fseries         = structure giving information on following series
;               sciplan_details = structure containing study information 
;                                 from sci_details planning database
;                                 including deferred pointing information;
;               study_details   = structure containing study information 
;                                 from study planning database;
;               raster_list     = structure array giving information on raster
;                                 tables;
;               cut_off_time = DOUBLE TAI format time before which tables may be overwritten
;
; Opt. Inputs : flag_study_info = structure giving information on associated flag study
;                                 ( cdhsid = 0 if no flag study, do not need to pass if
;                                   this study is a flag study )
;                                 Tags :
;                                    cdhsid      : CDHS ID of flag study (0 if no flag study)
;                                    cdhsx       : CDHS index of flag study
;                                    repoint     : Integer tag indicating whether OPS 
;                                                  repointing allowed.
;                                    pointing    : String indicating type of pointing for flag study.
;                                    detector    : character indicating detector for flag study.
;               flag_master_info = structure giving information on associated flag master
;                                  scheme. If not flag_master this has value 0. 
;                                  If study is a flag study then do not need.
;
; Outputs     : Writes study data to file.
;               Settles whether tables already loaded or still needs loading.
;               If study spans more than one series table may replicate series structure
;               for new tables required and fill in index and IDs.
;               Updates series.fstate. NB must be careful not to pass series structure by value
;               i.e should not pass tag or structure array element to do_raster.
;
; Opt. Outputs:	version = software version number if keyword set.
;
; Keywords    : VERSION    = Character string containing software version number;
;               ADJUST_ALLOWED : allow adjustment of mirror and slit positions to avoid 
;                                OPS movement if possible. If ADJUST_ALLOWED=2 then 
;                                explicit movement of the slit is allowed for NIS rasters.
;               ALTSCI         : indicates alternative study which does not want mirror and slit
;                                initial positions commanded.
;		MAX_SLIT_POS   : Use the number passed through this keyword as the maximum
;				 allowed slit position.  This value is used to offset GIS rasters
;				 in the N/S direction in such a way that they do not exceed 
;				 this value.
;
; Calls       :	get_utc, shorthex, do_raster, get_cdhs_index, fileout,
;               concat_dir, do_previous, do_engineering, do_following.
;                
; Common      :	fileoutblock :
;                 file_words  : INTARR() Number of command words written to each 
;                                        series table in study.
;                 file_pos    : INTARR() Position in file_output of lines for each series
;                                        table in study. 
;                 file_output : STRARR() Array containing lines written to each 
;                                        series table in study.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	Version 0.00,Martin Carter, RAL, 14/10/94
;
; Modified    :	Version 0.01,Martin Carter, RAL, 28/11/94
;                            Added proforma.
;               Version 0.1, Martin Carter, RAL, 6/2/95
;                            Corrected bug in proforma
;               Version 0.2, MKC, 16/2/95
;                            Removed time tagged study option i.e next series keyword
;                            Changed so that outputs science plan study Id rather than 
;                            study number to series file.
;               Version 0.3, MKC, 6/3/95
;                            Moved changing detector into do_raster.
;                            Added state structure to argument list.
;               Version 0.4, MKC, 15/3/95
;                            Modified do_raster.
;               Version 0.5, MKC, 23/3/95
;                            Correct VDS exposure command in do_raster.
;               Version 0.6, MKC, 27/3/95
;                            Added command mnemonic for CB5SIC in do_raster.
;               Version 0.7, MKC, 19/4/95
;                            Added deferred pointing structure and updated do_raster.
;                            Removed variable rasters and n_pointings tags in study details
;                            from display.
;                            Used new versions of get_series_id and get_cdhs_index.
;                            Changed output lay out.
;                            Modified argument list.
;                            Removed fundamental flag in do_raster.
;                            Removed state structure from argument list.
;                            Used STRTRIM.
;               Version 0.8  MKC, 25/04/95
;                            Added comment.
;               Version 0.9, MKC, 28/4/95
;               Version 1.0, MKC, 16/5/95
;                            Added non time tagged studies back in with new mechanism.
;                            Forced processing of each table even if already loaded
;                            so that keeps track of current position and slit number properly.
;                            Added initial study state and final states to series structure.
;               Version 1.1, MKC, 1/6/95
;                            Added printout of initial solar x and y for series.
;                            Changed do_raster argument list.
;               Version 1.2, MKC, 10/8/95
;                            Set BYTE study ID in series table to zero, was set to planning 
;                            study ID.
;                            Corrected bug which crashes code when study spanning more than one
;                            series table already exists in database.
;                            Added series structure rpt tag.
;               Version 1.3, MKC, 4/9/95
;                            Added READYBOTH command to start of each series to 
;                            allow recovery after health module action.
;               Version 1.4, MKC, 26/9/95
;                            Moved SFT to study level from raster level.
;               Version 1.5, MKC, 28/9/95
;                            Modified so that does increments arrays differently.
;                            Removed sciplan_details from do_raster argument list.
;               Version 1.6, MKC, 2/10/95
;                            Added flag study ID.
;                            Modified treatment of RDYBOTH.
;                            Added flag master logic.
;               Version 1.7, MKC, 12/10/95
;                            Passed flag_study flag onto do_raster.
;               Version 1.8, MKC, 23/10/95
;                            Renamed environment variables and used concat_dir.
;               Version 1.9, MKC, 2/11/95
;                            Explicitly disabled SFT if not used.
;                            Added logic for dealing with IEF repointing.
;                            Added flag master logic for getting IEF table.
;               Version 2.0, MKC, 3/11/95
;                            Changed flag receiver logic so that does not assume 
;                            flag study uses same detector as current study (but not
;                            commands).
;                            Changed so that CB5FILSE uses hex rather than mnemonic. 
;                            Corrected bug in CB2FTDIS command (needed CB5SIC)
;               Version 2.1, MKC, 7/11/95
;                            Added gset_id to state structure.
;                            Added position commands to previous study and update of istate 
;                            if non time-tagged study.
;                            Removed check on usable study.
;                            Added do_previous.
;                            Added extra length to study with nsfs.
;                            Modified printout.
;               Version 2.2, 24/11/95
;                            Modified format for series table commands.
;                            Changed so that CVT file shave .dt extension.
;               Version 2.3, 5/12/95
;                            Changed to mirror and slit span.
;                            Added engineering study logic.
;                            Added deferred pointing tags : dsolx, dsoly.
;                            Modified use of file_pos.
;                            Added do_engineering.
;               Version 2.4, 12/12/95
;                            Added do_flag_master, do_flag_receiver.
;                            Changed action on following study.
;               Version 2.5, 14/12/95
;                            Added reverse study processing.
;               Version 2.6, 2/1/96
;                            Added flag study processing.
;               Version 2.7, MKC, 15/1/96
;                            Removed READYBOTH for now at the start of each study. 
;               Version 2.8, MKC, 29/1/96
;                            Added adjust_allowed keyword.
;               Version 2.9, 11/3/96, MKC
;                            Fixed bug when series defined but not loaded and more than one table. 
;               Version 3.0, 15/5/96, MKC
;                            Modified do_raster to switch SFT on/off each raster.
;                            Removed SFT turn off at end of study.
;               Version 3.1, 20/5/96, MKC
;                            Corrected bug in do_raster.
;               Version 3.2, 5/9/96, MKC
;                            Added printout to indicate when mirror/slit adjust is used in do_raster.
;                            Modified adjust_allowed comment.
;               Version 3.3, MKC, 3/10/96
;                            Added time to allow previous tables to be overwritten to argument list.
;                            Set up raster pointing in cpt_details.
;                            Removed study_raster_detail from do_raster argument list.
;                            Combine repeat studies and repeat pointings into single set of series tables.
;                            Fixed so that when new table extends over a number of entries simply increments 
;                            cdhsid rather than interrogating database. 
;                            Changed to useing .nextst tag.               
;               Version 3.4, MKC, 28/11/96
;                            Fixed bug -- hadnt removed get_series_id call for studies spanning a number
;                            of tables so IDs got written out twice.         
;               Version 3.5, MKC, 24/2/97
;                            Added fseries to do_engineering argument list.
;               Version 3.6, MKC, 24/4/98
;                            Changed wording slightly to allow alternative studies as flagst='Y'
;                            Changed logic for recognition of FIXED, DEFERRED or EVENT pointing.
;               Version 3.7, MKC, 18/6/98
;                            Added ALTSCI keyword to comment out mirror and slit positions.     
;               Version 3.8, REY, 08/05/03
;                            Added the MAX_SLIT_POS keyword. 
;                            Adjusted software version.
;		Version 3.9, REY, 02/12/04
;			     Updated software version to 5.2.
;
; Version     :	Version 3.9, 02/12/04
;-
;**********************************************************

PRO wr_series_file, series, fseries, sciplan_details, study_details, raster_list, cut_off_time, $
                    flag_study_info, flag_master_info, VERSION=version, ADJUST_ALLOWED=adjust_allowed, $
                    ALTSCI=altsci, MAX_SLIT_POS = max_slit_pos

  COMMON fileoutblock, file_words, file_pos, file_output

  ; set version number 

  sw_version = '5.2'

  ; check if only version required

  IF KEYWORD_SET ( version ) THEN BEGIN

    series.vs = sw_version
    RETURN

  ENDIF

  ; some degug info

  IF !debug THEN PRINT, 'WR_SERIES_FILE'

  ; check if series loaded already

  IF !debug THEN IF series(0).ok THEN PRINT, 'Series table already loaded'

  ; check if engineering study

  IF sciplan_details.study_id EQ 0 THEN BEGIN

    ; engineering study

    do_engineering, sciplan_details, study_details, series, fseries

    ; return

    RETURN

  ENDIF

  ; create array to hold output

  file_output = ''    ; first entry in file_output is NULL
  file_pos    = [1,1] ; avoid blank first line
  file_words  = [0]   ; no commands so far

  ; set up flag master and flag receiver logicals
  ; NB flag_study_info not present if this study is itself a flag study
  ;    If no flag study then cdhs ID of flag study is 0

  IF series(0).flagst NE 'Y' THEN BEGIN

    ; see whether any associated flag studies or whether flag master

    flag_master   = sciplan_details.flag_master NE 0
    flag_receiver = flag_study_info.cdhsid NE 0

  ENDIF ELSE BEGIN

    ; this study is a flag study and so cannot be master or receiver

    flag_master   = 0
    flag_receiver = 0

  ENDELSE 

  ; output series table

  ; table body
  ; NB Any change to the commands output to the SERIES table may necessitate a change to 
  ;    the flag receiver logic.

  ; write command to go to standard mode
  ; this was put in in order that when the health module 
  ; kicked the CDS into SBY the next study would take it back up
  ; to RBOTH and see whether the error condition had gone away
  ; This has been taken out for now because CB5RBOTH has a 
  ; couple of waits in it which at the moment waste time
  ; NB Also requires changes to do_flag_receiver.

;  fileout, words=2, 'CB5RBOTH' + '        # tell macro to go to RDY BOTH mode'
;  fileout, ''
   
  ; increment study counter

  fileout, words=2, 'CB5INCS 0x0001' +  '  # increment study counter'
  fileout, ''

  ; flag receiver logic
  ; i.e this study has an associated flag study
  ; information printout comes later

  IF flag_receiver THEN do_flag_receiver, series, sciplan_details, raster_list(0), flag_study_info

  ; flag master logic

  IF flag_master THEN do_flag_master, flag_master_info

  ; turn off SFT
  ; if not used explicitly turn it off so that if interrupts previous study by mistake
  ; get expected SFT state.

  fileout, words=3, 'CB5SIC'            + '          # disable solar feature tracking'
  fileout, 'CB2FTDIS'
  fileout, 'TERMINATE'         
  fileout, ''

  ; some degug info

  IF !debug THEN PRINT, 'Start raster loop'

  ; set up current pointing and slit number state at the start of the study
  ; The series.istate.solarx/y contains the pointing of the first raster in the study 
  ; whether fixed or deferred and series.fstate.solarx/y contains the current pointing
  ; as the routine loops through the rasters. 

  series.fstate.solarx = series.istate.solarx
  series.fstate.solary = series.istate.solary
  series.fstate.slitn  = series.istate.slitn

  ; If the study is a flag study with fixed or deferred pointing then the current pointing
  ; in fstate is offset so as to allow time to reach the new position.

  IF series(0).flagst EQ 'Y' THEN BEGIN
  
    ; check for FIXED or DEFERRED pointing

    list = WHERE ( study_details.rasters.pointing EQ 1, count )

    IF count EQ 0 OR sciplan_details.n_pointings NE 0 THEN BEGIN

      ; offset current state

      series.fstate.solarx = series.fstate.solarx + 1000
      series.fstate.solary = series.fstate.solary + 1000

    ENDIF

  ENDIF  

  ; loop through rasters

  fileout, '# Fundamental rasters'
  fileout, ''

  FOR rstrno = 0, N_ELEMENTS ( raster_list ) - 1 DO BEGIN

    fileout, '# Raster ' + STRTRIM ( rstrno, 1 ) 
    fileout, ''

    ; output commands for raster
    ; NB must pass series by reference

    do_raster, raster_list( rstrno ), series, $
               ADJUST_ALLOWED=adjust_allowed, ALTSCI=altsci, MAX_SLIT_POS = max_slit_pos

  ENDFOR

  ; some debug info

  IF !debug THEN PRINT, 'End raster loop'

  ; check if following study follows sequentially from this one
  ; and if so add commands to go to initial state of following study

  IF NOT fseries.tt THEN do_following, series, fseries

  ; get number of tables used

  n_series = N_ELEMENTS(file_words)

  ; write out first table

  ; check that the IDs of any sub-series are defined  

  IF series(0).nsubs EQ 1 THEN BEGIN

    ; either only one table or table not been defined before

    ; check if study spans more than one table

    IF n_series GT 1 THEN BEGIN

     ; set number of sub-series

      series.nsubs = n_series

      ; copy initial series

      series = REPLICATE ( series, n_series )

      ; extra series not yet defined, need ID and load index
      ; duplication sets .ok = false
      ; since new ID cannot already be loaded so .ok stays false

      FOR t = 1, n_series - 1 DO BEGIN

        ; get new CDHS ID

        ; bypass database query to get new cdhs id

        series(t).cdhsid = series(0).cdhsid + t       

        ; get new CDHS index

        series(t) = get_cdhs_index ( 'SERIES', series(t), cut_off_time ) 
           
      ENDFOR

    ENDIF

  ENDIF ELSE BEGIN

    ; study spans more than one table and defined previously but not loaded

    ; check used same number of tables as previously

    IF series(0).nsubs NE n_series THEN MESSAGE, 'Invalid number of series tables for study' 

  ENDELSE

  ; Output series CVT files
  ; overwrite any existing files 
  ; write out tables even if dont need loading

  FOR t = 0, n_series-1 DO BEGIN

    ; get file name

    filename = concat_dir ( '$CDS_CP_CVTFILES_W' , 'series_' + STRTRIM ( series(t).cdhsx, 1 ) + '.dt' )

    IF !debug THEN PRINT, 'WRITING FILE : ', filename

    ; open file

    OPENW, unit, filename, /GET_LUN

    ; get current time

    get_utc, utc, /ECS

    ; file header info

    IF t EQ 0 THEN BEGIN

      ; check if repeated indefinitely

      IF series(0).rpt EQ 'Y' THEN BEGIN
        fileout, unit=unit, '# STUDY REPEATED INDEFINITELY'
        fileout, unit=unit, ''
      ENDIF

      ; first table in study

      fileout, unit=unit, '# SERIES TABLE FILE'
      fileout, unit=unit, '# Created ' + STRTRIM ( utc, 1 )
      fileout, unit=unit, '# Version ' + STRTRIM ( sw_version, 1 )
      fileout, unit=unit, '# Table = ' + '1 of ' + STRTRIM ( series(0).nsubs, 1 )
      fileout, unit=unit, '# CDHS id ' + STRTRIM ( series(0).cdhsid, 1)
      fileout, unit=unit, '# CDHS index ' + STRTRIM ( series(0).cdhsx, 1 )
      fileout, unit=unit, '# ID  = ' + STRTRIM ( series(0).id, 1 )
      fileout, unit=unit, '# VAR = ' + STRTRIM ( series(0).var, 1 )
      fileout, unit=unit, '# Initial solar x = ' + STRTRIM ( series(0).istate.solarx, 1) + ' arcsecs'
      fileout, unit=unit, '# Initial solar y = ' + STRTRIM ( series(0).istate.solary, 1) + ' arcsecs'
      fileout, unit=unit, '# Initial slitn   = ' + STRTRIM ( series(0).istate.slitn, 1) 
      fileout, unit=unit, '# Study gsetid    = ' + STRTRIM ( series(0).istate.gsetid, 1) 
      fileout, unit=unit, '# Final   solar x = ' + STRTRIM ( series(0).fstate.solarx, 1) + ' arcsecs'
      fileout, unit=unit, '# Final   solar y = ' + STRTRIM ( series(0).fstate.solary, 1) + ' arcsecs'
      fileout, unit=unit, '# Final   slitn   = ' + STRTRIM ( series(0).fstate.slitn, 1) 
      fileout, unit=unit, '# Final   gsetid  = ' + STRTRIM ( series(0).fstate.gsetid, 1) 
      fileout, unit=unit, ''

      ; check if flag study

      IF series(0).flagst EQ 'Y' THEN BEGIN

        fileout, unit=unit, '# This study is a standalone study (alternative or flag)'
        fileout, unit=unit, ''

      ENDIF

      ; check if next study follows sequentially

      IF NOT fseries.tt THEN BEGIN

        fileout, unit=unit, '# Next study is not time tagged and follows sequentially'
        fileout, unit=unit, ''

      ENDIF

      ; check if more table to follow

      IF n_series GT 1 THEN BEGIN

        fileout, unit=unit, '# Study split into ' + STRTRIM ( n_series, 1 ) + ' tables'
        FOR tt = 0, n_series - 1 DO fileout, unit=unit, $
            '# CDHS id /index : ' + STRTRIM ( series( tt ).cdhsid, 1 ) + ' / ' + STRTRIM ( series( tt ).cdhsx , 1)
        fileout, unit=unit, ''
 
      ENDIF
      
      ; display some study details

      fileout, unit=unit, '# Fundamental study details'
      fileout, unit=unit, '# study_id         = ' + STRTRIM ( study_details.study_id, 1)
      fileout, unit=unit, '# studyvar         = ' + STRTRIM ( study_details.studyvar, 1)
      fileout, unit=unit, '# obs_prog         = ' + STRTRIM ( study_details.obs_prog, 1)
      fileout, unit=unit, '# title            = ' + STRTRIM ( study_details.title, 1)
      fileout, unit=unit, '# category         = ' + STRTRIM ( study_details.category, 1)
      fileout, unit=unit, '# repeat studies   = ' + STRTRIM ( sciplan_details.n_repeat_s, 1)
      fileout, unit=unit, '# repeat pointings = ' + STRTRIM ( sciplan_details.n_pointings, 1)
      fileout, unit=unit, '# n_rasters0       = ' + STRTRIM ( study_details.n_rasters0, 1)
      fileout, unit=unit, '# n_rasters1       = ' + STRTRIM ( sciplan_details.n_rasters1, 1)
      fileout, unit=unit, '# sv_desc          = ' + STRTRIM ( study_details.sv_desc, 1)
      fileout, unit=unit, '# duration0        = ' + STRTRIM ( study_details.duration0, 1)
      fileout, unit=unit, '# duration1        = ' + STRTRIM ( study_details.duration1, 1)
      fileout, unit=unit, '# tracking         = ' + STRTRIM ( sciplan_details.tracking, 1)
      fileout, unit=unit, ''
  
      ; check whether reusable study or not

      IF series(0).reus EQ 'N' THEN BEGIN

        ; print relevant additional parameters

        fileout, unit=unit, '# Study not reusable'

        fileout, unit=unit, ''

      ENDIF

      ; print out flag study details

      IF flag_receiver THEN BEGIN

        PRINT, 'Study has associated flag study ID = ', STRTRIM ( flag_study_info.cdhsid, 1)
        PRINT, '                             index = ', STRTRIM ( flag_study_info.cdhsx, 1 )
        PRINT, ''

        fileout, unit=unit, '# study has associated flag study ID = ' + shorthex ( flag_study_info.cdhsid )
        fileout, unit=unit, '#                              index = ' + STRTRIM ( flag_study_info.cdhsx, 1)
        fileout, unit=unit, '# flag study has ' + flag_study_info.pointing + ' pointing' 
        fileout, unit=unit, ''

      ENDIF

      ; print out flag master details

      IF flag_master THEN BEGIN

        PRINT, 'Study is IEF flag master'
        PRINT, ''

        fileout, unit=unit, '# study is IEF flag master = ' + STRTRIM ( sciplan_details.flag_master, 1)
        fileout, unit=unit, ''

      ENDIF

    ENDIF ELSE BEGIN

      ; continuation table 

      fileout, unit=unit, '# CONTINUATION SERIES TABLE FILE'
      fileout, unit=unit, '# Created ' + STRTRIM ( utc, 1 )
      fileout, unit=unit, '# Version ' + STRTRIM ( sw_version, 1 )
      fileout, unit=unit, '# Table = ' + STRTRIM ( t + 1, 1 ) + ' of ' + STRTRIM (  series(t).nsubs, 1 )
      fileout, unit=unit, '# Lead CDHS id ' + STRTRIM ( series(0).cdhsid, 1)
      fileout, unit=unit, '# Lead CDHS index ' + STRTRIM ( series(0).cdhsx, 1)
      fileout, unit=unit, '# ID  = ' + STRTRIM ( series(t).id, 1)
      fileout, unit=unit, '# VAR = ' + STRTRIM ( series(t).var, 1)
      fileout, unit=unit, ''

    ENDELSE

    ; table header

    fileout, unit=unit, shorthex ( series(t).cdhsid ) + $
             '          # Series ID : ' + STRTRIM ( series(t).cdhsid, 1 )
    fileout, unit=unit, ''
  
    ; calculate length of table, include terminate or jump command
    ; NB length does not include the first two words in the table.
    ;    Running next sequence or terminating current sequence takes two words

    length = file_words(t)+2

    fileout, unit=unit, shorthex ( ISHFT ( 0, 8 ) OR length ) + $
             '          # Study ID / length : ' + '0' + ' / ' + STRTRIM ( length, 1 )          
    fileout, unit=unit, ''

    ; write out table body

    FOR k = file_pos(t), file_pos(t+1)-1 DO fileout, unit=unit, file_output( k )

    ; check if last table 

    IF t LT n_series - 1 THEN BEGIN

      ; jump to next series (NB needs an extra line space here because splitting study between 
      ; series may split a comment and a command in the table and dont want previous comment and 
      ; this next one without intervening line)

      fileout, unit=unit, ''
      fileout, unit=unit, '# Jump to next continuation table'
      fileout, unit=unit, ''

      fileout, unit=unit, 'CB5RUNS ' + shorthex( series(t+1).cdhsid) + $
               '  # run next series, index = ' + STRTRIM ( series (t+1).cdhsx, 1 )

      fileout, unit=unit, ''

    ENDIF ELSE BEGIN

      ; check termination required

      IF series(0).rpt EQ 'Y' THEN BEGIN

        ; study repeated indefinitely

        fileout, unit=unit, 'CB5RUNS ' + shorthex (series(0).cdhsid) + $
                 '  # repeat series, index = ' + STRTRIM ( series(0).cdhsx, 1 )
        fileout, unit=unit, ''

      ENDIF ELSE IF fseries.tt THEN BEGIN

        ; if next study time tagged then terminate series

        fileout, unit=unit, 'CB5ENDS' + '         # macro terminate sequence command'
        fileout, unit=unit, ''
   
      ENDIF ELSE BEGIN

        ; append jump to next series

        fileout, unit=unit, 'CB5RUNS ' + shorthex(fseries.cdhsid) + $
               '  # macro run next series, index = ' + STRTRIM ( fseries.cdhsx, 1 )
        fileout, unit=unit, ''

      ENDELSE

    ENDELSE

    ; close file and free logical unit 

    FREE_LUN, unit

    ; some debug info

    IF !debug THEN PRINT, 'Finished SERIES file'

  ENDFOR

END
