1	#! /bin/csh -f
     2	#for RTDC system
     3	#jhz 2018-02-26: intiate a script for developing/testing the prototype
     4	#                program swarm2uvfits
     5	#jhz 2018-04-23: updated for new disk names on RTDC/RG
     6	#jhz 2018-04-26: update for testing swarmRead for the LSB data               
     7	#jhz 2018-05-02: update for USB test               
     8	#jhz 2018-05-09: develop swarm2ms.py to execute CASA tasks
     9	#jhz 2018-05-10: export swarm2ms.py a bin area where swarmRead
    10	#                is located.
    11	#jhz 2018-05-11: add instruction lines
    12	#jhz 2018-05-24: updated for test on RTDC/RG system
    13	#jhz 2018-05-25: add casa alias to the new distribution 5.1.2
    14	#jhz 2018-05-25: updated for test on Hilo system (hilodr2) 
    15	#                no casa found
    16	#jhz 2018-05-29: add optinx to input variables
    17	#                add alias casa /usr/local/casa-release-4.7.0-1-el6/bin/casa (from Holly)
    18	#                on hilodr1 to test CASA 4.7.0 
    19	#jhz 2018-05-30: options to separate dual rx for CASA measurementSet
    20	#                outputs
    21	#                CASA 4.7.0 seems to not work correctly for SMA data
    22	#                on hilodr1
    23	#jhz 2018-05-31: test swarmio library
    24	#jhz 2018-06-13: updated the path for the new package-name
    25	#jhz 2018-06-14: add version control information to the package
    26	#jhz 2018-07-06: export to /opt/sma on RTDC system
    27	#jhz 2018-08-03: set up for Geoff Bower test
    28	#jhz 2018-08-07: add check/define LD_LIBRARY_PATH
    29	#jhz 2018-08-08: add options with the handle optidx:
    30	#0 => Default, convert SWARM to MS along with online flagging, for testing
    31	#1 => Convert SWARM to MS without applying online flagging, for testing
    32	#jhz 2018-08-10: RTDC tested OPTIONS = 1
    33	#jhz 2018-11-01: Implement OPTIONS 2 and 3
    34	#jhz 2018-11-29: add a feature swarmRead_dbg for debugging
    35	#jhz 2019-01-30: Implement ephemeris and tested for both hilo & rtdc9
    36	#jhz 2019-01-22: implemented antenna-based tsys imported from eng_read
    37	#                converted bl_tsys for pipeline tsys corrections options 3.
    38	#                tested options 3
    39	#jhz 2019-03-19: Debugging eng_read Tsys and pipeline tsys correction
    40	#jhz 2019-05-15: Tested for SMA-SWARM2CASA 1.0.5 release
    41	#jhz 2019-06-20: Testing for End-to-end CASA python script for SWARM data
    42	date
    43	# --------------------------------------------------------------------
    44	#
    45	# requirement -
    46	#
    47	# RH 6.7 to 6.9; or CentOS 6.7 to 6.9
    48	# CASA 5.1.1 and higher
    49	# --------------------------------------------------------------------
    50	#
    51	# set up c-shell and environment variables -
    52	#
    53	# INPUT_FILE_DIR     - the path of the SMA archived data produced by online 
    54	#                      system with the SWARM correlator
    55	# INPUT_FILE_NAME    - the file name of the SMA archived data that users want
    56	#                      to interpret to measurementSet of CASA
    57	# OUTPUT_FILE_PREFIX - the prefix of output file name,  for example, SMA180506
    58	#                      will result in SMA180506.ms for the data in measurementSet
    59	#                      for CASA       
    60	# PATH               - the location of the executable binary code and the Python 
    61	#                      script
    62	# SEPARATE_DRX       - a handle for daul receiver: 
    63	#                      "Y" to produce two separate RX files in CASA output    
    64	#                      "N" to produce a single dual RX file in CASA output  
    65	# SIDEBAND           - an integer for selection of sideband
    66	#                      set SIDEBAND = 0 for LSB
    67	#                      set SIDEBAND = 1 for USB
    68	# OPTIONS            - an integer for options allowing the pipeline
    69	#                      processing visibility with online flagging and 
    70	#                      Tsys. There have been four options implemented:
    71	#                      set OPTIONS = 0    % Default, convert SWARM to MS along 
    72	#                                           with online flagging, for testing
    73	#                      set OPTIONS = 1    % Convert SWARM to MS without applying 
    74	#                                           online flagging & Tsys corrections
    75	#                      set OPTIONS = 2    % Convert SWARM to MS with applying both 
    76	#                                           online flagging and Tsys
    77	#                      set OPTIONS = 3    % Convert SWARM to MS with applying no 
    78	#                                           online flagging but Tsys applied                     
    79	#
    80	#
    81	set    PATH = /opt/sma/SWARM2CASA
    82	set    PATHBIN = $PATH/bin
    83	if (! $?LD_LIBRARY_PATH) then
    84	        setenv LD_LIBRARY_PATH $PATH/lib
    85	else
    86	        setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$PATH/lib
    87	endif
    88	set    INPUT_FILE_DIR = /sma/data/science/mir_data.2018/
    89	set    INPUT_FILE_NAME = 181002_03:55:28     
    90	setenv OUTPUT_FILE_PREFIX SMA181002
    91	set    SEPARATE_DRX    = N
    92	set    OPTIONS         = 2  
    93	#----------------------------------------------------------------------
    94	echo 'Start swarm2casa.csh script -'
    95	echo ''
    96	echo "SWARM2CASA pipeline version: " `cat $PATH/VERSION`
    97	echo "loaded from PATH=$PATH"
    98	echo ''
    99	echo "OUTPUT_FILE_PREFIX=" $OUTPUT_FILE_PREFIX
   100	#
   101	# process start -
   102	#
   103	#for LSB -
   104	echo "Reading LSB data -"
   105	set SIDEBAND = 0
   106	$PATHBIN/swarmRead $INPUT_FILE_DIR $INPUT_FILE_NAME $OUTPUT_FILE_PREFIX $SIDEBAND $OPTIONS
   107	#for USB -
   108	echo "Reading USB data -"
   109	set SIDEBAND = 1
   110	$PATHBIN/swarmRead $INPUT_FILE_DIR $INPUT_FILE_NAME $OUTPUT_FILE_PREFIX $SIDEBAND $OPTIONS
   111	#
   112	# run CASA batch process -
   113	#
   114	#alias casa "/usr/local/casa-release-5.1.2-4.el6/bin/casa"
   115	alias casa "/opt/casa-release-5.3.0-143.el6/bin/casa"
   116	if($SEPARATE_DRX == "Y") then
   117	casa -c $PATHBIN/swarm2ms_d.py
   118	echo ''
   119	echo 'Two CASA measurementSet data outputs are produced: '
   120	echo $OUTPUT_FILE_PREFIX'_rx1.ms &' $OUTPUT_FILE_PREFIX'_rx2.ms' 
   121	else
   122	casa -c $PATHBIN/swarm2ms_s.py
   123	echo ''
   124	echo 'A single CASA measurementSet data output is produced: '
   125	echo $OUTPUT_FILE_PREFIX'.ms'
   126	endif
   127	echo 'Done, congratulation! exit from the script!'
   128	date
   129	exit