processing_components/calibration/operations.py
def apply_gaintable(vis: BlockVisibility, gt: GainTable, inverse=False, vis_slices=None, **kwargs) -> BlockVisibility:
    """Apply a gain table to a block visibility
    
    The corrected visibility is::
    
        V_corrected = {g_i * g_j^*}^-1 V_obs
        
    If the visibility data are polarised e.g. polarisation_frame("linear") then the inverse operator
    represents an actual inverse of the gains.
    
    :param vis: Visibility to have gains applied
    :param gt: Gaintable to be applied
    :param inverse: Apply the inverse (default=False)
    :return: input vis with gains applied
    
    """
    assert isinstance(vis, BlockVisibility), "vis is not a BlockVisibility: %r" % vis
    assert isinstance(gt, GainTable), "gt is not a GainTable: %r" % gt
    
    assert_vis_gt_compatible(vis, gt)
    
    if inverse:
        log.debug('apply_gaintable: Apply inverse gaintable')
    else:
        log.debug('apply_gaintable: Apply gaintable')
    
    is_scalar = gt.gain.shape[-2:] == (1, 1)
    if is_scalar:
        log.debug('apply_gaintable: scalar gains')
    
    for chunk, rows in enumerate(vis_timeslice_iter(vis, vis_slices=vis_slices)):
        if numpy.sum(rows) > 0:
            vistime = numpy.average(vis.time[rows])
            gaintable_rows = abs(gt.time - vistime) < gt.interval / 2.0
            
            # Lookup the gain for this set of visibilities
            gain = gt.data['gain'][gaintable_rows]
            gainwt = gt.data['weight'][gaintable_rows]
            
            # The shape of the mueller matrix is
            ntimes, nant, nchan, nrec, _ = gain.shape
            
            original = vis.vis[rows]
            originalwt = vis.weight[rows]
            applied = copy.deepcopy(original)
            appliedwt = copy.deepcopy(originalwt)
            for time in range(ntimes):
                antantwt = numpy.outer(gainwt, gainwt)
                if is_scalar:
                    if inverse:
                        lgain = numpy.ones_like(gain)
                        lgain[numpy.abs(gain) > 0.0] = 1.0 / gain[numpy.abs(gain) > 0.0]
                    else:
                        lgain = gain
                    clgain = numpy.conjugate(lgain)
                    for chan in range(nchan):
                        smueller = numpy.ma.outer(lgain[time, :, chan, 0],
                                                  clgain[time, :, chan, 0]).reshape([nant, nant])
                        applied[time, :, :, chan, 0] = original[time, :, :, chan, 0] * smueller
                        antantwt = numpy.outer(gainwt[time, :, chan, 0, 0], gainwt[time, :, chan, 0, 0])
                        applied[time, :, :, chan, 0][antantwt == 0.0] = 0.0
                        appliedwt[time, :, :, chan, 0][antantwt == 0.0] = 0.0
                else:
                    for a1 in range(vis.nants - 1):
                        for a2 in range(a1 + 1, vis.nants):
                            for chan in range(nchan):
                                mueller = numpy.kron(gain[time, a1, chan, :, :],
                                                     numpy.conjugate(gain[time, a2, chan, :, :]))
                                if inverse:
                                    # If the Mueller is singular, ignore it
                                    try:
                                        mueller = numpy.linalg.inv(mueller)
                                        applied[time, a2, a1, chan, :] = \
                                            numpy.matmul(mueller, original[time, a2, a1, chan, :])
                                    except numpy.linalg.linalg.LinAlgError:
                                        applied[time, a2, a1, chan, :] = \
                                            original[time, a2, a1, chan, :]
                                else:
                                    applied[time, a2, a1, chan, :] = \
                                        numpy.matmul(mueller, original[time, a2, a1, chan, :])
                                if (gainwt[time, a1, chan, 0, 0] <= 0.0) or (gainwt[time, a1, chan, 0, 0] <= 0.0):
                                    applied[time, a2, a1, chan, 0] = 0.0
                                    appliedwt[time, a2, a1, chan, 0] = 0.0
            
            vis.data['vis'][rows] = applied
    return vis
Apply a gain table to a block visibility
    
    The corrected visibility is::
    
        V_corrected = {g_i * g_j^*}^-1 V_obs
        
    If the visibility data are polarised e.g. polarisation_frame("linear") then the inverse operator
    represents an actual inverse of the gains.
    
    :param vis: Visibility to have gains applied
    :param gt: Gaintable to be applied
    :param inverse: Apply the inverse (default=False)
    :return: input vis with gains applied
    
    
Apply a gain table to a block visibility
The corrected visibility is:.



If the visibility data are polarised e.g. polarisation_frame("linear") then the inverse operator
represents an actual inverse of the gains.
Apply a gain table to a block visibility The corrected visibility is : . If the visibility data are polarised e . g . polarisation_frame ( " linear " ) then the inverse operator represents an actual inverse of the gains .
def apply_gaintable(vis: BlockVisibility, gt: GainTable, inverse=False, vis_slices=None, **kwargs) -> BlockVisibility:
    assert isinstance(vis, BlockVisibility), "vis is not a BlockVisibility: %r" % vis
    assert isinstance(gt, GainTable), "gt is not a GainTable: %r" % gt
    assert_vis_gt_compatible(vis, gt)
    if inverse:
        log.debug('apply_gaintable: Apply inverse gaintable')
    else:
        log.debug('apply_gaintable: Apply gaintable')
    is_scalar = gt.gain.shape[-2:] == (1, 1)
    if is_scalar:
        log.debug('apply_gaintable: scalar gains')
    for chunk, rows in enumerate(vis_timeslice_iter(vis, vis_slices=vis_slices)):
        if numpy.sum(rows) > 0:
            vistime = numpy.average(vis.time[rows])
            gaintable_rows = abs(gt.time - vistime) < gt.interval / 2.0
            gain = gt.data['gain'][gaintable_rows]
            gainwt = gt.data['weight'][gaintable_rows]
            ntimes, nant, nchan, nrec, _ = gain.shape
            original = vis.vis[rows]
            originalwt = vis.weight[rows]
            applied = copy.deepcopy(original)
            appliedwt = copy.deepcopy(originalwt)
            for time in range(ntimes):
                antantwt = numpy.outer(gainwt, gainwt)
                if is_scalar:
                    if inverse:
                        lgain = numpy.ones_like(gain)
                        lgain[numpy.abs(gain) > 0.0] = 1.0 / gain[numpy.abs(gain) > 0.0]
                    else:
                        lgain = gain
                    clgain = numpy.conjugate(lgain)
                    for chan in range(nchan):
                        smueller = numpy.ma.outer(lgain[time, :, chan, 0],
                                                  clgain[time, :, chan, 0]).reshape([nant, nant])
                        applied[time, :, :, chan, 0] = original[time, :, :, chan, 0] * smueller
                        antantwt = numpy.outer(gainwt[time, :, chan, 0, 0], gainwt[time, :, chan, 0, 0])
                        applied[time, :, :, chan, 0][antantwt == 0.0] = 0.0
                        appliedwt[time, :, :, chan, 0][antantwt == 0.0] = 0.0
                else:
                    for a1 in range(vis.nants - 1):
                        for a2 in range(a1 + 1, vis.nants):
                            for chan in range(nchan):
                                mueller = numpy.kron(gain[time, a1, chan, :, :],
                                                     numpy.conjugate(gain[time, a2, chan, :, :]))
                                if inverse:
                                    try:
                                        mueller = numpy.linalg.inv(mueller)
                                        applied[time, a2, a1, chan, :] = \
                                            numpy.matmul(mueller, original[time, a2, a1, chan, :])
                                    except numpy.linalg.linalg.LinAlgError:
                                        applied[time, a2, a1, chan, :] = \
                                            original[time, a2, a1, chan, :]
                                else:
                                    applied[time, a2, a1, chan, :] = \
                                        numpy.matmul(mueller, original[time, a2, a1, chan, :])
                                if (gainwt[time, a1, chan, 0, 0] <= 0.0) or (gainwt[time, a1, chan, 0, 0] <= 0.0):
                                    applied[time, a2, a1, chan, 0] = 0.0
                                    appliedwt[time, a2, a1, chan, 0] = 0.0
            vis.data['vis'][rows] = applied
    return vis
def apply_gaintable ( vis : BlockVisibility , gt : GainTable , inverse = False , vis_slices = None , ** kwargs ) -> BlockVisibility : assert isinstance ( vis , BlockVisibility ) , "vis is not a BlockVisibility: %r" % vis assert isinstance ( gt , GainTable ) , "gt is not a GainTable: %r" % gt assert_vis_gt_compatible ( vis , gt ) if inverse : log . debug ( 'apply_gaintable: Apply inverse gaintable' ) else : log . debug ( 'apply_gaintable: Apply gaintable' ) is_scalar = gt . gain . shape [ - 2 : ] == ( 1 , 1 ) if is_scalar : log . debug ( 'apply_gaintable: scalar gains' ) for chunk , rows in enumerate ( vis_timeslice_iter ( vis , vis_slices = vis_slices ) ) : if numpy . sum ( rows ) > 0 : vistime = numpy . average ( vis . time [ rows ] ) gaintable_rows = abs ( gt . time - vistime ) < gt . interval / 2.0 gain = gt . data [ 'gain' ] [ gaintable_rows ] gainwt = gt . data [ 'weight' ] [ gaintable_rows ] ntimes , nant , nchan , nrec , _ = gain . shape original = vis . vis [ rows ] originalwt = vis . weight [ rows ] applied = copy . deepcopy ( original ) appliedwt = copy . deepcopy ( originalwt ) for time in range ( ntimes ) : antantwt = numpy . outer ( gainwt , gainwt ) if is_scalar : if inverse : lgain = numpy . ones_like ( gain ) lgain [ numpy . abs ( gain ) > 0.0 ] = 1.0 / gain [ numpy . abs ( gain ) > 0.0 ] else : lgain = gain clgain = numpy . conjugate ( lgain ) for chan in range ( nchan ) : smueller = numpy . ma . outer ( lgain [ time , : , chan , 0 ] , clgain [ time , : , chan , 0 ] ) . reshape ( [ nant , nant ] ) applied [ time , : , : , chan , 0 ] = original [ time , : , : , chan , 0 ] * smueller antantwt = numpy . outer ( gainwt [ time , : , chan , 0 , 0 ] , gainwt [ time , : , chan , 0 , 0 ] ) applied [ time , : , : , chan , 0 ] [ antantwt == 0.0 ] = 0.0 appliedwt [ time , : , : , chan , 0 ] [ antantwt == 0.0 ] = 0.0 else : for a1 in range ( vis . nants - 1 ) : for a2 in range ( a1 + 1 , vis . nants ) : for chan in range ( nchan ) : mueller = numpy . kron ( gain [ time , a1 , chan , : , : ] , numpy . conjugate ( gain [ time , a2 , chan , : , : ] ) ) if inverse : try : mueller = numpy . linalg . inv ( mueller ) applied [ time , a2 , a1 , chan , : ] = numpy . matmul ( mueller , original [ time , a2 , a1 , chan , : ] ) except numpy . linalg . linalg . LinAlgError : applied [ time , a2 , a1 , chan , : ] = original [ time , a2 , a1 , chan , : ] else : applied [ time , a2 , a1 , chan , : ] = numpy . matmul ( mueller , original [ time , a2 , a1 , chan , : ] ) if ( gainwt [ time , a1 , chan , 0 , 0 ] <= 0.0 ) or ( gainwt [ time , a1 , chan , 0 , 0 ] <= 0.0 ) : applied [ time , a2 , a1 , chan , 0 ] = 0.0 appliedwt [ time , a2 , a1 , chan , 0 ] = 0.0 vis . data [ 'vis' ] [ rows ] = applied return vis
Apply a gain table to a block visibility
The corrected visibility is::
Apply a gain table to a block visibility The corrected visibility is ::
"""Apply a gain table to a block visibility
    
    The corrected visibility is::
    
        V_corrected = {g_i * g_j^*}^-1 V_obs
        
    If the visibility data are polarised e.g. polarisation_frame("linear") then the inverse operator
    represents an actual inverse of the gains.
    
    :param vis: Visibility to have gains applied
    :param gt: Gaintable to be applied
    :param inverse: Apply the inverse (default=False)
    :return: input vis with gains applied
    
    """ # Lookup the gain for this set of visibilities # The shape of the mueller matrix is # If the Mueller is singular, ignore it
input vis with gains applied
input vis with gains applied
vis
BlockVisibility
Visibility to have gains applied
Visibility to have gains applied
gt
GainTable
Gaintable to be applied
Gaintable to be applied
inverse
Apply the inverse (default=False)
Apply the inverse ( default = False )
vis_slices