#!/bin/csh -f
# Handling 4GHz uniform data taken from single 340GHz receivers for 1st chunk (CH0) 
#
# Responsible: Jun-Hui Zhao (SAO)
# Version 1.0 
# jhz 2011-05-02 create the scripts 
# jhz 2011-05-06 update the instruction lines
#
# testing data: 100201_03:31:32 from Ken (Taco) Young.
#
# demo illustration figures
# fig1.ps = Ch0 image of VY_CMa   
#
set darea=/sci0/data/100201_03:31:32
set outf=100201_03
goto continue
continue:
############################################
echo "1. READ THE MIR DATA INTO MIRIAD"
############################################
\rm -r $outf*
smalod in=$darea out=$outf \
rxif=1 sideband=2 options='flagch0'
$<
############################################
echo "2. INSPECT THE DATA"
############################################
uvindex vis=${outf}_rx1.lsb
$<
uvindex vis=${outf}_rx1.usb
$<
smauvplt vis=${outf}_rx1.lsb select='window(1)' \
        axis=time,ampl device=/xs nxy=1,1
$<
smauvplt vis=${outf}_rx1.usb select='window(1)' \
        axis=time,ampl device=/xs nxy=1,1
$<
smavarplt vis=${outf}_rx1.lsb device=/xs \
          xaxis=time \
          yaxis=systemp  nxy=2,4
$<
smavarplt vis=${outf}_rx1.usb device=/xs \
          xaxis=time \
          yaxis=systemp  nxy=2,4
$<
################################################
echo '3. CORRECTIONS FOR FRINGE AMPLITUDE'
################################################
\rm -r ${outf}_rx1.lsb.tsys
smafix vis=${outf}_rx1.lsb out=${outf}_rx1.lsb.tsys \
        device=/xs xaxis=time \
        yaxis=systemp nxy=2,4   \
        options=tsyscorr 
$<
\rm -r ${outf}_rx1.usb.tsys
smafix vis=${outf}_rx1.usb out=${outf}_rx1.usb.tsys \
        device=/xs xaxis=time \
        yaxis=systemp nxy=2,4   \
        options=tsyscorr         
$<
\rm -r ${outf}_rx1.lsb.tsys.ch0 ${outf}_rx1.usb.tsys.ch0
smasplt vis=${outf}_rx1.lsb.tsys  out=${outf}_rx1.lsb.tsys.ch0 \
	options=ch0 
$<
smasplt vis=${outf}_rx1.usb.tsys  out=${outf}_rx1.usb.tsys.ch0 \
	options=ch0

$<
qvack vis=${outf}_rx1.lsb.tsys.ch0 options=anticipate mode=source interval=1 
$<
qvack vis=${outf}_rx1.usb.tsys.ch0 options=anticipate mode=source interval=1 
$<
smablflag vis=${outf}_rx1.lsb.tsys.ch0 options=nobase \
        axis=real,imag device=/xs 
$<
smablflag vis=${outf}_rx1.usb.tsys.ch0 options=nobase \
        axis=real,imag device=/xs
#############################################
echo '4. GAIN CALIBRATIONS'
#############################################
$<
smamfcal vis=${outf}_rx1.lsb.tsys.ch0  \
select='source(0730-116)'  \
refant=2 interval=2 options=nopassol
$<
smagpplt vis=${outf}_rx1.lsb.tsys.ch0 device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4
$<
smamfcal vis=${outf}_rx1.usb.tsys.ch0  \
select='source(0730-116)'  \
refant=2 interval=2 options=nopassol
$<
smagpplt vis=${outf}_rx1.usb.tsys.ch0 device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4
$<
\rm -r ${outf}_rx1.lsb.tsys.ch0.g ${outf}_rx1.usb.tsys.ch0.g
uvaver vis=${outf}_rx1.lsb.tsys.ch0 \
       out=${outf}_rx1.lsb.tsys.ch0.g
$<
uvaver vis=${outf}_rx1.usb.tsys.ch0 \
       out=${outf}_rx1.usb.tsys.ch0.g
$<
#############################################
echo '5. BOOTSTRAPE THE FLUX DENSITY SCALE'
#############################################
$<
smaflux vis=${outf}_rx1.lsb.tsys.ch0.g \
	select='source(titan)' mirhome=$MIR
$<
smaflux vis=${outf}_rx1.usb.tsys.ch0.g \
        select='source(titan)' mirhome=$MIR
\rm -r ${outf}_rx1.lsb.tsys.ch0.ga ${outf}_rx1.usb.tsys.ch0.ga
uvaver vis=${outf}_rx1.lsb.tsys.ch0.g \
       out=${outf}_rx1.lsb.tsys.ch0.ga
$<
uvaver vis=${outf}_rx1.usb.tsys.ch0.g \
       out=${outf}_rx1.usb.tsys.ch0.ga
$<
smablflag vis=${outf}_rx1.lsb.tsys.ch0.ga \
        axis=time,ampl device=/xs
smablflag vis=${outf}_rx1.usb.tsys.ch0.ga \
        axis=time,ampl device=/xs
$<
##############################################
echo "6. CONTINUUM IMAGING"
#############################################
$<
\rm -r vycma.map vycma.beam vycma.icmp
invert vis=${outf}_rx1.lsb.tsys.ch0.ga,${outf}_rx1.usb.tsys.ch0.ga \
        map=vycma.map beam=vycma.beam \
        imsize=512,512 cell=0.1 robust=0 \
        options=systemp select='source(VY_CMa)'
$<
cgdisp in=vycma.map type=pixel region=arcsec,'boxes(-15,-15,15,15)' \
        xybin=1,1 device=/xs nxy=1,1 options=full,beambr,wedge \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=1,1
$<
clean map=vycma.map beam=vycma.beam out=vycma.icmp gain=0.08 \
        cutoff=0 niters=3500 \
        region=arcsec,'boxes(-1,-1,1,1)'
$<
\rm -r vycma.icln
restor model=vycma.icmp beam=vycma.beam map=vycma.map  \
        out=vycma.icln
$<
cgdisp in=vycma.icln type=pixel region=arcsec,'boxes(-10,-10,10,10)' \
        xybin=1,1 device=fig1.ps/cps nxy=1,1 options=beambr,wedge,blacklab \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=1,1
exit
