#! /bin/csh -f
#for RTDC system
#jhz 2018-02-26: intiate a script for developing/testing the prototype
#                program swarm2uvfits
#jhz 2018-04-23: updated for new disk names on RTDC/RG
#jhz 2018-04-26: update for testing swarmRead for the LSB data               
#jhz 2018-05-02: update for USB test               
#jhz 2018-05-09: develop swarm2ms.py to execute CASA tasks
#jhz 2018-05-10: export swarm2ms.py a bin area where swarmRead
#                is located.
#jhz 2018-05-11: add instruction lines
#jhz 2018-05-24: updated for test on RTDC/RG system
#jhz 2018-05-25: add casa alias to the new distribution 5.1.2
#jhz 2018-05-25: updated for test on Hilo system (hilodr2) 
#                no casa found
#jhz 2018-05-29: add optinx to input variables
#                add 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-05-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-05-31: test swarmio library
#jhz 2018-06-13: updated the path for the new package-name
#jhz 2018-06-14: add version control information to the package
#jhz 2018-07-06: export to /opt/sma on RTDC system
#jhz 2018-08-03: set up for Geoff Bower test
#jhz 2018-08-07: add check/define LD_LIBRARY_PATH
#jhz 2018-08-08: add options with the handle optidx:
#0 => Default, convert SWARM to MS along with online flagging, for testing
#1 => Convert SWARM to MS without applying online flagging, for testing
#jhz 2018-08-10: RTDC tested OPTIONS = 1
#jhz 2018-11-01: Implement OPTIONS 2 and 3
#jhz 2018-11-29: add a feature swarmRead_dbg for debugging
#jhz 2019-01-30: Implement ephemeris and tested for both hilo & rtdc9
#jhz 2019-01-22: implemented antenna-based tsys imported from eng_read
#                converted bl_tsys for pipeline tsys corrections options 3.
#                tested options 3
#jhz 2019-03-19: Debugging eng_read Tsys and pipeline tsys correction
#jhz 2019-05-15: Tested for SMA-SWARM2CASA 1.0.5 release
#jhz 2019-06-20: Testing for End-to-end CASA python script for SWARM data
#jhz 2019-07-18: Test dropping-iPointing scans patch
#jhz 2019-07-22: Test Tsys patches
#jhz 2019-08-01: Test Doppler tracking  error patch
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  
# SIDEBAND           - an integer for selection of sideband
#                      set SIDEBAND = 0 for LSB
#                      set SIDEBAND = 1 for USB
# OPTIONS            - an integer for options allowing the pipeline
#                      processing visibility with online flagging and 
#                      Tsys. There have been four options implemented:
#                      set OPTIONS = 0    % Default, convert SWARM to MS along 
#                                           with online flagging, for testing
#                      set OPTIONS = 1    % Convert SWARM to MS without applying 
#                                           online flagging & Tsys corrections
#                      set OPTIONS = 2    % Convert SWARM to MS with applying both 
#                                           online flagging and Tsys
#                      set OPTIONS = 3    % Convert SWARM to MS with applying no 
#                                           online flagging but Tsys applied                     
#
#
set    PATH = /opt/sma/SWARM2CASA
set    PATHBIN = $PATH/bin
if (! $?LD_LIBRARY_PATH) then
        setenv LD_LIBRARY_PATH $PATH/lib
else
        setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$PATH/lib
endif
set    INPUT_FILE_DIR = /sma/data/science/mir_data/
set    INPUT_FILE_NAME = 190314_11:50:03     
setenv OUTPUT_FILE_PREFIX SMA181002
set    SEPARATE_DRX    = N
set    OPTIONS         = 2  
#----------------------------------------------------------------------
echo 'Start swarm2casa.csh script -'
echo ''
echo "SWARM2CASA pipeline version: " `cat $PATH/VERSION`
echo "loaded from PATH=$PATH"
echo ''
echo "OUTPUT_FILE_PREFIX=" $OUTPUT_FILE_PREFIX
#
# process start -
#
#for LSB -
echo "Reading LSB data -"
set SIDEBAND = 0
$PATHBIN/swarmRead $INPUT_FILE_DIR $INPUT_FILE_NAME $OUTPUT_FILE_PREFIX $SIDEBAND $OPTIONS
#for USB -
echo "Reading USB data -"
set SIDEBAND = 1
$PATHBIN/swarmRead $INPUT_FILE_DIR $INPUT_FILE_NAME $OUTPUT_FILE_PREFIX $SIDEBAND $OPTIONS
#
# run CASA batch process -
#
#alias casa "/usr/local/casa-release-5.1.2-4.el6/bin/casa"
alias casa "/opt/casa-release-5.3.0-143.el6/bin/casa"
if($SEPARATE_DRX == "Y") then
casa -c $PATHBIN/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 $PATHBIN/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