;+
; Project     : SOHO - CDS     
;                   
; Name        : TP_GWIN_LOGIC
;               
; Purpose     : Produce Data Extraction Windows for GIS 
;               
; Explanation : Input Selected Lines and Widths. Routine Calculates
;               Data Extraction Windows (i.e. data to return to Earth)
;               for the GIS
;               
; Use         : tp_gwin_logic, obs_str, curr_lwidth, gis_def_ext, dew
;    
; Inputs      : obs_str     - structure of the defined observation, see tplan
;               curr_lwidth - current window width specified
;               gis_def_ext - which bands to extract in toto
;
; Opt. Inputs : None
;               
; Outputs     : dew  - data extraction windows array(n,4) where 2nd dimension
;                      is xstart,ystart,xlength,ylength. The 'y' values are
;                      retained for consistency with the VDS code but are
;                      set to 0 and 1 for all lines.
;               
; 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    : Major update for changes in observation structure.
;                                                   CDP, 14-Jul-94
;               Change specification of default extraction. CDP, 6-Dec-94
;               Add extraction window names.  CDP, 8-Mar-95
;               Reverse window definitions to allow for reveral of
;               GIS band data.  CDP, 31-May-95
;               Add flag windows.  CDP, 26-Jun-95
;
; Version     : Version 5, 26-Jun-95
;-            

pro tp_gwin_logic, obs_str, curr_lwidth, gis_def_ext, dew, dew_name, dew_flag


;
;  Set Up Arrays
;
gmask = intarr(8192)


;
; any lines defined?
;
nlines = where(obs_str.line_w.lam gt 0)
if nlines(0) ge 0 then nlines = n_elements(nlines) else nlines = 0

;
;  create max arrays needed, can be trimmed later.  
;
dew = intarr(100,4)
dew_name = strarr(100)
dew_flag = intarr(100)

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

;
; if have lines, include them unless they are overridden by default selection 
;
nw = -1
if nlines gt 0 then begin
   for i= 0,nlines-1 do begin
     lam   = obs_str.line_w(i).lam 
     order = obs_str.line_w(i).order
     band_num = which_gis_band(lam*order)
     if not gis_def_ext(band_num-1) then begin
        band = 'GIS'+string(band_num,format='(i1)')
        spixel = 0 > (wave2pix(band,lam*order) - (curr_lwidth/2)) < 2047
        epixel = 0 > (spixel + curr_lwidth - 1) < 2047

;
;  cater for reversal of GIS band data within telemetry.  If this is wrong
;  just DELETE these 3 lines
;
        temp = epixel
        epixel = 2047 - spixel
        spixel = 2047 - temp


        spixel = spixel + (band_num-1)*2048
        epixel = epixel + (band_num-1)*2048
   
        nw = nw + 1
        dew(nw,0) = spixel
        dew(nw,1) = 0
        dew(nw,2) = epixel-spixel+1
        dew(nw,3) = 1
        errmsg = ''
        temp = anyname2std(obs_str.line_w(i).id,lam,errmsg=errmsg)
        if temp eq '' then begin
           temp = 'WW_'+ strtrim(string(lam,form='(f6.1)'),2)
        endif
        dew_name(nw) = repchar(temp,'.','_') 
        dew_flag(nw) = obs_str.line_w(i).flag
      endif
   endfor
endif


;
;  any default extractions requested
;
for i=0,3 do begin
   if gis_def_ext(i) then begin
      nw = nw + 1
      dew(nw,0) = i*2048
      dew(nw,1) = 0
      dew(nw,2) = 2048
      dew(nw,3) = 1
      dew_name(nw) = 'BB_GIS_' + string(i+1,form='(i1)')
   endif
endfor


;
;  trim dew array
;
n = where( (dew(*,2) gt 0) and (dew(*,0) ge 0) )
if n(0) ge 0 then begin
   dew = dew(n,*)
   dew_name = dew_name(n)
   dew_flag = dew_flag(n) 
endif else begin
   dew = dew(0,*)
endelse

end
