#!/bin/csh -f
# Handling 4GHz hybrid-resolution data taken from single rx
# with ad hoc skipping chunks and also handling data with
# some of the spectral chunk filled with corrupted correlations 
#
# Responsible: Jun-Hui Zhao (SAO)
# Version 1.2  (Dec 13, 2011)
# Version 1.3  (Dec 14, 2011)
# testing data: 111108_09:56:47 from Glen Petitpas
# testing data available: /sma/SMAusers/jzhao/data/111206_10:24:13
#                         PI: SMA
# demo illustration figures
# fig1.ps = visibility plot of time vs. phase for IF1 of USB data
# fig2.ps = visibility plot of time vs. phase for IF2 of USB data
# fig3.ps = Spectrum for both amplitude and phase of USB data
# fig4.ps = Spectrum for both amplitude and phase of LSB data
#################################################################
#
#jhz 2011-12-01 produced first script to pre-calibration process
#               SMA data produced from ad hoc correlator configuration 
#               and map the SMA correlator spectral chunk ID to
#               serial number of spectral windows in Miriad. 
#jhz 2011-12-14 comments on the new features.
#               instruction on the continuing data reduction.
#
#################################################################
# set the path of testing data
#################################################################
set darea=/sci0/data/111108_09:56:47
set outf=111108_09
#set darea=/sma/SMAusers/jzhao/data/111206_10:24:13
#set outf=111206_10
#
#start from smalod
#
goto SMALOD
#goto UVINDX
#goto SMACHECK
#goto PRTHD
#goto SMAUVPLT
#goto SMAUVSPC
#goto SMASPLT
#goto PLOT
SMALOD:
echo " "
echo "1. READ THE MIR DATA INTO MIRIAD"
##############################################################
#
# feature: "options=noskip,flagch0" for transferring
#          all the data from online data, keeping 
#          ch0 (c1) continuum data unflagged.
#
############################################################# 
\rm -r $outf*
smalod in=$darea out=$outf \
rxif=0 sideband=2 options=noskip,flagch0
$<
UVINDX:
echo " "
echo "2. SCANS UVDATA"
#############################################################
#
# new feature: "options=smacorr" to map the SMA correlator spectral 
#               chunk ID to the serial number of spectral windows in Miriad
#
#############################################################
uvindex vis=${outf}_rx0.lsb options=smacorr
$<
uvindex vis=${outf}_rx0.usb options=smacorr
$<
#
SMACHECK:
#
echo " "
echo "3. CHECKS SMADATA AND FLAG BAD CORRELATION"
#############################################################
#
# new feature: "options=visflag visrnge=0.0,1E5" to
#               reject (by flagging bad) the bad correlations
#               outside the given amplitude range.
#
#############################################################

smacheck vis=${outf}_rx0.lsb flagval=flag var=systemp range=0,500 \
	options=visflag visrnge=0.0,1E5
$<
smacheck vis=${outf}_rx0.usb flagval=flag var=systemp range=0,500 \
	options=visflag visrnge=0.0,1E5
PRTHD:
#
echo " "
echo "4. PRTHD UVDATA HEADER AND MAP SMA CORRELATOR CONFIGURATION"
echo "   TO FREQUENCY CONFIGURATION IN MIRIAD FORMAT" 
####################################################################
#
# new feature: "options=smacorr" to map the SMA correlator spectral 
#               chunk ID to the serial number of spectral windows in Miriad
#
####################################################################
prthd in=${outf}_rx0.lsb options=smacorr
$<
$<
prthd in=${outf}_rx0.usb options=smacorr
SMAUVPLT:
#
echo " "
echo "5. PLOT IF1 and IF2 FOR LSB/USB DATA"
#####################################################################
#
# new feature: options='if1' and etc. allowing users
#              to choose spectral cunks from a given 
#              IF for visibility plotting. See inline 
#              doc for the details
#
#######################################################################
echo "IF1-LSB"
smauvplt vis=${outf}_rx0.lsb   \
     axis=time,amp device=/xs nxy=3,7 \
     options='if1' 
$<
echo "IF2-LSB"
smauvplt vis=${outf}_rx0.lsb   \
     axis=time,pha device=/xs nxy=3,7 \
     options='if2'
$<
echo "IF1-USB"
smauvplt vis=${outf}_rx0.usb   \
     axis=time,pha device=fig1.ps/cps nxy=3,7 \
     options='if1'
$<
echo "IF2-USB"
smauvplt vis=${outf}_rx0.usb   \
     axis=time,pha device=fig2.ps/cps nxy=3,7 \
     options='if2'
$<
SMAUVSPC:
echo "6. PLOT SPECTRA"
echo "4GH USB"
smauvspec vis=${outf}_rx0.usb \
        select='source(3c84)' \
        interval=1000 \
        axis=freq,both device=/xs  nxy=1,1 \
        options=nobase,avall
$<
echo "4GH LSB"
smauvspec vis=${outf}_rx0.lsb \
        select='source(3c84)' \
        interval=1000 \
        axis=freq,both device=/xs  nxy=1,1 \
        options=nobase,avall
#
PLOT:
echo " "
echo "8. PLOT SPECTRA with hard copy"
echo "4GH USB"
smauvspec vis=${outf}_rx0.usb \
        select='source(3c84)' \
        interval=1000 \
        axis=freq,both device=fig3.ps/cps  nxy=1,1 \
        options=nobase,avall
$<
echo "4GH LSB"
smauvspec vis=${outf}_rx0.lsb \
        select='source(3c84)' \
        interval=1000 \
        axis=freq,both device=fig4.ps/cps  nxy=1,1 \
        options=nobase,avall
$<
SMASPLT:
#
# split data to IF based
#
\rm -r ${outf}_rx0.lsb.if1 ${outf}_rx0.lsb.if2
smasplt vis=${outf}_rx0.lsb  out=${outf}_rx0.lsb.if1 options=if1
$<
smasplt vis=${outf}_rx0.lsb  out=${outf}_rx0.lsb.if2 options=if2
$<
\rm -r ${outf}_rx0.usb.if1 ${outf}_rx0.usb.if2
smasplt vis=${outf}_rx0.usb  out=${outf}_rx0.usb.if1 options=if1
$<
smasplt vis=${outf}_rx0.usb  out=${outf}_rx0.usb.if2 options=if2
#
#
################################################################################
# The procedure of calibrations for Tsys corrections, bandpass, gain and flux 
# density scale, please refer to the script for High-resolution modes (2.0 GHz)
# http://www.cfa.harvard.edu/sma/miriad/spec/SMAscripts/
# The relevant script can be downloaded from
# http://www.cfa.harvard.edu/sma/miriad/spec/SMAscripts/modes/HiRes/2GHz/Ver2.0/HiRes2GHz.csh
################################################################################
#
exit
