#!/bin/csh -f
# jhz
# Test bandpass solver using Cepha data at 321 GHz
# observed on 040830 
goto continue
#run
#
#load data and convert mir format to miriad format
smalod in=/home/miriad/SMAdata/data/sma/040830_04:59:20/ \
         out=cepha.l rxif=1 sideband=0 nscans=2
exit
\rm -r cepha.l_rx1.lsb.tsys
smafix vis=cepha.l_rx1.lsb out=cepha.l_rx1.lsb.tsys device=/xs xaxis=time \
        yaxis=systemp bant=6 gant=1 rant=10 nxy=4,2 \
        yrange=0,1200 rmsflag=2 dofit=2 \
        options=tsyscorr,dosour
#check tsys
smavarplt vis=cepha.l_rx1.lsb.tsys device=/xs xaxis=time \
        yaxis=systmp nxy=4,2 yrange=0,1200
exit
\rm -r cepha.l3to9.lsb.tsys
uvaver vis=cepha.l_rx1.lsb.tsys select='window(3,4,5,6,7,8,9)' \
        options=nocal,nopass out=cepha.l3to9.lsb.tsys
set i=1
\rm -r nep*.322114 
#
# split the sources for testing bandpass
#
#continue:
uvsplit vis=cepha.l3to9.lsb.tsys select='source(sat*)' \
	options=nocopy,nowindow
#continue:
# plplt plot planet model
# create uvmodel for each of the planets assume
# pointing at the center of planet disk and no
# limb brightness
#saturn
plplt source=saturn epoch=04aug30 \
        device=/xs freq=322.114 uvrange=0,240
\rm -r satmodel.uv
#
# model maj=17.06" min=15.64"  PA= -6.75
# Tb= 137 K, no limb darkening.
#using the same uv coverage as the real data
#
uvplanet vis=saturn.322114 out=satmodel.uv \
        select='source(sat*)' pltb=137 \
        options=nocal,nopass,replace
#
#plot model data
#
continue:
smauvplt vis=saturn.uv axis=time,ampl \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
exit
sleep 3
smauvplt vis=satmodel.uv axis=uvdistance,ampl \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
sleep 3
smauvplt vis=satmodel.uv axis=uvdistance,pha \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
sleep 3
smauvplt vis=satmodel.uv axis=real,imag \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
sleep 3
exit
#
# the handling model data
# compare weight methods
echo 'solve for antenna-based bandpass'
echo 'weight =-1'
# weight =-1; uniformly weighting the visibility
smamfcal vis=satmodel.uv \
    select='source(sat*)' \
    refant=1 interval=16000 \
    weight=-1 options=interpolate,opolyfit,wrap \
    polyfit=5,0
smagpplt vis=satmodel.uv device=/xs yaxis=amp \
    options=bandpass,wrap polyfit=5,0 \
    nxy=2,4 yrange=.85,1.15
sleep 10
smagpplt vis=satmodel.uv device=/xs yaxis=phase \
    options=bandpass \
    nxy=2,4
sleep 3
echo 'solve for antenna-based bandpass'
echo 'weight =1'
# weight =1; weighted by ampl_ave**2/sigma**2
smamfcal vis=satmodel.uv \
    select='source(sat*)' \
    refant=1 interval=16000 \
    weight=1 options=interpolate,opolyfit,wrap \
    polyfit=5,0
smagpplt vis=satmodel.uv device=/xs yaxis=amp \
    options=bandpass,wrap polyfit=5,0 \
    nxy=2,4 yrange=.85,1.15
sleep 10
smagpplt vis=satmodel.uv device=/xs yaxis=phase \
    options=bandpass \
    nxy=2,4
sleep 3
echo 'solve for antenna-based bandpass'
echo 'weight =2'
# weight =2; weighted by ampl_ave**4/sigma**4
smamfcal vis=satmodel.uv \
    select='source(sat*)' \
    refant=1 interval=16000 \
    weight=2 options=interpolate,opolyfit,wrap \
    polyfit=5,0
smagpplt vis=satmodel.uv device=/xs yaxis=amp \
    options=bandpass,wrap polyfit=5,0 \
    nxy=2,4 yrange=.85,1.15
sleep 10
smagpplt vis=satmodel.uv device=/xs yaxis=phase \
    options=bandpass \
    nxy=2,4
sleep 3
# the handling SMA data
cp -r saturn.322114 saturn.uv
# flag antenna 6
#uvflag vis=saturn.uvflag select='ant(6)' \
#	flagval=flag
#
#plot real SMA data
#
sleep 3
smauvplt vis=saturn.uv axis=uvdistance,ampl \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
sleep 3
smauvplt vis=saturn.uv axis=uvdistance,pha \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
sleep 3
smauvplt vis=saturn.uv axis=real,imag \
        options=nobase,nocal,nopass \
        device=/xs nxy=1,1
sleep 3
exit
continue:
echo 'solve for antenna-based bandpass'
# weight =-1; uniformly weighting the visibility
echo 'weight=-1'
smamfcal vis=saturn.uv \
    select='source(sat*)' \
    refant=1 interval=16000 \
    weight=-1 options=interpolate,wrap 
smagpplt vis=saturn.uv device=/xs yaxis=amp \
    options=bandpass,wrap polyfit=5,0 \
    nxy=2,4 yrange=-1,3
sleep 10 
smagpplt vis=saturn.uv device=/xs yaxis=phase \
    options=bandpass \
    nxy=2,4
# weight =2; uniformly weighting the visibility
#            by ~ ampl_ave**4/sigma**4
echo 'weight=2'
smamfcal vis=saturn.uv \
    select='source(sat*)' \
    refant=1 interval=16000 \
    weight=2 options=interpolate,wrap 
smagpplt vis=saturn.uv device=/xs yaxis=amp \
    options=bandpass,wrap polyfit=5,0 \
    nxy=2,4 yrange=-1,3
sleep 10
smagpplt vis=saturn.uv device=/xs yaxis=phase \
    options=bandpass \
    nxy=2,4
# weight =2; uniformly weighting the visibility
#            by ~ ampl_ave**4/sigma**4
# polynomial fit the solutions in SMAMFCAL
echo 'weight=2'
smamfcal vis=saturn.uv \
    select='source(sat*)' \
    refant=1 interval=16000 \
    weight=2 options=interpolate,opolyfit,wrap \
    polyfit=5,0
smagpplt vis=saturn.uv device=/xs yaxis=amp \
    options=bandpass,wrap polyfit=5,0 \
    nxy=2,4 yrange=-1,3
exit
