1	###############################################################################
     2	#                                                                             #
     3	# Use Python Script for CASA (5.3.0) and newer                                #
     4	# Jun-Hui Zhao (SAO): 2018-11-01 for reduction of SMA data                    #
     5	# Update-                                                                     #
     6	# jhz 2018-11-01 setup                                                        #
     7	# jhz 2018-11-08 for entry 1-5                                                #
     8	# jhz 2018-11-09 for entry 6-8                                                #
     9	# jhz 2019-02-21 rename the script (swarmDataAnalysis.py)                     # 
    10	#                from the original (swarmGUIdatainspect.py) that was under    #
    11	#                /home/miriad/jhz/SMADATA2CASA/SMA2UVFITS/SWARM2CASA/bin      #
    12	# jhz 2019-04-10 provides notes to the excutive lines                         #
    13	# jhz 2019-04-11 Make multiple entry options                                  #
    14	###############################################################################
    15	#
    16	#Useful links for SMA data editing -
    17	#Baseline corrections: https://www.cfa.harvard.edu/rtdc/SMAdata/baseline/
    18	#
    19	#!/usr/bin/python
    20	###############################
    21	#export CASA modules to python#
    22	###############################
    23	from sys import exit
    24	import time
    25	import os
    26	from gencal      import gencal
    27	from plotweather import plotweather
    28	from listobs     import listobs
    29	from plotants    import plotants
    30	from plotms      import plotms
    31	from flagdata    import flagdata
    32	from setjy       import setjy
    33	from gaincal     import gaincal
    34	from plotcal     import plotcal
    35	from bandpass    import bandpass
    36	from blcal       import blcal
    37	from applycal    import applycal
    38	from fluxscale   import fluxscale
    39	from split       import split
    40	from clearcal    import clearcal
    41	from uvcontsub   import uvcontsub
    42	from vishead     import vishead
    43	from cvel        import cvel
    44	from clean       import clean
    45	from viewer      import viewer
    46	from immoments   import immoments
    47	from imstat      import imstat
    48	from concat      import concat
    49	from uvsub       import uvsub
    50	from ft            import ft
    51	from exportuvfits  import exportuvfits
    52	from hanningsmooth import hanningsmooth
    53	from widebandpbcor import widebandpbcor
    54	from casac         import casac
    55	#####################
    56	#define variables - #
    57	#####################
    58	cl=casac.componentlist()
    59	T = True
    60	F = False
    61	##########################################
    62	#setup import data -                     #
    63	#the input measurementSet                #
    64	#must be created via the swarm2casa path #
    65	##########################################
    66	#
    67	#make sure the existing SMAswarmdata.ms
    68	#is what you want to look at.
    69	#If not existing, make a sympolic link to
    70	#you target data                        
    71	#
    72	##########################################
    73	datain = 'SMAswarmdata.ms'               #
    74	allspw = '0~15:3001~13000'               #
    75	##########################################
    76	def step0():
    77	 print "List observing parameters -\n"
    78	 user_check=raw_input('hit Return to run listobs -')
    79	 listobs(vis=datain)
    80	 print "Done with listobs for observing parameters!"
    81	#
    82	def step1():
    83	 print "Plot uv coverage -\n"
    84	 user_check=raw_input('hit Return to run plotms for uv coverage -')
    85	 plotms(vis=datain,
    86	       	xaxis='uwave',
    87	 	yaxis='vwave', 
    88		coloraxis='spw', 
    89		avgchannel='1024')
    90	 print "Done with plotms for uwave-vwave!"
    91	#
    92	 print "Plot Elev coverage -\n"
    93	 user_check=raw_input('hit Return to run plotms for Elev range -') 
    94	 plotms(vis=datain,
    95	       	xaxis='time',
    96		yaxis='elevation', 
    97		coloraxis='field', 
    98		spw='0:3001~13000',
    99	       	avgchannel='10000')
   100	 print "Done with plotms for time-elevation!"
   101	#
   102	 print "Plot hour angle (HA) range -\n"
   103	 user_check=raw_input('hit Return to run plotms for HA range -')
   104	 plotms(vis=datain,
   105		xaxis='hourang',
   106		yaxis='amp', 
   107		coloraxis='field', 
   108	        spw=allspw, 
   109		avgchannel='10000')
   110	 print "Done with plotms for HA range!"
   111	#
   112	 print "Plot fringe ampl vs time (spw as iteraxis) -\n"
   113	 user_check=raw_input('hit Return to run plotms for ampl vs UTC through each spw -')
   114	 plotms(vis=datain,
   115		xaxis='time',
   116		yaxis='amp', 
   117		coloraxis='field', 
   118		spw=allspw, 
   119		avgchannel='10000', 
   120		iteraxis='spw')
   121	 user_check=raw_input('hit Return to plot fringe ampl vs time through each BL -\n')
   122	 plotms(vis=datain,
   123		xaxis='time',
   124		yaxis='amp', 
   125		coloraxis='field',
   126	        spw=allspw, 
   127		avgchannel='10000', 
   128		iteraxis='baseline')
   129	 print "Done with plotms for amplitude vs time!"
   130	#
   131	 print "Plot fringe phas vs time (spw as iteraxis) -\n"
   132	 user_check=raw_input('hit Return to run plotms for phas vs UTC through each spw -')
   133	 plotms(vis=datain,
   134		xaxis='time',
   135		yaxis='phase', 
   136		coloraxis='field',   
   137	        spw=allspw, 
   138		avgchannel='10000', 
   139		iteraxis='spw')
   140	 user_check=raw_input('hit Return to plot fringe pha vs time (BL as iteraxis) -\n')
   141	 plotms(vis=datain,
   142		xaxis='time',
   143		yaxis='phase', 
   144		coloraxis='field',
   145	        spw=allspw, 
   146		avgchannel='10000', 
   147		iteraxis='baseline')
   148	 print "Done with plotms for phase vs time!"
   149	#
   150	 print "Plot spectra - Amplitude vs Frequency -"
   151	 user_check=raw_input('hit Return for Amplitude -')
   152	 plotms(vis=datain,
   153		xaxis='frequency',
   154		yaxis='amp', 
   155		coloraxis='spw', 
   156		field='0', 
   157		avgchannel='128', 
   158		iteraxis='baseline')
   159	 print "Done with plotms for spectra: amplitude vs frequency!"
   160	#
   161	 print "Plot spectra - Phase vs Frequency -"
   162	 user_check=raw_input('hit Return for Phase -')
   163	 plotms(vis=datain,
   164	        xaxis='frequency',
   165		yaxis='phase', 
   166		coloraxis='spw', 
   167		field='0', 
   168	   	avgchannel='128', 
   169		iteraxis='baseline')
   170	 print "Done with plotms for spectra: phase vs frequency!"
   171	#
   172	 print "Plot Structure of vis function - Amplitude vs UVwave -"
   173	 user_check=raw_input('hit Return for Amplitude -')
   174	 plotms(vis=datain,
   175	        xaxis='UVwave',
   176		yaxis='amp', 
   177		coloraxis='spw', 
   178	        avgchannel='200', 
   179		spw=allspw, 
   180		iteraxis='field')
   181	 print "Done with plotms for spectra: amplitude vs frequency!"
   182	#
   183	 print "Plot Structure of vis function - Phase vs UVwave -"
   184	 user_check=raw_input('hit Return for Phase -')
   185	 plotms(vis=datain,
   186	        xaxis='UVwave',
   187		yaxis='phase', 
   188		coloraxis='spw', 
   189	        avgchannel='200', 
   190		spw=allspw, 
   191		iteraxis='field')
   192	 print "Done with plotms for vis-structure: phase vs frequency!"
   193	#
   194	#
   195	def start():
   196	    print "You are in casapy for inspecting your data with GUI in CASA"
   197	    print "The program will help you go through the follwoing items"
   198	    print "Select data inspection item...........type" 
   199	    print "List data headers........................1"
   200	    print "Plot uv-coverage.........................2"
   201	    print "Plot Elevation range.....................3"
   202	    print "Plot Hour-Angle range....................4"
   203	    print "Plot Fringe Ampl vs Time.................5"
   204	    print "Plot Fringe Phas vs Time.................6"
   205	    print "Plot Spectral Ampl.......................7"
   206	    print "Plot Spectral Phas.......................8"
   207	    print "Plot Structure of Vis-function Ampl......9"
   208	    print "Plot Structure of Vis-function Phas......a"
   209	    print "Type any key to start this program."
   210	    next = raw_input("> ")
   211	    if "0" in next:
   212	          step0()
   213	    elif "1" in next:
   214	          step0()
   215	    elif "2" in next:
   216	          step0()
   217	    elif "3" in next:
   218	          step0()
   219	    elif "4" in next:
   220	          step0()
   221	    elif "5" in next:
   222	          step0()
   223	    elif "6" in next:
   224	          step0()
   225	    elif "7" in next:
   226	          step0()
   227	    elif "8" in next:
   228	          step0()
   229	    elif "9" in next:
   230	          step0()
   231	    elif "a" in next:
   232	          step0()
   233	    else:
   234	          step0()
   235	#
   236	#=====================================================================
   237	# Done
   238	#
   239	print "Done with the reduction"
   240	start()