def SumImageCube (inCube, outImage, err): """ Average the planes in an image Ignores all blank or zero images * inCube = cube to sum * outImage = output average plane, defined but not instantiated * err = Python Obit Error/message stack """ # Checks if not Image.PIsA(inCube): raise TypeError,"inCube MUST be a Python Obit Image" if not Image.PIsA(outImage): raise TypeError,"outImage MUST be a Python Obit Image" if not OErr.OErrIsA(err): raise TypeError,"err MUST be an OErr" # Clone output image inCube.List.set("BLC",[1,1,1]) inCube.List.set("TRC",[0,0,1]) inCube.Clone(outImage,err) inCube.List.set("TRC",[0,0,0]) OErr.printErrMsg(err, "Error initializing images") # Open files inCube.Open(Image.READONLY,err) outImage.Open(Image.WRITEONLY,err) OErr.printErrMsg(err, "Error opening images") nplane = inCube.Desc.Dict["inaxes"][2] count = 0 for i in range (1,nplane+1): plane = [i,1,1,1,1] inCube.GetPlane(None, plane, err) OErr.printErrMsg(err, "Error reading image") # Anything here rms = inCube.FArray.RawRMS if (not math.isnan(rms)) and (rms>0.0): count += 1 FArray.PAdd(inCube.FArray, outImage.FArray, outImage.FArray) # end loop norm = 1.0 / float(count) FArray.PSMul(outImage.FArray, norm) plane = [1,1,1,1,1] outImage.PutPlane(None, plane, err) OErr.printErrMsg(err, "Error writing image") inCube.Close(err) outImage.Close(err) OErr.printErrMsg(err, "Error closing images")
# 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 if iplane[0] > nChan: iplane[0] = 1
def PImageAdd (in1Image, in2Image, outImage, err, \ chkPos=False, factor1=1.0, factor2=1.0): """ Adds Pixels in in2Image from in1Image and write to outImage Adds scaled pixel values, writes history * in1Image = input Obit Python Image 1 * in2Image = input Obit Python Image 2 * outImage = output Obit Python Image, must be defined but not instantiated * err = Python Obit Error/message stack * chkPos = If true also check the coordinates on each axis Check is if pixels are within 0.01 of a pixel * factor1 = Scaling factor for in1Image * factor2 = Scaling factor for in2Image """ ################################################################ # Checks if not Image.PIsA(in1Image): raise TypeError("in1Image MUST be a Python Obit Image") if not Image.PIsA(in2Image): raise TypeError("in2Image MUST be a Python Obit Image") if not Image.PIsA(outImage): raise TypeError("outImage MUST be a Python Obit Image") if not err.IsA(): raise TypeError("err MUST be an OErr") # # Clone output from input 1 in1Image.Clone(outImage, err) # Open images Image.POpen(in1Image, Image.READONLY, err) Image.POpen(in2Image, Image.READONLY, err) Image.POpen(outImage, Image.WRITEONLY, err) # Get input descriptor to see how many planes in1Desc = in1Image.Desc in2Desc = in2Image.Desc # Check compatibility ImageDesc.PCheckCompat(in1Desc, in2Desc, chkPos=chkPos) inDescDict = in1Desc.Dict ndim = inDescDict["naxis"] inNaxis = inDescDict["inaxes"] # Work buffer inImageArray = Image.PGetFArray(in1Image) ImageBuffer1 = FArray.PCopy(inImageArray, err) ImageBuffer2 = FArray.PCopy(inImageArray, err) # list of planes to loop over (0-rel) if (ndim > 0) and (inNaxis[2] > 0): planes = list(range(inNaxis[2])) else: planes = [0] # Loop over planes for iPlane in planes: doPlane = [iPlane + 1, 1, 1, 1, 1] # Get image planes Image.PGetPlane(in1Image, ImageBuffer1, doPlane, err) Image.PGetPlane(in2Image, ImageBuffer2, doPlane, err) # Scale FArray.PSMul(ImageBuffer1, factor1) FArray.PSMul(ImageBuffer2, factor2) # Add FArray.PAdd(ImageBuffer1, ImageBuffer2, ImageBuffer2) # Write output Image.PPutPlane(outImage, ImageBuffer2, doPlane, err) # end loop over planes # Close in2Image.Close(err) in2Image.Close(err) outImage.Close(err) # Error? if err.isErr: OErr.printErrMsg(err, "Error subtracting Images") # Write history in1History = History.History("history", in1Image.List, err) in2History = History.History("history", in2Image.List, err) outHistory = History.History("history", outImage.List, err) # Copy Histories outHistory.Open(History.READWRITE, err) outHistory.TimeStamp(" Start Obit PImageAdd", err) outHistory.WriteRec(-1, "/ PImageAdd Input 1 History", err) outHistory.Close(err) info = in1Image.List.Dict # FITS? - copy header if ("FileType" in info) and (info["FileType"][2][0] == 0): History.PCopyHeader(in1History, outHistory, err) #Not needed History.PCopy(in1History, outHistory, err) outHistory.Open(History.READWRITE, err) outHistory.WriteRec(-1, "/ ", err) outHistory.WriteRec(-1, "/ ****** PImageAdd Input 2 History", err) outHistory.Close(err) info = in2Image.List.Dict # FITS? - copy header if ("FileType" in info) and (info["FileType"][2][0] == 0): History.PCopyHeader(in2History, outHistory, err) History.PCopy(in2History, outHistory, err) # Add this programs history outHistory.Open(History.READWRITE, err) outHistory.TimeStamp(" Start Obit PImageAdd", err) outHistory.WriteRec(-1, OSystem.PGetPgmName() + " factor1 = " + str(factor1), err) outHistory.WriteRec(-1, OSystem.PGetPgmName() + " factor2 = " + str(factor2), err) outHistory.Close(err)