#!/bin/csh -f
# jhz 2010-01-20 for e2e test 
# jhz 2010-03-11 updated
# jhz 2010-05-07 updated
# jhz 2010-08-21 updated
# jhz 2010-10-18 updated
# jhz 2010-12-11 updated
# jhz 2011-01-30 updated
# for the reduction of SMA DB data (standard 4GHz mode).
set darea=/sma/SMAusers/jzhao/data/090218_04:25:36
set out=090218
goto continue
echo "1. READ THE MIR DATA INTO MIRIAD"
continue:
\rm -r  ${out}_rx0.usb
smalod in=$darea out=${out} rxif=0 sideband=1  
$<
echo "2. INSPECT THE RAW UV DATA"
uvindex vis=${out}_rx0.usb
$<
smacheck vis=${out}_rx0.usb var=systemp flagval=flag \
        range=0,500
$<
echo "3. FRINGE AMPLITUDE CORRECTION"
\rm -r ${out}_rx0.usb.tsys
smafix vis=${out}_rx0.usb out=${out}_rx0.usb.tsys \
	device=/xs xaxis=time \
       	yaxis=systemp nxy=2,4   \
       	options=tsyscorr,2GHz
#editing and inspection
smablflag vis=${out}_rx0.usb.tsys axis=time,ampl device=/xs options=nobase 
$<
smauvspec vis=${out}_rx0.usb.tsys \
	select='source(3c273)' \
        interval=1000  \
        axis=freq,both device=/xs nxy=1,5
$<
smauvspec vis=${out}_rx0.usb.tsys \
        select='source(titan)' \
        interval=1000  \
        axis=freq,both device=/xs nxy=1,5
echo "4. BANDPASS CALIBRATIONS"
$<
\rm -r ${out}_rx0.usb.tsys.48
smasplt vis=${out}_rx0.usb.tsys  out=${out}_rx0.usb.tsys.48 
$<
smamfcal vis=${out}_rx0.usb.tsys.48 select='source(3c273)' edge=5,5  \
        weight=2 refant=3 interval=60000
$<
smagpplt vis=${out}_rx0.usb.tsys.48  device=/xs \
        yaxis=amp,phase \
        options=bandpass  polyfit=5 \
        nxy=1,7
$<
\rm -r ${out}_rx0.usb.tsys.48.bp
uvaver vis=${out}_rx0.usb.tsys.48 options=nocal \
        out=${out}_rx0.usb.tsys.48.bp
$<
uvflag vis=${out}_rx0.usb.tsys.48.bp \
	line=chan,1,5409,1 \
        flagval=flag
$<
smauvspec vis=${out}_rx0.usb.tsys.48.bp \
        select='source(3c273)'  \
        interval=1000  \
        axis=freq,both device=/xs nxy=3,5
$<
echo "5. GAIN CALIBRATION"
smamfcal vis=${out}_rx0.usb.tsys.48.bp  \
select='source(0730-116)'  \
refant=3 interval=2 options=nopassol
$<
smagpplt vis=${out}_rx0.usb.tsys.48.bp device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4
$<
\rm -r ${out}_rx0.usb.tsys.48.bp.g
uvaver vis=${out}_rx0.usb.tsys.48.bp out=${out}_rx0.usb.tsys.48.bp.g
$<
echo "6. BOOTSTRAPE THE FLUX DENSITY SCALE"
smaflux vis=${out}_rx0.usb.tsys.48.bp.g \
	select='source(titan)' mirhome=$MIR  
$<
\rm -r ${out}_rx0.usb.tsys.48.bp.g.av
uvaver vis=${out}_rx0.usb.tsys.48.bp.g out=${out}_rx0.usb.tsys.48.bp.g.av 
$<
\rm -r vycmasma.244409 vycmasma.line
uvsplit vis=${out}_rx0.usb.tsys.48.bp.g.av select='source(vycmasma)' \
	options=nowindow
$<
puthd in=vycmasma.244409/restfreq value=244.935643 type=double
$<
\rm -r  vycmasma.CS
mv vycmasma.244409 vycmasma.CS 
echo "7. L/C SEPARATION"
$<
\rm -r vycmasmaUSB.lin
uvlin vis=vycmasma.CS chans=1,700,1000,1600,2000,6000 out=vycmasmaUSB.lin \
        order=1 mode=line options=nowin
$<
\rm -r vycmasmaUSB.ch0
uvlin vis=vycmasma.CS chans=1,700,1000,1600,2000,6000 out=vycmasmaUSB.ch0 \
        order=1 mode=chan0 options=nowin
echo "8. CONTINUUM IMAGE"
$<
\rm -r vycmasma.map vycmasma.beam vycmasma.icmp
invert vis=vycmasmaUSB.ch0 map=vycmasma.map beam=vycmasma.beam \
        imsize=512,512 cell=0.3 robust=0 \
        options=nocal
$<
cgdisp in=vycmasma.map type=pixel region=arcsec,'boxes(-15,-15,15,15)' \
        xybin=1,1 device=/xs nxy=1,1 options=full,beambr,wedge,trlab,3val \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=0.5,0.5,0.5
$<
clean map=vycmasma.map beam=vycmasma.beam out=vycmasma.icmp gain=0.08 \
        cutoff=0 niters=3500 \
        region=arcsec,'boxes(-5,-5,5,5)'
$<
\rm -r vycmasma.icln
restor model=vycmasma.icmp beam=vycmasma.beam map=vycmasma.map  \
        out=vycmasma.icln
$<
cgdisp in=vycmasma.icln type=pixel region=arcsec,'boxes(-15,-15,15,15)' \
        xybin=1,1 device=/xs nxy=1,1 options=beambr,trlab \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=0.5,0.5,0.5
echo "9. CONSTRUCT LINE IMAGE CUBE" 
\rm -r vycmasmaCS.map vycmasmaCS.beam vycmasmaCS.icmp
invert vis=vycmasmaUSB.lin map=vycmasmaCS.map beam=vycmasmaCS.beam \
        imsize=512,512 cell=0.3 robust=2 \
        options=systemp line=vel,25,-25,4
$<
clean map=vycmasmaCS.map beam=vycmasmaCS.beam out=vycmasmaCS.icmp gain=0.08 \
        cutoff=0 niters=3500 \
        region=arcsec,'boxes(-5,-5,5,5)(1,25)'
$<
\rm -r vycmasmaCS.icln
restor model=vycmasmaCS.icmp beam=vycmasmaCS.beam map=vycmasmaCS.map  \
        out=vycmasmaCS.icln
$<
cgdisp in=vycmasmaCS.icln type=c region=arcsec,'boxes(-10,-10,10,10)(1,25)' \
        xybin=1,1 device=/xs nxy=5,5 options=beambr,3val,corner \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=1,1,1 slev=p,1 levs1=-30,30,40,50,60,70,80,90
echo "The end"
exit
