define logical do
let do no
if .not.exist(filename) then
  define chara filename*256 /global
  let filename &1 /prompt "File to be filtered"
  let do yes
else if pro%narg.gt.0 then
  let filename &1 /prompt "File to be filtered"
  let do no
  if pro%narg.eq.2 then
    let do &2
  else if .not.file('filename'".tab") then
    let filename &1 /prompt "File to be filtered"
    let do yes
  endif
endif
!
if (.not.exist(myfreq)) define double myfreq /global
!
begin data uv_fmodel.init
  TASK\FILE "Input UV Sample Table" UVSAMPLE$ 'filename'
  TASK\FILE "Input .LMV Cube "      IMAGE$ "model.lmv"
  TASK\FILE "Output UV Model Table" UVMODEL$ "M_"'filename'
  TASK\REAL "UV Model Table frequency" FREQUENCY$ 'MYFREQ*1D-3'  ! There is an issue with units here
  TASK\LOGICAL "Accurate (YES) or rough (NO) precision" LARGE$ YES
  TASK\GO
end data uv_fmodel.init
!
begin data uv_correlate.init
  TASK\CHARACTER "Model Kernel" MODEL$ "M_"'filename'
  TASK\CHARACTER "UV Data " UVDATA$ 'filename' 
  TASK\CHARACTER "Output correlation spectrum" SPECTRUM$ 'filename'.tab
  TASK\GO
end data uv_correlate.init
!
if (do) then
  read uv 'filename'.uvt
  !
  let myfreq (uv%nchan/2-uv%convert[1,1])*uv%freqres+uv%restfre
  VECTOR\RUN uv_fmodel * /nowin
  !
  VECTOR\RUN uv_correlate * /nowin
endif
!
if .not.file('filename'".tab") then
  Message E MATCH-FILTER "No such file "'filename'".tab"
  return
endif
set box 4 28 3.5 15.5           ! Change the default box
col x 1 y 3 /table 'filename'.tab
clear
limi /reve x      ! Beware things go in opposite directions
axis xu /label p
axis yr
axis yl
! Make sure we work in Frequency
col x 2
limi
axis xl
histo
label "Frequency (MHz)" /X
label "Correlated flux" /Y
!
! Return if no selected Line Catalog
if .not.exist(linedb%dbin) return
!
@ catalog
!
define integer nlines klines
define double fmin fmax
!
fmin = min(user_xmin,user_xmax)
fmax = max(user_xmin,user_xmax)
FIND 'fmin' 'fmax' !! /ENERGY 300 /SORT aij
if .not.exist(lines%n) return   !! Nothing found
if lines%n.eq.0 return          !! idem
!
! Draw all lines in range in BLUE
pen 3
if (lines%n.gt.0) then
  for k 1 to lines%n
    GREG1\DRAW text lines%frequency[k] user_ymax-0.15*(user_ymax-user_ymin) -
    'lines%species[k]'"("'lines%qnup[k]'"\s|"'lines%qnlow[k]'")" 9 60 /USER 
  next
else
  let plines -lines%n /new integer
  for k 1 to plines
    GREG1\DRAW text lines%frequency[k] user_ymax-0.15*(user_ymax-user_ymin) -
    'lines%lines[k]' 9 60 /USER 
  next
endif
pen 0 
