def GetPlane(self, array=None, plane=[1, 1, 1, 1, 1]): """ Get an image plane and store it in array. Use self.FArray if array is None. Default to first image plane. Return the opened plane. """ err_msg = ("Error getting plane '%s' " "from image '%s'" % (plane, self.name)) try: self.img.GetPlane(array, plane, self._err) if array is None: # Plane is retrieved to self.img.FArray # Return a deep copy. array = FArray.PCopy(self.img.FArray, self._err) except Exception: raise Exception(err_msg) handle_obit_err(err_msg, self._err) return array
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)
# Get average ratio - loop over planes sumX = 0.0; sumWt = 0.0 # Zero accumulators for plane in range(Idim[2]): print "Plane",plane+1 planeDim = [plane+1,1,1,1,1] # Only 3D IImage.GetPlane(None, planeDim, err) # Read Ipol plane (IData) VImage.GetPlane(None, planeDim, err) # Read Vpol plane (VData) # Get IPol Image plane statistics for clipping pos=[0,0] Imax = FArray.PMax(IData, pos) Irms = IData.RMS Imin = max(Imax*MaxFactor,Irms*RMSFactor) print "Max",Imax,"RMS",Irms,"clip",Imin # Blank noise and below DR limit WtData = FArray.PCopy(IData, err) # Work array copy of IPol DivData = FArray.PClone(IData, err) # Clone of IPol array OErr.printErrMsg(err, "Error copying FArray") FArray.PClipBlank(WtData, Imin, 1.0e30) # Determine mean fractional VPol FArray.PDiv(VData, WtData, DivData) # Print Mean ratio print "Mean ratio",DivData.Mean # Multiple fraction times weight FArray.PMul(DivData, WtData, DivData) # Accumulate sumX = sumX + DivData.Sum sumWt = sumWt + WtData.Sum del WtData, DivData # Some cleanup
def PScaleImage(inImage, scale, err): """ Scale the pixel values in an image Scale image, optionally by plane, scales any CC tables, writes history * inImage = Obit Python Image * scale = Scaling factor, if scalar, multiply all pixels, otherwise one value per image plane. * err = Python Obit Error/message stack """ ################################################################ # Checks if not Image.PIsA(inImage): raise TypeError("inImage MUST be a Python Obit Image") if not err.IsA(): raise TypeError("err MUST be an OErr") # # Open image Image.POpen(inImage, Image.READWRITE, err) # Get input descriptor to see how many planes inDesc = Image.PGetDesc(inImage) inDescDict = ImageDesc.PGetDict(inDesc) ndim = inDescDict["naxis"] inNaxis = inDescDict["inaxes"] # Work buffer inImageArray = Image.PGetFArray(inImage) ImageBuffer = FArray.PCopy(inImageArray, err) # Reset max/min inDescDict["minval"] = 1.0e20 inDescDict["maxval"] = -1.0e20 inImage.Desc.Dict = inDescDict # Update descriptor on image Image.PDirty(inImage) # Force update Image.PClose(inImage, 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 plane Image.PGetPlane(inImage, ImageBuffer, doPlane, err) # Scaling factor if type(scale) == list: scl = scale[iPlane] else: scl = scale # Scale FArray.PSMul(ImageBuffer, scl) # Write output Image.PPutPlane(inImage, ImageBuffer, doPlane, err) # Scale any CC table highVer = Image.PGetHighVer(inImage, "AIPS CC") if (iPlane + 1 <= highVer): CCTab = Image.PImageGetTable(inImage, Image.READWRITE, "AIPS CC", iPlane + 1, err) PCCScale(CCTab, 1, 0, scl, err) # end loop over planes # Write history inHistory = History.History("history", inImage.List, err) # Add this programs history inHistory.Open(History.READWRITE, err) inHistory.TimeStamp(" Start Obit ScaleImage", err) if type(scale) == list: i = -1 for iPlane in planes: i = i + 1 scl = scale[i] inHistory.WriteRec( -1, "ScaleImage / scale[" + str(i + 1) + "] = " + str(scl), err) else: inHistory.WriteRec(-1, "ScaleImage / scale = " + str(scale), err) inHistory.Close(err)