Ejemplo n.º 1
0
def PMaskCube(inImage, Mask, outImage, err):
    """
    Blank inImage where Mask is blanked or 0.0
    
    * inImage    = input Image cube
    * Mask       = 1 plane mask Image
    * outImage   = Output Image, must be defined.
    * err        = Python Obit Error/message stack
    """
    ################################################################
    # Checks
    if not Image.PIsA(outImage):
        print "Actually ",outImage.__class__
        raise TypeError,"outImage MUST be a Python Obit Image"
    if not Image.PIsA(inImage):
        print "Actually ",inImage.__class__
        raise TypeError,"inImage MUST be a Python Obit Image"
    if not Image.PIsA(Mask):
        print "Actually ",Mask.__class__
        raise TypeError,"Mask MUST be a Python Obit Image"
    if not OErr.OErrIsA(err):
        raise TypeError,"err MUST be an OErr"
    #  Clone output
    inImage.Clone(outImage, err)
    # Open files
    Image.POpen(outImage,Image.WRITEONLY, err)
    Image.POpen(inImage, Image.READONLY,  err)
    Image.POpen(Mask,    Image.READONLY,  err)
    OErr.printErrMsg(err, "Error opening images")
    #  how many planes?
    ndim       = inImage.Desc.Dict["naxis"]
    inNaxis    = inImage.Desc.Dict["inaxes"]
    # list of planes to loop over (0-rel)
    if (ndim>2) and (inNaxis[2]>0):  
        planes = range(inNaxis[2])
    else:
        planes = [0]
    # Read Mask plane
    Image.PGetPlane (Mask, None, [1,1,1,1,1], err)
    OErr.printErrMsg(err, "Error reading mask image")
    # Mask where exactly 0.0
    FArray.PInClip(Mask.FArray, -1.0e-25, 1.0e-25, FArray.fblank)
    # Loop over planes
    for iPlane in planes:
        doPlane = [iPlane+1,1,1,1,1]
        # Get image plane
        Image.PGetPlane (inImage, None, doPlane, err)
        OErr.printErrMsg(err, "Error reading input image")
        # Make sure compatable
        if not FArray.PIsCompatable(inImage.FArray, Mask.FArray):
            raise RuntimeError,"inImage and Mask incompatable"
        # Mask where blanked
        FArray.PBlank (inImage.FArray, Mask.FArray, outImage.FArray)
        # Write
        Image.PPutPlane(outImage, None, doPlane, err)
        OErr.printErrMsg(err, "Error Writing blanked image ")
        # end loop over planes
    # close files
    Image.PClose(outImage, err)
    Image.PClose(inImage, err)
    Image.PClose(Mask, err)
    # Write history
    inHistory  = History.History("history", inImage.List, err)
    outHistory = History.History("history", outImage.List, err)
    # Copy History
    History.PCopy(inHistory, outHistory, err)
    outHistory.Open(History.READWRITE, err)
    outHistory.TimeStamp(" Start Obit PMaskCube",err)
    outHistory.Close(err)
Ejemplo n.º 2
0
def PWeightImageEq(inImage, factor, SumWtImage, SumWt2, err, minGain=0.1,
                   iblc=[1,1,1], itrc=[0,0,1], restart=0, hwidth=2, doGPU=False,
                   planeWt=False, OTFRA=None, OTFDec=None, inWtImage=None,
                   maxRMS=None, minAccWt=0.15):
    """
    Sum an image onto Weighting accumulators using PB corrections
    
    Version for equatorial in/output and no relative rotation
    Calculate the weights for an image from the primary beam pattern
    And accumulate into the correct locations in the accumulation images.

    * inImage    = Image to be accumulated
    * factor     = Additional multiplication factor, normally 1.0
                   >0 => use the factor/RMS of each image plane
    * SumWtImage = First output image, must be defined (i.e. files named)
                   but not fully created.
    * SumWt2     = Second output image, like SumWtImage
    * err        = Python Obit Error/message stack
    * minGain    = minimum allowed gain (lower values blanked).
    * iblc       = BLC in plane to start selection
    * itrc       = TRC in plane to end selection
    * restart    = restart channel no. 0-rel
    * hwidth     = half width of interpolation kernal [1-4] default 2
    * doGPU      = If true and GPU enables, use a GPU for the interpolation.
    *              NB: routine will fail if GPU is not enabled.
    * planeWt    = if True generate weight image per input plane
    * OTFoffsets = if >1 then make beam using multiple pointing offsets
                   "Aussie mode" OTF. must also go=ive OTFRA, OTFDec
    * OTFRA      = Array of RA offsets in deg not corrected for Declination
    * OTFDec     = Array of Declinations offsets in deg, same size as OTFRA
    * inWtImage  = Beam (weight) image to use if not None
                   MUST have the same size as inImage 
    * maxRMS     = if given, the maximum RMS allowed
    * minAccWt   =  min. acceptable max. weight, otherwise ignore
    """
    ################################################################
    # Checks
    if not Image.PIsA(inImage):
        print "Actually ",inImage.__class__
        raise TypeError,"inImage MUST be a Python Obit Image"
    if not Image.PIsA(SumWtImage):
        print "Actually ",SumWtImage.__class__
        raise TypeError,"SumWtImage MUST be a Python Obit Image"
    if not Image.PIsA(SumWt2):
        print "Actually ",SumWt2.__class__
        raise TypeError,"SumWt2 MUST be a Python Obit Image"
    if not OErr.OErrIsA(err):
        raise TypeError,"err MUST be an OErr"
    #
    t0 = os.times()[4]    # Initial time
    haveWtImage = inWtImage != None   # Weight image given
    # Set BLC,TRC 
    inImage.List.set("BLC",[iblc[0], iblc[1],1,1,1,1,1])
    inImage.List.set("TRC",[itrc[0], itrc[1],0,0,0,0,0])
    # Open accumulation files
    Image.POpen(inImage, Image.READONLY, err)  # pythpn gets confused
    Image.POpen(SumWtImage, Image.READWRITE, err)
    Image.POpen(SumWt2, Image.READWRITE, err)
    #  Get output descriptor to see how many planes
    outDesc     = Image.PGetDesc(SumWtImage)
    outDescDict = ImageDesc.PGetDict(outDesc)
    outNaxis    = outDescDict["inaxes"]
    print "Accumulation naxis",outNaxis
    #  Get input descriptor to see how many planes
    inDesc     = Image.PGetDesc(inImage)
    inDescDict = ImageDesc.PGetDict(inDesc)
    ndim       = inDescDict["naxis"]
    inNaxis    = inDescDict["inaxes"]
    finterp    = None  # GPU not yet enabled
    # Range of planes
    bpln = max (1,iblc[2]); 
    epln = min (inNaxis[2], itrc[2])
    if epln<bpln:
        epln = inNaxis[2]
    npln = epln-bpln+1
    # Test if compatible
    if npln < outNaxis[2]:
        print "input has",npln,"planes selected and output has",outNaxis[2]
        raise RuntimeError,"input image has too few planes "
    if (ndim>0) and (inNaxis[2]>1):  # list of 0-rel planes to loop over
        planes = range(bpln+restart-1,bpln+npln-1)
    else:
        planes = [0]
    #
    if inWtImage:
        inWtImage.List.set("BLC",[iblc[0], iblc[1],1,1,1,1,1])
        inWtImage.List.set("TRC",[itrc[0], itrc[1],0,0,0,0,0])
        inWtImage.Open(Image.READONLY,err)     # Open/close to update
        inWtImage.Close(err)
    XPixelImage = None; YPixelImage = None; InterpWtImage = None;InterpWt = None
    InterpWtWt = None; WtImage = None
    # Loop over planes
    for iPlane in planes:
        doPlane  = [iPlane+1,1,1,1,1]        # Input plane
        outPlane = [iPlane+2-bpln,1,1,1,1]   # output plane
        if not (iPlane%20):
            print "At plane", iPlane+1,'t=%6.1f sec'%(os.times()[4]-t0)
        # Get image 
        inImage.List.set("BLC",[iblc[0], iblc[1],1,1,1,1,1])
        inImage.List.set("TRC",[itrc[0], itrc[1],0,0,0,0,0])
        Image.PGetPlane (inImage, None, doPlane, err)
        OErr.printErrMsg(err, "Error reading image "+str(iPlane)+" for "+Image.PGetName(inImage))
        #
        # Make weight image if needed, first pass or planeWt
        if WtImage == None:
            WtImage = Image.Image("WeightImage")
            Image.PCloneMem(inImage, WtImage, err)
        # The interpolated versions
        if not InterpWtImage:
            InterpWtImage = Image.Image("InterpWtImage")
            Image.PClone2(inImage, SumWtImage, InterpWtImage, err)
        # input x, y pixels for output
        if  (not XPixelImage) or (not YPixelImage):
            XPixelImage = Image.Image("XPixelImage")
            YPixelImage = Image.Image("YPixelImage")
            Image.PClone2(inImage, SumWtImage, XPixelImage, err)
            Image.PClone2(inImage, SumWtImage, YPixelImage, err)
            ImageUtil.PGetXYPixels(WtImage, InterpWtImage, XPixelImage, YPixelImage, err)
        
        # Special weighting?
        if factor<0.0:
            RMS = inImage.FArray.RMS
            fact = abs(factor)/RMS
        else:
            fact = factor
        if planeWt:
            pln = [iPlane+1,1,1,1,1]
        else:
            pln = [max(1,inNaxis[2]/2),1,1,1,1]
        if haveWtImage:
            # Beam provided, extract relevant plane to a memory resident WtImage
            OErr.printErrMsg(err, "Error reading wt image "+str(iPlane)+" for "+
                             Image.PGetName(inWtImage))
            # Interpolate to WtImage
            ImageUtil.PInterpolateImage(inWtImage, WtImage, err, \
                                        inPlane=doPlane, hwidth=hwidth, finterp=finterp)
            OErr.printErrMsg(err, "Error interpolating wt plane "+str(doPlane))
            
        elif planeWt or (iPlane==0):
            # Normal or OTF Beam?
            if (OTFRA==None):
                ImageUtil.PPBImage(inImage, WtImage, err, minGain, outPlane=pln)
                pass
            else:
                ImageUtil.POTFBeam (inImage, WtImage, OTFRA, OTFDec, err, minGain, outPlane=pln)
            OErr.printErrMsg(err, "Error making weight image for "+Image.PGetName(inImage))
        
        # Check maximum weight for first plane
        if iPlane==0:
            pos = [0,0]
            maxWt = FArray.PMax(WtImage.FArray,pos)
            print "Maximum weight",maxWt
            if maxWt<minAccWt:
                print "Less than minAccWt",minAccWt,"skipping"
                break
            
        # Interpolated weight image
        if  not InterpWt:
            InterpWt = Image.Image("InterpWt")
            Image.PClone2(inImage, SumWtImage, InterpWt, err)
            # Is GPU interpolation requested?
            if doGPU:
                finterp = GPUFInterpolate.PCreate("GPUinterp", WtImage.FArray, 
                                                  XPixelImage.FArray, YPixelImage.FArray, 
                                                  hwidth, err)
                OErr.printErrMsg(err, "Creating GPU FInterpolator")
                InterpWt.Desc.Dict['inaxes'], WtImage.Desc.Dict['inaxes']
            ImageUtil.PInterpolateImage(WtImage, InterpWt, err, \
                                        XPix=XPixelImage, YPix=YPixelImage,
                                        hwidth=hwidth, finterp=finterp)
            OErr.printErrMsg(err, "Error interpolating wt*wt "+Image.PGetName(inImage))
        # Interpolated weight image Squared
        if not InterpWtWt:
            InterpWtWt = Image.Image("InterpWtWt")
            Image.PClone2(inImage, SumWtImage, InterpWtWt, err)
            # Determine alignment
            inDesc = Image.PGetDesc(InterpWtImage)       # get descriptors
            inDescDict = ImageDesc.PGetDict(inDesc)
            outDesc = Image.PGetDesc(SumWtImage)
            outDescDict = ImageDesc.PGetDict(outDesc)
            naxis = inDescDict["inaxes"]                # find input center pixel in output
            pos1 = [int(naxis[0]*0.5+0.5), int(naxis[1]*0.5+0.5)]
            xpos1 = [float(pos1[0]),float(pos1[1])]
            xpos2 = ImageDesc.PCvtPixel (inDesc, xpos1, outDesc, err)
            pos2 = [int(xpos2[0]+0.5), int(xpos2[1]+0.5)]
            # Is GPU interpolation requested?
            if doGPU:
                del finterp
                finterp = GPUFInterpolate.PCreate("GPUinterp", inImage.FArray, 
                                                  XPixelImage.FArray, YPixelImage.FArray, 
                                                  hwidth, err)
                OErr.printErrMsg(err, "Creating GPU FInterpolator")
        # End init wt image
        # Special weighting or editing?
        if (factor<0.0) or maxRMS:
            # Get image 
            Image.PGetPlane (inImage, None, doPlane, err)
            OErr.printErrMsg(err, "Error reading image "+str(iPlane)+" for "+Image.PGetName(inImage))
            RMS = inImage.FArray.RMS
            # This plane acceptable?
            if maxRMS and ((RMS>maxRMS) or (RMS<=0.0)):
                #print 'drop plane',doPlane[0],'RMS',RMS
                continue
            if (factor<0.0):
                fact = abs(factor)/RMS
            else:
                fact = factor
            if not (iPlane%20):
                print "Factor",fact, "plane",iPlane,"RMS",RMS
        else:
            fact = factor
        # Interpolate image plane
        ImageUtil.PInterpolateImage(inImage, InterpWtImage, err, \
                                    inPlane=doPlane, XPix=XPixelImage, YPix=YPixelImage,
                                    hwidth=hwidth, finterp=finterp)
        OErr.printErrMsg(err, "Error interpolating plane "+str(doPlane))
        # Interpolated image times beam
        FArray.PMul(InterpWtImage.FArray, InterpWt.FArray, InterpWtImage.FArray)
        #
        # Read accumulation image planes
        Image.PGetPlane(SumWtImage, None, outPlane, err)
        OErr.printErrMsg(err, "Error reading accumulation image ")
        #
        # Accumulate
        FArray.PShiftAdd (SumWtImage.FArray, pos2, InterpWtImage.FArray, pos1, fact, SumWtImage.FArray)
        Image.PPutPlane(SumWtImage, None, outPlane, err)
        OErr.printErrMsg(err, "Error writing accumulation image ")

        # Square weight image
        Image.PGetPlane(SumWt2,  None, outPlane, err)
        FArray.PMul(InterpWt.FArray, InterpWt.FArray, InterpWtWt.FArray)
        
        # Blank weight whereever image is blank or zero
        FArray.PInClip(InterpWt.FArray, -1.0e-20, 1.0e-20, FArray.PGetBlank())
        # Blank weight squared where image * Wt is blanked
        FArray.PBlank (InterpWtWt.FArray, InterpWt.FArray, InterpWtWt.FArray);
        # Accumulate Wt*Wt
        FArray.PShiftAdd (SumWt2.FArray, pos2, InterpWtWt.FArray,pos1, fact, SumWt2.FArray)
        #
        # Write output
        Image.PPutPlane(SumWt2, None, outPlane, err)
        OErr.printErrMsg(err, "Error writing accumulation image ")
        # Cleanup if doing a weight image per plane (continuum)
        if planeWt:
            del WtImage, XPixelImage, YPixelImage;
            WtImage = None;XPixelImage=None; YPixelImage=None;
       # end loop over planes
    # close output
    Image.PClose(inImage, err)
    Image.PClose(SumWtImage, err)
    Image.PClose(SumWt2, err)
    del XPixelImage, YPixelImage, InterpWtImage, InterpWtWt, 
    if WtImage:
        del WtImage; WtImage = None
    if finterp!=None:
        del finterp
Ejemplo n.º 3
0
def PAccumIxWt(im, wt, factor, accum, accumwt, err):
    """
    Accumulate im * wt into accum
    
    Used to accumulate images which don't need PB corrections
    and have a weight image.

    * im      = image to accumulate
    * wt      = weight image corresponding to accum
    * factor  = Additional multiplication factor, normally 1.0
    * accum   = image into which to accumulate im*wt
    * accumwt = image into which to accumulate wt
    * err     = Python Obit Error/message stack
    """
    ################################################################
    # Checks
    if not Image.PIsA(im):
        print "Actually ",im.__class__
        raise TypeError,"im MUST be a Python Obit Image"
    if not Image.PIsA(wt):
        print "Actually ",wt.__class__
        raise TypeError,"wt MUST be a Python Obit Image"
    if not Image.PIsA(accum):
        print "Actually ",accum.__class__
        raise TypeError,"accum MUST be a Python Obit Image"
    
    #
    # Open files
    #Image.POpen(im, 1, err)
    Image.POpen(accum, Image.READWRITE, err)
    Image.POpen(accumwt, Image.READWRITE, err)
    #  Get output descriptor to see how many planes
    outDesc     = accum.Desc
    outDescDict = outDesc.Dict
    outNaxis    = outDescDict["inaxes"]
    print "Accumulation naxis",outNaxis
    #  Get input descriptor to see how many planes
    inDesc     = im.Desc
    inDescDict = inDesc.Dict
    ndim       = inDescDict["naxis"]
    inNaxis    = inDescDict["inaxes"]
    #print "debug input naxis is ",inNaxis
    # Test if compatible
    if inNaxis[2] < outNaxis[2]:
        print "input has",inNaxis[2],"planes and output",outNaxis[2]
        raise RuntimeError,"input image has too few planes "
    if (ndim>0) and (inNaxis[2]>0):  # list of planes to loop over (0-rel)
        planes = range(inNaxis[2])
    else:
        planes = [0]
    #
    # Loop over planes
    for iPlane in planes:
        doPlane = [iPlane+1,1,1,1,1]
        # Get image 
        Image.PGetPlane (im, None, doPlane, err)
        #OErr.printErrMsg(err, "Error reading image for "+Image.PGetName(im))
        imArray = im.FArray
        # Get Weight
        Image.PGetPlane (wt, None, doPlane, err)
        #OErr.printErrMsg(err, "Error reading image for "+Image.PGetName(wt))
        WtArray = wt.FArray
        #
        # Make image*Wt memory resident image
        ImageWt = Image.Image("ImageXwt")
        Image.PCloneMem(im, ImageWt, err)
        ImageWtArray = ImageWt.FArray
        FArray.PMul(imArray, WtArray, ImageWtArray);

        #
        # Now the interpolated versions to be summed to the accumulation arrays
        InterpWtImage = Image.Image("InterpWtImage")
        Image.PClone2(im, accum, InterpWtImage, err)
        ImageUtil.PInterpolateImage(ImageWt, InterpWtImage, err)
        #OErr.printErrMsg(err, "Error interpolating image "+Image.PGetName(im))
        InterpWt = Image.Image("InterpWt")
        Image.PClone2(im, accum, InterpWt, err)
        ImageUtil.PInterpolateImage(wt, InterpWt, err)
        #OErr.printErrMsg(err, "Error interpolating wt "+Image.PGetName(im))
        
        #
        # Read accumulation image plane
        Image.PGetPlane(accum, None, doPlane, err)
        Image.PGetPlane(accumwt,  None, doPlane, err)
        #OErr.printErrMsg(err, "Error reading accumulation image ")
        #
        # Determine alignment
        inDesc      = InterpWtImage.Desc
        inDescDict  = inDesc.Dict
        outDesc     = accum.Desc
        outDescDict = outDesc.Dict
        naxis       = inDescDict["inaxes"]    # find input center pixel in output
        pos1        = [int(naxis[0]*0.5+0.5), int(naxis[1]*0.5+0.5)]
        xpos1       = [float(pos1[0]),float(pos1[1])]
        xpos2       = ImageDesc.PCvtPixel (inDesc, xpos1, outDesc, err)
        #OErr.printErrMsg(err, "Error converting pixel locations for "+Image.PGetName(im))
        pos2        = [int(xpos2[0]+0.5), int(xpos2[1]+0.5)]
        #
        # Accumulate
        accumArray = accum.FArray
        InterpWtArray = InterpWtImage.FArray
        FArray.PShiftAdd (accumArray, pos2, InterpWtArray,  pos1, factor, accumArray)
        accumwtArray = accumwt.FArray
        InterpWtWtArray = InterpWt.FArray
        # Blank weight whereever image is blank or zero
        FArray.PInClip(InterpWtArray, -1.0e-20, 1.0e-20, FArray.PGetBlank())
        FArray.PBlank (InterpWtWtArray, InterpWtArray, InterpWtWtArray);
        FArray.PShiftAdd (accumwtArray,     pos2, InterpWtWtArray,pos1, factor, accumwtArray)
        #
            
        # Write output
        Image.PPutPlane(accum, None, doPlane, err)
        Image.PPutPlane(accumwt, None, doPlane, err)
        #OErr.printErrMsg(err, "Error writing accumulation image ")
        # Cleanup, 
        del accumArray, accumwtArray, InterpWtArray, InterpWtWtArray, ImageWtArray, WtArray, imArray 
        # end loop over planes
    # close output
    #Image.PClose(im, err)
    Image.PClose(accum, err)
    Image.PClose(accumwt, err)
Ejemplo n.º 4
0
                xpix = XPixIm.FArray
                ypix = YPixIm.FArray
                # Set interpolator to input image
                FInterpolate.PSetDesc(fi, x.Desc)
                FInterpolate.PReplace(fi, inArr)
                # Interpolate
                for ix in range(0, ncell):
                    for iy in range(0, ncell):
                        pos = [space * (ix - half), space * (iy - half)]
                        pix = ImageDesc.PGetPixel(x.Desc, pos, err)
                        val = FInterpolate.PPixel(fi, pix, err)
                        work.set(val, ix, iy)
                # end interpolation loops
                # Accumulate weight image, zero where image blanked
                FArray.PFill(work2, weight[ires])
                FArray.PBlank(work2, work, work2)
                FArray.PDeblank(work2, 0.0)  # Replace any blanking with 0.0
                FArray.PAdd(accW, work2, accW)
                # Image * wt
                FArray.PDeblank(work, 0.0)  # Replace any blanking with 0.0
                FArray.PSMul(work, weight[ires])
                # Accumulate image*wt
                FArray.PAdd(accWI, work, accWI)
                ires += 1
                # Next resolution

            # end resolution loop
            FArray.PDiv(accWI, accW, accWI)  # Normalize to accWI
            # Write output
            outIm.PutPlane(accWI, oplane, err)
            iplane[0] += 1  # Next input plane