#!/bin/csh -f
# Handling 4GHz uniform data taken from single 340GHz receivers 
#
# Responsible: Jun-Hui Zhao (SAO)
# Version 1.2 
# jhz 2011-03-03 create based on the demo version uni4GHz2009.csh
# jhz 2011-03-07 updated with the identification of 30SiO(7-6) at 296.575 GHz 
# jhz 2011-06-09 changed the data path to CF where the test data located.
# jhz 2011-08-08 added goto TAG
# testing data: 100201_03:31:32 from Ken (Taco) Young
#
# demo illustration figures
# fig1.ps = continuum image of VY_CMa   
# fig2.ps = spectrum from the vis data of VY_CMa 
# fig3.ps = spectrum from  line image cube VY_CMa (30SiO at 296.575 GHz)  
# fig4.ps = image of integrated line flux VY_CMa  (30SiO at 296.575 GHz)
set darea=/sma/SMAusers/jzhao/data/100201_03:31:32
set outf=100201_03
goto SMALOD
#goto INSPECT
#goto SMAFIX
#goto BPASS
#goto GCAL
#goto SMAFLUX
#goto UVLIN0
#goto CMAP
#goto UVLINL
#goto LMCUBE
#goto MOMENT
SMALOD:
echo "1. READ THE MIR DATA INTO MIRIAD"
\rm -r $outf*
smalod in=$darea out=$outf \
rxif=1 sideband=2
$<
INSPECT:
echo "2. INSPECT THE DATA"
uvindex vis=${outf}_rx1.lsb
$<
uvindex vis=${outf}_rx1.usb
$<
smauvplt vis=${outf}_rx1.lsb \
        axis=time,ampl device=/xs nxy=1,1
$<
smauvplt vis=${outf}_rx1.usb \
        axis=time,ampl device=/xs nxy=1,1
$<
smauvspec vis=${outf}_rx1.lsb \
        select='source(VY_CMa)' \
        interval=1000 hann=1 \
        axis=chan,both device=/xs nxy=1,3
$<
smauvspec vis=${outf}_rx1.usb \
        select='source(VY_CMa)' \
        interval=1000 hann=1 \
        axis=chan,both device=/xs nxy=1,3
$<
smauvspec vis=${outf}_rx1.lsb \
        select='source(IK_Tau)' \
        interval=1000 hann=1 \
        axis=chan,both device=/xs nxy=1,3
$<
smauvspec vis=${outf}_rx1.usb \
        select='source(IK_Tau)' \
        interval=1000 hann=1 \
        axis=chan,both device=/xs nxy=1,3

$<
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
$<
SMAFIX:
echo "3. SYSTEM TEMPERATURE CORRECTIONS"
\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         
$<
smablflag vis=${outf}_rx1.lsb.tsys \
        axis=time,ampl device=/xs 
$<
smablflag vis=${outf}_rx1.usb.tsys \
        axis=time,ampl device=/xs
$<
smauvspec vis=${outf}_rx1.usb.tsys \
        select='source(3c273)' \
        interval=1000  \
        axis=freq,both device=/xs nxy=1,5
$<
smauvspec vis=${outf}_rx1.usb.tsys \
        select='source(3c273)' \
        interval=1000  \
        axis=freq,both device=/xs nxy=1,5
$<
\rm -r ${outf}_rx1.lsb.tsys.if1 ${outf}_rx1.lsb.tsys.if2
smasplt vis=${outf}_rx1.lsb.tsys  out=${outf}_rx1.lsb.tsys.if1 options=if1
$<
smasplt vis=${outf}_rx1.lsb.tsys  out=${outf}_rx1.lsb.tsys.if2 options=if2
\rm -r ${outf}_rx1.usb.tsys.if1 ${outf}_rx1.usb.tsys.if2
$<
smasplt vis=${outf}_rx1.usb.tsys  out=${outf}_rx1.usb.tsys.if1 options=if1
$<
smasplt vis=${outf}_rx1.usb.tsys  out=${outf}_rx1.usb.tsys.if2 options=if2
BPASS:
echo '4. BANDPASS CALIBRATION'
$<
smamfcal vis==${outf}_rx1.lsb.tsys.if1  select='source(3c273)' edge=5,5 \
        weight=2 refant=2 interval=60000
$<
smagpplt vis=${outf}_rx1.lsb.tsys.if1  device=/xs \
        yaxis=amp,phase \
        options=bandpass  polyfit=5 \
        nxy=1,8
$<
smamfcal vis=${outf}_rx1.lsb.tsys.if2 select='source(3c273)' edge=5,5  \
        weight=2 refant=2 interval=60000
$<
smagpplt vis=${outf}_rx1.lsb.tsys.if2  device=/xs \
        yaxis=amp,phase \
        options=bandpass  polyfit=5 \
        nxy=1,8
$<
smamfcal vis=${outf}_rx1.usb.tsys.if1 select='source(3c273)' edge=5,5 \
        weight=2 refant=2 interval=60000
$<
smagpplt vis=${outf}_rx1.usb.tsys.if1  device=/xs \
        yaxis=amp,phase \
        options=bandpass  polyfit=5 \
        nxy=1,8
$<
smamfcal vis=${outf}_rx1.usb.tsys.if2 select='source(3c273)' edge=5,5 \
        weight=2 refant=2 interval=60000
$<
smagpplt vis=${outf}_rx1.usb.tsys.if2  device=/xs \
        yaxis=amp,phase \
        options=bandpass  polyfit=5 \
        nxy=1,8
$<
\rm -r ${outf}_rx1.lsb.tsys.if1.bp ${outf}_rx1.lsb.tsys.if2.bp 
uvaver vis=${outf}_rx1.lsb.tsys.if1 options=nocal \
       out=${outf}_rx1.lsb.tsys.if1.bp
$<
uvaver vis=${outf}_rx1.lsb.tsys.if2 options=nocal \
       out=${outf}_rx1.lsb.tsys.if2.bp
$<
\rm -r ${outf}_rx1.usb.tsys.if1.bp ${outf}_rx1.usb.tsys.if2.bp 
uvaver vis=${outf}_rx1.usb.tsys.if1 options=nocal \
       out=${outf}_rx1.usb.tsys.if1.bp
$<
uvaver vis=${outf}_rx1.usb.tsys.if2 options=nocal \
       out=${outf}_rx1.usb.tsys.if2.bp
GCAL:
echo '5. GAIN CALIBRATION'
$<
smamfcal vis=${outf}_rx1.lsb.tsys.if1.bp  \
select='source(0730-116)'  \
refant=2 interval=2 options=nopassol
$<
smagpplt vis=${outf}_rx1.lsb.tsys.if1.bp device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4
$<
smamfcal vis=${outf}_rx1.lsb.tsys.if2.bp  \
select='source(0730-116)'  \
refant=2 interval=2 options=nopassol
$<
smagpplt vis=${outf}_rx1.lsb.tsys.if2.bp device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4

$<
smamfcal vis=${outf}_rx1.usb.tsys.if1.bp  \
select='source(0730-116)'  \
refant=2 interval=2 options=nopassol
$<
smagpplt vis=${outf}_rx1.usb.tsys.if1.bp device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4
$<
smamfcal vis=${outf}_rx1.usb.tsys.if2.bp  \
select='source(0730-116)'  \
refant=2 interval=2 options=nopassol
$<
smagpplt vis=${outf}_rx1.usb.tsys.if2.bp device=/xs \
        yaxis=amp,pha  \
        options=gains  polyfit=6 \
        nxy=2,4
$<
\rm -r ${outf}_rx1.lsb.tsys.if1.bp.g ${outf}_rx1.lsb.tsys.if2.bp.g
uvaver vis=${outf}_rx1.lsb.tsys.if1.bp \
       out=${outf}_rx1.lsb.tsys.if1.bp.g
$<
uvaver vis=${outf}_rx1.lsb.tsys.if2.bp \
       out=${outf}_rx1.lsb.tsys.if2.bp.g
$<
\rm -r ${outf}_rx1.usb.tsys.if1.bp.g ${outf}_rx1.usb.tsys.if2.bp.g
uvaver vis=${outf}_rx1.usb.tsys.if1.bp \
       out=${outf}_rx1.usb.tsys.if1.bp.g
$<
uvaver vis=${outf}_rx1.usb.tsys.if2.bp \
       out=${outf}_rx1.usb.tsys.if2.bp.g
$<
SMAFLUX:
echo '6. FLUX DENSITY SCALE'
$<
smaflux vis=${outf}_rx1.lsb.tsys.if1.bp.g \
	select='source(titan)' mirhome=$MIR
$<
smaflux vis=${outf}_rx1.lsb.tsys.if2.bp.g \
        select='source(titan)' mirhome=$MIR
$<
smaflux vis=${outf}_rx1.usb.tsys.if1.bp.g \
        select='source(titan)' mirhome=$MIR
$<
smaflux vis=${outf}_rx1.usb.tsys.if2.bp.g \
        select='source(titan)' mirhome=$MIR
$<
\rm -r ${outf}_rx1.lsb.tsys.if1.bp.ga ${outf}_rx1.lsb.tsys.if2.bp.ga
uvaver vis=${outf}_rx1.lsb.tsys.if1.bp.g \
       out=${outf}_rx1.lsb.tsys.if1.bp.ga
$<
uvaver vis=${outf}_rx1.lsb.tsys.if2.bp.g \
       out=${outf}_rx1.lsb.tsys.if2.bp.ga
$<
\rm -r ${outf}_rx1.usb.tsys.if1.bp.ga ${outf}_rx1.usb.tsys.if2.bp.ga
uvaver vis=${outf}_rx1.usb.tsys.if1.bp.g \
       out=${outf}_rx1.usb.tsys.if1.bp.ga
$<
uvaver vis=${outf}_rx1.usb.tsys.if2.bp.g \
       out=${outf}_rx1.usb.tsys.if2.bp.ga
$<
smablflag vis=${outf}_rx1.lsb.tsys.if1.bp.ga \
        axis=time,ampl device=/xs
smablflag vis=${outf}_rx1.lsb.tsys.if2.bp.ga \
        axis=time,ampl device=/xs
smablflag vis=${outf}_rx1.usb.tsys.if1.bp.ga \
        axis=time,ampl device=/xs
smablflag vis=${outf}_rx1.usb.tsys.if2.bp.ga \
        axis=time,ampl device=/xs
$<
UVLIN0:
echo "7. LINE-FREE DATA"
\rm -r ${outf}_rx1.lsb.tsys.if1.bp.ga.ch0 
\rm -r ${outf}_rx1.lsb.tsys.if2.bp.ga.ch0 
\rm -r ${outf}_rx1.usb.tsys.if1.bp.ga.ch0
\rm -r ${outf}_rx1.usb.tsys.if2.bp.ga.ch0
uvlin vis=${outf}_rx1.lsb.tsys.if1.bp.ga chans=1,3072 out=${outf}_rx1.lsb.tsys.if1.bp.ga.ch0 \
        order=1 mode=chan0 options=nowin
$<
uvlin vis=${outf}_rx1.lsb.tsys.if2.bp.ga chans=1,3072 out=${outf}_rx1.lsb.tsys.if2.bp.ga.ch0 \
        order=1 mode=chan0 options=nowin
$<
uvlin vis=${outf}_rx1.usb.tsys.if1.bp.ga chans=1,3072 out=${outf}_rx1.usb.tsys.if1.bp.ga.ch0 \
        order=1 mode=chan0 options=nowin
$<
uvlin vis=${outf}_rx1.usb.tsys.if2.bp.ga chans=1,3072 out=${outf}_rx1.usb.tsys.if2.bp.ga.ch0 \
        order=1 mode=chan0 options=nowin
CMAP:
echo "8. CONTINUUM IMAGE"
$<
\rm -r vycma.map vycma.beam vycma.icmp
invert vis=${outf}_rx1.lsb.tsys.if2.bp.ga.ch0,${outf}_rx1.lsb.tsys.if1.bp.ga.ch0,${outf}_rx1.usb.tsys.if2.bp.ga.ch0,${outf}_rx1.usb.tsys.if1.bp.ga.ch0 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
$<
smauvspec vis=${outf}_rx1.usb.tsys.if1.bp.ga hann=7 \
        interval=1000  options=avall,nobase \
        axis=freq,both device=fig2.ps/cps nxy=1,1 select='source(VY_CMa)'
$<
\rm -r vycma.SiO 
uvaver vis=${outf}_rx1.usb.tsys.if1.bp.ga  \
	select='window(17,18,19,20),source(VY_CMa)' out=vycma.SiO 
$<
smauvspec vis=vycma.SiO hann=7 \
        interval=1000  options=avall,nobase \
        axis=chan,both device=/xs nxy=1,1
$<
UVLINL:
echo '8. CONTINUUM FREE DATA'
\rm -r vycma.SiO.lin 
uvlin vis=vycma.SiO chans=1,100,400,500 out=vycma.SiO.lin \
        order=1 mode=line options=nowin
$<
smauvspec vis=vycma.SiO.lin hann=7 \
        interval=1000  options=avall,nobase \
        axis=vel,both device=/xs nxy=1,1
$<
#30SiO(7-6) at 296.575 GHz
puthd in=vycma.SiO.lin/restfreq value=296.575 type=double
$<
smauvspec vis=vycma.SiO.lin hann=7 \
        interval=1000  options=avall,nobase \
        axis=vel,both device=/xs nxy=1,1
$<
LMCUBE:
echo '9. LINE IMAGING'
\rm -r vycma.l.map vycma.l.beam vycma.l.icmp
invert vis=vycma.SiO.lin map=vycma.l.map beam=vycma.l.beam \
        imsize=256,256 cell=0.1 robust=0 \
        options=systemp line=vel,110,-120,3 
$<
clean map=vycma.l.map beam=vycma.l.beam out=vycma.l.icmp gain=0.08 \
        cutoff=0 niters=3500 \
        region=arcsec,'boxes(-1,-1,1,1)(1,110)'
$<
\rm -r vycma.l.icln
restor model=vycma.l.icmp beam=vycma.l.beam map=vycma.l.map  \
        out=vycma.l.icln
$<
cgdisp in=vycma.l.icln type=pixel region=arcsec,'boxes(-10,-10,10,10)(1,110)' \
        xybin=1,1 device=/xs nxy=9,8 options=beambr,wedge,blacklab \
        labtyp=arcsec,arcsec range=0,0,lin,2 cols1=7 csize=1,1
echo "spectrum of the SiO line"
$<
imspect in=vycma.l.icln region=arcsec,'boxes(-0.,-0.,0.,0.)(1,110)' \
	device=fig3.ps/ps yrange=-0.5,5. 
$<
MOMENT:
echo '10. MOMENT'
\rm -r vycma.l.flux
moment in=vycma.l.icln region='quarter(30,80)' out=vycma.l.flux mom=0 \
clip=-10,0.4
$<
echo "Display the restored clean image"
$<
cgdisp in=vycma.l.flux,vycma.l.flux type=c,p \
        region=arcsec,'boxes(-5,-5,5,5)' \
        xybin=1,1 device=fig4.ps/cps 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
