casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
gaincal_unflag_nearest.py
Go to the documentation of this file.
00001 import numpy as np
00002 import time
00003 import pylab as pl
00004 from casa import table as tb
00005 
00006 def fill_flagged_soln(caltable='', doplot=False):
00007     """
00008     This is to replace the gaincal solution of flagged/failed solutions by the nearest valid 
00009     one.
00010     If you do not do that and applycal blindly with the table your data gets 
00011     flagged between  calibration runs that have a bad/flagged solution at one edge.
00012     Can be pretty bad when you calibrate every hour or more 
00013     (when you are betting on self-cal) of observation (e.g L-band of the EVLA)..one can 
00014     lose the whole hour of good data without realizing !
00015     """
00016     tb.open(caltable, nomodify=False)
00017     flg=tb.getcol('FLAG')
00018     sol=tb.getcol('SOLUTION_OK')
00019     ant=tb.getcol('ANTENNA1')
00020     gain=tb.getcol('GAIN')
00021     t=tb.getcol('TIME')
00022     dd=tb.getcol('CAL_DESC_ID')
00023     maxant=np.max(ant)
00024     maxdd=np.max(dd)
00025     npol=len(gain[:,0,0])
00026     nchan=len(gain[0,:,0])
00027     
00028     k=1
00029     if(doplot):
00030         pl.ion()
00031         pl.figure(1)
00032         pl.plot(t[(ant==k)], sol[0,0,(ant==k)], 'b+')
00033         pl.plot(t[(ant==k)], flg[0,0,(ant==k)], 'r+')
00034         pl.twinx()
00035         pl.plot(t[(ant==k)], abs(gain[0,0,(ant==k)]), 'go')
00036     print 'maxant', maxant
00037     numflag=0.0
00038     for k in range(maxant+1):
00039         for j in range (maxdd+1):
00040             subflg=flg[:,:,(ant==k) & (dd==j)]
00041             subt=t[(ant==k) & (dd==j)]
00042             subsol=sol[:,:,(ant==k) & (dd==j)]
00043             subgain=gain[:,:,(ant==k) & (dd==j)]
00044             #print 'subgain', subgain.shape
00045             for kk in range(1, len(subt)):
00046                 for chan in range(nchan):
00047                     for pol in range(npol):
00048                         if(subflg[pol,chan,kk] and not subflg[pol,chan,kk-1]):
00049                             numflag += 1.0
00050                             subflg[pol,chan,kk]=False
00051                             subsol[pol, chan, kk]=True
00052                             subgain[pol,chan,kk]=subgain[pol,chan,kk-1]
00053                         if(subflg[pol,chan,kk-1] and not subflg[pol,chan,kk]):
00054                             numflag += 1.0
00055                             subflg[pol,chan,kk-1]=False
00056                             subsol[pol, chan, kk-1]=True
00057                             subgain[pol,chan,kk-1]=subgain[pol,chan,kk]
00058             flg[:,:,(ant==k) & (dd==j)]=subflg
00059             sol[:,:,(ant==k) & (dd==j)]=subsol
00060             gain[:,:,(ant==k) & (dd==j)]=subgain
00061 
00062 
00063     print 'numflag', numflag
00064     if(doplot):
00065         pl.figure(2)
00066         k=1
00067         #pl.clf()
00068         pl.plot(t[(ant==k)], sol[0,0,(ant==k)], 'b+')
00069         pl.plot(t[(ant==k)], flg[0,0,(ant==k)], 'r+')
00070         pl.twinx()
00071         pl.plot(t[(ant==k)], abs(gain[0,0,(ant==k)]), 'go')
00072         pl.title('antenna='+str(k))
00073      
00074     ###
00075     tb.putcol('FLAG', flg)
00076     tb.putcol('SOLUTION_OK', sol)
00077     tb.putcol('GAIN', gain)
00078     tb.done()