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