;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_VWIN_LOGIC
;               
; Purpose     : Produce Data Extraction and VDS Extraction Windows  
;               
; Explanation : Input Selected Lines and Widths. Routine Calculates
;               Data Extraction Windows (i.e. data to return to Earth)
;               and VDS Extraction Windows (i.e. windows to extract
;               from VDS). If VDS background windows are required these
;               are dealt with.
;               
; Use         : tp_vwin_logic, obs_str, curr_lheight, vds_extension, back,$
;                              dew, vdsew 
;    
; Inputs      : obs_str          - structure of the defined observation, see 
;                                  tplan_com for details.
;               curr_lheight     - current line window height 
;
;               curr_lwidth      - current line window width 
;
;               vds_extension    - include background windows or not
;
;               back             - intarr(4,4) specifying background windows
;                                  (help in tp_obs.vds_back.win_def)
;
;               nis_def_ext      - flag to indicate what default (no lines)
;                                  extraction is
;
; Opt. Inputs : None
;               
; Outputs     : dew  - data extraction windows array(n,4) where 2nd dimension
;                      is xstart,ystart,xlength,ylength.
;
;               vdsew - as above for VDS extraction windows.
;
;               dew_name - name to be allocated to extraction window
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : WAVE2PIX
;               FIND_DATA_RUNS
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Planning, technical
;               
; Prev. Hist. : Based on original by R A Harrison, RAL.
;
; Written     : C D Pike, RAL, 16-Nov-1993
;               
; Modified    : Fixed bug when no windows defined.  CDP, 21-Dec-93
;               Fix bug for VDS y positions.        CDP, 21-Mar-94
;               Added vds_extension option.         CDP, 21-Mar-94
;               To handle windows off either edge.  CDP, 24-Mar-94
;               Major update for new logic and 
;               structures.                         CDP, 13-Jul-94
;               Introduce new logic for background 
;               windows                             CDP, 20-Oct-94
;               Major rewrite to cope with changed 
;               logic of 'all spectrum'.            CDP, 28-Nov-94
;               Include new VDS window calculation. CDP, 04-Jan-95
;               Add extraction window names.        CDP, 8-Mar-95
;               Add flag indicator.                 CDP, 26-Jun-95
;               Fix bug introduced with flags.      CDP, 04-Jul-95
;               Flip of orientation of CCD Y values CDP, 16-Oct-95
;
; Version     : Version 13, 16-Oct-95  
;-            

pro tp_vwin_logic, obs_str, curr_lheight, curr_lwidth, vds_extension, $
                   nis_def_ext, back, dew, vdsew, dew_name, dew_flag


;
;  Set Up Arrays
;
;offset    = obs_str.state.vds_offset
length    = curr_lheight/2
width     = curr_lwidth

dew      = intarr(100,4)
dew_flag = intarr(100)
dew_name = strarr(100)
vdsew    = intarr(100,4)
nlines   = where(obs_str.line_w.lam gt 0)
nw = -1

;
; state the obvious
;
obs_str.line_list.detector = 'N'

;
;  if all CCD requested let's not waste time
;  (VDS windows only valid for quadrant extraction)
;
if nis_def_ext(2) then begin
   dew = intarr(1,4)
   vddew = intarr(1,4)
   dew(0,*)   = [0,0,1024,1024]
   dew_name(0) = 'All CCD'
   dew_flag(0) = 0
   vdsew(0,*) = [0,0,512,512]
   return
endif


;
;  create windows from lines if any.  If they are overridden by the
;  default selection of all that spectral range then ignore
;

if nlines(0) ge 0 then begin
   nlines = n_elements(nlines)
   for i=0,nlines-1 do begin
     lam   = obs_str.line_w(i).lam
     order = obs_str.line_w(i).order
     if lam*order lt 500 then begin
        if not nis_def_ext(0) then begin
           nw = nw + 1
           spixel = 0 > (wave2pix('NIS1',lam*order) - (width/2)) < 1023
           epixel = 0 > (spixel + width) < 1023
           sp_off = 512 - ((120-length)/2.) - length
           dew(nw,0) = spixel
           dew(nw,1) = sp_off
           dew(nw,2) = epixel-spixel
           dew(nw,3) = length
           ermsg = ''
           temp = anyname2std(obs_str.line_w(i).id,lam, errmsg=ermsg)
           if temp eq '' then begin
              temp = 'WW_'+ strtrim(string(lam,form='(f7.2)'),2)
           endif
           dew_name(nw) = repchar(temp,'.','_')
           dew_flag(nw) = obs_str.line_w(i).flag
        endif
     endif else begin
        if not nis_def_ext(1) then begin
           nw = nw + 1
           spixel = 0 > (wave2pix('NIS2',lam*order) - (width/2)) < 1023
           epixel = 0 > (spixel + width) < 1023
           sp_off = 512 - ((120-length)/2.) - length + 120
           dew(nw,0) = spixel
           dew(nw,1) = sp_off
           dew(nw,2) = epixel-spixel
           dew(nw,3) = length
           errmsg = ''
           temp = anyname2std(obs_str.line_w(i).id,lam, errmsg=errmsg)
           if temp eq '' then begin
              temp = 'WW_'+ strtrim(string(lam,form='(f7.2)'),2)
           endif
           dew_name(nw) = repchar(temp,'.','_')
           dew_flag(nw) = obs_str.line_w(i).flag
        endif
     endelse
   endfor
endif


;
;  any default extractions requested?
;

if nis_def_ext(0) then begin
   nw = nw + 1
   dew(nw,0) = 0
   dew(nw,1) = 452 - length/2 
   dew(nw,2) = 1024
   dew(nw,3) = length 
   dew_name(nw) = 'BB_NIS_1'
endif
  
if nis_def_ext(1) then begin
   nw = nw + 1 
   dew(nw,0) = 0
   dew(nw,1) = 572 - length/2
   dew(nw,2) = 1024
   dew(nw,3) = length 
   dew_name(nw) = 'BB_NIS_2'
endif

;
;  apply the alignment offsets
;
;n = where(dew(*,1) le 511)
;if n(0) ge 0 then dew(n,1) = dew(n,1) ;+ offset(0)

;n = where(dew(*,1) gt 511)
;if n(0) ge 0 then dew(n,1) = dew(n,1) ;+ offset(1)


;
; were background windows specified?
; if so update number of extraction windows
;
if vds_extension then begin
   nw = nw + 1
   dew(nw:nw+3,*) = back 
   nw = nw + 3
endif

;
;  trim dew arrays
;
if nw ge 0 then begin
   dew = dew(0:nw,*) 
   dew_name = dew_name(0:nw)
   dew_flag = dew_flag(0:nw)
endif else begin
   dew = intarr(1,4)
   dew(0,*) = [0,0,0,0]
   dew_flag = [0]
   dew_name = ''
endelse

;
;  flip the Y start values because the 500A spectrum is actually at LOWER
;  Y row values.
;
dew(*,1) = 1024 - dew(*,1) - dew(*,3)

;
;  calculate VDS windows according to readout mode
;

errmsg = ''
vdsew = get_vds_win(dew, obs_str.state.vds_read, errmsg=errmsg)
if errmsg ne '' then begin
   bell
   print,errmsg
endif

;
;  and trim
;
n = where(vdsew(*,2) gt 0)
if n(0) ge 0 then vdsew = vdsew(n,*)

   
end
