;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_WRT_LLIST()
;               
; Purpose     : Store the line list definition into the database.
;               
; Explanation : Transfers from the TPLAN internal database to the one
;               required by the database access routine and add the
;               definition to the database if it is new. Returns the ID
;               of the line list.
;               
; Use         : id = tp_wrt_llist(tp_obs, msg_widg)
;    
; Inputs      : tp_obs   - the standard TPLAN structure.
;               msg_widg - widget ID for messages
;               flag_state - says whether flag windows are included or not
;               base       - base widget in which to put error message widget.
;
; Opt. Inputs : None
;               
; Outputs     : Function returns ID of line list (negative if a problem)
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : ADD_LINELIST()
;               CHK_LINELIST()
;
; Common      : None
;               
; Restrictions: Only for internal TPLAN use.
;               
; Side effects: Line list database could be updated.
;               
; Category    : Planning, technical
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 25-Aug-94
;               
; Modified    : Check for existence within database first.  CDP, 13-Oct-94
;               Handle case of no lines according to new definitions.
;                                                           CDP, 22-Feb-95
;               Don't force any particular TITLE.  CDP, 2-Mar-95
;               Take account of flag lines.        CDP, 10-Nov-95
;               Change error message if line list exists under another guise.
;                                                  CDP, 14-nov-95
;
;
; Version     : Version 5, 14-Nov-95
;-            

function tp_wrt_llist, tp_obs, msg_widg, flag_state, base

;
;  how many lines involved - see later
;
num = tp_obs.line_list.n_lines

;
;  In mk_raster if no lines are defined then the extraction windows must
;  be being defined by the 'default extraction flags' eg All NIS1 or NIS2 or
;  full CCD or just background windows. In this case the ID is just left at
;  zero.
;
if num eq 0 then begin
   widget_control, msg_widg, set_val='No line list to write.',/append
   return,num
endif

;
;  real number of lines depends whether flag lines are involved or not
;
if flag_state eq 0 then begin
   n = where(tp_obs.data_win2(0:num-1).win_flag eq 0,count)
   if count gt 0 then use_num = n  else return,0
endif else begin
   use_num = indgen(num)
endelse
   

;
;  create the structures needed for database access
;

lines = {line,  $
         linename: ' ',$
         wavelnth: 0.0,$
         gr_order:   0,$
         cen_pix:    0,$
         waveband:   0}
lines = replicate(lines,n_elements(use_num))

db_ll = {ll_id: 0, $
         detector: ' ',$
         ll_desc:  ' ',$
         lines:    lines}

db_ll.detector = tp_obs.line_list.detector
db_ll.ll_desc  = tp_obs.line_list.ll_desc
for i=0,n_elements(use_num)-1 do begin
   db_ll.lines(i).linename = tp_obs.line_list2(use_num(i)).linename
   db_ll.lines(i).wavelnth = tp_obs.line_list2(use_num(i)).wavelnth
   db_ll.lines(i).gr_order = tp_obs.line_list2(use_num(i)).gr_order
   db_ll.lines(i).cen_pix  = tp_obs.line_list2(use_num(i)).cen_pix
   db_ll.lines(i).waveband = tp_obs.line_list2(use_num(i)).waveband
endfor


;
;  check whether definition already exists
;
status = chk_linelist(db_ll)
if status ge 0 then begin
   widget_control, msg_widg, set_val=string('Data base entry for this ',$
                             'line list already exists. ID = ',$
                              status,form='(2a,i3)'), /append
   
   tp_obs.line_list.ll_id  = status
   tp_obs.line_list2.ll_id = status
   tp_obs.data_win.ll_id   = status
   tp_obs.raster_v.ll_id   = status

   get_linelist,status,temp
   temp = temp.ll_desc
   if temp ne db_ll.ll_desc then begin
      bell,3
      xack,'Line list already exists with description <'+strtrim(temp,2)+'>',$
            xoff=500,yoff=500,group=base,/modal
      tp_obs.line_list.ll_desc  = temp
   endif   

   return, status
endif

;
;  try and write this line list to the database (note ID is already zero 
;  by definition so we are OK)
;
errmsg = ''
status = add_linelist(db_ll, errmsg=errmsg)
if errmsg ne '' then begin
   widget_control, msg_widg, set_val=errmsg, /append
   status = -1
endif else begin
   widget_control, msg_widg, set_val=string('New linelist added with ID = ',$
                                             db_ll.ll_id), /append
   tp_obs.line_list.ll_id  = db_ll.ll_id
   tp_obs.line_list2.ll_id = db_ll.ll_id
   tp_obs.raster_v.ll_id   = db_ll.ll_id
   tp_obs.data_win.ll_id   = db_ll.ll_id
endelse


return, status


end   
