;+
; NAME:
;       sswhere 
;
; PURPOSE:
;	To produce an SS string for use with YODATroutine.
;       Given a roadmap or index of many frames you can select
;       a narrower subset. 
;       When using index remember that the returned string 
;       contains indices into the limited set defined by index,
;       NOT the full set defined by roadmap.
;          
; CALLING SEQUENCE:
; 	ss=sswhere(roadmap)
;
;       or
;
; 	sr=sswhere(index) then ss=ss(sr)
;        
;
; METHOD/OPERATION:
;
;	This routine opens a widget. There is are sliders to set
;       the minimum frame percentage and the maximum amount of 
;	saturation, and buttons to select the image parameters. 
;	The logic is OR within categories, and AND between categories, 
;	except for the filters, where you can choose either AND or OR 
;	logic, with OR being the default (unless there is only one 
;	A filter or one B filter). If you choose AND then you must 
;	select a filter in both the A & B lists obtaining frames with 
;	that combination of filters. If you choose OR logic you get
;	all frames with any of the selected filters, thus you
;	only need to select a filter in A or B (if you do both 
;	you get all frames with EITHER filter).
;
;       When you have finished setting the buttons, press
;       'Test' and you can see how many images match the parameters
;       you have selected. Pressing 'Select' will add the currently
;       selected images to a cumulative total SS string. You can choose 
;       multiple sets of parameters, and the resulting SS strings are
;       OR'd together, while the text widget informs you of the total
;       images selected so far. Whenever you are done press 'Exit'. 
;       If you want to start over press 'Clear'.
;       By popular request an 'All' button has been added to the DPE
;       list, which sets all the DPE values at once (either on or off).
;       Selection of any single DPE value resets this button. 
;
;       You can also call either PLOT_FOV to select the field of view
;	or SHOW_OBS3 for selecting the time range. You DO NOT have to
;	do either, if you do then these selections are added to the 
;	rest of the criterion to give a result. This can be seen with 'test'.
;	Each routine has its own CLEAR button which undoes any current
;	selection, returning to the default (everything). The general 
;	clear button also resets these.           
;
;  	NB: The handling of the image percentage is done with direct
;	access to the input structure (in mk_mask) rather than through
;	one of the "gt_" routines.
;
; KEYWORDS:	ar	when set allows post-widget selection of data 
;			from a specific active region
;
; CALLS: SSWHERE_EVENT, MK_MASK, PLOT_FOV, SHOW_OBS3, NEW_WIN
;
; COMMON: initflg,wdgtid,wdgtlbs,wdgtind,pssbck
;        
; HISTORY:
;	drafted by A. McAllister, Feb. 1992
;       test and clear options added by A.McA., Apr. 1992
;       DPEs rearranged and 'All' added, A.McA., May 1992
;       Compression buttons added,  A.McA., May 1992.
;       Logic switch added, A.McA., July 1992.
;       DP Mode option added, A.McA., Nov. 1992.
;	Rearranged layout, made single choice defaults, A.McA., Jan. 1993.
;	Added option to call PLOT_FOV and SHOW_OBS3, added saturation
;	        slider, changed default logic to 'OR', A.McA., Mar. 1993.
;	Added AR option as a tag on at the end, A.McA., June 1993.
;	Corrected the indexing in the mduval for loops, A.McA., Jan. 1994.
;	Corrected "no saturation" message display, A.McA., Mar. 1995. 
;	Trapped for single DPE value error, A.McA., Apr. 1996.
;       Merged SLF 28-Oct-1993 change (use /original in call to get_res)
;       RDB  7-Jan-97  Changed layout to fix problem of too big a widget: 
;               more columns in DPE; Clears of FOV and Time/Seq in row; 
;               test/select/clear boxed; message box _after_ test/select etc. 
;               *** Conditional (on IDL version) call to XMANAGER - /block keyword
; 
; TO DO:
;       PFI options?
;       'All' buttons on the filters? Perhaps if there are alot?
;       Change calling sequence for index to include ss and create
;       a real ss as output?
;-

FUNCTION sswhere,indmap,ar=ar

; this procedure sets up the widget structure for sswhere.
; the uvalues contain the menu name
; the values contain the button name
; the wdgtlbs common block contains the menu lists
; the wdgtind common block holds gt_ function indices.
; the initflg common holds the init flag for sswhere_event

COMMON initflg,iflag,maxsat
COMMON wdgtid,whole,complete,satur,butts,b1,b2,b3,b4,b5,b6,dpes,all,txtwgt
COMMON wdgtlbs,mode_labs,dpmode_labs,res_labs,comp_labs,filta_labs,filtb_labs,dpe_labs
COMMON wdgtind,unq_modes,unq_dpmodes,unq_res,unq_comp,unq_filta,unq_filtb,unq_dpes
COMMON pssbck,indmp,ssw

indmp=indmap
ssw=0		;???????

;parent widget

whole=WIDGET_BASE(TITLE='SS Where',/COLUMN)

;Some slider widgets

; Image completeness

   complete=WIDGET_SLIDER(whole,TITLE=' 0%   Frame Completeness   100%',/DRAG, $
                         minimum=0, maximum=100,uvalue='completeness',/frame)

   WIDGET_CONTROL,complete,set_value=100              ;set initial value

; Saturtrated pixel count

   tags = tag_names(indmp)

   if (tags(0) eq 'GEN') then begin

     maxsat=max(indmp.sxt.percentover)
     
   endif else begin

     maxsat=max(indmp.percentover)
     
   endelse

   if maxsat ne 0 then begin

     title=' 0   Saturated Pixels   '+string(maxsat,format='(i3)')

     satur=WIDGET_SLIDER(whole,TITLE=title,/DRAG,minimum=0,maximum=maxsat, $
  						uvalue='saturation',/frame)

     WIDGET_CONTROL,satur,set_value=maxsat             ;set initial value

   endif else begin

     txtwgt2=WIDGET_LABEL(whole,xsize=30,ysize=1,/frame)
     message='There is no saturation in this data.'
     WIDGET_CONTROL,txtwgt2,set_value=message

   endelse


;Mode widgets

   mds=WIDGET_BASE(whole,/ROW)
   mds1=WIDGET_BASE(mds,/COLUMN,/frame)
   
   txt1=WIDGET_LABEL(mds1,value='Modes')

   raw_modes=gt_expmode(indmp)             ;raw mode values in the data set
   sort_modes=raw_modes(sort(raw_modes))   ;create sorted vector
   unq_modes=sort_modes(uniq(sort_modes))  ;get sorted unique values
   modes=['Normal','Dark','Calibration']   ;create label vector
   mode_labs=modes(unq_modes)              ;make button labels

   tmp=shift(size(mode_labs),1)                    
   mduval=mode_labs                        ;get string array
   for i=0,tmp(0)-1 do mduval(i)='Modes'   ;set user values
   
   ;create the menu

   XMENU,mode_labs,mds1,uvalue=mduval,BUTTONS=b1,/ROW,/nonexclusive,/frame   

   if n_elements(unq_modes) eq 1 then WIDGET_CONTROL,b1(0),set_button=1

;DP Mode widget

   mds2=WIDGET_BASE(mds,/COLUMN,/frame)
   
   txt8=WIDGET_LABEL(mds2,value='DP Modes')

   raw_dpmodes=gt_dp_mode(indmp)           ;raw dp mode values in the data set
   sort_dpmodes=raw_dpmodes(sort(raw_dpmodes))   ;create sorted vector
   unq_dpmodes=sort_dpmodes(uniq(sort_dpmodes))  ;get sorted unique values
   dpmodes=['0','DP-Mem','ACP-Mem','STT-Mem','TCU-Mem','5','BCS-Mem', $
          'BFM-Mem','8','Flare','10','BCS-OUT','Night','Quiet',     $
          '14','15']                             ;create label vector
   dpmode_labs=dpmodes(unq_dpmodes)                ;make button labels

   tmp=shift(size(dpmode_labs),1)                     
   dmuval=dpmode_labs                        ;get string array
   for i=0,tmp(0)-1 do dmuval(i)='DP Modes'   ;set user values
   
   ;create the menu

   XMENU,dpmode_labs,mds2,uvalue=dmuval,BUTTONS=b2,/ROW,/nonexclusive,/frame   

   if n_elements(unq_dpmodes) eq 1 then WIDGET_CONTROL,b2(0),set_button=1

;Resolution widget

   typ=WIDGET_BASE(whole,/ROW)
   typ1=WIDGET_BASE(typ,/COLUMN,/frame)

   txt2=WIDGET_LABEL(typ1,value= 'Resolution')

   raw_res=gt_res(indmp,/original)       ;raw resolution values in the data set
   sort_res=raw_res(sort(raw_res))       ;create sorted vector
   unq_res=sort_res(uniq(sort_res))      ;get sorted unique values
   res=['Full','Half','Quarter']         ;create label vector
   res_labs=res(unq_res)                 ;make button labels

   tmp=shift(size(res_labs),1)                    
   rsuval=res_labs                             ;get string array
   for i=0,tmp(0)-1 do rsuval(i)='Resolution'  ;set user values
   
   ;create the menu

   XMENU,res_labs,typ1,uvalue=rsuval,BUTTONS=b3,/ROW,/nonexclusive,/frame   

   if n_elements(unq_res) eq 1 then WIDGET_CONTROL,b3(0),set_button=1

;Compression widget

   typ2=WIDGET_BASE(typ,/COLUMN,/frame)

   txt7=WIDGET_LABEL(typ2,value= 'Compression')

   raw_comp=gt_comp(indmp)                 ;raw compression values 
   sort_comp=raw_comp(sort(raw_comp))       ;create sorted vector
   unq_comp=sort_comp(uniq(sort_comp))      ;get sorted unique values
   comp=['Compressed','Low 8','High 8']         ;create label vector
   comp_labs=comp(unq_comp)                 ;make button labels

   tmp=shift(size(comp_labs),1)                     
   cpuval=comp_labs                             ;get string array
   for i=0,tmp(0)-1 do cpuval(i)='Compression'  ;set user values
   
   ;create the menu

   XMENU,comp_labs,typ2,uvalue=cpuval,BUTTONS=b4,/ROW,/nonexclusive,/frame   

   if n_elements(unq_comp) eq 1 then WIDGET_CONTROL,b4(0),set_button=1

;Filter widgets

   txt3=WIDGET_LABEL(whole,value='Filters')

   filts=WIDGET_BASE(whole,/ROW)

;filts children

   acol=WIDGET_BASE(filts,/COLUMN,/frame)
   logc=WIDGET_BASE(filts,/COLUMN)
   bcol=WIDGET_BASE(filts,/COLUMN,/frame)

;A filter widget

   txt5=WIDGET_LABEL(acol,value='A Filters')

   raw_filta=gt_filta(indmp)                ;raw filter A values in the data set
   sort_filta=raw_filta(sort(raw_filta))          ;create sorted vector
   unq_filta=sort_filta(uniq(sort_filta))              ;get sorted unique values
   afilts=['Open','Narrow Band','Quartz', $
           'Diffuser','Wide Band', 'Neutral Density']   ;create label vector
   filta_labs=afilts(unq_filta-1)                         ;make button labels

   tmp=shift(size(filta_labs),1)                     
   fauval=filta_labs                             ;get string array
   for i=0,tmp(0)-1 do fauval(i)='A Filters'     ;set user values
   
   ;create the menu

   XMENU,filta_labs,acol,uvalue=fauval,BUTTONS=b5,/COLUMN,/nonexclusive 

   if n_elements(unq_filta) eq 1 then WIDGET_CONTROL,b5(0),set_button=1

;Logic widget

   nams=['AND','OR']
   luval=['Logics','Logics']

   XMENU,nams,logc,uvalue=luval,BUTTONS=butts,/COLUMN,/exclusive
   ;set the button default below

;B filter widget 

   txt6=WIDGET_LABEL(bcol,value='B Filters')

   raw_filtb=gt_filtb(indmp)                ;raw filter B values in the data set
   sort_filtb=raw_filtb(sort(raw_filtb))          ;create sorted vector
   unq_filtb=sort_filtb(uniq(sort_filtb))              ;get sorted unique values
   bfilts=['Open','Thin Aluminium','Sandwich', $
           'Beryllium','Thick Aluminium', 'Magnesium']   ;create label vector
   filtb_labs=bfilts(unq_filtb-1)                          ;make button labels

   tmp=shift(size(filtb_labs),1)                     
   fbuval=filtb_labs                             ;get string array
   for i=0,tmp(0)-1 do fbuval(i)='B Filters'     ;set user values

   ;create the menu

   XMENU,filtb_labs,bcol,uvalue=fbuval,BUTTONS=b6,/COLUMN,/nonexclusive

   if n_elements(unq_filtb) eq 1 then WIDGET_CONTROL,b6(0),set_button=1

;choose default logic

   if (n_elements(unq_filta) eq 1) or (n_elements(unq_filtb) eq 1 ) then begin  

     WIDGET_CONTROL,butts(0),set_button=1

   endif else begin

     WIDGET_CONTROL,butts(1),set_button=1

   endelse

;DPE widget

;think about case where there are many many values

   raw_dpes=gt_dpe(indmp)                ;raw dpe values in the data set
   sort_dpes=raw_dpes(sort(raw_dpes))      ;create sorted vector
   unq_dpes=sort_dpes(uniq(sort_dpes))     ;get sorted unique values
   dpe_labs=STRING(unq_dpes)+'  '            ;make button labels

   tmp=shift(size(dpe_labs),1)                     
   dpuval=dpe_labs                             ;get string array
   for i=0,tmp(0)-1 do dpuval(i)='DPE Values'  ;set user values
   tmp=shift(tmp,-1)				
   if n_elements(tmp) eq 3 then tmp=[tmp(0),1,tmp(1),tmp(2)]  ;trap single value


   txt4=WIDGET_LABEL(whole,value='DPE Values')

   dpes=WIDGET_BASE(whole,/ROW,/frame)

   ;create the menu
   
   i=0
;   mid1 = (fix((tmp(1)-1)/4)-1 ) < (tmp(1)-1) 	;less 1 for ALL button
;   mid2 = (fix((tmp(1)-1)/4*2) ) < (tmp(1)-1)
;   mid3 = (fix((tmp(1)-1)/4*3) ) < (tmp(1)-1)
   clen = fix((tmp(1)+2)/5.+0.5)
   mid1 = (clen - 2) < (tmp(1)-1)
   mid2 = (mid1 + clen) < (tmp(1)-1)
   mid3 = (mid2 + clen) < (tmp(1)-1)
   mid4 = (mid3 + clen) < (tmp(1)-1)
;;   print,tmp(1),clen
;;   print,mid1,mid2,mid3,mid4

   dpesc1=WIDGET_BASE(dpes,/COLUMN,/nonexclusive)
   if n_elements(unq_dpes) ne 1 then begin
     all=WIDGET_BUTTON(dpesc1,value='  ALL  ',uvalue='all DPE')
   endif
   while i le mid1 do begin 
     dpeb=WIDGET_BUTTON(dpesc1,value=dpe_labs(i),uvalue='DPE Values')
     i=i+1
   endwhile
   

   dpesc2=WIDGET_BASE(dpes,/COLUMN,/nonexclusive)
   while i le mid2 do begin 
     dum=WIDGET_BUTTON(dpesc2,value=dpe_labs(i),uvalue='DPE Values')
     i=i+1
   end
   

   dpesc3=WIDGET_BASE(dpes,/COLUMN,/nonexclusive)
   while i le mid3 do begin 
     dum=WIDGET_BUTTON(dpesc3,value=dpe_labs(i),uvalue='DPE Values')
     i=i+1
   end
   

   dpesc4=WIDGET_BASE(dpes,/COLUMN,/nonexclusive)
   while i le mid4 do begin 
     dum=WIDGET_BUTTON(dpesc4,value=dpe_labs(i),uvalue='DPE Values')
     i=i+1
   end
   
   dpesc5=WIDGET_BASE(dpes,/COLUMN,/nonexclusive)
   while i le (tmp(1)-1) do begin 
     dum=WIDGET_BUTTON(dpesc5,value=dpe_labs(i),uvalue='DPE Values')
     i=i+1
   end

   if n_elements(unq_dpes) eq 1 then WIDGET_CONTROL,dpes,set_button=1

;Call other routines widget

  options=WIDGET_BASE(whole,/ROW)

;PLOT_FOV widget

   fovbase=WIDGET_BASE(options,/ROW,/frame)

   plotfov=WIDGET_BUTTON(fovbase,value=' Field of View ',uvalue='Field of View')

   rsfov=WIDGET_BUTTON(fovbase,value=' Clear ',uvalue='CL FOV')

;SHOW_OBS3 widget

   obsbase=WIDGET_BASE(options,/ROW,/frame)

   showobs3=WIDGET_BUTTON(obsbase,value=' Time, Sequence ',uvalue='Time, Sequence')

   rsobs=WIDGET_BUTTON(obsbase,value=' Clear ',uvalue='CL OBS')


;Control widgets

   cntrl=WIDGET_BASE(whole,/ROW)
   ccntrl=WIDGET_BASE(cntrl,/ROW,/frame)

;Test widget

   tst=WIDGET_BUTTON(ccntrl,value=' Test ',uvalue='Test')

;Select widget

   selct=WIDGET_BUTTON(ccntrl,value=' Select ',uvalue='Select')

;Clear widget

   clear=WIDGET_BUTTON(ccntrl,value=' Clear ',uvalue='Clear')

;Exit widget

   cexit=WIDGET_BASE(cntrl,/ROW,/frame)
   exit=WIDGET_BUTTON(cexit,value='  EXIT  ',uvalue='Exit')  

;A message text widget

   txtwgt=WIDGET_TEXT(whole,xsize=40,ysize=2,/frame)
   

WIDGET_CONTROL, /REALIZE, whole

iflag =0           ;initialize the initflag

XMANAGER, 'sswhere',whole

if keyword_set(ar) then begin

coords=' '
read,'Enter the Active Region coordinates, [ew,ns]: ', coords
;call tom's thing with ssw as input

endif 

return,ssw

end
