#! /bin/csh -f
set dt    = 110904
set hour = 03:52:01
set sb    = usb 
set nn    = 1 
set rxid  = 1 
set rx    = rx${rxid}
set edge  = 2 
set bwmode  = 4   
set smplnt = 1  
set fname = "$dt""_""$rx".$sb
echo $fname
#set fcal = saturn
#set fcal = ganymede
#set fcal = titan
#set fcal = uranus
set fcal = callisto
#set fcal = venus
#set fcal = jupiter
#set fcal = neptune
#set fcal = mars
#set gcal  = 1337-129 
#set gcal  = 1733-130
#set gcal  = 1743-038
#set gcal  = 1744-312
#set gcal  = 1749+096
#set gcal  = 1751+096
#set gcal  = 1830+063
#set gcal  = 1924-292
#set gcal = nrao530
#set gcal  = sgrastar
#set gcal = bllac
#set gcal  = 3c273
#set gcal  = 3c279
#set gcal  = 3c286
#set gcal  = 3c345
#set gcal  = 3c454.3
#set gcal  = I18360
#set gcal = whyasio 
#set gcal = 0854+201
#set gcal = 0927+390
set  gcal = 3c84 
#set  gcal = 0319+415
#BCAL + PCAL
#set bcal  = 3c454.3 
#set bcal = venus
#set bcal = 3c279,1325-430
#set bcal = 3c279
#set  bcal = 3c84,mars
#set  bcal = 3c84,3c279
set bcal=callisto,3c84
#set bcal=uranus,3c84 
#set bcal=neptune,3c84
#set bcal=neptune,0319+415
#set  bcal = 3c84
set  font  = 3c84
#set font = 0319+415
#set font  = 1055+018
#set font  = 1337-129
#set font  = 1733-130
#set font  = 1743-038
#set font  = 1744-312
#set font  = 1749+096
#set font  = 1751+096
#set font  = 1830+063
#set font  = 1924-292
#set font  = 3c273
#set font  = 3c279
#set font  = 3c286
#set font  = 3c345
#set font  = 3c454.3
#set font  = nrao530
#set font  = sgrastar
#set font  = bllac
#set font  = G35.2N
#set font  = I18360
#set font = whyasio 
#set font = 0854+201
#set font = 0927+390
set refant  = 3
set avetime = 5
set source  = $font
set flxvis  = $source"_"$sb.tsys.bp.flx.gcal
set imsize  = 2048 
set cell    = 0.1
set rob     = 0
set clreg   = 'arcsec,box(-10,-10,10,10)'
set clcut1  = 0.10
set clcut2  = 0.02
set disreg  = 'arcsec,box(-18,-18,18,18)'
set statreg_ons = 'arcsec,box(-3,-3,3,3)'
set statreg_ofs = 'arcsec,box(-11,-11,-6,-6)'
set irms    = 0.2
set qurms   = 0.025  
set vrms    = 0.125
###
#goto SMALOD         # load the data
#goto COMBINE        # combine
#goto INSPECT        # inspect the data
goto TSYSFIX        # tsys correction
#goto SMASPLT        # smasplt the 4GHz data
#goto FLAG1          # flagging-1
#goto FLAG2          # flagging-2
#goto FLAG3          # flagging-3
#goto APPLYF         # apply flagging
#goto BPCAL          # bandpass calibration
goto APPLYBP        # apply bandpass solutions
#goto FLUX           # derive flux scale
goto SPLIT          # split data into single source files
goto GCAL           # complex gain calibration
goto DMAP           # do polarization images
goto SAVE
goto ZAP
SMALOD:
#set DATPATH =  /2012/flux/mir_data/
#set DATPATH =  /2012/pointing/mir_data/
#set DATPATH =  /2012/baseline/mir_data/
#set DATPATH =  /2012/engineering/mir_data/
#set DATPATH =  /2012/priming/mir_data/
#set DATPATH =  /2012/science/mir_data/ 
set DATPATH =  /2011/science/mir_data/
#set DATPATH =  /2011/priming/mir_data/
#set DATPATH =  /2011/flux/mir_data/
#set DATPATH =  /2011/baseline/mir_data/
#set DATPATH =  /2011/engineering/mir_data/
#set DATPATH =  /2011/pointing/mir_data/
echo $fname
\rm -fr $fname
smalod in=${DATPATH}/$dt"_"$hour out=$dt rxif=$rxid \
   options=circular,ipoint sideband=$nn  
 set lab=tsys.bp.flx
 set out=tsys.bp.flx.gcal
 set out2=tsys.bp.flx.gcal.pol
exit
COMBINE:
\rm -r $fname
uvaver vis=a$fname,b$fname out=$fname
INSPECT:
uvindex  vis=$fname 
smacheck vis=$fname var=systemp flagval=flag range=0,1000
smauvplt vis=$fname axis=time,ampl device=/xs nxy=1,1
exit
TSYSFIX:
smablflag vis=$fname axis=time,systemp device=/xs  options=noba 
\rm -fr    $fname.tsys
smafix vis=$fname out=$fname.tsys device=/xw nxy=1,1 \
       options=tsyscorr,dosour
########################
SMASPLT:
if  ($bwmode == 4) then
set if = ifsall
#set if = if1
\rm -r tmp.uv
smasplt vis=$fname.tsys out=tmp.uv options=$if
\rm -r $fname.tsys
mv tmp.uv $fname.tsys
endif
########################
FLAG1:
uvflag vis=$fname.tsys flagval=f edge=$edge,$edge 
FLAG2:
foreach src ($bcal $gcal $fcal)
 echo; echo $src
 blflag vis=$fname.tsys axis=time,ampl device=/xs select='sou('$src')'
end

APPLYF:
\rm -fr $fname.gcal
uvcal  vis=$fname.tsys options=unflagged out=$fname.gcal

#BPCAL:
smauvspec vis=$fname.gcal select='sou('$bcal')' \
          device=1/xs interval=9999 axis=freq,both nxy=1,1 options=nopass
smablflag vis=$fname.gcal select='sou('$bcal')' axis=time,ampl device=/xs options=noba
BPCAL:
uvflag vis=$fname.gcal select='ant(2),window(25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48)' flagval=f
smamfcal select='sou('$bcal')' refant=$refant \
	interval=3000 weight=2 \
        vis=$fname.gcal edge=$edge,$edge options=msmooth,averrll smooth=3,1,0.9 
smagpplt vis=$fname.gcal select='sou('$bcal')' \
         device=/xs yaxis=phase,amp \
         nxy=2,4 options=bandpass polyfit=3
$<
smauvspec vis=$fname.gcal select='sou('$bcal')' \
          device=1/xs interval=9999 axis=freq,both nxy=1,1 options=nopass
$<
smauvspec vis=$fname.gcal select='sou('$bcal')' \
          device=2/xs interval=9999 axis=freq,both nxy=1,1
APPLYBP:
\rm -fr    $fname.gcal.bp
uvaver vis=$fname.gcal out=$fname.gcal.bp options=nocal
FLUX:
blflag vis=$fname.gcal.bp axis=uvdistance,amp device=/xs  options=noba select='so('$fcal')'
smablflag vis=$fname.gcal.bp axis=time,systemp device=/xs  options=noba select='so('$fcal')'
if  ($smplnt == 1) then
selfcal vis=$fname.gcal.bp select='so('$fcal')' options=pha interval=3
puthd in=$fname.gcal.bp/interval type=double value=1000.0
\rm -r tmp.uv
uvaver vis=$fname.gcal.bp out=tmp.uv
\rm -r $fname.gcal.bp
mv tmp.uv $fname.gcal.bp
smaflux vis=$fname.gcal.bp select='so('$fcal')' mirhome=$MIR options=vector
else
if ($smplnt == 0) then
smaflux vis=$fname.gcal.bp select='so('$fcal')' mirhome=$MIR
endif
if ($smplnt == -1) then
smaflux vis=$fname.gcal.bp select='so('$fcal')' mirhome=$MIR \
 options=nofqav
endif

endif
SPLIT:
foreach so ($fcal $gcal $font)
\rm -fr $so'_'$sb.tsys.bp.flx tmp.uv
uvaver vis=$fname.gcal.bp select='sou('$so')' \
         out=tmp.uv 
uvlin vis=tmp.uv order=1 mode=chan0 options=nowin out=$so'_'$sb.tsys.bp.flx 
end

GCAL:
set vis=$gcal'_'$sb.tsys.bp.flx
selfcal vis=$vis refant=${refant} \
	 options=pha interval=0.5 
smagpplt vis=$vis device=1/xs yaxis=amp,pha nxy=2,4
smauvplt vis=$vis device=1/xs axis=tim,pha nxy=2,3
smauvplt vis=$vis device=1/xs axis=tim,pha nxy=2,3
FLAG3:
set vis=$gcal'_'$sb.tsys.bp.flx
blflag vis=$vis axis=uvdistance,ampl device=/xs options=noba
blflag vis=$vis axis=time,ampl device=/xs options=noba
blflag vis=$vis axis=time,phas device=/xs options=noba
APPLYG:
gpcopy  vis=$gcal"_"$sb.tsys.bp.flx out=$font"_"$sb.tsys.bp.flx mode=copy
\rm -rf $font"_"$sb.tsys.bp.flx.g
uvaver  vis=$font"_"$sb.tsys.bp.flx out=$font"_"$sb.tsys.bp.flx.g interval=${avetime} 
set vis=$font'_'$sb.tsys.bp.flx.g
smauvplt vis=$vis device=/xs axis=real,imag options=noba
selfcal vis=$vis refant=${refant} \
        options=amp \
        interval=0.5
uvflux vis=$vis options=point
DMAP:
set source=$font
set flxvis  = $source"_"$sb.tsys.bp.flx.g
$<
uvflux vis=$flxvis options=point
blflag vis=$flxvis axis=time,ampl device=/xs options=noba 
blflag vis=$flxvis axis=time,phas device=/xs options=noba
uvflux vis=$flxvis options=point
$<
$<
\rm -fr $source.$sb.i $source.$sb.b
invert vis=$flxvis beam=$source.$sb.b \
	imsize=$imsize cell=$cell \
	map=$source.$sb.i \
	options=systemp  \
	robust=$rob
echo 'Press return to continue'; $<
set dev=dirty_map.ps/cps
set dev=/xs
cgdisp in=$source.$sb.i,$source.$sb.i type=p,c \
	device=$dev \
	labtyp=arcsec nxy=1,1 slev=p,10 \
	levs1=1,2,3,4,5,6,7,8,9 \
	options=wedge,mirror region=$disreg

CLEAN:
#######################################
echo ' Clean dirty images for four stokes'
echo ' Press return to continue'; $<
#######################################
\rm -fr $source.$sb.icmp
clean map=$source.$sb.i beam=$source.$sb.b out=$source.$sb.icmp \
	region=$clreg gain=0.1  niters=500 cutoff=$clcut1
set stk=i
\rm -fr $source.$sb.$stk'cln'
restor map=$source.$sb.$stk beam=$source.$sb.b out=$source.$sb.$stk'cln' \
	model=$source.$sb.$stk'cmp'
DISPLAY:
#######################################
echo 'Display Stokes I image '
#######################################
set dev=$font'_'i.ps/cps 
set dev=/xs
cgdisp in=$source.$sb.icln,$source.$sb.icln type=p,c \
       device=$dev \
       labtyp=arcsec nxy=1,1 slev=p,10 levs1=1,2,3,4,5,6,7,8,9 \
       options=wedge,mirror,blacklab cols1=7 \
       region=$disreg range=0,0,lin,2
STAT:
########################################################
echo 'Make statistics from the images'
########################################################
foreach stk (i)
echo 'Stokes  '$stk'. Press return to continue'; $<
imstat in=$source.$sb.$stk'cln' region=$statreg_ons 
end
$<
foreach stk (i)
echo 'Stokes  '$stk'. Press return to continue'; $<
imstat in=$source.$sb.$stk'cln' region=$statreg_ofs
end
exit
SAVE:
set flxvis  = $source"_"$sb.tsys.bp.flx.g
\rm -r $source"_"${dt}.$sb.tsys.bp.flx.g
mv $source"_"$sb.tsys.bp.flx.g $source"_"${dt}.$sb.tsys.bp.flx.g
cp FLUX.csh FLUX${dt}.csh
exit
ZAP:
\rm -fr ${dt}_*


