let mapping_error yes
on error return
! @ p_uv_detect UV_Results [UV_Data   ImageModel]
!
define character*256 cresult
if pro%narg.eq.3 then
  clean\read uv &2  /range 3 -3 channel ! Drop edge channels
  advanced\uv_preview    ! Show potential lines
  clean\uv_baseline   ! Remove continuum
  clean\read clean &3
endif
!
let cresult &1
!
! Now get the Kepler filter as much as possible
!
! This is a bit too much.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!! map_resample clean /like UV
!
! We want the same channel spacing (in velocity, approximately...)
! and the thing centered on the specified systemic velocity, i.e.
! we want the Reference channel to become Channel (N+1)/2
!
! 1) Find out the Velocity spread of the Model around the VELOFF
define real vmin vmax vdelta
define integer nout
vmin = abs((1-clean%convert[1,3])*clean%velres)    !! +clean%veloff
vmax = abs((clean%dim[3]-clean%convert[1,3])*clean%velres) !! +clean%veloff
vdelta = min(vmin,vmax)
!
! 2) Find out the number of output channels (approx...)
nout = 2*int(vdelta/abs(uv%velres))+1
if (nout.lt.12) then
  if (abs(vmin-vmax).gt.0.2*vmax) then
    message W CORRELATE "Kernel is not centered - check model velocity"
    exa nout vmin vmax vdelta
    if (nout.lt.4) then
      message E CORRELATE "Kernel has less than  4 channels"
      return error
    endif
    return error
  endif
  message W CORRELATE "Kernel has less than 12 channels"
endif
!
! 3) Resample in a temporary data cube
if exist(my_model) delete /var my_model
define image my_model['clean%dim[1]','clean%dim[2]',nout] * real /global
clean\map_resample clean nout (nout+1)/2d0 clean%veloff uv%velres my_model
clean\specify blanking 0 /for my_model ! Remove blanking
!
calibrate\model my_model /mode IMAGE
!
advanced\uv_correlate 'cresult'
!
advanced\uv_preview 1 3.0 /file 'cresult' 0
RETURN
!
! This was a previous simple display
!
greg1\column x 2 y 3 /file 'cresult'.dat
gtvl\clear
greg1\limits
greg1\axis xl
greg1\axis yl
greg1\axis yr
greg1\histogram
greg1\column x 1 y 3
lim * * = =
greg1\axis xu /label p
greg1\label "Frequency (MHz)" /X
greg1\draw text 0 2 "Velocity (km/s)" 5 0 /char 8
greg1\column x 2 y 3
lim * * = =
greg1\column /close
!
