;Overlay a contour plot over a previously tv'd image
;input:
;  data_ - 2D array of data to plot
;  x     - left edge of where to plot, in pixels. Like similar parameter in tv, but required.
;  y     - bottom edge of where to plot, in pixels. Like similar parameter in tv, but required.
;  lo=   - Minimum contour to plot. Defaults to minimum of data_
;  hi=   - Maximum contour to plot. Defaults to maximum of data_
;  interval - Contour interval. Required
;  _extra= - All other parameters are passed to contour()
;output: none
pro tvcontour,data_,x,y,lo=lo,hi=hi,interval,_extra=extra
  data=data_
  if !d.name eq 'WIN' or !d.name eq 'X' then begin
    s=size(data,/dim)
    w=!d.x_size-s[0]-x
    h=!d.y_size-s[1]-y
    if w lt 0 then begin
      if w lt -s[0] then return
      data=data[0:s[0]+w-1,*]
    end
    if h lt 0 then begin
      if h lt -s[1] then return
      data=data[*,0:s[1]+h-1]
    end
  end 
  if n_elements(lo) eq 0 then lo=(fix(min(data,/nan)/interval)+1)*interval
  if n_elements(hi) eq 0 then hi=(fix(max(data,/nan)/interval)  )*interval
  s=size(data,/dimensions)
  n_contour=(hi-lo)/interval+1
  if n_contour lt 1 then n_contour=1
  levels=linterp(0,lo,N_contour-1,hi,findgen(n_contour))
  w=where(logical_and(levels ge min(data),levels le max(data)),nw)
  if nw gt 0 then levels=levels[w]
  contour,data,/device,/noerase,xrange=[0,s[0]],yrange=[0,s[1]],$ 
          /follow,xstyle=5,ystyle=5,position=[x,y,x+s[0],y+s[1]], $
          levels=levels,_extra=extra
end
