;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_RECALC_DEW
;               
; Purpose     : Recalculate the data extraction (and VDS) windows.
;               
; Explanation : Taking the current lines and detector parameter setup,
;               recalculate the data extraction windows and the VDS 
;               hardware windows if appropriate.
;
; Use         : Only within TPLAN
;
; Inputs      : mode - mode of last event in tplan, to check for exits.
;                      all others in common.
;               
; Opt. Inputs : None
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : TP_G(V)WIN_LOGIC
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Technical planning.
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 26-Sep-94
;               
; Modified    : Add CCD readout times etc.                   CDP, 28-Nov-94
;               Cut out writing ll_desc to text widget.      CDP, 24-Mar-95
;               Transferred duration calculation to calc_ras_dur. 
;                                                            CDP, 10-May-95
;               Limit possible number of dex windows to 48 (GIS) and 24 (VDS)
;               and number of VDS hardware windows to 10.    CDP, 31-May-95
;
;               Cater for flag windows.                      CDP, 26-Jun-95
;               Cut out flash message for too many windows.  CDP, 20-Jul-95
;               Allow descriptions to be 64 chars long.      CDP, 17-Oct-95
;               Change call to tp_get_desc.                  CDP, 07-Nov-95
;               Include curr_lheight in call to tp_get_desc. CDP, 10-Apr-96
;               Expand format for raster duration widget.    CDP, 28-Apr-96
;               Cater for new raster duration calculation.   CDP, 23-Sep-97
;               Use RASTER_DUR instead of CALC_RAS_DUR for 
;                   time2dump calculation.                   CDP, 12-Dec-97
;
; Version     : Version 12, 12-Dec-97
;-            

pro tp_recalc_dew, mode

;
;  communications
;
@tplan_com

;
;  Load the line window details into the output structure
;
tp_obs.line_w = line_window
tp_update_lines, tp_obs


;
;  clear structures (data windows only).  First save the window
;  definitions so that if they are the same on exit then the dw_id
;  can be retained.
;
win_in = tp_obs.data_win2.win_def
tp_clear_lines, /win


;
;  First for NIS
;
case tp_obs.raster_p.detector of

  'N': begin
;
;    load background windows
;
          back = tp_obs.vds_backwin.win_def
          tp_vwin_logic, tp_obs, curr_lheight, curr_lwidth, vds_extension, $ 
                         nis_def_ext, back, dew, vdsew, dew_name, dew_flag

 
          n = where(dew(*,3) gt 0)
          if n(0) ge 0 then begin
             if max(n) ge 24 then begin
                if mode ne 'EXIT' then begin
                  bell
                  widget_control, info_line,$
                      set_v='Too many data extraction windows.',/app
                  widget_control, wdw_info_2,set_val='Too many windows'
                endif
                tp_obs.vds_win(*).vwin_ind = 0
                tp_obs.vds_win(*).vwin_def(*,*) = 0
                tp_obs.data_win2(*).win_ind = 0
                tp_obs.data_win2(*).win_def(*,*) = 0
                tp_obs.data_win2(*).win_name = ''
                tp_obs.data_win2(*).win_flag = 0
                return
             endif
             for i=0,max(n) do begin
               tp_obs.data_win2(i).win_ind = i
               tp_obs.data_win2(i).win_def = dew(i,*)
               tp_obs.data_win2(i).win_name = dew_name(i)
               tp_obs.data_win2(i).win_flag = dew_flag(i)
             endfor
          endif
          tp_obs.data_win.detector = 'N'
        
        
          tp_obs.data_win.ll_id = tp_obs.raster_v.ll_id
          tp_obs.data_win.n_windows = max(n) + 1
          if vds_extension and not nis_def_ext(2) then begin
             tp_obs.data_win2(max(n)-3:max(n)).win_name='Background'
          endif
        
          n = where(vdsew(*,3) gt 0)
          if n(0) ge 0 then begin
             if max(n) ge 10 then begin
                if mode ne 'EXIT' then begin
                  flash_msg, info_line,'Too many VDS hardware windows.',/app
                  widget_control, wdw_info_2,set_val='Too many windows'
                endif
                tp_obs.vds_win(*).vwin_ind = 0
                tp_obs.vds_win(*).vwin_def(*,*) = 0
                tp_obs.data_win2(*).win_ind = 0
                tp_obs.data_win2(*).win_def(*,*) = 0
                tp_obs.data_win2(*).win_name = ''
                tp_obs.data_win2(*).win_flag = 0
                return
             endif
             for i=0,max(n) do begin
               tp_obs.vds_win(i).vwin_ind = i
               tp_obs.vds_win(i).vwin_def = vdsew(i,*)
             endfor
          endif
       end
;
;  then for GIS
;
  'G': begin
         tp_gwin_logic, tp_obs, curr_lwidth, gis_def_ext, $
                        dew, dew_name, dew_flag
         n = where(dew(*,3) gt 0)
         if n(0) ge 0 then begin
            if max(n) ge 48 then begin
                if mode ne 'EXIT' then begin
                  bell
                  widget_control, info_line,$
                       set_v='Too many data extraction windows.',/app
                  widget_control, wdw_info_2,set_val='Too many windows'
                endif
               tp_obs.data_win2(*).win_ind = 0
               tp_obs.data_win2(*).win_def(*,*) = 0
               tp_obs.data_win2(*).win_name = ''
               tp_obs.data_win2(*).win_flag = 0
               return
            endif
            tp_obs.data_win2(*).win_def(*) = 0
            for i=0,max(n) do begin
              tp_obs.data_win2(i).win_ind = i
              tp_obs.data_win2(i).win_def = dew(i,*)
              tp_obs.data_win2(i).win_name = dew_name(i)
              tp_obs.data_win2(i).win_flag = dew_flag(i)
            endfor
         endif
         tp_obs.data_win.detector = 'G'
         tp_obs.data_win.ll_id = tp_obs.raster_v.ll_id
         tp_obs.data_win.n_windows = max(n) + 1
       end


  else: return

endcase        

;
;  was the result the same as before? If so keep the ID
;
if same_data(win_in, tp_obs.data_win2.win_def) then begin
   tp_obs.data_win.dw_id = tp_obs.raster_v.dw_id
   tp_obs.data_win2.dw_id = tp_obs.raster_v.dw_id
   if tp_obs.raster_p.detector eq 'N' then begin
      tp_obs.vds_win.dw_id = tp_obs.raster_v.dw_id
   endif
endif 


;
;  Read the various editable text widgets in case user didn't hit return.
;  Not if EXITing because widgets don't exist by this stage
;
if mode ne 'EXIT' then begin

;
;  redisplay extraction windows if visible
;
   if xregistered('tp_display_dew') then begin
      widget_control, dew_image, get_val=wind
      wset,wind
      erase
      tp_plot_dew, tp_obs, /soft
   endif

;
;  double check we have the line list descriptor in case it has
;  changed in the logic programs
;
   widget_control, wll_info_2,get_val=temp
   tp_obs.line_list.ll_desc = strmid(strtrim(temp(0),2),0,64)


   tp_get_desc, tp_obs, current_zone, flag_state, curr_lheight
   widget_control, wdw_info_2,set_val=tp_obs.data_win.dw_desc
   widget_control, wrp_info_2,set_val=tp_obs.raster_p.ras_desc
   widget_control, wrv_info_2,set_val=tp_obs.raster_v.rv_desc


;
;  calculate rough telemetry requirements
;
   rasdur = raster_dur(ras=tp_obs, time2dump=time2dump, /dump)
   widget_control,tlm_info_2,set_val=string(time2dump,form='(f8.1)')
;
;  redisplay the database IDs
;
   widget_control, frid, set_val=string(tp_obs.raster_p.ras_id,form='(i3)')
   widget_control, rvid, set_val=string(tp_obs.raster_v.ras_var,form='(i3)')
   widget_control, llid, set_val=string(tp_obs.raster_v.ll_id,form='(i3)')
   widget_control, dwid, set_val=string(tp_obs.raster_v.dw_id,form='(i3)')


endif
   
end

