#! /bin/csh -f
#for RTDC system
#jhz 2018-2-26: initiate a script for developing/testing the prototype
#               program swarm2uvfits
#jhz 2018-4-23: updated for new disk names on RTDC/RG
#jhz 2018-4-26: updated for testing swarmRead for the LSB data               
#jhz 2018-5-02: updated for USB test               
#jhz 2018-5-09: developed swarm2ms.py to execute CASA tasks
#jhz 2018-5-10: exported swarm2ms.py to a bin area where swarmRead
#               is located.
#jhz 2018-5-11: added instruction lines
#jhz 2018-5-24: updated for test on RTDC/RG system
#jhz 2018-5-25: added casa alias to the new distribution 5.1.2
#jhz 2018-5-25: updated for test on Hilo system (hilodr2) 
#               no casa found
#jhz 2018-5-29: added optinx to input variables
#               added alias casa /usr/local/casa-release-4.7.0-1-el6/bin/casa (from Holly)
#               on hilodr1 to test CASA 4.7.0 
#jhz 2018-5-30: options to separate dual rx for CASA measurementSet
#               outputs
#               CASA 4.7.0 seems to not work correctly for SMA data
#               on hilodr1
#jhz 2018-5-31: test swarmio library
#               
date
# --------------------------------------------------------------------
#
# requirement -
#
# RH 6.7 to 6.9; or CentOS 6.7 to 6.9
# CASA 5.1.1 and higher
# --------------------------------------------------------------------
#
# set up c-shell and environment variables -
#
# INPUT_FILE_DIR     - the path of the SMA archived data produced by online 
#                      system with the SWARM correlator
# INPUT_FILE_NAME    - the file name of the SMA archived data that users want
#                      to interpret to measurementSet of CASA
# OUTPUT_FILE_PREFIX - the prefix of output file name,  for example, SMA180506
#                      will result in SMA180506.ms for the data in measurementSet
#                      for CASA       
# PATH               - the location of the executable binary code and the Python 
#                      script
# SEPARATE_DRX       - a handle for daul receiver: 
#                      "Y" to produce two separate RX files in CASA output    
#                      "N" to produce a single dual RX file in CASA output                                                                 
set    INPUT_FILE_DIR = /sma/data/science/mir_data/
set    INPUT_FILE_NAME = 180506_03:52:53 
setenv OUTPUT_FILE_PREFIX SMA180506
set    PATH = /reduction/jzhao/SMA2CASA_TESTING/swarm2measurementSet/bin/ 
set    SEPARATE_DRX    = N
#----------------------------------------------------------------------
echo 'Start swarm2uvfits.csh script -'
echo ''
#
# process start -
#
#for LSB -
$PATH/swarmRead $INPUT_FILE_DIR $INPUT_FILE_NAME $OUTPUT_FILE_PREFIX 0 0
#for USB -
$PATH/swarmRead $INPUT_FILE_DIR $INPUT_FILE_NAME $OUTPUT_FILE_PREFIX 1 0
#
# run CASA batch process -
#
alias casa "/usr/local/casa-release-5.1.2-4.el6/bin/casa"
if($SEPARATE_DRX == "Y") then
casa -c $PATH/swarm2ms_d.py
echo ''
echo 'Two CASA measurementSet data outputs are produced: '
echo $OUTPUT_FILE_PREFIX'_rx1.ms &' $OUTPUT_FILE_PREFIX'_rx2.ms' 
else
casa -c $PATH/swarm2ms_s.py
echo ''
echo 'A single CASA measurementSet data output is produced: '
echo $OUTPUT_FILE_PREFIX'.ms'
endif
echo 'Done, congratulation! exit from the script!'
date
exit 
