;+
; Project     :	SOHO - CDS
;
; Name        :	DO_ENGINEERING
;
; Purpose     :	Creates the series table for an engineering study.
;
; Explanation :	Studies labelled with a study_id of zero are engineering studies.
;               This routine creates the series table which will be loaded 
;               onto the CDHS. The table is created by copying an existing 
;               engineering file corresponding to the particular study variation
;               and appending a CDHS ID and a few comments. the series file name
;               gives the CDHS index as usual.
;
; Use         : <do_engineering, scipla_details, study_details, series, fseries>
;
; Inputs      : 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;
;               series          = structure array giving information on series
;                                 tables;
;               fseries         = structure giving information on following series
;               
; Opt. Inputs : None.
;
; Outputs     : Writes commands to a series file. Updates series.fstate.
;               NB must pass series by reference.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	get_ops_pos, get_ops_delay, get_slitn_delay, shorthex, fileout.
;                
; Common      :	None.
;
; Restrictions:	Any change to commands output requires a change to wr_series_file.
;
; 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, 23/1/96
;                 Explicitly set fstate to home position.
;             	Version 0.2, MKC, 24/2/97
;                 Added fseries to argument list.
;                 Added CB5ENDS or CB5RUNS command explicitly to series table file.
;                 Adopted rule that engineering study must not change pointing or slit number.
;             	Version 0.3, MKC, 21/4/97
;                 Copied istate to fstate.
;
; Version     :	Version 0.3, 21/4/97
;-
;**********************************************************

PRO do_engineering, sciplan_details, study_details, series, fseries

    ; set software version no.

    sw_version = 0.0

    ; NB series should not be an array 

    ; set up final state slit number and solarx/y

    series.fstate = series.istate

    ; get file name

    filename = concat_dir ( '$CDS_CP_CVTFILES_W' , 'series_' + STRTRIM ( series(0).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

    fileout, unit=unit, '# SERIES ENGINEERING 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, ''

    ; 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, '# 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

    ; table header

    fileout, unit=unit, shorthex ( series(0).cdhsid ) + $
             '          # Series ID : ' + STRTRIM ( series(0).cdhsid, 1 )
    fileout, unit=unit, ''
  
    ;  set up file name based on studyvar ID

    engfile = concat_dir ( GETENV ( 'CDS_CP_ENG' ), 'engineering_' + STRTRIM ( series(0).var, 1) + '.dt' )

    ; close file and free logical unit 

    FREE_LUN, unit

    ; some debug info

    IF !debug THEN PRINT, 'Finished SERIES file'

    ; copy table body, including study id and length from file

    IF !debug THEN PRINT,  'cat ' + engfile + ' >> ' + filename

    SPAWN, 'cat ' + engfile + ' >> ' + filename

    ; re-open file

    OPENW, unit, filename, /GET_LUN, /APPEND

    ; 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

    ; close file and free logical unit 

    FREE_LUN, unit

END

