#! /bin/csh -f
#jhz 2015-10-20
#jhz 2016-01-12
#jhz 2016-01-27
#jhz 2016-01-28 make a templet using sma data:
#               160115_03:45:40
#               processed with Miriad4GHz software - 
#               <a href="https://www.cfa.harvard.edu/sma/miriad/miriad4GHz/SMAMiriad4GHz.html">(SMA-Miriad 1.5.1)</a>
#
#
#
#
#
#
#
#Pre-process 2:Editing & Calibration of ASIC data pre-proccessed SMA new
#              format data produced from hybrid correlator (ASIC+SWARM)
#              since 2014-11-16
echo "                                                                      "
echo "######################################################################"
echo "# swarmcali_a.csh: script to calibrate ASIC data produced from        "
echo "# Pre-processing 1 including data inspection, editing, corrections for"
echo "# atmosphere attenuation in amplitude, bandpass, complex gains and    "
echo "# flux-density scale.                                                 "
echo "# Two options for the output format from this stage: UVFITS or Miriad "
echo "# Then, in principle, the processed SMA data after pre-process 1 and 2"
echo "# can be further processed anywhere else (CASA, Miriad, AIPS and etc.)"
echo "######################################################################"
#
#Target                (font) - 
#Bandpass calibrator   (bcal) -
#Gain calibrator       (gcal, or gcal1, gcal2) -
#Flux scale calibrator (fcal) -
#
echo "                                                                      "
echo "######################################################################"
echo "# Users need to carefully review the following project-depended setup "
echo "# from lines 41 - 86 prior to executing this script                   "
echo "######################################################################"
echo "                                                                      "
#
#set the name of the SMA raw data
#
set file = 160115_03:45:40
#
echo $file        
#
set dt          = 160115 
#
echo ' '
echo 'prefix of data files = ' $dt
echo ' '
#
set hour        = 03:45:40
set sb          = lsb
set nn          = 0 
set rxid        = 0
set rx          = rx${rxid}
set adge        =  7 
set nchan_as    = 6130
set bpsmth      = 3,1,0.9
set maxtsys     = 500
set maxspw      = 2 
set fname       = "$dt""_""$rx".$sb
set oname       = "$dt""_""$rx"
set fcal        = uranus 
set gcal1       = 0721+713
set gcal2       = 0841+708 
set gcal        = 0721+713  
set bcal        = 3C84
set font        = N2403 
set refant      = 1 
set avetime     = 5
set source      = $font
#twogcal    =  T two gain calibrators' scheme
#twogcal    =  F one ain calibrator scheme
set twogcal     = T
#
echo ' '
echo 'using two gain calibrator scheme? ' $twogcal
echo ' '
#
set DATPATH     = ../2015B-S041/
                      # The data path contains the pre-processed output data,
                      # e.g., two tarballs -
                      #            160115_rx0.lsb.tar and 160115_rx0.usb.tar
############################################################################
#Sub-process handles -
############################################################################
#goto DETAR           # DETAR the tarballs produced from Pre-process 1 
#goto INSPECT         # inspect the data
#goto UVCOVER         # check the uv-coverage
#goto UVSPC           # plot uv spectra of bandpass calibrator
#goto SMACHECK        # flag bad system temperature
#goto SMAFIX          # apply Tsys to vis data
#goto QVACK           # elminate first interval after switch source
#goto FRINGE-AMP      # plot fringe amplitude
#goto CLOSUR-PH       # plot closure phase
#goto SMAFLAG         # flag bad vis
#goto BPASS           # do bandpass for ASIC data
#goto GPPLT           # plot bandpass solutions for ASIC data
#goto RCLOSUR-PH      # re-examine the closure phase after bandpass
#goto BPAPPL          # apply bandpass solutions
#goto SMAFLUX         # determine flux density scale and apply the scale to the data
#goto APPFLUX         # apply flux scale
#goto GCAL            # calculate complex gains
#goto GCAL1-ASIC      # calculate complex gains for ASIC data 1 calibrator
#goto GCAL2-ASIC      # calculate complex gains for ASIC data 1 calibrator
#goto UVFITS          # export calibrated data in UVFITS
#goto ZAP             # clean 
#
############################################################################
#
OTHERSIDEBAND:
set fname = "$dt""_""$rx".$sb
DETAR:
echo ${DATPATH}/$fname.tar
\rm -fr ${DATPATH}/$fname.asic ${DATPATH}/$fname.sw1 ${DATPATH}/$fname.sw2
tar xvf ${DATPATH}/$fname.tar 
$<
INSPECT:
echo 'INSPECT: ASIC'
uvindex  vis=$fname.asic

$<
UVCOVER:
echo 'UV-COVERAGE: ASIC'
smauvplt vis=$fname.asic axis=uc,vc \
        options=nobas device=/xs

$<
UVSPC:
echo 'spectra: ASIC'
smauvspec vis=$fname.asic \
        select='source('$bcal')' \
        interval=1000 hann=1 \
        axis=freq,both device=/xs nxy=1,6

$<
SMACHECK:
echo 'check ASIC system temperature:'
smacheck vis=$fname.asic flagval=flag var=systemp \
	range=0,$maxtsys 

$<
SMAFIX:
echo 'Fringe amplitude correction with Tsys: ASIC'
\rm -r $fname.asic.tsys
smafix vis=$fname.asic out=$fname.asic.tsys \
        device=/xs xaxis=time \
        yaxis=systemp nxy=2,4   \
        options=tsyscorr \
        bant=5 gant=1

$<
QVACK:
echo 'quack the integration during source switching: ASIC'
qvack vis=$fname.asic.tsys mode=source interval=1

$<
FRINGE-AMP:
echo 'Fringe: ampl'
smauvplt  vis=$fname.asic.tsys axis=time,ampl options=nobas device=/xs

$<
CLOSUR-PH:
echo 'Closure: phase'
closure vis=$fname.asic.tsys select='source('$gcal')' \
        interval=1 device=/xs nxy=4,7 line=chan,1,1,$nchan_as,1

$<
SMAFLAG:
echo 'baseline-based vis flag: ASIC'
smablflag vis=$fname.asic.tsys axis=time,ampl options=nobas device=/xs

$<
BPASS:
echo 'solving for antenna-based bandpass: ASIC'
smamfcal vis=$fname.asic.tsys select='source('$bcal')' edge=$adge,$adge \
     weight=2 refant=$refant interval=1000000 options=msmooth smooth=$bpsmth 

$<
GPPLT:
smagpplt vis=$fname.asic.tsys device=/xs \
        yaxis=amp \
        options=bandpass  \
        nxy=2,4 yrange=0,2

$<
smagpplt vis=$fname.asic.tsys device=/xs \
        yaxis=phase \
        options=bandpass  \
        nxy=2,4 yrange=-180,180

$<
RCLOSUR-PH:
echo 'Re-exam Closure phase: ASIC'
closure vis=$fname.asic.tsys select='source('$gcal')' \
        interval=1 device=/xs nxy=4,7 line=chan,1,1,$nchan_as,1

$<
BPAPPL:
echo 'Apply BP solutions: ASIC'
\rm -r $fname.asic.tsys.bp
uvaver vis=$fname.asic.tsys out=$fname.asic.tsys.bp


$<
SMAFLUX:
echo 'Determine flux scale: ASIC'
smaflux vis=$fname.asic.tsys.bp \
        select='source('$fcal')' mirhome=$MIR

$<
APPFLUX:
\rm -r $fname.asic.tsys.bp.a
echo 'Apply flux scale solution: ASIC'
uvaver vis=$fname.asic.tsys.bp out=$fname.asic.tsys.bp.a \
        options=nopass

$<
GCAL:
echo 'Two gain calibrator = ' $twogcal
if ($twogcal == 'F') then
GCAL1-ASIC:
echo 'Gain calibration one-calibrator scheme: ASIC'
$<
mfcal vis=$fname.asic.tsys.bp.a  \
        select='source('$gcal')' edge=$adge,$adge refant=$refant interval=5 \
        options=nopassol 

$<
smagpplt vis=$fname.asic.tsys.bp.a device=/xs \
        yaxis=amp \
        options=gains  \
        nxy=2,4 yrange=0,2

$<
smagpplt vis=$fname.asic.tsys.bp.a device=/xs \
        yaxis=phase \
        options=gains  \
        nxy=2,4 yrange=-180,180
endif

$<
if ($twogcal == 'T') then
GCAL2-ASIC:
echo 'Gain calibration two-calibrator scheme: ASIC'

$<
\rm -r gcal2.a
cp -r $fname.asic.tsys.bp.a gcal2.a

$<
mfcal vis=$fname.asic.tsys.bp.a  \
        select='source('$gcal1')' edge=$adge,$adge refant=$refant interval=5 \
        options=nopassol  

$<
gpedit vis=$fname.asic.tsys.bp.a options=ampl

$<
smagpplt vis=$fname.asic.tsys.bp.a device=/xs \
        yaxis=amp \
        options=gains  \
        nxy=2,4 yrange=0,2

$<
smagpplt vis=$fname.asic.tsys.bp.a device=/xs \
        yaxis=phase \
        options=gains  \
        nxy=2,4 yrange=-180,180

$<
mfcal vis=gcal2.a  \
        select='source('$gcal2')' edge=$adge,$adge refant=$refant interval=5 \
        options=nopassol 

$<
gpedit vis=gcal2.a options=ampl 

$<
smagpplt vis=gcal2.a device=/xs \
        yaxis=amp \
        options=gains  \
        nxy=2,4 yrange=0,2

$<
smagpplt vis=gcal2.a device=/xs \
        yaxis=phase \
        options=gains  \
        nxy=2,4 yrange=-180,180
$<
gpcopy vis=gcal2.a out=$fname.asic.tsys.bp.a mode=merge
echo 'Merged gain solutions from two calibrators'

$<
smagpplt vis=$fname.asic.tsys.bp.a device=/xs \
        yaxis=amp \
        options=gains  \
        nxy=2,4 yrange=0,2

$<
\rm -r $fname.asic.tsys.bp.p
uvaver vis=$fname.asic.tsys.bp.a out=$fname.asic.tsys.bp.p

$<
mfcal vis=$fname.asic.tsys.bp.p  \
        select='source('$gcal2,$gcal1')'  refant=$refant interval=1 \
        options=nopassol edge=$adge,$adge

$<
gpedit vis=$fname.asic.tsys.bp.p options=phase

$<
gpplt vis=$fname.asic.tsys.bp.p device=/xs \
        yaxis=phase \
        options=gains  \
        nxy=2,4 yrange=-180,180

$<
\rm -r $fname.asic.tsys.bp.a
uvaver vis=$fname.asic.tsys.bp.p out=$fname.asic.tsys.bp.a \
	options=nopass
endif

$<
echo 'Done with editing and calibrations for' $sb 'band!'
echo ''
echo "#############################################################"
echo 'Calibrated Miriad data file = ' $fname.asic.tsys.bp.a
echo 'Use this data file to continue the imaging process in Miriad!'
echo "#############################################################"
echo ''

$<
UVFITS:
echo 'Export calibrated UVFITS: ASIC'
\rm -r $fname.asic.cal.uvfits
fits in=$fname.asic.tsys.bp.a op='uvout' out=$fname.asic.cal.uvfits

$<
echo 'Export UVFITS done'
echo ''
echo "#############################################################"
echo 'Output UVFITS = '$fname.asic.cal.uvfits
echo 'Use this data file to continue the imaging process elsewhere!'
echo "#############################################################"
echo ''

$<
#
#switching sideband from lsb to usb
#
if ($sb == lsb) then
set sb = usb
set nn = 1
goto OTHERSIDEBAND
endif

$<
echo ''
echo 'Done with pre-process 2, congratulation!'
echo ''
exit

ZAP:
\rm -r $fname.asic.tsys.bp $fname.asic.tsys.bp.p $fname.asic.tsys $fname.asic gcal.a
exit
#
# end of pre-process 2
#
