;+
; Project     :	SOHO - CDS
;
; Name        :	FILEOUT
;
; Purpose     :	Writes out CDS SERIES tables to array or file.
;
; Explanation :	A study may span a number of series tables. In order
;               to cope with this the study is first written to an array
;               to calculate the number of tables needed. This routine
;               directs the output to either an array or to a file.
;               Allows a maximum of 60-12=48 series tables in a study.
;
; Use         : <fileout, str, WORDS=words, UNIT=unit>
;
; Inputs      : str = character string array forming lines to be output;
;
; Opt. Inputs : words : number of command words formed by line;
;               unit  : logical unit number for file output.
;
; Outputs     : Writes data to file or to common block.
;
; Opt. Outputs:	version = software version number if keyword set.
;
; Keywords    : WORDS = Character string containing software version number.
;                       If set then updates number of words used in table.
;               UNIT  = Integer giving logical unit number for file output.
;                       If set then routine outputs line to file rather than array.
;
; Calls       :	None.
;                
; Common      :	fileoutblock :
;                 file_words  : INTARR(n) Number of command words written to each 
;                                         series table in study.
;                 file_pos    : INTARR(n+1) Position in file_output of lines for each series
;                                         table in study. 
;                 file_output : STRARR(m) Array containing lines written to 
;                                         series tables in study.
;                               n is no. of series tables which are daisy chained 
;                               together to form a single study.
;                               m is no. of lines of text written for study
;               NB initial values are set up in wr_series_file.
;
; 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.
;                       0.1, Martin Carter, RAL, 16/02/95
;                            Added check on table lengths nad changed warning wording
;                       0.2, MKC, 18/5/95
;                            Modified warning message slightly.
;                       0.3, MKC, 1/8/95
;                            Made warning slightly more comprehensible.
;                       0.4, MKC, 11/8/95
;                            Removed warning entirely.
;                       0.5, MKC, 4/9/95
;                            Modified to deal with RDY BOTH command in series table.
;                       0.6, MKC, 28/9/95
;                            Modified so that does increments arrays differently.
;                       0.7, MKC, 2/10/95
;                            Modified treatment of RDYBOTH
;                       0.8, MKC, 8/12/95
;                            Modified use of file_pos.
;                       0.9, MKC, 2/2/96
;                            Allowed str input to be array,
;
; Version     :	Version 0.9, 2/2/96
;-
;**********************************************************

PRO fileout, str, WORDS=words, UNIT=unit

  COMMON fileoutblock, file_words, file_pos, file_output

  ; get no. of tables so far

  n = N_ELEMENTS(file_words)

  IF KEYWORD_SET(words) THEN BEGIN

    ; NB series table is 128 words long
    ;    table ID, study ID and length, and checksum take 3 words
    ;    terminate series or jump to next series take two words
    ;    leaves 123 words 

    IF file_words(n-1)+words GT 123 THEN BEGIN

      ; overflowed current table, start new one

      IF n GE 48 THEN MESSAGE, 'STUDY TOO LARGE, TABLE ID DATABASE NOW INVALID'

      file_words = [file_words, 0] 
      file_pos   = [file_pos, file_pos(n)]
      n          = n + 1

    ENDIF

    ; increment words in table

    file_words(n-1) = file_words(n-1) + words
      
  ENDIF 

  IF KEYWORD_SET(unit) THEN BEGIN

    FOR k = 0, N_ELEMENTS(str)-1 DO PRINTF, unit, str(k)

  ENDIF ELSE BEGIN

    file_output = [file_output, str]

    file_pos(n) = file_pos(n) + N_ELEMENTS(str)

  ENDELSE

END
