;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_RUN_RASTER
;
; Purpose     : Sets up MSSL command.
;
; Explanation : Sets up raster comand.
;               Uses no. of raster/raster number for raster ID.
;
; Use         : < bytes = eis_cpt_run_raster( engineering_parameters, raster_detail, linelist ) >
;
; Inputs      : engineering_parameters  : STRUCTURE of type EIS_engineering_parameters.
;               raster_detail           : STRUCTURE of type EIS_raster_detail.
;               linelist                : INT linelist table index.
;
; Opt. Inputs : None.
;
; Outputs     : bytes  : BYTARR[23] command.
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 26/11/04
;
; Modified    : Version 0.0, 26/11/04, MKC
;               Version 0.1, 02/03/05, MKC
;                 Changed event response tags in raster to control flags.
;               Version 0.2, 11/04/05, MKC
;                 Corrected bits used for R/O nodes and science operations control.
;                 Reversed order of readout nodes.
;                 Added normal or special readout sequence ID.
;               Version 0.3, 15/04/05, MKC
;                 Changed format of !EIS_CPT_COMMANDS.
;               Version, 0.2, 27/04/05, MKC
;                 Removed !EIS_CPT_COMMANDS. Put in command directly.
;               Version 0.3, 04/07/05, MKC
;                 Corrected Raster ID.
;                 Changed raster_number and number of rasters arguments to raster_id.
;               Version 0.4, 12/01/07, MKC
;                 Removed number of exposures per raster position tag from raster detail.
;               Version 0.5, 17/04/07, MKC
;                 Removed raster id argument.
;                 Set raster id internally.
;               Version 0.6, 6/9/07, JAR
;                 Modified ASRC handling
;               Version 0.7, 22/01/08, MKC
;                 Added big-endian comments.
;
; Version     : 0.7, 22/01/08
;-
;**********************************************************

FUNCTION eis_cpt_run_raster, engineering_parameters, raster_detail, linelist

  ; get number of exposures per raster position

  dummy = WHERE(raster_detail.exposure_times NE 0, nexp)

  ; set up command array

  command = BYTARR(23)

  ; set up run raster command BC1

  command[0] = '86'XB

  ; raster ID
  ; use number of rasters/raster number

  command[1:2] = eis_cpt_set_raster_id(raster_detail.raster_id)

  ; fine mirror initial position

  command[3:4] = eis_cpt_uinttobytes(raster_detail.fine_mirror_position)

  ; loop counter

  command[5:6] = eis_cpt_uinttobytes(raster_detail.number_of_mirror_positions)

  ; data compression parameters

  command[7:8] = eis_cpt_uinttobytes(raster_detail.data_compression_parameter)

  ; OCB

  command[9:10] = BYTE(engineering_parameters.CAM_readout_parameters.On_Chip_Binning)

  ; CAM CSG Flush Sequence ID
  ; NB MSByte first (big-endian)

  command[11] = BYTE(engineering_parameters.CAM_readout_parameters.CAM_CSG_flush_sequence_ID)

  ; number of CCD flushes
  ; NB LSByte last (big-endian)

  command[12] = BYTE(engineering_parameters.CAM_readout_parameters.flush_CCDs)

  ; number of exposures per raster position
  ; NB MSByte first (big-endian)

  command[13] = BYTE(nexp)

  ; ASRC control parameters
  ; NB LSByte last (big-endian)
  ; JAR 6-Sep-2006 - corrected bug : bit order wrong way around

  command[14] = ABS(raster_detail.ASRC_step)

  IF raster_detail.ASRC_step LT 0 THEN command[14] = command[14] OR '80'XB

  ; R/O nodes and number of raster repeats
  ; NB LSByte last (big-endian)

  command[15:16] = eis_cpt_uinttobytes(raster_detail.number_of_raster_repeats)

  ; set R/O nodes
  ; NB MSByte first (big-endian)

  IF engineering_parameters.CAM_readout_parameters.Readout_nodes[0] NE 0 THEN command[15] = command[15] OR '10'XB

  IF engineering_parameters.CAM_readout_parameters.Readout_nodes[1] NE 0 THEN command[15] = command[15] OR '20'XB

  IF engineering_parameters.CAM_readout_parameters.Readout_nodes[2] NE 0 THEN command[15] = command[15] OR '40'XB

  IF engineering_parameters.CAM_readout_parameters.Readout_nodes[3] NE 0 THEN command[15] = command[15] OR '80'XB

  ; ASRC skip
  ; NB MSByte first (big-endian)

  command[17] = raster_detail.ASRC_skip

  ; CAM CSG R/O sequence ID
  ; NB LSByte last (big-endian)

  IF raster_detail.special_hardware_window_flag THEN BEGIN

    command[18] = engineering_parameters.CAM_readout_parameters.CAM_CSG_special_readout_sequence_ID

  ENDIF ELSE BEGIN

    command[18] = engineering_parameters.CAM_readout_parameters.CAM_CSG_normal_readout_sequence_ID

  ENDELSE

  ; mirror step size

  command[19:20] = eis_cpt_uinttobytes(raster_detail.fine_mirror_step)

  ; line list
  ; NB MSByte first (big-endian)

  command[21] = BYTE(linelist)

  ; science operations control
  ; NB LSByte last (big-endian)

  IF raster_detail.XRT_flare_control_flag THEN command[22] = command[22] OR '20'XB

  IF raster_detail.AEC_control_flag       THEN command[22] = command[22] OR '10'XB

  IF raster_detail.EIS_event_control_flag THEN command[22] = command[22] OR '08'XB

  IF raster_detail.EIS_flare_control_flag THEN command[22] = command[22] OR '04'XB

  ; return command

  RETURN, command

END





