#! /bin/csh -f
#
#jhz 2015-07-20
#jhz 2015-07-22
#jhz 2015-07-23
#jhz 2015-07-24
#jhz 2015-07-24 for swarm+asic data
#jhz 2016-06-22 for two quadrant swarm + asic
#jhz 2017-09-15 for swarm only in dual receivers
#jhz 2017-10-19 add a note for Miriad versions for this script 
date
echo "                                                                      "
echo "######################################################################"
echo "#   Input: SMA online archived data                                   "
echo "#   Tool:  Miriad SMA-WB5.0.0 and more recent versions                "
echo "#                                                                     "
echo "# swarmDRXload.csh: script to convert SMA data produced from SWARM    "
echo "# correlator sampled with two receivers simultaneously                "
echo "#                                                                     "
echo "#   Production: Two sideband separated datasets -                     "
echo "#               each contains two receiver data                       "
echo "#   Output format: Miriad                                             "
echo "#   Next: further reduction with Miriad SMA-WB5.0.0 or more recent    "
echo "#         versions; or run swarmDRX03pp.csh for rx pair 230 240 or    "
echo "#                          swarmDRX12pp.csh for rx pair 340 400       "
echo "#         to produce datasets for further calibration with globally   "
echo "#         distributed Miriad, CASA and AIPS                           "  
echo "######################################################################"
echo "                                                                      "
############################################################################
#
#begin of users' input
#
#set the name of the SMA raw data
#
set file = 170717_09:31:42 
echo $file
#
#the observing date for the prefix of the output file names 
#
set dt    = 170717
#
#set the data path, e.g. for RTDC in Cambridge, MA
#
#set DATPATH = /2015/science/mir_data/ 
#set DATPATH = /2016/science/mir_data/
#set DATPATH = /2017/science/mir_data/
#
#after 2017 August for RTDC in Cambridge, MA
#set DATPATH = /sma/data/science/mir_data/
#
#set DATPATH = /run/media/miriad/SMAmac1/data/2016
#set DATPATH = /media/8TBBackup/data/2017
set DATPATH = ./
#
#end of users' input
#
#####################################
#                                   #
# The pre-processing routine starts #
#                                   #
#####################################
#
#set the receiver
#
set rxid  = 0       #0 for 230 GHz
                    #1 for 345 GHz
                    #2 for 400 GHz
                    #3 for 240 GHz
                    #for both bands if the data sampled in dual receivers
                    #with the SWARM correlator only. 
set rx    = rx${rxid}
#
#set the sideband
#
set sb    = lsb
set nn    = 0       #0 for lsb 
                    #1 for usb
#
#set number of spectral window for SWARM data
#
set maxspw      = 4 
#
#the handle of switch sideband
#
OTHERSIDEBAND:
set fname = "$dt""_""$rx".$sb
#goto SPLIT-SWARM    #split the swarm chunks
#
#start the pipeline to convert new SMA format to Miriad 
#
SMALOD:
echo "Loading "$sb "data: "$fname
\rm -fr $fname
smalod in=${DATPATH}/$file out=$dt rxif=$rxid \
   options=circular sideband=$nn nscans=0
#
#check the smaloded data with miriad program uvindex
#
\rm -r $fname.uvindex.log
uvindex vis=$fname log=$fname.uvindex.log
#
echo "Successfully inspected vis=" $fname
echo " "
echo "done with sb -" $sb
echo "reset sideband parameter -"
echo " "
set sb    = usb
set nn    = 1       #0 for lsb 
                    #1 for usb
set fname = "$dt""_""$rx".$sb
echo "Loading "$sb "data: "$fname
\rm -fr $fname
smalod in=${DATPATH}/$file out=$dt rxif=$rxid \
   options=circular sideband=$nn nscans=0,
#
##check the smaloded data with miriad program uvindex
#
\rm -r $fname.uvindex.log
uvindex vis=$fname log=$fname.uvindex.log
#
#echo "Successfully inspected vis=" $fname
echo " "
echo "done with sb -" $sb 
echo " "
echo "End the pre-processing successfully!"
echo " "
##########################
#                        #
# End of the pre-process #
#                        #
##########################
date
exit
