def phase(antenna1, antenna2):
    t1 = t.query('ANTENNA1= ' + str(antenna1) + ' ' + 'AND ANTENNA2= ' +
                 str(antenna2))
    print '***FLAG ANALYSIS***'
    datapoint = 0
    noflags = 0
    flag = t1.getcolslice("FLAG", [start_chan, pol], [end_chan, pol])
    phase = t1.getcolslice("DATA", [start_chan, pol], [end_chan, pol])
    time = t1.getcol("TIME")
    time = time / (24 * 3600)  #Convert MJD in seconds to days
    timehr = (time - time[0]) * 24  #Hours since observation start
    ant1 = t1.getcell("ANTENNA1", 0)
    ant2 = t1.getcell("ANTENNA2", 0)
    print 'Baseline = ' + str(ant1) + ' ' + str(ant2)
    length = len(flag)
    mphase = []
    elevation = []

    for x in range(len(time)):
        total = 0 + 0j
        count = 0
        tmpflag = flag[x]
        tmpphase = phase[x]
        altaz = co.altaz(time[x].item(), RAdeg, DECdeg)
        alt = altaz[0]
        elevation.append(alt)
        for i in range(0, end_chan):
            if tmpflag[i]:
                noflags = noflags + 1
                datapoint = datapoint + 1
            else:
                test = tmpphase[i].item()
                total = test + total
                count = count + 1
                datapoint = datapoint + 1

        if (count != 0):
            spectral_mean = total / count
            phase_element = arctan2(spectral_mean.imag, spectral_mean.real)
            mphase.append(phase_element)
        else:
            mphase.append(nan)
    print len(mphase)
    print len(time)
    print len(elevation)
    print 'Total number of datapoints = ' + str(datapoint)
    print 'Total number of flags = ' + str(noflags)
    percentage = (float(noflags) / float(datapoint)) * 100
    print 'Percentage baseline flagged for correlation ' + str(
        pol) + ' = ' + str(percentage) + '%'

    return elevation, timehr, mphase
def phase(antenna1,antenna2):
	t1=t.query('ANTENNA1= '+str(antenna1) +' '+'AND ANTENNA2= '+str(antenna2))
	print '***FLAG ANALYSIS***'
	datapoint=0
	noflags=0
	flag=t1.getcolslice("FLAG", [start_chan,pol], [end_chan,pol])
	phase = t1.getcolslice("DATA", [start_chan,pol], [end_chan,pol])
	time=t1.getcol("TIME")
	time = time/(24*3600) #Convert MJD in seconds to days
	timehr=(time-time[0])*24 #Hours since observation start
	ant1=t1.getcell("ANTENNA1",0)
	ant2=t1.getcell("ANTENNA2",0)
	print 'Baseline = '+str(ant1)+' ' +str(ant2)
	length=len(flag)
	mphase=[]
	elevation=[]
		   
	for x in range(len(time)):
            total=0+0j
	    count=0
	    tmpflag=flag[x]
	    tmpphase=phase[x]	
	    altaz=co.altaz(time[x].item(),RAdeg,DECdeg)
	    alt=altaz[0]
	    elevation.append(alt) 
	    for i in range(0, end_chan):    
                if tmpflag[i]:
                    noflags=noflags+1
		    datapoint=datapoint+1
		else:  
                    test=tmpphase[i].item()
		    total=test+total  
		    count=count+1
		    datapoint=datapoint+1
			   
	    if (count !=0):
                spectral_mean=total/count
		phase_element=arctan2(spectral_mean.imag,spectral_mean.real)
		mphase.append(phase_element)
	    else: mphase.append(nan)   
	print len(mphase)
	print len(time)	
	print len(elevation)
	print 'Total number of datapoints = '+str(datapoint)
	print 'Total number of flags = '+str(noflags)
	percentage=(float(noflags)/float(datapoint))*100
	print 'Percentage baseline flagged for correlation '+str(pol)+' = '+str(percentage)+'%'

	return elevation,timehr,mphase
def amp_phase(antenna1,antenna2):

        mampl=[]
        mphase=[]
        elevation=[]
        uvdist=[]
	time_hr=[]
	t2=t.query('ANTENNA1= '+str(antenna1) +' '+'AND ANTENNA2= '+str(antenna2))

	datapoint=0
	noflags=0
	flag=t2.getcolslice("FLAG", [start_chan,pol], [end_chan,pol])
	ampl = numpy.absolute (t2.getcolslice(data_name, [start_chan,pol], [end_chan,pol]))

	phase = t2.getcolslice(data_name, [start_chan,pol], [end_chan,pol])

	time=t2.getcol("TIME")
	timed = time/(24*3600) #Convert MJD in seconds to days
	timehr=(timed-timed[0])*24 #Hours since observation start
	uvw=t2.getcol("UVW")
	ant1=t2.getcell("ANTENNA1",0)
	ant2=t2.getcell("ANTENNA2",0)
	print 'Baseline = '+str(ant1)+' ' +str(ant2)
	print '***FLAG ANALYSIS***'
		   
	for x in range(len(time)):             
            atotal=0
	    ptotal=0+0j
	    count=0

	    altaz=co.altaz(time[x].item(),123.40025,48.2179139) #Calculate corresponding elevation
	    alt=altaz[0]
	    elevation.append(alt)

	    tmpuvw=uvw[x]                                    #Calculate uv distance
	    uvdist_1=sqrt((tmpuvw[0]**2)+(tmpuvw[1]**2))
	    uvdist.append(uvdist_1)
	    time_hr.append(timehr[x])
	    tmpflag=flag[x]  #Create temporary array for DATA and FLAG in each row, each 1x no of channels in dimension 
	    tmpamp=ampl[x]
	    tmpphase=phase[x]
			   
	    for i in range(0, end_chan):         #If tmpflag=true, set corresponding absolute amplitude data value to 0, and exclude from mean calculation.
                if tmpflag[i]:
                    noflags=noflags+1
		    datapoint=datapoint+1
		else:  
                    atest=tmpamp[i].item()
		    atotal=atest+atotal 
		    ptest=tmpphase[i].item()
		    ptotal=ptest+ptotal 
		    count=count+1
		    datapoint=datapoint+1
			  
	    if (count !=0):
                aspectral_mean=atotal/count
		pspectral_mean=ptotal/count
		mampl.append(aspectral_mean)
		phase_element=arctan2(pspectral_mean.imag,pspectral_mean.real)#%(2*pi)
		mphase.append(phase_element)

	    else: 
                mampl.append(nan)
		mphase.append(nan)
		
	print 'Total number of datapoints = '+str(datapoint)
	print 'Total number of flags = '+str(noflags)
	percentage=(float(noflags)/float(datapoint))*100
	print 'Percentage baseline flagged for correlation '+str(pol)+' = '+str(percentage)+'%'
	return mampl,mphase,time_hr,uvdist,elevation
def amp_phase(antenna1, antenna2):

    mampl = []
    mphase = []
    elevation = []
    uvdist = []
    time_hr = []
    t2 = t.query('ANTENNA1= ' + str(antenna1) + ' ' + 'AND ANTENNA2= ' +
                 str(antenna2))

    datapoint = 0
    noflags = 0
    flag = t2.getcolslice("FLAG", [start_chan, pol], [end_chan, pol])
    ampl = numpy.absolute(
        t2.getcolslice(data_name, [start_chan, pol], [end_chan, pol]))

    phase = t2.getcolslice(data_name, [start_chan, pol], [end_chan, pol])

    time = t2.getcol("TIME")
    timed = time / (24 * 3600)  #Convert MJD in seconds to days
    timehr = (timed - timed[0]) * 24  #Hours since observation start
    uvw = t2.getcol("UVW")
    ant1 = t2.getcell("ANTENNA1", 0)
    ant2 = t2.getcell("ANTENNA2", 0)
    print 'Baseline = ' + str(ant1) + ' ' + str(ant2)
    print '***FLAG ANALYSIS***'

    for x in range(len(time)):
        atotal = 0
        ptotal = 0 + 0j
        count = 0

        altaz = co.altaz(time[x].item(), 123.40025,
                         48.2179139)  #Calculate corresponding elevation
        alt = altaz[0]
        elevation.append(alt)

        tmpuvw = uvw[x]  #Calculate uv distance
        uvdist_1 = sqrt((tmpuvw[0]**2) + (tmpuvw[1]**2))
        uvdist.append(uvdist_1)
        time_hr.append(timehr[x])
        tmpflag = flag[
            x]  #Create temporary array for DATA and FLAG in each row, each 1x no of channels in dimension
        tmpamp = ampl[x]
        tmpphase = phase[x]

        for i in range(
                0, end_chan
        ):  #If tmpflag=true, set corresponding absolute amplitude data value to 0, and exclude from mean calculation.
            if tmpflag[i]:
                noflags = noflags + 1
                datapoint = datapoint + 1
            else:
                atest = tmpamp[i].item()
                atotal = atest + atotal
                ptest = tmpphase[i].item()
                ptotal = ptest + ptotal
                count = count + 1
                datapoint = datapoint + 1

        if (count != 0):
            aspectral_mean = atotal / count
            pspectral_mean = ptotal / count
            mampl.append(aspectral_mean)
            phase_element = arctan2(pspectral_mean.imag,
                                    pspectral_mean.real)  #%(2*pi)
            mphase.append(phase_element)

        else:
            mampl.append(nan)
            mphase.append(nan)

    print 'Total number of datapoints = ' + str(datapoint)
    print 'Total number of flags = ' + str(noflags)
    percentage = (float(noflags) / float(datapoint)) * 100
    print 'Percentage baseline flagged for correlation ' + str(
        pol) + ' = ' + str(percentage) + '%'
    return mampl, mphase, time_hr, uvdist, elevation