#! /bin/csh -f
#  
# script to tinker with a single calibrator (e.g. fringetest)
#
# see also array health:   CARMA/scripts/python/observer/arrayHealth/fringeCheck.csh
#
# Default: fringe.3C273.2008jun18.4
# For default settingsf rms in the 6 windows is:  
# 22.11 22.20 22.54 22.72 23.42 25.72 mJy/beam  (all, minus the 31x31 center)
# 20.53 20.59 20.43 20.84 21.54 24.39           (quarter)

#
set vis=fringe.3C273.2008jun18.4.miriad   #> IFILE  vis=fringe.3C273.2008jun18.4.miriad
#
set linecal=0                             #> RADIO  linecal=0      0,1
set bandpassg=0                           #> RADIO  bandpassg=0    0,1
set selfcal=1                             #> RADIO  selfcal=1      0,1,2
set ampcal=0                              #> RADIO  ampcal=0       0,1
#
set interval=2                            #> SCALE  interval=2     0.05:4:0.05
set refant=7                              #> RADIO  refant=7       1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
set badant=0                              #> CHECK  badant=0       1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0
#
set imsize=0                              #> RADIO  imsize=0       0,64,65,128,129,256,257,512,513,1024,1025
set cell=0                                #> SCALE  cell=0         0:4:0.05
set mfs=0                                 #> RADIO  mfs=0          0,1
set gsv=0                                 #> RADIO  gsv=0          0,1
set systemp=0                             #> RADIO  systemp=0      0,1
set mosaic=0                              #> RADIO  mosaic=0       0,1
set method=clean                          #> RADIO  method=clean   clean,maxen
set niters=250                            #> SCALE  niters=250     0:1000:1
set rms=0.03                              #> SCALE  rms=0.02       0:0.2:0.001
set ds9=0                                 #> RADIO  ds9=0          0,cm,res
set gaincut=0.95                          #> SCALE  gaincut=0.95   0:1:0.01
set win=0                                 #> CHECK  win=0          0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
set nchan=15                              #> SCALE  nchan=15       1:256:1
set casa=0                                #> RADIO  casa=0         0:1

#
foreach arg ($*)
  set $arg
end
set bench=ftp://ftp.astro.umd.edu/pub/carma/data/fringe.3C273.2008jun18.4.miriad.tar.gz
# -----------------------------------------------------------------------

if ($ampcal) then
 set selfcal_options=(options=amp,noscale,apriori)
else
 set selfcal_options=(options=phase)
endif

set invert_options=(options=double)
if ($systemp) set invert_options=$invert_options,systemp
if ($mfs)     set invert_options=$invert_options,mfs
if ($mosaic)  set invert_options=$invert_options,mosaic

if ($bandpassg) then
  set uvcat1_options=()
else
  set uvcat1_options=(options=nocal)
endif

# ------------------------------------------------------------------------

rm -rf vis0 vis1 vis2  vis0.* vis1.* vis2.*  >& /dev/null

set dat=$vis
if ($dat:e == gz) then
  set tmp1=$dat:r
  set tmp2=$tmp1:r
  set dat=$tmp2:t
  if (-d $dat) then
    echo Using local dataset $dat
  else
    tar zxf $dat
    echo Rewrote tar.gz as local dataset $dat
  endif
endif

if (! -d $dat) then
  echo Dataset $dat does not exist
  echo Try grabbing $bench
  exit 1
endif

listobs vis=$dat > vis0.listobs

set select="-source(noise),-auto"
if ($win != 0) then
  set select="$select,win($win)"
endif
uvcat vis=$dat out=vis0 select=$select

if ($badant != 0) then
  uvflag vis=vis0 select="ant($badant)" flagval=flag
endif

# lINE calibration
if ($linecal) linecal vis=vis0
uvcat vis=vis0 out=vis1 

# PASSBAND calibration
mfcal vis=vis1 refant=$refant interval=$interval
gplist vis=vis1 options=phase
gplist vis=vis1 options=amp
uvcat vis=vis1 out=vis2 $uvcat1_options

# GAIN calibration
if ($selfcal) then
  if ($selfcal == 1) then
    selfcal vis=vis2 interval=$interval refant=$refant $selfcal_options 
  else
    mselfcal vis=vis2 interval=$interval refant=$refant $selfcal_options 
  endif
  gplist vis=vis2 options=phase
  gplist vis=vis2 options=amp
endif


echo closure vis=vis2

if ($cell == 0.0) set cell=0
if ($imsize == 0 && $cell != 0) exit 0

#
# ----------------------------------------------------------------------
# Mapping

if ($gsv) puthd in=vis2/senmodel value=GSV type=ascii

set vis=vis2

# HACK test: 
if (0) then
  rm -rf $vis.tmp
  mv $vis $vis.tmp 
  #uvedit vis=$vis.tmp out=$vis ra=3 dec=30
  uvedit vis=$vis.tmp out=$vis ra=12,29,8 dec=2,03,30
endif

# an extra copy
if (1) then
  rm -rf $vis.tmp
  mv $vis $vis.tmp
  uvcat vis=$vis.tmp out=$vis
endif

invert vis=$vis  map=$vis.mp beam=$vis.bm \
    imsize=$imsize cell=$cell \
    line=chan,1,1,$nchan,$nchan \
    $invert_options 


if ($mosaic) then
  if ($method == clean) then
    mossdi map=$vis.mp beam=$vis.bm \
      niters=$niters \
      out=$vis.cc > clean.log
  else
    mosmem map=$vis.mp beam=$vis.bm \
      niters=$niters \
      out=$vis.cc > maxen.log
  endif
  mospsf beam=$vis.bm out=$vis.psf 
  imfit in=$vis.psf object=beam 'region=rel,box(-32,-32,31,31)' > $vis.psf.log
  set bmaj=`grep "Major axis" $vis.psf.log | awk '{print $4}'`
  set bmin=`grep "Minor axis" $vis.psf.log | awk '{print $4}'`
  set bpa=`grep "Position angle" $vis.psf.log | awk '{print $4}'`
  echo $bmaj $bmin $bpa
  restor model=$vis.cc beam=$vis.bm map=$vis.mp \
    out=$vis.cm fwhm=$bmaj,$bmin pa=$bpa
  restor model=$vis.cc beam=$vis.bm map=$vis.mp \
    out=$vis.res fwhm=$bmaj,$bmin pa=$bpa mode=residual

  mossen in=$vis.mp sen=$vis.sen gain=$vis.gain
  maths exp="<$vis.sen>" mask="<$vis.gain>".gt.$gaincut out=$vis.senc
  # maths exp="<$vis.sen>" mask="<$vis.gain>".eq.1.0 out=$vis.senc
else
  if ($method == clean) then
     #  hogbom clark steer any
     clean map=$vis.mp beam=$vis.bm \
        niters=$niters \
        out=$vis.cc > clean.log
  else
     # maximum entropy
     maxen map=$vis.mp beam=$vis.bm \
        niters=$niters rms=$rms \
        out=$vis.cc > maxen.log
  endif
  restor model=$vis.cc beam=$vis.bm map=$vis.mp \
    out=$vis.cm
  restor model=$vis.cc beam=$vis.bm map=$vis.mp \
    out=$vis.res mode=residual
endif

#

if ($ds9 != 0) mirds9 $vis.$ds9

echo IMSTAT on full image, vis.cm
imstat in=$vis.cm
echo IMSTAT on inner quarter, vis.cm
imstat in=$vis.cm "region=quarter"

echo IMSTAT on full image, vis.res
imstat in=$vis.res
echo IMSTAT on inner quarter, vis.res
imstat in=$vis.res "region=quarter"


if ($casa) then
  uvcat vis=$vis out=mir4ms
  carmafiller vis=mir4ms ms=ms
  set py=myclean.py
  echo "# casapy script"             > $py
  echo "# run as:   casapy -c $py"  >> $py
  echo "clean('ms','im')"           >> $py
  echo "viewer('im.image')"         >> $py
  echo $py written, run as:   casapy -c $py
  echo Or paste the following commands into your casapy shell:
  cat $py
endif
