;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_raster_detail_default
;
; Purpose     : Generates default structure EIS_raster_detail.
;
; Explanation : Sets up default values for structure EIS_raster_detail.
;
; Use         : < s = EIS_CPT_raster_detail_default(nwindows,xadd,node) >
;
; Inputs      : nwindows : INT no of data extraction windows.
;               xadd     : INT parameter to modify data extaction windows.
;               node     : UINT indicating readout node.
;
; Opt. Inputs : None.
;
; Outputs     : s : STRUCTURE of type EIS_raster_detail.
;
; Opt. Outputs: None.
;
; Keywords    : SPECIAL         : FLAG indicating special window should be used.
;               AEC_TABLE       : FLAG if AEC used for raster
;               XRT_FLARE       : FLAG if XRT flare hunting used for raster.
;               EIS_FLARE       : FLAG if EIS flare hunting used for raster.
;               EIS_EVENT       : FLAG if EIS event hunting used for raster.
;               EXPOSURES       : LONARR[] sets up exposure times in ms.
;               EXPDELAYS       : LONARR[] sets up exposure delay times in ms.
;               SLITSLOT        : UINT slit/slot used 0-3.
;               HARDWARE_WINDOW : INTARR[4] new hardware window.
;               YSTART          : INT ystart for raster
;               NREPEATR        : INT no. of raster repeats
;               ASRC_STEP       : INT ASRC step size
;               ASRC_SKIP       : INT ASRC step frequency
;               MIRROR_POS      : INT mirror initial position
;               MIRROR_STEP     : INT mirror step size
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 29/09/04
;
; Modified    : Version 0.1, 23/11/04, MKC
;                 Added raster_description and linelist description tags.
;                 Removed raster_title.
;                 Added ASRC_skip.
;               Version 0.2, 28/02/05, MKC
;                 Removed EIS XRT flare response, EIS flare response, EIS event response and AEC control structures
;                 and replaced them with flags.
;                 Removed flat_field tag and changed no. of exposures range.
;               Version 0.3, 02/04/05, MKC
;                 Added variable no. of data extraction windows.
;                 Modified data extraction window with xadd.
;               Version 0.4, 15/04/05, MKC
;                 Modified default exposure times and no. of exposures.
;                 Added SPECIAL keyword and changed special hardware window.
;               Version 0.5, 24/06/05, MKC
;                 Changed INT values to UINT.
;                 Removed coarse mirror position and insert flag tags.
;                 Added event control flags.
;               Version 0.6, 29/06/05, MKC
;                 Added node argument..
;               Version 0.7, 08/09/05, MKC
;                 Added raster identifier.
;                 Added exposures.
;                 Added nmirror.
;                 Changed fine mirror step to UINT.
;                 Changed default fine mirror step to 1.
;               Version 0.8, 10/01/07, MKC
;                 Added SLITSLOT keyword.
;                 Added exposure delays tag and keyword.
;                 Removed number of exposures per raster position tag.
;               Version 0.9, 16/04/07, MKC
;                 Added hardware window keyword.
;                 Added ystart keyword.
;               Version 1.0, 26/02/08, MKC
;                 Added ASRC control and mirror movement keywords.
;                 Moved defaults for ASRC and mirror control.
;                 Used N_ELEMENTS NE 0 rather than KEYWORD_SET throughout.
;               Version 1.1, 13/06/08, MKC
;                 Changed so that ystart does not overwrite special hardware window ystart always.
;                 Changed so that hardware window does not affect special hardware window.
;
; Version     : 1.1, 13/06/08
;-
;**********************************************************

FUNCTION eis_cpt_raster_detail_default, nwindows, xadd, node, SPECIAL=special, $
  AEC_TABLE=aec_table, XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, EIS_EVENT=eis_event, $
  EXPOSURES=exposures, EXPDELAYS=expdelays, NMIRROR = nmirror, SLITSLOT=slitslot, HARDWARE_WINDOW=hardware_window, $
  YSTART=ystart, NREPEATR=nrepeatr, ASRC_STEP=asrc_step, ASRC_SKIP=asrc_skip, MIRROR_POS=mirror_pos, MIRROR_STEP=mirror_step

  ; generate structure

  s = {EIS_raster_detail}

  ; raster_description       : STRING mnemonic identifying the raster.

  s.raster_description = 'Default raster'

  ; linelist_description     : STRING mnemonic identifying the line list.

  s.linelist_description = 'Default linelist'+$
                           ', NWINDOWS='+eis_cpt_strtrim(nwindows)+$
                           ', XADD='+eis_cpt_strtrim(xadd)+$
                           ', NODE='+eis_cpt_strtrim(node)+$
                           ', SPECIAL='+eis_cpt_strtrim(KEYWORD_SET(special))+$
                           ', AEC='+eis_cpt_strtrim(KEYWORD_SET(aec_table))+$
                           ', XRT='+eis_cpt_strtrim(KEYWORD_SET(xrt_flare))+$
                           ', EIS='+eis_cpt_strtrim(KEYWORD_SET(eis_flare))+$
                           ', EVT='+eis_cpt_strtrim(KEYWORD_SET(eis_event))

  ; raster_id        : ULONG uniquely identifying this raster to the planning tools.
  ;                            Last 16 bits must not be 0 or 0xFFFF.
  ; set up a 'unique' raster identifier

  id = ULONG(nwindows) ; 5 bits

  id = id OR ISHFT(xadd,5) ; 10 bits

  id = id OR ISHFT(node,15) ; 2 bits

  id = id OR ISHFT(KEYWORD_SET(special),17) ; 1 bit

  id = id OR ISHFT(KEYWORD_SET(aec_table),18) ; 1 bit

  id = id OR ISHFT(KEYWORD_SET(xrt_flare),19) ; 1 bit

  id = id OR ISHFT(KEYWORD_SET(eis_event),20) ; 1 bit

  s.raster_id = id

  ; linelist_id              : ULONG used by planning tools.

  s.linelist_id = 0L

  ; duration                 : ULONG indicates the expected duration
  ;                            in seconds of the raster and raster repeats.

  s.duration = 0UL ; will be estimated later

  ; data                     : ULONG the amount of data in bytes expected
  ;                            from running the raster and raster repeats.

  s.data = 0 ; will be estimated later

  ; slit_slot                : UINT the slit slot to be used for the raster.
  ;                            Range 0-3.
  ;                               0 : slit width 1"
  ;                               1 : slot width 250"
  ;                               2 : slit width 2"
  ;                               3 : slot width 40"

  IF N_ELEMENTS(slitslot) NE 0 THEN s.slit_slot = slitslot

  ; fine_mirror_position     : UINT the initial fine mirror encoder position.
  ;                            See engineering parameters for range.

  IF N_ELEMENTS(mirror_pos) NE 0 THEN s.fine_mirror_position = mirror_pos

  ; fine_mirror_step         : UINT the fine mirror encoder step size between
  ;                            raster positions, Ensure fine mirror
  ;                            limit not exceeded. See engineering
  ;                            parameters for range.

  IF N_ELEMENTS(mirror_step) NE 0 THEN s.fine_mirror_step = mirror_step

  ; number_of_mirror_positions : UINT the number of raster positions,
  ;                              range 1-65535.

  IF N_ELEMENTS(nmirror) NE 0 THEN s.number_of_mirror_positions = nmirror

  ; NB exposures = [0L] is not valid

  exps = [500L, 1000L, 4500L, 0L, 0L, 0L, 0L, 0L]  ; 6 secs worth of exposures

  IF KEYWORD_SET(exposures) THEN BEGIN

    dummy = WHERE(exposures NE 0, nexp)

    IF nexp NE 0 THEN exps = exposures

  ENDIF

  ; exposure_times           : ULONG[8] exposure time for each exposure in
  ;                            the exposure loop in milliseconds,
  ;                            range 10-655350 in steps of 10 ms.
  ;                            NB  Any non-zero time gives an exposure.

  s.exposure_times = exps

  ; exposure_delays          : UINT[8] time delays between exposures in the exposure loop
  ;                            in milliseconds, range 1-65535.
  ;                            NB  Any non-zero time gives a delay.

  IF KEYWORD_SET(expdelays) NE 0 THEN s.exposure_delays = expdelays

  ; number_of_raster_repeats : UINT the number of times to repeat the raster,
  ;                            range 1-4095.

  IF N_ELEMENTS(nrepeatr) NE 0 THEN s.number_of_raster_repeats = nrepeatr

  ; data_compression_parameter : UINT selects the data compression scheme
  ;                              to be used, range 0-7 (tbc).

  s.data_compression_parameter = 0 ; no compression ?

  ;  ASRC_step                  : INT the fine mirror step performed at the
  ;                               start of each raster in order to compensate
  ;                               for Anti Solar Rotation Compensation.
  ;                               If no ASRC then 0.
  ;                               Range -127->+127.

  IF N_ELEMENTS(asrc_step) NE 0 THEN s.ASRC_step = asrc_step

  ; ASRC_skip                   : UINT defines how often the Anti Solar Rotation
  ;                               Compensation fine mirror step performed at the
  ;                               start of each raster is performed during raster
  ;                               repeats A value of 'n' implies ASRC step performed
  ;                               every 'n'th raster. If no ASRC then should be 0.
  ;                               Range 0-255.

  IF N_ELEMENTS(asrc_skip) NE 0 THEN s.ASRC_skip = asrc_skip

  ; XRT_flare_control_flag   : UINT flag indicating whether to respond to an XRT flare event.

  IF KEYWORD_SET(xrt_flare) THEN s.XRT_flare_control_flag = 1 ELSE s.XRT_flare_control_flag = 0

  ; EIS_flare_control_flag   : UINT flag indicating whether to generate an EIS flare event.

  IF KEYWORD_SET(eis_flare) THEN s.EIS_flare_control_flag = 1 ELSE s.EIS_flare_control_flag = 0

  ; EIS_event_control_flag   : UINT flag indicating whether to generate an EIS event

  IF KEYWORD_SET(eis_event) THEN s.EIS_event_control_flag = 1 ELSE s.EIS_event_control_flag = 0

  ; AEC_control_flag         : UINT flag indicating whether to use AEC control

  IF KEYWORD_SET(aec_table) THEN s.AEC_control_flag = 1 ELSE s.AEC_control_flag = 0

  ; special_hardware_window_flag : UINT flag defining whether to use the normal
  ;                                hardware window (0) or the special hardware window (1).

  IF KEYWORD_SET(special) THEN BEGIN
    s.special_hardware_window_flag = 1 ; special hardware window
  ENDIF ELSE BEGIN
    s.special_hardware_window_flag = 0 ; normal  hardware window
  ENDELSE

  ; special_hardware_window  : UINT[4] coordinates of special Camera hardware window :
  ;                              0 : X start (0-2147)
  ;                              1 : Y start (0-1023)
  ;                              2 : X length (1-2148)
  ;                              3 : Y length (1-1024)

  s.special_hardware_window = [50, 448, 1024, 128]

  ; check for ystart change

  IF KEYWORD_SET(ystart) THEN s.special_hardware_window[1]= ystart

  ; CAM_hardware_window      : UINT[4] defines the normal hardware readout window :
  ;                              0 : X start (0-2147)
  ;                              1 : Y start (0-1023)
  ;                              2 : X length (1-2148)
  ;                              3 : Y length (1-1024)
  ; values taken from FM_csg_prom_V1.txt KJR 03-09-03
  ; subject to adjustment of x length by one pixel (see MCRW)

  IF KEYWORD_SET(hardware_window) THEN s.normal_hardware_window = hardware_window ELSE s.normal_hardware_window = [50, 256, 1024, 512]

  ; number_of_data_extraction_windows : UINT the number of data extraction windows. Range 1-25.

  s.number_of_data_extraction_windows = nwindows

  ; data_extraction_windows  : Structure array[25] of type EIS_data_extraction_window.

  FOR k = 0, nwindows-1 DO s.data_extraction_windows[k] = EIS_CPT_data_extraction_window_default(xadd, node)

  ; if EIS flare control used set flag on first window

  IF s.EIS_flare_control_flag THEN s.data_extraction_windows[0].EIS_flare_line_flag = 1

  ; if EIS event control used set flag on first window

  IF s.EIS_event_control_flag THEN s.data_extraction_windows[0].EIS_event_line_flag = 1

  ; if AEC table used set flag on first window

  IF s.AEC_control_flag THEN s.data_extraction_windows[0].EIS_AEC_line_flag = 1

  ; estimate duration and data volume

  eis_cpt_raster_detail_estimates, s

  ; return structure

  RETURN, s

END