def PBackFFT(FFTrev, inArray, outArray, err): """ Back transform half plane complex to real inArray is FFTed (half plane complex - real) to outArray * FFTref = FFT object to FT inArray * inArray = CArray with image to be FFTed must be a size compatable with FFTrev * outArray = FArray for output must be a size compatable with FT of inArray * err = Python Obit Error/message stack """ ################################################################ # Checks if not FFT.PIsA(FFTrev): print("Actually ",FFTrev.__class__) raise TypeError("FFTrev MUST be a Python Obit FFT") if not CArray.PIsA(inArray ): print("Actually ",inArray.__class__) raise TypeError("inArray MUST be a Python Obit CArray") if not FArray.PIsA(outArray ): print("Actually ",outArray.__class__) raise TypeError("outArray MUST be a Python Obit FArray") if not OErr.OErrIsA(err): raise TypeError("err MUST be an OErr") # # FFT PFFTC2R (FFTrev, inArray, outArray) FArray.PCenter2D (outArray)
def PMakeBeamMask(inImage, inFFT, err): """ Make uv plane weighting array Creates an FArray the size of a plane in inImage, FFT, takes real part and normalizes the central value to one Resulting array is returned. * inImage = Python Image whose FArray is to be converted to a weight mask * inFFT = Python Obit fortward FFT object * err = Python Obit Error/message stack """ ################################################################ # Checks if not Image.PIsA(inImage): print "Actually ", inImage.__class__ raise TypeError, "inImage MUST be a Python Obit Image" if not FFT.PIsA(inFFT): print "Actually ", inFFT.__class__ raise TypeError, "inFFT MUST be a Python Obit FFT" # # Make copy of data array inArray = inImage.FArray outArray = FArray.PClone(inArray, err) #OErr.printErrMsg(err, "Error duplicating FArray for "+Image.PGetName(inImage)) # Add model PCreateModel(inImage, outArray) # Pad for FFT FFTdim = FFT.PGetDim(inFFT) FFTArray = FArray.PClone(inArray, err) naxis = FFTdim[0:2] # Make output big enough for FFT FFTArray = FArray.FArray("FFT array", naxis) PPadArray(inFFT, outArray, FFTArray) del outArray # Cleanup # Swaparoonie FArray.PCenter2D(FFTArray) # FFT uvArray = PCreateFFTArray(inFFT) PFFTR2C(inFFT, FFTArray, uvArray) del FFTArray # Cleanup # Extract Real part naxis = CArray.PGetNaxis(uvArray)[0:2] maskArray = FArray.FArray("Mask array for " + Image.PGetName(inImage), naxis) CArray.PReal(uvArray, maskArray) del uvArray # Cleanup # Normalize pos = [0, 1 + naxis[1] / 2] peak = FArray.PMax(maskArray, pos) norm = 1.0 / peak FArray.PSMul(maskArray, norm) return maskArray
def PAccumImage(FFTfor, inImage, wtArray, accArray, workArray, err): """ Accumulate the weighted FT of an FArray inImage is FFTed, multiplied by wtArray and accumulated into accArray * FFTfor = FFT object to FT inArray * inImage = Image to be accumulated must be a size compatable with FFTfor returned with contents swapped for FFTs * wtArray = FArray containing accumulation weights must be a size compatable with FT of inArray * accArray = CArray in which the results are to be accumulated must be a size compatable with FT of inArray * workArray= CArray for temporary storage of FT of inArray * err = Python Obit Error/message stack """ ################################################################ # Checks if not FFT.PIsA(FFTfor): print("Actually ",FFTfor.__class__) raise TypeError("FFTfor MUST be a Python Obit FFT") if not Image.PIsA(inImage ): print("Actually ",inImage.__class__) raise TypeError(" inImage MUST be a Python Obit Image") if not FArray.PIsA(wtArray): print("Actually ",wtArray.__class__) raise TypeError("wtArray MUST be a Python Obit FArray") if not CArray.PIsA(accArray): print("Actually ",accArray.__class__) raise TypeError("accArray MUST be a Python Obit CArray") if not CArray.PIsA(workArray): print("Actually ",workArray.__class__) raise TypeError("workArray MUST be a Python Obit CArray") if not OErr.OErrIsA(err): raise TypeError("err MUST be an OErr") # # Check compatability if not CArray.PIsCompatable (accArray, workArray): raise TypeError("accArray and workArray incompatible") # Get array from image inArray = Image.PGetFArray(inImage) # Swaparoonie FArray.PCenter2D (inArray) # FFT PFFTR2C (FFTfor, inArray, workArray) # Multiply by weights CArray.PFMul(workArray, wtArray, workArray) # Scale by inverse of beam area to get units the same # Get image info from descriptor desc = inImage.Desc descDict = desc.Dict beamMaj = 3600.0 * descDict["beamMaj"] beamMin = 3600.0 * descDict["beamMin"] cdelt = descDict["cdelt"] factor = (abs(cdelt[1])/beamMaj) * (abs(cdelt[1])/beamMin) CArray.PSMul(workArray, factor) # Accumulate CArray.PAdd(accArray, workArray, accArray)
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)
# 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) # 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) # 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)