;+
; Project     :	SOHO - CDS
;
; Name        :	ADD_IEF
;
; Purpose     :	Adds an IEF flag parameter data set to the IEFTAB and IEFWIN databases.
;
; Explanation : Adds the parameters which define the flag generation scheme 
;               to the IEFTAB and IEFWIN databases. Assigns the ID numbers.
;
; Use         : < result = add_ief ( def ) >
;
; Inputs      : def = anonymous structure containing IEF database values.
;                  def.ieftab_id  = I*2      IEFTAB database ID.
;                                            Initially, this is zero.  It is updated by this routine to
;				             reflect the ID number in the database.
;                  def.desc       = C*65     Description of event generation scheme.
;                  def.upp_era    = 4(I*2)   Upper limit for each ERA measure (unsigned INT16)
;                  def.low_era    = 4(I*2)   Lower limit for each ERA measure (unsigned INT16)
;                  def.discrim    = I*2      Discriminator level (unsigned INT16)
;                  def.ev_type    = I*2      Event type (4 bits)
;                  def.pmode      = I*2      Pointing mode : NIS=0, VF=1, GIS=2 (2 bits)
;                  def.cmode      = I*2      Centroid mode : POS=0, NEG=1, ABS=2 (2 bits)
;                  def.c_banda    = I*2      Slit centre band A (INT16)
;                  def.c_bandb    = I*2      Slit centre band B (INT16)
;                  def.c_row      = I*2      Central row (INT16)
;                  def.detector   = C*1      'N'= NIS, 'G'= GIS.
;                  def.wins       =  structure array containing window info
;                    wins.iefwin_id  = I*2    Window ID.
;                                             If this is non-zero then window will be added to IEFWIN 
;                                             database and window ID set.
;                    wins.win_name   = C*40   Window name
;                    wins.ev_desc    = C*65   Description of event detection scheme employed
;                    wins.win_def    = 4(I*2) Window definition ( xstart, ystart, xsize, ysize )
;                    wins.px_offset  = I*2    Pixel offset defining ROI around peak pixel.
;                    wins.exp_era    = 4(I*2) Expected values for each ERA in window.
;                    wins.dev_era    = 4(I*2) Deviations for each ERA in window.
;                    wins.wgt_era    = 4(I*2) Weights for each direct ERA in window.
;                    wins.wgt_dera   = 4(I*2) Weights for each differential ERA in window.
;
; Outputs     :	Returns database ID no. in input structure.
;        	The result of the function is a logical value representing
;		whether or not the operation was successful, where 1 is
;		successful and 0 is unsuccessful.
;
; Keywords    :	
;       ERRMSG    = If defined and passed, then any error messages will be
;                   returned to the user in this parameter rather than
;                   depending on the MESSAGE routine in IDL.  If no errors are
;                   encountered, then a null string is returned.  In order to
;                   use this feature, ERRMSG must be defined first, e.g.
;
;                       ERRMSG = ''
;                       Result = ADD_IEFWIN( ERRMSG=ERRMSG, ... )
;                       IF ERRMSG NE '' THEN ...
;
; Restrictions:	Only this routine can be used to definitions to
;		the database.  Modifying the database by hand could corrupt its
;		integrity.
;
;		The data types and sizes of the structure elements must match
;		the definitions in the database.  The string lengths must not
;		exceed the lengths defined in the database.
;
; Side effects:	Once a definition has been added to the database,
;		it must never be removed.
;
; Calls       :	add_ieftab, add_iefwin.
;                
; Common      :	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	Adapted from add_datawin.
;
; Written     :	Version 0.0, Martin Carter, RAL, 18/10/95
;
; Modified    :	Version 0.1, MKC
;                            Added logic to deal with IEFWIN_ID set
;
; Version     :	Version 0.1, 7/11/95
;-
;**********************************************************

FUNCTION add_ief, def, ERRMSG=errmsg
;
	ON_ERROR, 2
;
;  Make sure that the ID keyword is initialized.
;
	ieftab_id = -1
;
;  Initialize RESULT to represent non-success.  If the routine is successful,
;  this value will be updated below.
;
	result = 0
;
;  Check the input parameters
;
        IF N_PARAMS() NE 1 THEN BEGIN
           message = 'Syntax:  Result = ADD_IEF ( def )'
	   GOTO, HANDLE_ERROR
        ENDIF

;  Make sure that the data window list ID number is less than or equal to zero.
;
	IF def.ieftab_id GT 0 THEN BEGIN
           message= 'The ID number must be LE 0. '+$
                    'The final value is assigned by this routine'
           GOTO, HANDLE_ERROR
	ENDIF

; test no. of windows

  IF (N_ELEMENTS(def.wins) LT 1 OR N_ELEMENTS(def.wins) GT 3 ) NE 0 THEN BEGIN
     message = 'No. of IEF windows must be in range 1-3'
     GOTO, HANDLE_ERROR
  ENDIF

;  Set up table structure

   ieftab = { ieftab_id  : def.ieftab_id,          $
              desc       : def.desc,               $
              upp_era    : def.upp_era,            $
              low_era    : def.low_era,            $
              discrim    : def.discrim,            $
              ev_type    : def.ev_type,            $
              pmode      : def.pmode,              $
              cmode      : def.cmode,              $
              c_banda    : def.c_banda,            $
              c_bandb    : def.c_bandb,            $
              c_row      : def.c_row,              $
              detector   : def.detector,           $
              n_iefwins  : FIX( N_ELEMENTS ( def.wins) ), $
              iefwin_ids : INTARR(3) }

;  Add the IEF windows to the IEFWIN database if IEFWIN_ID not already set

   FOR w = 0, N_ELEMENTS ( def.wins ) - 1 DO BEGIN

      ; set up window structure

      iefwin =  { iefwin_id : def.wins(w).iefwin_id, $
                  win_name  : def.wins(w).win_name,  $
                  ev_desc   : def.wins(w).ev_desc,   $
                  detector  : def.detector,          $
                  win_def   : def.wins(w).win_def,   $
                  px_offset : def.wins(w).px_offset, $
                  exp_era   : def.wins(w).exp_era,   $
                  dev_era   : def.wins(w).dev_era,   $
                  wgt_era   : def.wins(w).wgt_era,   $
                  wgt_dera  : def.wins(w).wgt_dera }

      ; check if iefwin ID needs setting

      IF iefwin.iefwin_id EQ 0 THEN BEGIN

        res = add_iefwin ( iefwin, ERRMSG=errmsg ) 

        IF res EQ 0 THEN BEGIN
          message = 'IEFWIN error'
          GOTO, HANDLE_ERROR
        ENDIF

      ENDIF

      ieftab.iefwin_ids(w) = iefwin.iefwin_id

   ENDFOR

;  Add the IEF table info to the IEFTAB database

   res = add_ieftab ( ieftab, ERRMSG=errmsg )

   IF res EQ 0 THEN BEGIN
      message = 'IEFTAB error'
      GOTO, HANDLE_ERROR
   ENDIF

;  Update the ID numbers in the structure.  Signal success.
;
   def.ieftab_id = ieftab.ieftab_id
   def.wins.iefwin_id = ieftab.iefwin_ids(0:ieftab.n_iefwins-1)

   result = 1
   GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
	IF N_ELEMENTS ( errmsg ) NE 0 THEN errmsg = 'ADD_IEF: ' + message $
		ELSE MESSAGE, message, /CONTINUE
;
;  Close the database, and return whether the routine was successful or not.
;
FINISH:
;
	RETURN, result

	END