	pro inspec_f, data_struct, frq_range, tim_interval = tim_interval, $
            frange = frange, not_index = not_index, frq_rg = frq_rg
;
;+
;PURPOSE:
;	This procedure selects frequency range interactively
;	for image analysis purpose. 
;INPUT:
;	data_struct: the standard OVSA data structure 
;OUTPUT:
;	frq_range: (2, n) array; first dimension defines the
;	starting and ending frequency indices or the frequencies, 
;	and second dimension gives the number of frequency channels.
;OPTIONAL INPUT:
;	tim_interval: if not given, using the time range defined
;	   in the data structure; you may use the output from
;	   inspec_t.pro; MUST be in seconds. 
;	frange: if given, the spectral range for display is specified.
;OPTIONAL INPUT:
;	frq_rg: keep the frequency range information in frequencies rather
; 	   than the indices.
;KEYWORD:
;	not_index: if set, the returned frq_range gives the starting
;	    and ending frequencies rather than the frequency indices.   
;HISTORY:
;	2001 March 27 first version at NJIT. -- JQ
;	2001 March 28 modified to use Total Power (TP) instead of
;	     dynamical spectra to select frequency. Dynamical spectra
;	     now are only used for summary display. -- JQ
;	2001 March 30 more options. -- JQ
;-
;

print, ''
print, '***** Now in inspec_f.pro to select frequency range. *****'
print, '***** NOTE: tim_interval input MUST be in seconds!   *****'
print, ''
		; extract data
flx = (float(data_struct.tp) + imaginary(data_struct.tp))/2.		; total power
siz = size(flx)
tim = data_struct.t * 3600.d
frq = data_struct.f
n_frq = n_elements(frq) 
if n_elements(frange) eq 0 then fr = [0, n_frq - 1] else fr = frange
frq = frq(fr(0):fr(1))
n_frq = n_elements(frq) 
flx = flx(*,0, fr(0):fr(1))
min_frq = min(frq)
max_frq = max(frq)

		; determine time range; produce a spectrum for each time sub-interval
if n_elements(tim_interval) ne 0 then begin
 	n_tim = n_elements(tim_interval(0, *))
	tbeg_ind = lonarr(n_tim)
	tend_ind = lonarr(n_tim)
	tbeg = tim_interval(0, *)
	tend = tim_interval(1, *)
	tim_txt = strarr(n_tim)
	for i = 0, n_tim - 1 do begin
            tbeg_ind(i) = where(abs(tbeg(i) - tim) eq min(abs(tbeg(i) - tim)))
            tend_ind(i) = where(abs(tend(i) - tim) eq min(abs(tend(i) - tim)))
	    tim_txt(i) = timstr(tbeg(i)) + ' - ' + timstr(tend(i)) 
	endfor
endif else begin
	tbeg = min(tim, tbeg_ind)  	
	tend = max(tim, tend_ind)  	
	n_tim = 1
endelse

flx_tp = fltarr(n_tim, n_frq)
for i = 0, n_tim - 1 do flx_tp(i, *) = $
  reform(congrid(flx(tbeg_ind(i):tend_ind(i),0,*), 1,1, n_frq, /interp)) 

print, ''
print, 'Time range for the total power spectrum is from ' + timstr(min(tbeg)) + ' - ' + timstr(max(tend))
print, ''
print, 'Spectral range of the data is from ' + string(min_frq, format = '(f4.1)') $
        + ' - ' + string(max_frq, format = '(f4.1)') + ' GHz '
print, ''
;print, frq
;print, ''

xwinsiz = 640 
ywinsiz = 480
window, 1, xs = xwinsiz, ys = ywinsiz, r = 2, title = 'Select Frequency' 

loadct, 38
count = 0
redo:
erase
plot_oo, frq, flx_tp(0, *), xtitle = 'Frequency (GHz)', ytitle = 'Total Power', $
         pos = [80, 80, xwinsiz - 20, ywinsiz - 20], psym = -1, /dev, $
	 yr = [fix(min(flx_tp))>1.e-2, fix(max(flx_tp))+20], /nodata, /ynoz, $
	 xr = [fix(min(frq)), fix(max(frq))+2], xst = 1, yst = 1
for i = 0, n_tim - 1 do oplot, frq, flx_tp(i, *), psym = -1, lin = i, color = float(i)/n_tim * 255. + 30
legend, tim_txt, lin = indgen(n_tim), /left, /bottom, box = 0, color = $
        findgen(n_tim)/n_tim * 255. + 30, psym = -1, charsize = 1.2
if count gt 0 then begin
   for i = 0, count - 1 do begin
	plots, [fst(i), fst(i)], 10^!y.crange, /data
	plots, [fed(i), fed(i)], 10^!y.crange, /data
	xyouts, 10^((alog10(fst(i))+alog10(fed(i)))/2.), 10^!y.crange(0) * 1.2, $
              '# '+strcompress(string(i),/rem), /data, size = 1.2, align = 0.5 	
   endfor	
endif

box = bytarr(30, 30)
box(*, *) = 255b
tv, box, 0, 0
tv, box, xwinsiz - 30, 0
xyouts, 35, 12, 'L: accept and go on', /dev, size = 1
xyouts, 35, 2, 'R: re-do to change', /dev, size = 1
xyouts, xwinsiz - 200, 12, 'L: accept and finish', /dev, size = 1
xyouts, xwinsiz - 200, 2, 'R: withdraw and finish', /dev, size = 1



;window, 1, xs = 640, ys = 300, r = 2, title = 'Select Frequency'
;loadct, 38
;count = 0
;redo:
;erase
;tvscl, congrid(flx, 590, 250, /interp)<max(flx)*0.8>(min(flx)*1.2), 40, 40
;contour, flx, tim, frq, /fill, /color, xst = 5, yr = [1, 30], yst = 5, /ylog, $
;         pos = [40, 40, 630, 290], /dev, lev = indgen(256)/255.*(max(flx)-min(flx)) + min(flx)
;contour, flx, tim, frq, /nodata, xst = 5, ytitle = 'Frequency (GHz)', yst = 1, $
;         pos = [40, 40, 630, 290], /dev, /noerase, lev = [0.01, 0.1, 1, 2, 3, 4, 5]
;timeaxis, title = 'Time in UT', form = 'h$:m$:s$',nticks = 5,size = 1.,dy = 1.5, $
;          font = font
;if count gt 0 then begin
;   for i = 0, count - 1 do plots, !x.crange, [fst(i), fst(i)], /data, color = 0
;   for i = 0, count - 1 do plots, !x.crange, [fed(i), fed(i)], /data, color = 0
;endif
;box = bytarr(30, 30)
;box(*, *) = 255b
;tv, box, 0, 0
;tv, box, 610, 0
;xyouts, 35, 12, 'L: accept and go on', /dev  
;xyouts, 35, 2, 'R: re-do to change', /dev  
;xyouts, 450, 12, 'L: accept and finish', /dev  
;xyouts, 450, 2, 'R: withdraw and finish', /dev  

key = ''
repeat begin
	print, '--------------- Frequency range #' + string(count, format = '(i2)') + '-------------------'
	print, ''
	print, 'Click on the plot to enter the starting frequency, '
	print, 'then click on the plot to enter the ending frequency.'
	print, ''
 
	cursor, x1, y1, /data, /down
        tmp = min(abs(frq - x1), mm)
        frq1 = frq(mm)
        frq_ind1 = mm
  	plots, [frq1, frq1], 10^!y.crange, /data
        print, 'Starting frequency == ' + string(frq1, format = '(f4.1)') + ' GHz'
	print, ''
	cursor, x2, y2, /data, /down
        tmp = min(abs(frq - x2), mm)
        frq2 = frq(mm)
        frq_ind2 = mm
  	plots, [frq2, frq2], 10^!y.crange, /data
	xyouts, 10^((alog10(frq1)+alog10(frq2))/2.), 10^!y.crange(0) * 1.2, $
              '# '+ strcompress(string(count), /rem), /data, size = 1.2, align = 0.5 	
        print, 'Ending frequency   == ' + string(frq2, format = '(f4.1)') + ' GHz'
	print, ''
	print, 'Frequency interval == ' + string(frq2-frq1, format = '(f4.1)') + ' GHz'
        if (x2  - x1) lt 0. then begin
	   print, ''
           print, 'Not a valid interval; redo it! '
           print, ''
           goto, redo
        endif
        print, ''
	print, '-- Hit left mouse in lower-left white box to accept and go on -- '
	print, '-- Hit right mouse in lower-left white box to change the entry -- '
	print, '-- Hit left mouse in lower-right white box to accept and finish -- '
	print, '-- Hit right mouse in lower-right white box to withdraw and finish -- '
	print, ''
	morehit:        
	cursor, xkey, ykey, /dev, /down 
	mouse_ind = !err
        box_ind = ''
        if (xkey gt 0 and xkey lt 30 and ykey gt 0 and ykey lt 30) then box_ind = 'L' 
        if (xkey gt xwinsiz - 30 and xkey lt xwinsiz and ykey gt 0 and ykey lt 30) then box_ind = 'R' 
	if box_ind eq '' then goto, morehit      	
        if box_ind eq 'L' then begin
		case mouse_ind of
		   1:	begin	
                         if count eq 0 then begin
                           fst = frq1
                           fed = frq2
			   frq_ind = [frq_ind1, frq_ind2] 
                         endif else begin
                           fst = [fst, frq1]
                           fed = [fed, frq2]
			   frq_ind = [[frq_ind], [frq_ind1, frq_ind2]] 
                         endelse
                         count = count + 1
                       	end
		   4:	begin
			   ;print, count
			   goto, redo
			end
		   else: goto, morehit 	
		endcase
	endif
        if box_ind eq 'R' then begin
		case mouse_ind of
		   1:	begin	
                         if count eq 0 then begin
                           fst = frq1
                           fed = frq2
			   frq_ind = [frq_ind1, frq_ind2] 
                         endif else begin
                           fst = [fst, frq1]
                           fed = [fed, frq2]
			   frq_ind = [[frq_ind], [frq_ind1, frq_ind2]] 
                         endelse
                         count = count + 1
                       	end
		   4:	
		   else: goto, morehit
		endcase
	endif
	

        ;read, 'Hit "c" to change, "w" to withdraw and finish, "q" to accept and finish, $
	;and any other key to accept and go on for the next ==> ', key
	;print, ''      
	;case strupcase(key) of       
	;	'C':	goto, redo
	;	'W':
	;	else:	begin
      	;		  if count eq 0 then begin
        ;           	    fst = frq1
        ;           	    fed = frq2
        ;        	  endif else begin
   	;	   	    fst = [fst, frq1]
   	;	   	    fed = [fed, frq2]
        ;      		  endelse
        ;  		  count = count + 1
	;		end
	;endcase        	
;endrep until (strupcase(key) eq 'Q' or strupcase(key) eq 'W')
endrep until (box_ind eq 'R')
     
		; display final results

;erase
;tvscl, congrid(flx, 590, 250, /interp)<max(flx)*0.8>(min(flx)*1.2), 40, 40
;;contour, flx, tim, frq, /fill, /color, xst = 5, yr = [1, 30], yst = 5, /ylog, $
;;         pos = [40, 40, 630, 290], /dev, lev = indgen(256)/255.*(max(flx)-min(flx)) + min(flx)
;contour, flx, tim, frq, /nodata, xst = 5, ytitle = 'Frequency (GHz)', yst = 1, $
;         pos = [40, 40, 630, 290], /dev, /noerase, lev = [0.01, 0.1, 1, 2, 3, 4, 5]
;timeaxis, title = 'Time in UT', form = 'h$:m$:s$',nticks = 5,size = 1.,dy = 1.5, $
;          font = font

plot_oo, frq, flx_tp(0, *), xtitle = 'Frequency (GHz)', ytitle = 'Total Power', $
         pos = [80, 80, xwinsiz - 20, ywinsiz - 20], /dev, psym = -1, /nodata, $
	 yr = [fix(min(flx_tp))>1.e-2, fix(max(flx_tp))+20], /ynoz, $
	 xr = [fix(min(frq)), fix(max(frq))+2], xst = 1, yst = 1
for i = 0, n_tim - 1 do oplot, frq, flx_tp(i, *), psym = -1, lin = i, color = float(i)/n_tim * 255. + 30
legend, tim_txt, lin = indgen(n_tim), /left, /bottom, box = 0, color = $
        findgen(n_tim)/n_tim * 255. + 30, psym = -1, charsize = 1.2

if count gt 0 then begin
   for i = 0, count - 1 do begin
	plots, [fst(i), fst(i)], 10^!y.crange, /data
	plots, [fed(i), fed(i)], 10^!y.crange, /data
	xyouts, 10^((alog10(fst(i))+alog10(fed(i)))/2.), 10^!y.crange(0) * 1.2, $
              '# '+ strcompress(string(i), /rem), /data, size = 1.2, align = 0.5 	
   endfor	
   frq_range = fltarr(2, count)
   frq_range(0, *) = fst
   frq_range(1, *) = fed
   
   print, ''
   print, '-----------------------------------------------------
   print, 'Selected frequency ranges:    '	
   print, ''
   for i = 0, count - 1 do print, 'Channel ' + string(i, format = '(i2)') + ':	' + string(frq_range(0, i), format = '(f4.1)'), $
                                  ' -- ', string(frq_range(1, i), format = '(f4.1)') + ' GHz'	
   print, ''

		; return frequency index rather than frequencies
   if not keyword_set(not_index) then frq_range = frq_ind

   frq_rg = frq(frq_ind)
endif else begin
   print, 'No selection made! So use all frequencies.'
   frq_ind = [0, n_frq-1]
   frq_rg = frq(frq_ind)
   if not keyword_set(not_index) then frq_range = frq_ind else frq_range = frq_rg
endelse
print, ''
;hlp, frq_range, frq_ind, frq_rg

loadct, 0
end
