;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_WRT_IEF()
;               
; Purpose     : Write IEF definition to the IEF database.
;               
; Explanation : Takes the data extraction windows defined as flag windows in 
;               MK_RASTER and writes them out to the IEF data base.
;               
; Use         : Only within MK_RASTER
;               status = tp_wrt_ief(tp_obs, info_line)
;    
; Inputs      : tp_obs  -  the internal mk_raster structure
;               
; Opt. Inputs : None
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : DEF_IEF, ADD_IEF
;
; Common      : None
;               
; Restrictions: Only updates the window definitions (size, position) and
;               does not define the event generation statistic.
;               
; Side effects: None
;               
; Category    : Technical planning, IEF
;               
; Prev. Hist. : None
;
; Written     : C D Pike, 8-Nov-95
;               
; Modified    : Check for existence of ief_id tag to cater for older 
;               structures.   CDP, 19-Jan-96
;
;
; Version     : Version 2, 19-Jan-96
;-            

function tp_wrt_ief, tp_obs, info_line, base

;
;  how many windows are designated as flags?
;
win_id = where(tp_obs.data_win2.win_flag gt 0, count)
if count eq 0 then begin
   widget_control, info_line, set_v='No flag windows to store', /append
   if tag_exist(tp_obs.raster_v,'ief_id') then tp_obs.raster_v.ief_id = 0
   return,1
endif
nwin = n_elements(win_id)
   
;
;  get the default definition
;
def = def_ief(nwin)


;
;  get the description
;
xinput,text,'Enter IEF description [max 50 chars]',$
             xoff=500,yoff=500,status=status,/modal,group=base
if status then begin
   def.desc = strmid(text,0,49)
endif else begin
   get_utc,utc
   def.desc = anytim2cal(utc)
endelse

;
;  update the window locations
;
for i=0,nwin-1 do begin
   def.wins(i).win_def = tp_obs.data_win2(win_id(i)).win_def
endfor

;
;  add to the IEF database
;
ok = add_ief(def)

if ok then begin
    tp_obs.raster_v.ief_id = def.ieftab_id
    widget_control, info_line, /append, $
                    set_v='New IEF ID = '+string(def.ieftab_id,form='(i3)')
    print,'New IEF ID = ',def.ieftab_id
    return,1
endif else begin
    return,-1
endelse

end

