;+
; NAME:
;     	
; PURPOSE:
; 	plot dynamic spectrum (Total power) and get user-selected 
; 	range of time and frequency
; CATEGORY:
;	OVSA APC imaging
; MODIFICATION HISTORY:
;     Written 05-MAR-2001 by JQ
;-

function inspec_tf, tp, t, f,titl

nt  = n_elements(tp[*,0])
nf  = n_elements(tp[0,*])

xwt=650 & ywt=320
xw =600 & yw =260
x0 =40  & y0 = 35

Print,' INSPEC_TF: * * * * * '
print,'     Select Time & Frequency ranges for investigation'
Print,'          using the OVSA Dynamic Specurum'
Print,'     Tip: make a rough (wide enough) selection here  '
Print,'          since you can do a finer selection later using'
print,'          time profiles and spectra, respectively'
print,' '


START:
idon=0
image=bytscl(congrid(tp,xw,yw,/cubic))
    x=congrid(t,xw,/interp)
    y=congrid(f,yw,/interp)

AGAIN:
image=congrid(image,xw,yw,/cubic)
    x=congrid(x,xw,/interp)
    y=congrid(y,yw,/interp)

window,0,xs=xwt,ys=ywt,r=2,title='Select Time/Frequency Ranges'
!p.multi=0
device,decompo=0
loadct,5

 tv,image,x0,y0,xs=xw,ys=yw,/dev
 contour,image,x,y,xst=1,yst=1,/noerase,/nodata,/follow,nlev=5, $
 	pos=[x0,y0,x0+xw,y0+yw],/dev, $
	xtit='Time [UT]', ytit='Frequency [GHz]',tit=titl

; profiles,image,wsize=1.0 ; Not very helpful

 print,'Choose one :'
 print,' 1. Want to select a smaller range'
 print,' 2. Proceed to show me time profiles under the current selection'
 read,ikeywd,prompt='Which option [1,2]?: '

 if (ikeywd lt 1) or (ikeywd gt 2) then begin
	print,'Wrong option no. Try again'
	goto, start
 endif
 if ikeywd eq 2 then goto,gonow
 idon=1

 print,' '
 print,'To select a range of time & frequency for investigation, '
 print,'-Left mouse button: move the box by dragging'
 print,'-Middle mouse button: resize the box by dragging'
 print,'-Right mouse button: exit'
 box_cursor,lbx,lby,ncx,ncy
;---take out x,y-margins
siz=size(image)
i1=(lbx-x0)>0<(siz[1]-2) & i2=(lbx-x0+ncx)>(i1+1)<(siz[1]-1)
j1=(lby-y0)>0<(siz[2]-2) & j2=(lby-y0+ncy)>(j1+1)<(siz[2]-1)

;---show what you chose
oplot,[x[i1],x[i2]],[y[j1],y[j1]]
oplot,[x[i2],x[i2]],[y[j1],y[j2]]
oplot,[x[i2],x[i1]],[y[j2],y[j2]]
oplot,[x[i1],x[i1]],[y[j2],y[j1]]
;---

 print,'Choose: 3. Well-done'
 print,'        4. Zoom-in'
 print,'        5. Restart'
 read,ikeywd,prompt='Which option [3,4,5]?: '

 if (ikeywd lt 3) or (ikeywd gt 5) then begin
	print,'Wrong option no. Try again'
	goto, start
 endif

if ikeywd eq 3 then goto, gonow
if ikeywd eq 5 then goto, start

;if user choose ikeywd=4 we cut the part out and redo it 
x=x[i1:i2]
y=y[j1:j2]
image=image[i1:i2, j1:j2]
idon=0

goto,again

gonow:		; CLBOX(es) are set. Proceed . . .

; Figure out the indexes in the original time & freq arrays,
; so i1,i2,j1,j2 calculated here here are not same as in the above.

; in case it is just original or after being cut out
if idon eq 0 then begin
  i1=0 & i2=n_elements(x)-1
  j1=0 & j2=n_elements(y)-1
endif
  hama=(t-x[i1])^2 & there=where(hama eq min(hama)) & i1=there[0]
  hama=(t-x[i2])^2 & there=where(hama eq min(hama)) & i2=there[0]
  hama=(f-y[j1])^2 & there=where(hama eq min(hama)) & j1=there[0]
  hama=(f-y[j2])^2 & there=where(hama eq min(hama)) & j2=there[0]
  i1=i1>0<(nt-2) & i2=i2>(i1+1)<(nt-1)
  j1=j1>0<(nf-2) & j2=j2>(j1+1)<(nf-1)

print,'Your first selection is'
print,'   Time range:',t[i1],t[i2]
print,'   Freq range:',f[j1],f[j2]

 box= [i1,i2,j1,j2]
return, box

end
