Пример #1
0
def PImageFFT(inImage, outAImage, outPImage, err):
    """
    FFTs an Image
    
    FFT inImage and write as real and imaginary as full plane (hermetian) 

    * inImage   = input Obit Python Image 1
      Any BLC and/or TRC set will be honored
    * outAImage = output Obit Python Amplitude image of FFT
      must be defined but not instantiated
    * outPImage = output Obit Python Phase (deg) image of FFT
      must be defined but not instantiated
    * err       = Python Obit Error/message stack
    """
    ################################################################
    # Checks
    if not Image.PIsA(inImage):
        raise TypeError("inImage MUST be a Python Obit Image")
    if not Image.PIsA(outAImage):
        raise TypeError("outAImage MUST be a Python Obit Image")
    if not Image.PIsA(outPImage):
        raise TypeError("outPImage MUST be a Python Obit Image")
    if not err.IsA():
        raise TypeError("err MUST be an OErr")
    #
    # Clone output images
    inImage.Clone(outAImage, err)
    inImage.Clone(outPImage, err)
    OErr.printErrMsg(err, "Error initializing images")

    # Size of FFT
    inImage.Open(Image.READONLY, err)
    inImage.Read(err)
    OErr.printErrMsg(err, "Error reading input")
    inHead = inImage.Desc.Dict
    FFTdim = [
        FFT.PSuggestSize(inHead["inaxes"][0]),
        FFT.PSuggestSize(inHead["inaxes"][1])
    ]

    # Create float arrays for FFT size
    inFArray = FArray.FArray("inF", naxis=FFTdim)
    outFArray = FArray.FArray("outF", naxis=FFTdim)

    # Create FFT for full complex FFT
    FFTfor = FFT.FFT("FFT", 1, 1, 2, FFTdim)

    # Create complex arrays for FFT size
    inCArray = CArray.CArray("inC", naxis=FFTdim)
    outCArray = CArray.CArray("outC", naxis=FFTdim)

    #Loop over planes
    nplane = inImage.Desc.Dict['inaxes'][2]
    for iax in range(1, nplane + 1):
        inImage.GetPlane(None, [iax, 1, 1, 1, 1], err)
        OErr.printErrMsg(err, "Error reading input")
        # Pad input into work FArray
        FArray.PPad(inImage.FArray, inFArray, 1.0)
        # and God said "The center of an FFT will be at the corners"
        FArray.PCenter2D(inFArray)
        # Zero output FArray and use as imaginary part
        FArray.PFill(outFArray, 0.0)
        # Copy input to scratch CArray
        CArray.PComplex(inFArray, outFArray, inCArray)

        # FFT
        FFT.PC2C(FFTfor, inCArray, outCArray)

        # Extract amplitude, write
        CArray.PAmp(outCArray, outFArray)
        # and God said "The center of an FFT will be at the corners"
        FArray.PCenter2D(outFArray)
        outAImage.FArray = FeatherUtil.PExtract(FFTfor, outFArray,
                                                outAImage.FArray, err)
        OErr.printErrMsg(err, "Error extracting output amplitude plane")
        outAImage.PutPlane(outAImage.FArray, [iax, 1, 1, 1, 1], err)

        # Extract phase, write
        CArray.PPhase(outCArray, outFArray)
        # To degrees
        FArray.PSMul(outFArray, 57.2956)
        # and God said "The center of an FFT will be at the corners"
        FArray.PCenter2D(outFArray)
        outPImage.FArray = FeatherUtil.PExtract(FFTfor, outFArray,
                                                outPImage.FArray, err)
        OErr.printErrMsg(err, "Error extracting output phase plane")
        outPImage.PutPlane(outPImage.FArray, [iax, 1, 1, 1, 1], err)
        # Error?
        OErr.printErrMsg(err, "Error writing output phase image")
        # end loop over planes

    # Fix headers
    outAImage.Open(Image.READWRITE, err)
    FFTHeaderUpdate(outAImage, FFTdim, err)
    outAImage.Close(err)
    OErr.printErrMsg(err, "Error writing output amplitude image")

    outPImage.Open(Image.READWRITE, err)
    FFTHeaderUpdate(outPImage, FFTdim, err)
    outPImage.Close(err)
    OErr.printErrMsg(err, "Error writing output phase image")

    # get any BLC, TRC for history
    info = inImage.List.Dict
    blc = [1, 1, 1, 1, 1, 1, 1]
    if 'BLC' in info:
        blc = info["BLC"][2]
    trc = [0, 0, 0, 0, 0, 0, 0]
    if 'TRC' in info:
        trc = info["TRC"][2]

    # Write history
    i = 0
    imtype = ("Amplitude", "Phase")
    for outImage in (outAImage, outPImage):
        inHistory = History.History("history", inImage.List, err)
        outHistory = History.History("history", outImage.List, err)
        # Copy History
        # FITS? - copy header
        if ("FileType" in info) and (info["FileType"][2][0] == 0):
            History.PCopyHeader(inHistory, outHistory, err)
        #Not needed History.PCopy(inHistory, outHistory, err)
        # Add this programs history
        outHistory.Open(History.READWRITE, err)
        outHistory.TimeStamp(" Start Obit PImageFFT", err)
        outHistory.TimeStamp(OSystem.PGetPgmName() + " BLC = " + str(blc), err)
        outHistory.TimeStamp(OSystem.PGetPgmName() + " TRC = " + str(trc), err)
        outHistory.TimeStamp(OSystem.PGetPgmName() + " type = " + imtype[i],
                             err)
        i += 1
        outHistory.Close(err)
Пример #2
0
FArray.PCenter2D(inFArray)
# Zero output FArray and use as imaginary part
FArray.PFill(outFArray, 0.0)

# Create FFT for full complex FFT
FFTfor = FFT.FFT("FFT", 1, 1, 2, FFTdim)

# Create complex arrays for FFT size
inCArray  = CArray.CArray("inC", naxis=FFTdim)
outCArray = CArray.CArray("outC", naxis=FFTdim)

# Copy input to scratch CArray
CArray.PComplex(inFArray, outFArray, inCArray)

# FFT
FFT.PC2C(FFTfor, inCArray, outCArray)

# Extract amplitude
CArray.PAmp(outCArray, outFArray)
# and God said "The center of an FFT will be at the corners"
FArray.PCenter2D(outFArray)

# Extract output portion and write
outAImage.Open(Image.WRITEONLY,err)
outAImage.FArray = FeatherUtil.PExtract (FFTfor, outFArray, outAImage.FArray, err)
OErr.printErrMsg(err, "Error extracting output amplitude image")
outAImage.WriteFA(outAImage.FArray, err)
# Fix header
FFTHeaderUpdate(outAImage, FFTdim, err)
outAImage.Close(err)
OErr.printErrMsg(err, "Error writing output amplitude image")