;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_WRT_VRAST()
;               
; Purpose     : Store the raster variation definition into the database.
;               
; Explanation : Checks whether the current raster variation definition
;               already exists in the database, if not then create a new
;               entry and report new ID.
;               
; Use         : id = tp_wrt_vrast(tp_obs, msg_widg)
;    
; Inputs      : tp_obs   - the standard TPLAN structure.
;               msg_widg - widget ID for messages.
;               
; Opt. Inputs : None
;               
; Outputs     : Function returns ID of raster variation (<0 if a problem)
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : ADD_V_RASTER()
;               CHK_V_RASTER()
;
; Common      : None
;               
; Restrictions: Only for internal TPLAN use.
;               
; Side effects: Raster variation database could be updated.
;               
; Category    : Planning, technical
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 13-Oct-94
;               
; Modified    : Allow Line list ID to be zero (ie no spectral lines)
;                                                       CDP, 22-Feb-95
;               Fix bad status return, CDP, 2-Mar-95
;               Add calculation of raster duration before storing.  CDP, 16-Oct-97
;               Add new LOW++ telemetry rate.    CDP, 12-Dec-97
;
; Version     : Version 5, 12-Dec-97
;-            

function tp_wrt_vrast, tp_obs, msg_widg

;
;  check whether have necessary IDs loaded
;
text = ''
if tp_obs.raster_v.ras_id le 0 then begin
   text = 'Fundamental raster ID not defined.'
endif
if tp_obs.raster_v.ll_id lt 0 then begin
   text = 'Line list ID not defined.'
endif
if tp_obs.raster_v.dw_id le 0 then begin
   text = text + ' Data window ID not defined.'
endif
if text ne '' then begin 
   widget_control, msg_widg, set_val=text,/append
   return, -1
endif

;
;  get up to date estimate of duration, in case it's not been done manually
;
case tp_obs.raster_v.tel_rate of
     'H': tpi = 103.5
     'M': tpi = 205
     'P': tpi = 717.1
     'L': tpi = 1249
    else: tpi = 205
endcase
tp_obs.raster_v.duration = raster_dur(ras=tp_obs,tpi=tpi)

;
;  check whether definition already exists
;
status = chk_v_raster(tp_obs.raster_v)

if n_elements(status) gt 1 then begin


   widget_control, msg_widg,/append,$
        set_val=string('Raster variation definition already exists.',$
                       'raster  ID = ',status(0),' variation ID = ',$
                        status(1),form='(2a,i3,a,i3)')
;
;  if the raster IDs agree then give this variation the same id
;  and return
;
        if status(0) eq tp_obs.raster_v.ras_id then begin
           tp_obs.raster_v.ras_var = status(1)
           status = 1
        endif else begin
;
;  if raster id's are different then treat this as a completely new
;  variation of the current raster ID
;          
           tp_obs.raster_v.ras_var = 0
           errmsg = ''
           temp = tp_obs.raster_v
           status = add_v_raster(temp, errmsg=errmsg)
           tp_obs.raster_v = temp
           if errmsg ne '' then begin
              widget_control,msg_widg,set_val=errmsg,/append
              status = -1
           endif
           if status then begin
              widget_control,msg_widg,/append,$
                 set_v=string('Added this as variation ID ',$
                               tp_obs.raster_v.ras_var,$
                              ' of the current fundamental raster',$
                               form='(a,i2,a)')
           endif
        endelse

endif else begin

;
;  this variation not previously known....
;

   errmsg = ''
   temp = tp_obs.raster_v
   temp.ras_var = 0
   status = add_v_raster(temp, errmsg=errmsg)

   if errmsg ne '' then begin
      widget_control, msg_widg, set_val=errmsg,/append
      status = -1
   endif else begin
      widget_control, msg_widg,/append,$
                      set_val=string('Variation stored with ID = ',temp.ras_var)
      tp_obs.raster_v.ras_var = temp.ras_var
   endelse

endelse

return, status


end   
