#! /bin/csh -f
#
# select one of two options given below; the default is for OPTION1.
# for OPTION2, users need to remove "#" in front of "goto OPTION2" next line
#goto OPTION2
#
#################################################################
#OPTION1
#for imaging and plotting with calibrated data (INPUTCALEDDATA)
#################################################################
OPTION1:
set OK    = 0 
set dt    = 110221
set hour  = 07:07:30
set sb    = usb
set InputCaledDATA = ${dt}.caled.${sb}
#or
set InputCaledDATA = 110827_041441_NGC7538-IRS1_usb 
                           # for example, calibratedmultisourcetst.uv
                           # 110827_041441_NGC7538-IRS1_usb
                           # 110827_041441_NGC7538-IRS1_lsb
                           # 110526_061914_sgrastar_usb 
                           # 110526_061914_sgrastar_lsb 
set source      = NGC7538- # for example, n7538, NGC7538-, sgrastar, dmtau 
#setup start channel and end channel for continuum imaging
set cstartCH = 1           # start channel
set cendCH   = 6144        # end channel
#setup line free channels
set lchn1    = 2           # start line-free channel at smaller channel side 
                           # of the spectral feature
set lchn2    = 1900        # end line-free channel at smaller channel side
                           # of the spectral feature
set rchn1    = 2400        # start line-free channel at larger channel side 
                           # of the spectral feature
set rchn2    = 5500        # end line-free channel at larger channel side
                           # of the spectral feature
#setup start channel velocity, velocity resolution, number of channels 
#for lcube imaging
set lstartCH = -100        # start channel velocity (km/s)
set resolCH  = 1           # channel resolution in velocity (km/s)
set totalnCH = 200         # number of channels to image
set chrms    = 0.3         # 3 sigma in Jy/beam/chan 
echo 'input calibrated vis= ' $InputCaledDATA
goto ALL                   # for continuum image, plot spectrum, 
                           # line image cube and moment0 
#goto CIMAGE               # for continuum image only
#goto PLOTSPC              # for plot spectrum
#goto UVLIN                # line and continuum separation
#goto LCUBE                # for lcube and moment0
#goto MOMNT                # for moment0
#################################################################
#OPTION2
#for end2end procedure from loading raw SMA archive (RTDC system) 
#to editing, calibrating and imaging
#################################################################
OPTION2:
echo "for end2end procedure from loading raw SMA archive (RTDC system) to editing, calibrating and imaging"
echo "not yet!"
#########################################################################
#User may edit the input variable for OPTION1
#########################################################################
ALL:
echo "for imaging and plotting with calibrated data of" $InputCaledDATA
echo 'source =' $source
set OK=1
#continuum image
CIMAGE:
set imap = ${source}.imap
set beam = ${source}.beam
set icmp = ${source}.icmp
set icln = ${source}.icln
echo $imap $beam $icmp
#setup image size and cell size
set ims = 512
set cel = 0.02
echo $cstartCH
set startLineCh = $cstartCH
echo $cendCH 
@ totalNumbCh = $cendCH - $startLineCh + 1 
\rm -r $imap $beam $icmp $icln
invert vis=$InputCaledDATA map=${source}.imap beam=${source}.beam \
    imsize=$ims cell=$cel sup=0 line=channel,1,$startLineCh,$totalNumbCh \
    select="source("$source")" options=nochk
clean  map=${source}.imap beam=${source}.beam out=${source}.icmp \
       gain=0.1 cutoff=0 niters=2500 region='quarter(1,1)'
restor model=${source}.icmp beam=${source}.beam map=${source}.imap out=${source}.icln
cgdisp in=${source}.icln type=pix region='quarter(1,1)' options=beambr,trlab,3val \
        labtyp=arcsec,arcsec xybin=1,1 device=/xs range=0,0,lin,1
if ($OK == 1 ) then
echo "Do you want to plot the spectrum?  Hit the enter key if yes."
$<
 goto PLOTSPC
endif
exit
PLOTSPC:
uvspec vis=$InputCaledDATA select="source("$source")" options=nobase,avall \
	interval=100000 axis=chan,amp device=/xs nxy=1,1
$<
PLOTSPC:
uvspec vis=$InputCaledDATA select="source("$source")" options=nobase,avall \
        interval=100000 axis=vel,amp device=/xs nxy=1,1
if ($OK == 1 ) then
echo "Do you want to make image cube and momemt0?  Hit the enter key if yes."
$<
goto UVLIN 
exit
UVLIN:
echo "line free channel ", $lchn1,$lchn2,$rchn1,$rchn2
\rm -r $InputCaledDATA.lin
uvlin vis=$InputCaledDATA out=$InputCaledDATA.lin mode=line \
        order=1 chans=$lchn1,$lchn2,$rchn1,$rchn2 options=nowin,nochk
$<
LCUBE:
set imap = ${source}.l.imap
set beam = ${source}.l.beam
set icmp = ${source}.l.icmp
set icln = ${source}.l.icln
#
##setup image size and cell size
set ims=256
set cel=0.02
\rm -r  $imap $beam $icmp $icln
invert vis=$InputCaledDATA map=$imap beam=$beam  \
        imsize=$ims cell=$cel robust=0 options=nochk \
        line=vel,$totalnCH,$lstartCH,$resolCH  select="source("$source")"
clean map=$imap beam=$beam out=$icmp gain=0.1 \
	niters=500 region=quarter"(1",200")"
restor model=$icmp beam=$beam map=$imap out=$icln 
imstat in=$icln region='boxes(1,1,20,20)' 
MOMNT:
set icln = ${source}.l.icln
set mom0 = ${source}.l.m0
\rm -r $mom0
moment in=$icln region='image(1',$totalnCH')' out=$mom0 mom=0 \
clip=-1.0e10,$chrms
cgdisp in=$mom0 type=c \
        region=arcsec,'quarter' \
        xybin=1,1 device=/xs nxy=1,1 options=beambr,wedge,blacklab \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=1.2,1.2 \
        slev=p,1 levs1=7.5,10,15,25,35,45,55,65,75,85,95	
exit
